mirror of
https://github.com/edgelesssys/constellation.git
synced 2025-05-04 15:25:00 -04:00
upgrade: support Kubernetes components (#839)
* upgrade: add Kubernetes components to NodeVersion * update rfc
This commit is contained in:
parent
4b43311fbd
commit
f14af0c3eb
56 changed files with 897 additions and 738 deletions
|
@ -12,9 +12,11 @@ import (
|
|||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
updatev1alpha1 "github.com/edgelesssys/constellation/operators/constellation-node-operator/v2/api/v1alpha1"
|
||||
"github.com/edgelesssys/constellation/operators/constellation-node-operator/v2/internal/constants"
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
k8sErrors "k8s.io/apimachinery/pkg/api/errors"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||
|
@ -22,7 +24,7 @@ import (
|
|||
)
|
||||
|
||||
// InitialResources creates the initial resources for the node operator.
|
||||
func InitialResources(ctx context.Context, k8sClient client.Writer, imageInfo imageInfoGetter, scalingGroupGetter scalingGroupGetter, uid string) error {
|
||||
func InitialResources(ctx context.Context, k8sClient client.Client, imageInfo imageInfoGetter, scalingGroupGetter scalingGroupGetter, uid string) error {
|
||||
logr := log.FromContext(ctx)
|
||||
controlPlaneGroupIDs, workerGroupIDs, err := scalingGroupGetter.ListScalingGroups(ctx, uid)
|
||||
if err != nil {
|
||||
|
@ -50,8 +52,8 @@ func InitialResources(ctx context.Context, k8sClient client.Writer, imageInfo im
|
|||
imageVersion = ""
|
||||
}
|
||||
|
||||
if err := createNodeImage(ctx, k8sClient, imageReference, imageVersion); err != nil {
|
||||
return fmt.Errorf("creating initial node image %q: %w", imageReference, err)
|
||||
if err := createNodeVersion(ctx, k8sClient, imageReference, imageVersion); err != nil {
|
||||
return fmt.Errorf("creating initial node version %q: %w", imageReference, err)
|
||||
}
|
||||
for _, groupID := range controlPlaneGroupIDs {
|
||||
groupName, err := scalingGroupGetter.GetScalingGroupName(groupID)
|
||||
|
@ -110,22 +112,61 @@ func createAutoscalingStrategy(ctx context.Context, k8sClient client.Writer, pro
|
|||
return err
|
||||
}
|
||||
|
||||
// createNodeImage creates the initial nodeimage resource if it does not exist yet.
|
||||
func createNodeImage(ctx context.Context, k8sClient client.Writer, imageReference, imageVersion string) error {
|
||||
err := k8sClient.Create(ctx, &updatev1alpha1.NodeImage{
|
||||
TypeMeta: metav1.TypeMeta{APIVersion: "update.edgeless.systems/v1alpha1", Kind: "NodeImage"},
|
||||
// createNodeVersion creates the initial nodeversion resource if it does not exist yet.
|
||||
func createNodeVersion(ctx context.Context, k8sClient client.Client, imageReference, imageVersion string) error {
|
||||
k8sComponentsRef, err := findLatestK8sComponentsConfigMap(ctx, k8sClient)
|
||||
if err != nil {
|
||||
return fmt.Errorf("finding latest k8s-components configmap: %w", err)
|
||||
}
|
||||
err = k8sClient.Create(ctx, &updatev1alpha1.NodeVersion{
|
||||
TypeMeta: metav1.TypeMeta{APIVersion: "update.edgeless.systems/v1alpha1", Kind: "NodeVersion"},
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: constants.NodeImageResourceName,
|
||||
Name: constants.NodeVersionResourceName,
|
||||
},
|
||||
Spec: updatev1alpha1.NodeImageSpec{
|
||||
ImageReference: imageReference,
|
||||
ImageVersion: imageVersion,
|
||||
Spec: updatev1alpha1.NodeVersionSpec{
|
||||
ImageReference: imageReference,
|
||||
ImageVersion: imageVersion,
|
||||
KubernetesComponentsReference: k8sComponentsRef,
|
||||
},
|
||||
})
|
||||
if k8sErrors.IsAlreadyExists(err) {
|
||||
return nil
|
||||
} else if err != nil {
|
||||
return err
|
||||
}
|
||||
return err
|
||||
return nil
|
||||
}
|
||||
|
||||
// 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".
|
||||
func findLatestK8sComponentsConfigMap(ctx context.Context, k8sClient client.Client) (string, error) {
|
||||
var configMaps corev1.ConfigMapList
|
||||
err := k8sClient.List(ctx, &configMaps, client.InNamespace("kube-system"))
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("listing configmaps: %w", err)
|
||||
}
|
||||
|
||||
// collect all k8s-components configmaps
|
||||
componentConfigMaps := make(map[string]time.Time)
|
||||
for _, configMap := range configMaps.Items {
|
||||
if strings.HasPrefix(configMap.Name, "k8s-components") {
|
||||
componentConfigMaps[configMap.Name] = configMap.CreationTimestamp.Time
|
||||
}
|
||||
}
|
||||
if len(componentConfigMaps) == 0 {
|
||||
return "", fmt.Errorf("no configmaps found")
|
||||
}
|
||||
|
||||
// find latest configmap
|
||||
var latestConfigMap string
|
||||
var latestTime time.Time
|
||||
for configMap, creationTime := range componentConfigMaps {
|
||||
if creationTime.After(latestTime) {
|
||||
latestConfigMap = configMap
|
||||
latestTime = creationTime
|
||||
}
|
||||
}
|
||||
return latestConfigMap, nil
|
||||
}
|
||||
|
||||
// createScalingGroup creates an initial scaling group resource if it does not exist yet.
|
||||
|
@ -136,7 +177,7 @@ func createScalingGroup(ctx context.Context, config newScalingGroupConfig) error
|
|||
Name: strings.ToLower(config.groupName),
|
||||
},
|
||||
Spec: updatev1alpha1.ScalingGroupSpec{
|
||||
NodeImage: constants.NodeImageResourceName,
|
||||
NodeVersion: constants.NodeVersionResourceName,
|
||||
GroupID: config.groupID,
|
||||
AutoscalerGroupName: config.autoscalingGroupName,
|
||||
Min: 1,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue