2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-08-29 10:49:44 -04:00
|
|
|
package cloudcmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2022-12-19 10:52:15 -05:00
|
|
|
"time"
|
2022-08-29 10:49:44 -04:00
|
|
|
|
2022-11-24 10:39:33 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/cli/internal/helm"
|
2022-11-15 09:40:49 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/attestation/measurements"
|
2022-12-19 10:52:15 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/config"
|
2022-09-21 07:47:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/constants"
|
2022-12-19 02:08:46 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/kubernetes/kubectl"
|
2022-08-29 10:49:44 -04:00
|
|
|
corev1 "k8s.io/api/core/v1"
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
|
|
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
|
|
|
"k8s.io/client-go/dynamic"
|
|
|
|
"k8s.io/client-go/kubernetes"
|
|
|
|
"k8s.io/client-go/tools/clientcmd"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Upgrader handles upgrading the cluster's components using the CLI.
|
|
|
|
type Upgrader struct {
|
2022-11-24 10:39:33 -05:00
|
|
|
stableInterface stableInterface
|
|
|
|
dynamicInterface dynamicInterface
|
|
|
|
helmClient helmInterface
|
2022-08-29 10:49:44 -04:00
|
|
|
|
2022-11-10 04:27:24 -05:00
|
|
|
outWriter io.Writer
|
2022-08-29 10:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewUpgrader returns a new Upgrader.
|
2022-12-19 10:52:15 -05:00
|
|
|
func NewUpgrader(outWriter io.Writer, log debugLog) (*Upgrader, error) {
|
2022-08-29 10:49:44 -04:00
|
|
|
kubeConfig, err := clientcmd.BuildConfigFromFlags("", constants.AdminConfFilename)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("building kubernetes config: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
kubeClient, err := kubernetes.NewForConfig(kubeConfig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("setting up kubernetes client: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// use unstructured client to avoid importing the operator packages
|
|
|
|
unstructuredClient, err := dynamic.NewForConfig(kubeConfig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("setting up custom resource client: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-12-19 02:08:46 -05:00
|
|
|
helmClient, err := helm.NewClient(kubectl.New(), constants.AdminConfFilename, constants.HelmNamespace, log)
|
2022-11-24 10:39:33 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("setting up helm client: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-08-29 10:49:44 -04:00
|
|
|
return &Upgrader{
|
2022-11-24 10:39:33 -05:00
|
|
|
stableInterface: &stableClient{client: kubeClient},
|
|
|
|
dynamicInterface: &dynamicClient{client: unstructuredClient},
|
|
|
|
helmClient: helmClient,
|
|
|
|
outWriter: outWriter,
|
2022-08-29 10:49:44 -04:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Upgrade upgrades the cluster to the given measurements and image.
|
2022-11-29 05:39:07 -05:00
|
|
|
func (u *Upgrader) Upgrade(ctx context.Context, imageReference, imageVersion string, measurements measurements.M) error {
|
2022-08-29 10:49:44 -04:00
|
|
|
if err := u.updateMeasurements(ctx, measurements); err != nil {
|
|
|
|
return fmt.Errorf("updating measurements: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-11-29 05:39:07 -05:00
|
|
|
if err := u.updateImage(ctx, imageReference, imageVersion); err != nil {
|
2022-08-29 10:49:44 -04:00
|
|
|
return fmt.Errorf("updating image: %w", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-11-29 05:39:07 -05:00
|
|
|
// GetCurrentImage returns the currently used image version of the cluster.
|
2022-08-31 05:59:07 -04:00
|
|
|
func (u *Upgrader) GetCurrentImage(ctx context.Context) (*unstructured.Unstructured, string, error) {
|
2023-01-11 10:07:07 -05:00
|
|
|
imageStruct, err := u.dynamicInterface.getCurrent(ctx, "constellation-version")
|
2022-08-31 05:59:07 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
spec, ok := imageStruct.Object["spec"]
|
|
|
|
if !ok {
|
|
|
|
return nil, "", errors.New("image spec missing")
|
|
|
|
}
|
|
|
|
retErr := errors.New("invalid image spec")
|
|
|
|
specMap, ok := spec.(map[string]any)
|
|
|
|
if !ok {
|
|
|
|
return nil, "", retErr
|
|
|
|
}
|
2022-11-29 05:39:07 -05:00
|
|
|
currentImageVersion, ok := specMap["imageVersion"]
|
2022-08-31 05:59:07 -04:00
|
|
|
if !ok {
|
|
|
|
return nil, "", retErr
|
|
|
|
}
|
2022-11-29 05:39:07 -05:00
|
|
|
imageVersion, ok := currentImageVersion.(string)
|
2022-08-31 05:59:07 -04:00
|
|
|
if !ok {
|
|
|
|
return nil, "", retErr
|
|
|
|
}
|
|
|
|
|
2022-11-29 05:39:07 -05:00
|
|
|
return imageStruct, imageVersion, nil
|
2022-08-31 05:59:07 -04:00
|
|
|
}
|
|
|
|
|
2022-12-19 10:52:15 -05:00
|
|
|
// UpgradeHelmServices upgrade helm services.
|
2023-01-04 07:55:10 -05:00
|
|
|
func (u *Upgrader) UpgradeHelmServices(ctx context.Context, config *config.Config, timeout time.Duration, allowDestructive bool) error {
|
|
|
|
return u.helmClient.Upgrade(ctx, config, timeout, allowDestructive)
|
2022-12-19 10:52:15 -05:00
|
|
|
}
|
|
|
|
|
2022-11-24 10:39:33 -05:00
|
|
|
// KubernetesVersion returns the version of Kubernetes the Constellation is currently running on.
|
|
|
|
func (u *Upgrader) KubernetesVersion() (string, error) {
|
|
|
|
return u.stableInterface.kubernetesVersion()
|
|
|
|
}
|
|
|
|
|
2022-11-15 09:40:49 -05:00
|
|
|
func (u *Upgrader) updateMeasurements(ctx context.Context, newMeasurements measurements.M) error {
|
2022-11-24 10:39:33 -05:00
|
|
|
existingConf, err := u.stableInterface.getCurrent(ctx, constants.JoinConfigMap)
|
2022-08-29 10:49:44 -04:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("retrieving current measurements: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-11-15 09:40:49 -05:00
|
|
|
var currentMeasurements measurements.M
|
2022-08-29 10:49:44 -04:00
|
|
|
if err := json.Unmarshal([]byte(existingConf.Data[constants.MeasurementsFilename]), ¤tMeasurements); err != nil {
|
|
|
|
return fmt.Errorf("retrieving current measurements: %w", err)
|
|
|
|
}
|
2022-11-15 09:40:49 -05:00
|
|
|
if currentMeasurements.EqualTo(newMeasurements) {
|
|
|
|
fmt.Fprintln(u.outWriter, "Cluster is already using the chosen measurements, skipping measurements upgrade")
|
|
|
|
return nil
|
2022-08-29 10:49:44 -04:00
|
|
|
}
|
|
|
|
|
2022-11-24 04:57:58 -05:00
|
|
|
// don't allow potential security downgrades by setting the warnOnly flag to true
|
|
|
|
for k, newM := range newMeasurements {
|
|
|
|
if currentM, ok := currentMeasurements[k]; ok && !currentM.WarnOnly && newM.WarnOnly {
|
|
|
|
return fmt.Errorf("setting enforced measurement %d to warn only: not allowed", k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-29 10:49:44 -04:00
|
|
|
// backup of previous measurements
|
|
|
|
existingConf.Data["oldMeasurements"] = existingConf.Data[constants.MeasurementsFilename]
|
|
|
|
|
2022-11-15 09:40:49 -05:00
|
|
|
measurementsJSON, err := json.Marshal(newMeasurements)
|
2022-08-29 10:49:44 -04:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("marshaling measurements: %w", err)
|
|
|
|
}
|
|
|
|
existingConf.Data[constants.MeasurementsFilename] = string(measurementsJSON)
|
2022-11-24 10:39:33 -05:00
|
|
|
_, err = u.stableInterface.update(ctx, existingConf)
|
2022-08-29 10:49:44 -04:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("setting new measurements: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-11-10 04:27:24 -05:00
|
|
|
fmt.Fprintln(u.outWriter, "Successfully updated the cluster's expected measurements")
|
2022-08-29 10:49:44 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-11-29 05:39:07 -05:00
|
|
|
func (u *Upgrader) updateImage(ctx context.Context, imageReference, imageVersion string) error {
|
|
|
|
currentImage, currentImageVersion, err := u.GetCurrentImage(ctx)
|
2022-08-29 10:49:44 -04:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("retrieving current image: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-11-29 05:39:07 -05:00
|
|
|
if currentImageVersion == imageVersion {
|
2022-11-10 04:27:24 -05:00
|
|
|
fmt.Fprintln(u.outWriter, "Cluster is already using the chosen image, skipping image upgrade")
|
2022-08-29 10:49:44 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-11-29 05:39:07 -05:00
|
|
|
currentImage.Object["spec"].(map[string]any)["image"] = imageReference
|
|
|
|
currentImage.Object["spec"].(map[string]any)["imageVersion"] = imageVersion
|
2022-11-24 10:39:33 -05:00
|
|
|
if _, err := u.dynamicInterface.update(ctx, currentImage); err != nil {
|
2022-08-29 10:49:44 -04:00
|
|
|
return fmt.Errorf("setting new image: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-11-10 04:27:24 -05:00
|
|
|
fmt.Fprintln(u.outWriter, "Successfully updated the cluster's image, upgrades will be applied automatically")
|
2022-08-29 10:49:44 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-11-24 10:39:33 -05:00
|
|
|
type dynamicInterface interface {
|
2022-08-29 10:49:44 -04:00
|
|
|
getCurrent(ctx context.Context, name string) (*unstructured.Unstructured, error)
|
|
|
|
update(ctx context.Context, obj *unstructured.Unstructured) (*unstructured.Unstructured, error)
|
|
|
|
}
|
|
|
|
|
2022-11-24 10:39:33 -05:00
|
|
|
type stableInterface interface {
|
2022-08-29 10:49:44 -04:00
|
|
|
getCurrent(ctx context.Context, name string) (*corev1.ConfigMap, error)
|
|
|
|
update(ctx context.Context, configMap *corev1.ConfigMap) (*corev1.ConfigMap, error)
|
2022-11-24 10:39:33 -05:00
|
|
|
kubernetesVersion() (string, error)
|
2022-08-29 10:49:44 -04:00
|
|
|
}
|
|
|
|
|
2022-11-24 10:39:33 -05:00
|
|
|
type dynamicClient struct {
|
2022-08-29 10:49:44 -04:00
|
|
|
client dynamic.Interface
|
|
|
|
}
|
|
|
|
|
|
|
|
// getCurrent returns the current image definition.
|
2022-11-24 10:39:33 -05:00
|
|
|
func (u *dynamicClient) getCurrent(ctx context.Context, name string) (*unstructured.Unstructured, error) {
|
2022-08-29 10:49:44 -04:00
|
|
|
return u.client.Resource(schema.GroupVersionResource{
|
|
|
|
Group: "update.edgeless.systems",
|
|
|
|
Version: "v1alpha1",
|
2023-01-11 10:07:07 -05:00
|
|
|
Resource: "nodeversions",
|
2022-08-29 10:49:44 -04:00
|
|
|
}).Get(ctx, name, metav1.GetOptions{})
|
|
|
|
}
|
|
|
|
|
|
|
|
// update updates the image definition.
|
2022-11-24 10:39:33 -05:00
|
|
|
func (u *dynamicClient) update(ctx context.Context, obj *unstructured.Unstructured) (*unstructured.Unstructured, error) {
|
2022-08-29 10:49:44 -04:00
|
|
|
return u.client.Resource(schema.GroupVersionResource{
|
|
|
|
Group: "update.edgeless.systems",
|
|
|
|
Version: "v1alpha1",
|
2023-01-11 10:07:07 -05:00
|
|
|
Resource: "nodeversions",
|
2022-08-29 10:49:44 -04:00
|
|
|
}).Update(ctx, obj, metav1.UpdateOptions{})
|
|
|
|
}
|
|
|
|
|
2022-11-24 10:39:33 -05:00
|
|
|
type stableClient struct {
|
2022-08-29 10:49:44 -04:00
|
|
|
client kubernetes.Interface
|
|
|
|
}
|
|
|
|
|
|
|
|
// getCurrent returns the cluster's expected measurements.
|
2022-11-24 10:39:33 -05:00
|
|
|
func (u *stableClient) getCurrent(ctx context.Context, name string) (*corev1.ConfigMap, error) {
|
2022-08-29 10:49:44 -04:00
|
|
|
return u.client.CoreV1().ConfigMaps(constants.ConstellationNamespace).Get(ctx, name, metav1.GetOptions{})
|
|
|
|
}
|
|
|
|
|
|
|
|
// update updates the cluster's expected measurements in Kubernetes.
|
2022-11-24 10:39:33 -05:00
|
|
|
func (u *stableClient) update(ctx context.Context, configMap *corev1.ConfigMap) (*corev1.ConfigMap, error) {
|
2022-08-29 10:49:44 -04:00
|
|
|
return u.client.CoreV1().ConfigMaps(constants.ConstellationNamespace).Update(ctx, configMap, metav1.UpdateOptions{})
|
|
|
|
}
|
2022-11-24 10:39:33 -05:00
|
|
|
|
|
|
|
func (u *stableClient) kubernetesVersion() (string, error) {
|
|
|
|
serverVersion, err := u.client.Discovery().ServerVersion()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return serverVersion.GitVersion, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type helmInterface interface {
|
2023-01-04 07:55:10 -05:00
|
|
|
Upgrade(ctx context.Context, config *config.Config, timeout time.Duration, allowDestructive bool) error
|
2022-12-19 10:52:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type debugLog interface {
|
|
|
|
Debugf(format string, args ...any)
|
|
|
|
Sync()
|
2022-11-24 10:39:33 -05:00
|
|
|
}
|