2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-07-29 09:00:15 -04:00
|
|
|
// Package deploy provides functions to deploy initial resources for the node operator.
|
|
|
|
package deploy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2022-09-15 10:48:32 -04:00
|
|
|
"strings"
|
2022-07-29 09:00:15 -04:00
|
|
|
|
2023-01-04 17:28:24 -05:00
|
|
|
mainconstants "github.com/edgelesssys/constellation/v2/internal/constants"
|
2023-01-04 13:04:28 -05:00
|
|
|
updatev1alpha1 "github.com/edgelesssys/constellation/v2/operators/constellation-node-operator/v2/api/v1alpha1"
|
2023-07-05 01:27:34 -04:00
|
|
|
cspapi "github.com/edgelesssys/constellation/v2/operators/constellation-node-operator/v2/internal/cloud/api"
|
2023-01-04 13:04:28 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/operators/constellation-node-operator/v2/internal/constants"
|
2023-01-03 06:09:53 -05:00
|
|
|
corev1 "k8s.io/api/core/v1"
|
2022-07-29 09:00:15 -04:00
|
|
|
k8sErrors "k8s.io/apimachinery/pkg/api/errors"
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2023-06-06 09:22:06 -04:00
|
|
|
"k8s.io/apimachinery/pkg/types"
|
2022-07-29 09:00:15 -04:00
|
|
|
"sigs.k8s.io/controller-runtime/pkg/client"
|
2022-11-25 08:49:26 -05:00
|
|
|
"sigs.k8s.io/controller-runtime/pkg/log"
|
2022-07-29 09:00:15 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// InitialResources creates the initial resources for the node operator.
|
2023-01-03 06:09:53 -05:00
|
|
|
func InitialResources(ctx context.Context, k8sClient client.Client, imageInfo imageInfoGetter, scalingGroupGetter scalingGroupGetter, uid string) error {
|
2022-11-25 08:49:26 -05:00
|
|
|
logr := log.FromContext(ctx)
|
2023-06-06 09:22:06 -04:00
|
|
|
|
|
|
|
if err := cleanupPlaceholders(ctx, k8sClient); err != nil {
|
|
|
|
return fmt.Errorf("cleaning up placeholder node version: %w", err)
|
|
|
|
}
|
|
|
|
logr.Info("cleaned up placeholders")
|
|
|
|
|
2023-07-05 01:27:34 -04:00
|
|
|
scalingGroups, err := scalingGroupGetter.ListScalingGroups(ctx, uid)
|
2022-07-29 09:00:15 -04:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("listing scaling groups: %w", err)
|
|
|
|
}
|
2023-07-05 01:27:34 -04:00
|
|
|
if len(scalingGroups) == 0 {
|
|
|
|
return errors.New("determining initial node image: no scaling group found")
|
2022-07-29 09:00:15 -04:00
|
|
|
}
|
|
|
|
|
2022-09-15 10:48:32 -04:00
|
|
|
if err := createAutoscalingStrategy(ctx, k8sClient, scalingGroupGetter.AutoscalingCloudProvider()); err != nil {
|
2022-07-29 09:00:15 -04:00
|
|
|
return fmt.Errorf("creating initial autoscaling strategy: %w", err)
|
|
|
|
}
|
2023-07-05 01:27:34 -04:00
|
|
|
imageReference, err := scalingGroupGetter.GetScalingGroupImage(ctx, scalingGroups[0].GroupID)
|
2022-07-29 09:00:15 -04:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("determining initial node image: %w", err)
|
|
|
|
}
|
2023-01-04 10:44:47 -05:00
|
|
|
imageVersion, err := imageInfo.ImageVersion()
|
2022-11-25 08:49:26 -05:00
|
|
|
if err != nil {
|
|
|
|
// do not fail if the image version cannot be determined
|
|
|
|
// this is important for backwards compatibility
|
|
|
|
logr.Error(err, "determining initial node image version")
|
|
|
|
imageVersion = ""
|
|
|
|
}
|
|
|
|
|
2023-01-03 06:09:53 -05:00
|
|
|
if err := createNodeVersion(ctx, k8sClient, imageReference, imageVersion); err != nil {
|
|
|
|
return fmt.Errorf("creating initial node version %q: %w", imageReference, err)
|
2022-07-29 09:00:15 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// createAutoscalingStrategy creates the autoscaling strategy resource if it does not exist yet.
|
2022-09-15 10:48:32 -04:00
|
|
|
func createAutoscalingStrategy(ctx context.Context, k8sClient client.Writer, provider string) error {
|
2022-07-29 09:00:15 -04:00
|
|
|
err := k8sClient.Create(ctx, &updatev1alpha1.AutoscalingStrategy{
|
|
|
|
TypeMeta: metav1.TypeMeta{APIVersion: "update.edgeless.systems/v1alpha1", Kind: "AutoscalingStrategy"},
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Name: constants.AutoscalingStrategyResourceName,
|
|
|
|
},
|
|
|
|
Spec: updatev1alpha1.AutoscalingStrategySpec{
|
|
|
|
Enabled: true,
|
|
|
|
DeploymentName: "constellation-cluster-autoscaler",
|
|
|
|
DeploymentNamespace: "kube-system",
|
2022-09-15 10:48:32 -04:00
|
|
|
AutoscalerExtraArgs: map[string]string{
|
|
|
|
"cloud-provider": provider,
|
|
|
|
"logtostderr": "true",
|
|
|
|
"stderrthreshold": "info",
|
|
|
|
"v": "2",
|
|
|
|
"namespace": "kube-system",
|
|
|
|
},
|
2022-07-29 09:00:15 -04:00
|
|
|
},
|
|
|
|
})
|
|
|
|
if k8sErrors.IsAlreadyExists(err) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-01-03 06:09:53 -05:00
|
|
|
// createNodeVersion creates the initial nodeversion resource if it does not exist yet.
|
|
|
|
func createNodeVersion(ctx context.Context, k8sClient client.Client, imageReference, imageVersion string) error {
|
2023-01-04 17:28:24 -05:00
|
|
|
latestComponentCM, err := findLatestK8sComponentsConfigMap(ctx, k8sClient)
|
2023-01-03 06:09:53 -05:00
|
|
|
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"},
|
2022-07-29 09:00:15 -04:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2023-02-09 09:54:12 -05:00
|
|
|
Name: mainconstants.NodeVersionResourceName,
|
2022-07-29 09:00:15 -04:00
|
|
|
},
|
2023-01-03 06:09:53 -05:00
|
|
|
Spec: updatev1alpha1.NodeVersionSpec{
|
|
|
|
ImageReference: imageReference,
|
|
|
|
ImageVersion: imageVersion,
|
2023-01-04 17:28:24 -05:00
|
|
|
KubernetesComponentsReference: latestComponentCM.Name,
|
|
|
|
KubernetesClusterVersion: latestComponentCM.Data[mainconstants.K8sVersionFieldName],
|
2022-07-29 09:00:15 -04:00
|
|
|
},
|
|
|
|
})
|
|
|
|
if k8sErrors.IsAlreadyExists(err) {
|
|
|
|
return nil
|
2023-01-03 06:09:53 -05:00
|
|
|
} else if err != nil {
|
|
|
|
return err
|
2022-07-29 09:00:15 -04:00
|
|
|
}
|
2023-01-03 06:09:53 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-06-06 09:22:06 -04:00
|
|
|
// cleanupPlaceholders deletes the existing resources from older operator versions if they are placeholders.
|
|
|
|
func cleanupPlaceholders(ctx context.Context, k8sClient client.Client) error {
|
|
|
|
if err := cleanupPlaceholderAutoscalingStrategy(ctx, k8sClient); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := cleanupPlaceholderScalingGroups(ctx, k8sClient); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return cleanupPlaceholderNodeVersion(ctx, k8sClient)
|
|
|
|
}
|
|
|
|
|
|
|
|
func cleanupPlaceholderAutoscalingStrategy(ctx context.Context, k8sClient client.Client) error {
|
|
|
|
logr := log.FromContext(ctx)
|
|
|
|
autoscalingStrategy := &updatev1alpha1.AutoscalingStrategy{}
|
|
|
|
err := k8sClient.Get(ctx, types.NamespacedName{Name: constants.AutoscalingStrategyResourceName}, autoscalingStrategy)
|
|
|
|
if k8sErrors.IsNotFound(err) {
|
|
|
|
logr.Info("no old autoscalingstrategy resource found - skipping cleanup", "name", constants.AutoscalingStrategyResourceName)
|
|
|
|
return nil
|
|
|
|
} else if err != nil {
|
|
|
|
logr.Info("cleaning up old autoscalingstrategy resource", "name", constants.AutoscalingStrategyResourceName, "error", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if autoscalingStrategy.Spec.AutoscalerExtraArgs["cloud-provider"] != constants.PlaceholderImageName {
|
|
|
|
logr.Info("old autoscalingstrategy resource is not a placeholder - skipping cleanup", "name", constants.AutoscalingStrategyResourceName)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
logr.Info("deleting old autoscalingstrategy resource", "name", constants.AutoscalingStrategyResourceName)
|
|
|
|
return k8sClient.Delete(ctx, autoscalingStrategy)
|
|
|
|
}
|
|
|
|
|
|
|
|
// cleanupPlaceholderScalingGroups deletes the existing scalinggroup resource from older operator versions if they are placeholders.
|
|
|
|
func cleanupPlaceholderScalingGroups(ctx context.Context, k8sClient client.Client) error {
|
|
|
|
logr := log.FromContext(ctx)
|
|
|
|
names := []string{constants.PlaceholderControlPlaneScalingGroupName, constants.PlaceholderWorkerScalingGroupName}
|
|
|
|
for _, name := range names {
|
|
|
|
scalingGroup := &updatev1alpha1.ScalingGroup{}
|
|
|
|
err := k8sClient.Get(ctx, types.NamespacedName{Name: name}, scalingGroup)
|
|
|
|
if k8sErrors.IsNotFound(err) {
|
|
|
|
logr.Info("no old scalinggroup resource found - skipping cleanup", "name", name)
|
|
|
|
continue
|
|
|
|
} else if err != nil {
|
|
|
|
logr.Info("cleaning up old scalinggroup resource", "name", name, "error", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if scalingGroup.Spec.AutoscalerGroupName != name || scalingGroup.Spec.GroupID != name {
|
|
|
|
logr.Info("real scalinggroup resource found - skipping cleanup", "name", name)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
logr.Info("cleaning up old scalinggroup resource")
|
|
|
|
if err := k8sClient.Delete(ctx, scalingGroup); err != nil {
|
|
|
|
logr.Info("cleaning up old scalinggroup resource", "name", name, "error", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// cleanupPlaceholder deletes the existing nodeversion resource from older operator versions if it was a placeholder.
|
|
|
|
func cleanupPlaceholderNodeVersion(ctx context.Context, k8sClient client.Client) error {
|
|
|
|
logr := log.FromContext(ctx)
|
|
|
|
nodeVersion := &updatev1alpha1.NodeVersion{}
|
|
|
|
err := k8sClient.Get(ctx, types.NamespacedName{Name: mainconstants.NodeVersionResourceName}, nodeVersion)
|
|
|
|
if k8sErrors.IsNotFound(err) {
|
|
|
|
logr.Info("no old nodeversion resource found - skipping cleanup")
|
|
|
|
return nil
|
|
|
|
} else if err != nil {
|
|
|
|
logr.Info("cleaning up old nodeversion resource", "error", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if nodeVersion.Spec.ImageReference != constants.PlaceholderImageName {
|
|
|
|
logr.Info("real nodeversion resource found - skipping cleanup")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
logr.Info("cleaning up old nodeversion resource")
|
|
|
|
return k8sClient.Delete(ctx, nodeVersion)
|
|
|
|
}
|
|
|
|
|
2023-01-03 06:09:53 -05:00
|
|
|
// 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".
|
2023-01-04 17:28:24 -05:00
|
|
|
func findLatestK8sComponentsConfigMap(ctx context.Context, k8sClient client.Client) (corev1.ConfigMap, error) {
|
2023-01-03 06:09:53 -05:00
|
|
|
var configMaps corev1.ConfigMapList
|
|
|
|
err := k8sClient.List(ctx, &configMaps, client.InNamespace("kube-system"))
|
|
|
|
if err != nil {
|
2023-01-04 17:28:24 -05:00
|
|
|
return corev1.ConfigMap{}, fmt.Errorf("listing configmaps: %w", err)
|
2023-01-03 06:09:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// collect all k8s-components configmaps
|
2023-01-04 17:28:24 -05:00
|
|
|
componentConfigMaps := []corev1.ConfigMap{}
|
2023-01-03 06:09:53 -05:00
|
|
|
for _, configMap := range configMaps.Items {
|
|
|
|
if strings.HasPrefix(configMap.Name, "k8s-components") {
|
2023-01-04 17:28:24 -05:00
|
|
|
componentConfigMaps = append(componentConfigMaps, configMap)
|
2023-01-03 06:09:53 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(componentConfigMaps) == 0 {
|
2023-01-04 17:28:24 -05:00
|
|
|
return corev1.ConfigMap{}, fmt.Errorf("no configmaps found")
|
2023-01-03 06:09:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// find latest configmap
|
2023-01-04 17:28:24 -05:00
|
|
|
var latestConfigMap corev1.ConfigMap
|
|
|
|
for _, cm := range componentConfigMaps {
|
|
|
|
if cm.CreationTimestamp.After(latestConfigMap.CreationTimestamp.Time) {
|
|
|
|
latestConfigMap = cm
|
2023-01-03 06:09:53 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return latestConfigMap, nil
|
2022-07-29 09:00:15 -04:00
|
|
|
}
|
|
|
|
|
2022-11-25 08:49:26 -05:00
|
|
|
type imageInfoGetter interface {
|
2023-01-04 10:44:47 -05:00
|
|
|
ImageVersion() (string, error)
|
2022-11-25 08:49:26 -05:00
|
|
|
}
|
|
|
|
|
2022-07-29 09:00:15 -04:00
|
|
|
type scalingGroupGetter interface {
|
|
|
|
// GetScalingGroupImage retrieves the image currently used by a scaling group.
|
|
|
|
GetScalingGroupImage(ctx context.Context, scalingGroupID string) (string, error)
|
|
|
|
// GetScalingGroupName retrieves the name of a scaling group.
|
2022-09-15 10:48:32 -04:00
|
|
|
GetScalingGroupName(scalingGroupID string) (string, error)
|
|
|
|
// GetScalingGroupName retrieves the name of a scaling group as needed by the cluster-autoscaler.
|
|
|
|
GetAutoscalingGroupName(scalingGroupID string) (string, error)
|
2022-07-29 09:00:15 -04:00
|
|
|
// ListScalingGroups retrieves a list of scaling groups for the cluster.
|
2023-07-05 01:27:34 -04:00
|
|
|
ListScalingGroups(ctx context.Context, uid string) ([]cspapi.ScalingGroup, error)
|
2022-09-15 10:48:32 -04:00
|
|
|
// AutoscalingCloudProvider returns the cloud-provider name as used by k8s cluster-autoscaler.
|
|
|
|
AutoscalingCloudProvider() string
|
2022-07-29 09:00:15 -04:00
|
|
|
}
|