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"
|
|
|
|
"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"
|
|
|
|
"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)
|
2022-07-29 09:00:15 -04:00
|
|
|
controlPlaneGroupIDs, workerGroupIDs, err := scalingGroupGetter.ListScalingGroups(ctx, uid)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("listing scaling groups: %w", err)
|
|
|
|
}
|
|
|
|
if len(controlPlaneGroupIDs) == 0 {
|
|
|
|
return errors.New("determining initial node image: no control plane scaling group found")
|
|
|
|
}
|
|
|
|
if len(workerGroupIDs) == 0 {
|
|
|
|
return errors.New("determining initial node image: no worker scaling group found")
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
imageReference, err := scalingGroupGetter.GetScalingGroupImage(ctx, controlPlaneGroupIDs[0])
|
|
|
|
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
|
|
|
}
|
|
|
|
for _, groupID := range controlPlaneGroupIDs {
|
2022-09-15 10:48:32 -04:00
|
|
|
groupName, err := scalingGroupGetter.GetScalingGroupName(groupID)
|
2022-07-29 09:00:15 -04:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("determining scaling group name of %q: %w", groupID, err)
|
|
|
|
}
|
2022-09-15 10:48:32 -04:00
|
|
|
autoscalingGroupName, err := scalingGroupGetter.GetAutoscalingGroupName(groupID)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("determining autoscaling group name of %q: %w", groupID, err)
|
|
|
|
}
|
2022-11-21 12:01:23 -05:00
|
|
|
newScalingGroupConfig := newScalingGroupConfig{k8sClient, groupID, groupName, autoscalingGroupName, updatev1alpha1.ControlPlaneRole}
|
|
|
|
if err := createScalingGroup(ctx, newScalingGroupConfig); err != nil {
|
2022-07-29 09:00:15 -04:00
|
|
|
return fmt.Errorf("creating initial control plane scaling group: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, groupID := range workerGroupIDs {
|
2022-09-15 10:48:32 -04:00
|
|
|
groupName, err := scalingGroupGetter.GetScalingGroupName(groupID)
|
2022-07-29 09:00:15 -04:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("determining scaling group name of %q: %w", groupID, err)
|
|
|
|
}
|
2022-09-15 10:48:32 -04:00
|
|
|
autoscalingGroupName, err := scalingGroupGetter.GetAutoscalingGroupName(groupID)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("determining autoscaling group name of %q: %w", groupID, err)
|
|
|
|
}
|
2022-11-21 12:01:23 -05:00
|
|
|
newScalingGroupConfig := newScalingGroupConfig{k8sClient, groupID, groupName, autoscalingGroupName, updatev1alpha1.WorkerRole}
|
|
|
|
if err := createScalingGroup(ctx, newScalingGroupConfig); err != nil {
|
2022-07-29 09:00:15 -04:00
|
|
|
return fmt.Errorf("creating initial worker scaling group: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
|
|
|
// createScalingGroup creates an initial scaling group resource if it does not exist yet.
|
2022-11-21 12:01:23 -05:00
|
|
|
func createScalingGroup(ctx context.Context, config newScalingGroupConfig) error {
|
|
|
|
err := config.k8sClient.Create(ctx, &updatev1alpha1.ScalingGroup{
|
2022-07-29 09:00:15 -04:00
|
|
|
TypeMeta: metav1.TypeMeta{APIVersion: "update.edgeless.systems/v1alpha1", Kind: "ScalingGroup"},
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2022-11-21 12:01:23 -05:00
|
|
|
Name: strings.ToLower(config.groupName),
|
2022-07-29 09:00:15 -04:00
|
|
|
},
|
|
|
|
Spec: updatev1alpha1.ScalingGroupSpec{
|
2023-02-09 09:54:12 -05:00
|
|
|
NodeVersion: mainconstants.NodeVersionResourceName,
|
2022-11-21 12:01:23 -05:00
|
|
|
GroupID: config.groupID,
|
|
|
|
AutoscalerGroupName: config.autoscalingGroupName,
|
2022-09-15 10:48:32 -04:00
|
|
|
Min: 1,
|
|
|
|
Max: 10,
|
2022-11-21 12:01:23 -05:00
|
|
|
Role: config.role,
|
2022-07-29 09:00:15 -04:00
|
|
|
},
|
|
|
|
})
|
|
|
|
if k8sErrors.IsAlreadyExists(err) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
ListScalingGroups(ctx context.Context, uid string) (controlPlaneGroupIDs []string, workerGroupIDs []string, err 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
|
|
|
}
|
2022-11-21 12:01:23 -05:00
|
|
|
|
|
|
|
type newScalingGroupConfig struct {
|
|
|
|
k8sClient client.Writer
|
|
|
|
groupID string
|
|
|
|
groupName string
|
|
|
|
autoscalingGroupName string
|
|
|
|
role updatev1alpha1.NodeRole
|
|
|
|
}
|