mirror of
https://github.com/edgelesssys/constellation.git
synced 2025-01-12 07:59:29 -05:00
operator: add cluster version to nodeversion
This commit is contained in:
parent
9bfe2a81ed
commit
620436626b
@ -16,7 +16,7 @@ spec:
|
|||||||
- name: v1alpha1
|
- name: v1alpha1
|
||||||
schema:
|
schema:
|
||||||
openAPIV3Schema:
|
openAPIV3Schema:
|
||||||
description: NodeVersion is the Schema for the nodeimages API.
|
description: NodeVersion is the Schema for the nodeversions API.
|
||||||
properties:
|
properties:
|
||||||
apiVersion:
|
apiVersion:
|
||||||
description: 'APIVersion defines the versioned schema of this representation
|
description: 'APIVersion defines the versioned schema of this representation
|
||||||
@ -31,7 +31,7 @@ spec:
|
|||||||
metadata:
|
metadata:
|
||||||
type: object
|
type: object
|
||||||
spec:
|
spec:
|
||||||
description: NodeVersionSpec defines the desired state of NodeImage.
|
description: NodeVersionSpec defines the desired state of NodeVersion.
|
||||||
properties:
|
properties:
|
||||||
image:
|
image:
|
||||||
description: ImageReference is the image to use for all nodes.
|
description: ImageReference is the image to use for all nodes.
|
||||||
@ -40,13 +40,17 @@ spec:
|
|||||||
description: ImageVersion is the CSP independent version of the image
|
description: ImageVersion is the CSP independent version of the image
|
||||||
to use for all nodes.
|
to use for all nodes.
|
||||||
type: string
|
type: string
|
||||||
|
kubernetesClusterVersion:
|
||||||
|
description: KubernetesClusterVersion is the advertised Kubernetes
|
||||||
|
version of the cluster.
|
||||||
|
type: string
|
||||||
kubernetesComponentsReference:
|
kubernetesComponentsReference:
|
||||||
description: KubernetesComponentsReference is a reference to the ConfigMap
|
description: KubernetesComponentsReference is a reference to the ConfigMap
|
||||||
containing the Kubernetes components to use for all nodes.
|
containing the Kubernetes components to use for all nodes.
|
||||||
type: string
|
type: string
|
||||||
type: object
|
type: object
|
||||||
status:
|
status:
|
||||||
description: NodeVersionStatus defines the observed state of NodeImage.
|
description: NodeVersionStatus defines the observed state of NodeVersion.
|
||||||
properties:
|
properties:
|
||||||
budget:
|
budget:
|
||||||
description: Budget is the amount of extra nodes that can be created
|
description: Budget is the amount of extra nodes that can be created
|
||||||
|
@ -19,6 +19,8 @@ type NodeVersionSpec struct {
|
|||||||
ImageVersion string `json:"imageVersion,omitempty"`
|
ImageVersion string `json:"imageVersion,omitempty"`
|
||||||
// KubernetesComponentsReference is a reference to the ConfigMap containing the Kubernetes components to use for all nodes.
|
// KubernetesComponentsReference is a reference to the ConfigMap containing the Kubernetes components to use for all nodes.
|
||||||
KubernetesComponentsReference string `json:"kubernetesComponentsReference,omitempty"`
|
KubernetesComponentsReference string `json:"kubernetesComponentsReference,omitempty"`
|
||||||
|
// KubernetesClusterVersion is the advertised Kubernetes version of the cluster.
|
||||||
|
KubernetesClusterVersion string `json:"kubernetesClusterVersion,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// NodeVersionStatus defines the observed state of NodeVersion.
|
// NodeVersionStatus defines the observed state of NodeVersion.
|
||||||
|
@ -42,6 +42,10 @@ spec:
|
|||||||
description: ImageVersion is the CSP independent version of the image
|
description: ImageVersion is the CSP independent version of the image
|
||||||
to use for all nodes.
|
to use for all nodes.
|
||||||
type: string
|
type: string
|
||||||
|
kubernetesClusterVersion:
|
||||||
|
description: KubernetesClusterVersion is the advertised Kubernetes
|
||||||
|
version of the cluster.
|
||||||
|
type: string
|
||||||
kubernetesComponentsReference:
|
kubernetesComponentsReference:
|
||||||
description: KubernetesComponentsReference is a reference to the ConfigMap
|
description: KubernetesComponentsReference is a reference to the ConfigMap
|
||||||
containing the Kubernetes components to use for all nodes.
|
containing the Kubernetes components to use for all nodes.
|
||||||
|
@ -13,6 +13,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
mainconstants "github.com/edgelesssys/constellation/v2/internal/constants"
|
||||||
updatev1alpha1 "github.com/edgelesssys/constellation/v2/operators/constellation-node-operator/v2/api/v1alpha1"
|
updatev1alpha1 "github.com/edgelesssys/constellation/v2/operators/constellation-node-operator/v2/api/v1alpha1"
|
||||||
"github.com/edgelesssys/constellation/v2/operators/constellation-node-operator/v2/internal/constants"
|
"github.com/edgelesssys/constellation/v2/operators/constellation-node-operator/v2/internal/constants"
|
||||||
corev1 "k8s.io/api/core/v1"
|
corev1 "k8s.io/api/core/v1"
|
||||||
@ -113,7 +114,7 @@ func createAutoscalingStrategy(ctx context.Context, k8sClient client.Writer, pro
|
|||||||
|
|
||||||
// createNodeVersion creates the initial nodeversion resource if it does not exist yet.
|
// createNodeVersion creates the initial nodeversion resource if it does not exist yet.
|
||||||
func createNodeVersion(ctx context.Context, k8sClient client.Client, imageReference, imageVersion string) error {
|
func createNodeVersion(ctx context.Context, k8sClient client.Client, imageReference, imageVersion string) error {
|
||||||
k8sComponentsRef, err := findLatestK8sComponentsConfigMap(ctx, k8sClient)
|
latestComponentCM, err := findLatestK8sComponentsConfigMap(ctx, k8sClient)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("finding latest k8s-components configmap: %w", err)
|
return fmt.Errorf("finding latest k8s-components configmap: %w", err)
|
||||||
}
|
}
|
||||||
@ -125,7 +126,8 @@ func createNodeVersion(ctx context.Context, k8sClient client.Client, imageRefere
|
|||||||
Spec: updatev1alpha1.NodeVersionSpec{
|
Spec: updatev1alpha1.NodeVersionSpec{
|
||||||
ImageReference: imageReference,
|
ImageReference: imageReference,
|
||||||
ImageVersion: imageVersion,
|
ImageVersion: imageVersion,
|
||||||
KubernetesComponentsReference: k8sComponentsRef,
|
KubernetesComponentsReference: latestComponentCM.Name,
|
||||||
|
KubernetesClusterVersion: latestComponentCM.Data[mainconstants.K8sVersionFieldName],
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
if k8sErrors.IsAlreadyExists(err) {
|
if k8sErrors.IsAlreadyExists(err) {
|
||||||
@ -138,31 +140,29 @@ func createNodeVersion(ctx context.Context, k8sClient client.Client, imageRefere
|
|||||||
|
|
||||||
// findLatestK8sComponentsConfigMap finds most recently created k8s-components configmap in the kube-system namespace.
|
// findLatestK8sComponentsConfigMap finds most recently created k8s-components configmap in the kube-system namespace.
|
||||||
// It returns an error if there is no or multiple configmaps matching the prefix "k8s-components".
|
// It returns an error if there is no or multiple configmaps matching the prefix "k8s-components".
|
||||||
func findLatestK8sComponentsConfigMap(ctx context.Context, k8sClient client.Client) (string, error) {
|
func findLatestK8sComponentsConfigMap(ctx context.Context, k8sClient client.Client) (corev1.ConfigMap, error) {
|
||||||
var configMaps corev1.ConfigMapList
|
var configMaps corev1.ConfigMapList
|
||||||
err := k8sClient.List(ctx, &configMaps, client.InNamespace("kube-system"))
|
err := k8sClient.List(ctx, &configMaps, client.InNamespace("kube-system"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("listing configmaps: %w", err)
|
return corev1.ConfigMap{}, fmt.Errorf("listing configmaps: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// collect all k8s-components configmaps
|
// collect all k8s-components configmaps
|
||||||
componentConfigMaps := make(map[string]time.Time)
|
componentConfigMaps := []corev1.ConfigMap{}
|
||||||
for _, configMap := range configMaps.Items {
|
for _, configMap := range configMaps.Items {
|
||||||
if strings.HasPrefix(configMap.Name, "k8s-components") {
|
if strings.HasPrefix(configMap.Name, "k8s-components") {
|
||||||
componentConfigMaps[configMap.Name] = configMap.CreationTimestamp.Time
|
componentConfigMaps = append(componentConfigMaps, configMap)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if len(componentConfigMaps) == 0 {
|
if len(componentConfigMaps) == 0 {
|
||||||
return "", fmt.Errorf("no configmaps found")
|
return corev1.ConfigMap{}, fmt.Errorf("no configmaps found")
|
||||||
}
|
}
|
||||||
|
|
||||||
// find latest configmap
|
// find latest configmap
|
||||||
var latestConfigMap string
|
var latestConfigMap corev1.ConfigMap
|
||||||
var latestTime time.Time
|
for _, cm := range componentConfigMaps {
|
||||||
for configMap, creationTime := range componentConfigMaps {
|
if cm.CreationTimestamp.After(latestConfigMap.CreationTimestamp.Time) {
|
||||||
if creationTime.After(latestTime) {
|
latestConfigMap = cm
|
||||||
latestConfigMap = configMap
|
|
||||||
latestTime = creationTime
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return latestConfigMap, nil
|
return latestConfigMap, nil
|
||||||
|
@ -12,6 +12,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
mainconstants "github.com/edgelesssys/constellation/v2/internal/constants"
|
||||||
updatev1alpha1 "github.com/edgelesssys/constellation/v2/operators/constellation-node-operator/v2/api/v1alpha1"
|
updatev1alpha1 "github.com/edgelesssys/constellation/v2/operators/constellation-node-operator/v2/api/v1alpha1"
|
||||||
"github.com/edgelesssys/constellation/v2/operators/constellation-node-operator/v2/internal/constants"
|
"github.com/edgelesssys/constellation/v2/operators/constellation-node-operator/v2/internal/constants"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
@ -184,6 +185,7 @@ func TestCreateAutoscalingStrategy(t *testing.T) {
|
|||||||
|
|
||||||
func TestCreateNodeVersion(t *testing.T) {
|
func TestCreateNodeVersion(t *testing.T) {
|
||||||
k8sComponentsReference := "k8s-components-sha256-reference"
|
k8sComponentsReference := "k8s-components-sha256-reference"
|
||||||
|
k8sClusterVersion := "1.20.0"
|
||||||
testCases := map[string]struct {
|
testCases := map[string]struct {
|
||||||
createErr error
|
createErr error
|
||||||
existingNodeVersion *updatev1alpha1.NodeVersion
|
existingNodeVersion *updatev1alpha1.NodeVersion
|
||||||
@ -200,6 +202,7 @@ func TestCreateNodeVersion(t *testing.T) {
|
|||||||
ImageReference: "image-reference",
|
ImageReference: "image-reference",
|
||||||
ImageVersion: "image-version",
|
ImageVersion: "image-version",
|
||||||
KubernetesComponentsReference: k8sComponentsReference,
|
KubernetesComponentsReference: k8sComponentsReference,
|
||||||
|
KubernetesClusterVersion: k8sClusterVersion,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -218,6 +221,7 @@ func TestCreateNodeVersion(t *testing.T) {
|
|||||||
ImageReference: "image-reference2",
|
ImageReference: "image-reference2",
|
||||||
ImageVersion: "image-version2",
|
ImageVersion: "image-version2",
|
||||||
KubernetesComponentsReference: "components-reference2",
|
KubernetesComponentsReference: "components-reference2",
|
||||||
|
KubernetesClusterVersion: "cluster-version2",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
wantNodeVersion: &updatev1alpha1.NodeVersion{
|
wantNodeVersion: &updatev1alpha1.NodeVersion{
|
||||||
@ -229,6 +233,7 @@ func TestCreateNodeVersion(t *testing.T) {
|
|||||||
ImageReference: "image-reference2",
|
ImageReference: "image-reference2",
|
||||||
ImageVersion: "image-version2",
|
ImageVersion: "image-version2",
|
||||||
KubernetesComponentsReference: "components-reference2",
|
KubernetesComponentsReference: "components-reference2",
|
||||||
|
KubernetesClusterVersion: "cluster-version2",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -247,6 +252,9 @@ func TestCreateNodeVersion(t *testing.T) {
|
|||||||
Name: k8sComponentsReference,
|
Name: k8sComponentsReference,
|
||||||
CreationTimestamp: metav1.Time{Time: time.Unix(1, 0)},
|
CreationTimestamp: metav1.Time{Time: time.Unix(1, 0)},
|
||||||
},
|
},
|
||||||
|
Data: map[string]string{
|
||||||
|
mainconstants.K8sVersionFieldName: k8sClusterVersion,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user