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"
|
2023-03-03 03:38:23 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/cli/internal/image"
|
2022-11-15 09:40:49 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/attestation/measurements"
|
2023-02-09 09:54:12 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/compatibility"
|
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"
|
2023-02-09 09:54:12 -05:00
|
|
|
internalk8s "github.com/edgelesssys/constellation/v2/internal/kubernetes"
|
2022-12-19 02:08:46 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/kubernetes/kubectl"
|
2023-03-03 03:38:23 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/versions"
|
2023-02-09 09:54:12 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/versions/components"
|
2023-03-03 03:38:23 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/versionsapi"
|
2023-02-09 09:54:12 -05:00
|
|
|
updatev1alpha1 "github.com/edgelesssys/constellation/v2/operators/constellation-node-operator/v2/api/v1alpha1"
|
2022-08-29 10:49:44 -04:00
|
|
|
corev1 "k8s.io/api/core/v1"
|
2023-02-09 09:54:12 -05:00
|
|
|
k8serrors "k8s.io/apimachinery/pkg/api/errors"
|
2022-08-29 10:49:44 -04:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
2023-02-09 09:54:12 -05:00
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
2022-08-29 10:49:44 -04:00
|
|
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
|
|
|
"k8s.io/client-go/dynamic"
|
|
|
|
"k8s.io/client-go/kubernetes"
|
|
|
|
"k8s.io/client-go/tools/clientcmd"
|
|
|
|
)
|
|
|
|
|
2023-02-09 09:54:12 -05:00
|
|
|
// ErrInProgress signals that an upgrade is in progress inside the cluster.
|
|
|
|
var ErrInProgress = errors.New("upgrade in progress")
|
|
|
|
|
2022-08-29 10:49:44 -04:00
|
|
|
// 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
|
2023-03-03 03:38:23 -05:00
|
|
|
imageFetcher imageFetcher
|
|
|
|
outWriter io.Writer
|
|
|
|
log debugLog
|
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,
|
2023-03-03 03:38:23 -05:00
|
|
|
imageFetcher: image.New(),
|
2022-11-24 10:39:33 -05:00
|
|
|
outWriter: outWriter,
|
2023-02-09 09:54:12 -05:00
|
|
|
log: log,
|
2022-08-29 10:49:44 -04:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-03-03 03:38:23 -05:00
|
|
|
// UpgradeHelmServices upgrade helm services.
|
|
|
|
func (u *Upgrader) UpgradeHelmServices(ctx context.Context, config *config.Config, timeout time.Duration, allowDestructive bool) error {
|
|
|
|
return u.helmClient.Upgrade(ctx, config, timeout, allowDestructive)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpgradeNodeVersion upgrades the cluster's NodeVersion object and in turn triggers image & k8s version upgrades.
|
|
|
|
// The versions set in the config are validated against the versions running in the cluster.
|
|
|
|
func (u *Upgrader) UpgradeNodeVersion(ctx context.Context, conf *config.Config) error {
|
|
|
|
imageReference, err := u.imageFetcher.FetchReference(ctx, conf)
|
2023-02-09 09:54:12 -05:00
|
|
|
if err != nil {
|
2023-03-03 03:38:23 -05:00
|
|
|
return fmt.Errorf("fetching image reference: %w", err)
|
2023-02-09 09:54:12 -05:00
|
|
|
}
|
|
|
|
|
2023-03-03 03:38:23 -05:00
|
|
|
imageVersion, err := versionsapi.NewVersionFromShortPath(conf.Image, versionsapi.VersionKindImage)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("parsing version from image short path: %w", err)
|
2023-02-09 09:54:12 -05:00
|
|
|
}
|
|
|
|
|
2023-03-03 03:38:23 -05:00
|
|
|
currentK8sVersion, err := versions.NewValidK8sVersion(conf.KubernetesVersion)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("getting Kubernetes version: %w", err)
|
2023-02-09 09:54:12 -05:00
|
|
|
}
|
2023-03-03 03:38:23 -05:00
|
|
|
versionConfig := versions.VersionConfigs[currentK8sVersion]
|
2023-02-09 09:54:12 -05:00
|
|
|
|
2023-03-03 03:38:23 -05:00
|
|
|
nodeVersion, err := u.checkClusterStatus(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2022-08-29 10:49:44 -04:00
|
|
|
}
|
|
|
|
|
2023-03-03 03:38:23 -05:00
|
|
|
upgradeErrs := []error{}
|
|
|
|
upgradeErr := &compatibility.InvalidUpgradeError{}
|
|
|
|
err = u.updateImage(&nodeVersion, imageReference, imageVersion.Version)
|
|
|
|
if errors.As(err, &upgradeErr) {
|
|
|
|
upgradeErrs = append(upgradeErrs, fmt.Errorf("skipping image upgrades: %w", err))
|
2022-08-29 10:49:44 -04:00
|
|
|
}
|
|
|
|
|
2023-03-03 03:38:23 -05:00
|
|
|
components, err := u.updateK8s(&nodeVersion, versionConfig.ClusterVersion, versionConfig.KubernetesComponents)
|
|
|
|
if errors.As(err, &upgradeErr) {
|
|
|
|
upgradeErrs = append(upgradeErrs, fmt.Errorf("skipping Kubernetes upgrades: %w", err))
|
2023-02-09 09:54:12 -05:00
|
|
|
}
|
2023-01-31 06:12:19 -05:00
|
|
|
|
2023-03-03 03:38:23 -05:00
|
|
|
if len(upgradeErrs) == 2 {
|
|
|
|
return errors.Join(upgradeErrs...)
|
2023-02-09 09:54:12 -05:00
|
|
|
}
|
|
|
|
|
2023-03-03 03:38:23 -05:00
|
|
|
if err := u.updateMeasurements(ctx, conf.GetMeasurements()); err != nil {
|
|
|
|
return fmt.Errorf("updating measurements: %w", err)
|
2023-02-09 09:54:12 -05:00
|
|
|
}
|
2023-03-03 03:38:23 -05:00
|
|
|
updatedNodeVersion, err := u.applyUpgrade(ctx, &components, nodeVersion)
|
2022-08-31 05:59:07 -04:00
|
|
|
if err != nil {
|
2023-03-03 03:38:23 -05:00
|
|
|
return fmt.Errorf("applying upgrade: %w", err)
|
2022-08-31 05:59:07 -04:00
|
|
|
}
|
2023-03-03 03:38:23 -05:00
|
|
|
if updatedNodeVersion.Spec.ImageReference != imageReference ||
|
|
|
|
updatedNodeVersion.Spec.ImageVersion != imageVersion.Version ||
|
|
|
|
updatedNodeVersion.Spec.KubernetesComponentsReference != components.ObjectMeta.Name ||
|
|
|
|
updatedNodeVersion.Spec.KubernetesClusterVersion != versionConfig.ClusterVersion {
|
|
|
|
return errors.New("unexpected value in updated nodeVersion object")
|
|
|
|
}
|
|
|
|
|
|
|
|
return errors.Join(upgradeErrs...)
|
|
|
|
}
|
2022-08-31 05:59:07 -04:00
|
|
|
|
2023-03-03 03:38:23 -05:00
|
|
|
func (u *Upgrader) applyUpgrade(ctx context.Context, components *corev1.ConfigMap, nodeVersion updatev1alpha1.NodeVersion) (updatev1alpha1.NodeVersion, error) {
|
|
|
|
_, err := u.stableInterface.createConfigMap(ctx, components)
|
2023-02-09 09:54:12 -05:00
|
|
|
// If the map already exists we can use that map and assume it has the same content as 'configMap'.
|
|
|
|
if err != nil && !k8serrors.IsAlreadyExists(err) {
|
2023-03-03 03:38:23 -05:00
|
|
|
return updatev1alpha1.NodeVersion{}, fmt.Errorf("creating k8s-components ConfigMap: %w. %T", err, err)
|
2022-08-31 05:59:07 -04:00
|
|
|
}
|
2023-02-09 09:54:12 -05:00
|
|
|
|
|
|
|
raw, err := runtime.DefaultUnstructuredConverter.ToUnstructured(&nodeVersion)
|
|
|
|
if err != nil {
|
2023-03-03 03:38:23 -05:00
|
|
|
return updatev1alpha1.NodeVersion{}, fmt.Errorf("converting nodeVersion to unstructured: %w", err)
|
2022-08-31 05:59:07 -04:00
|
|
|
}
|
2023-02-09 09:54:12 -05:00
|
|
|
u.log.Debugf("Triggering Kubernetes version upgrade now")
|
|
|
|
// Send the updated NodeVersion resource
|
|
|
|
updated, err := u.dynamicInterface.update(ctx, &unstructured.Unstructured{Object: raw})
|
|
|
|
if err != nil {
|
2023-03-03 03:38:23 -05:00
|
|
|
return updatev1alpha1.NodeVersion{}, fmt.Errorf("updating NodeVersion: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var updatedNodeVersion updatev1alpha1.NodeVersion
|
|
|
|
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(updated.UnstructuredContent(), &updatedNodeVersion); err != nil {
|
|
|
|
return updatev1alpha1.NodeVersion{}, fmt.Errorf("converting unstructured to NodeVersion: %w", err)
|
2023-02-09 09:54:12 -05:00
|
|
|
}
|
|
|
|
|
2023-03-03 03:38:23 -05:00
|
|
|
return updatedNodeVersion, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *Upgrader) checkClusterStatus(ctx context.Context) (updatev1alpha1.NodeVersion, error) {
|
|
|
|
nodeVersion, err := u.getConstellationVersion(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return updatev1alpha1.NodeVersion{}, fmt.Errorf("retrieving current image: %w", err)
|
2022-08-31 05:59:07 -04:00
|
|
|
}
|
2023-03-03 03:38:23 -05:00
|
|
|
|
|
|
|
if upgradeInProgress(nodeVersion) {
|
|
|
|
return updatev1alpha1.NodeVersion{}, ErrInProgress
|
2023-02-09 09:54:12 -05:00
|
|
|
}
|
2023-03-03 03:38:23 -05:00
|
|
|
|
|
|
|
return nodeVersion, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// updateImage upgrades the cluster to the given measurements and image.
|
|
|
|
func (u *Upgrader) updateImage(nodeVersion *updatev1alpha1.NodeVersion, newImageReference, newImageVersion string) error {
|
|
|
|
currentImageVersion := nodeVersion.Spec.ImageVersion
|
|
|
|
|
|
|
|
if err := compatibility.IsValidUpgrade(currentImageVersion, newImageVersion); err != nil {
|
|
|
|
return err
|
2022-08-31 05:59:07 -04:00
|
|
|
}
|
|
|
|
|
2023-03-03 03:38:23 -05:00
|
|
|
u.log.Debugf("Updating local copy of nodeVersion image version from %s to %s", nodeVersion.Spec.ImageVersion, newImageVersion)
|
|
|
|
nodeVersion.Spec.ImageReference = newImageReference
|
|
|
|
nodeVersion.Spec.ImageVersion = newImageVersion
|
|
|
|
|
2023-02-09 09:54:12 -05:00
|
|
|
return nil
|
2022-12-19 10:52:15 -05:00
|
|
|
}
|
|
|
|
|
2023-03-03 03:38:23 -05:00
|
|
|
func (u *Upgrader) updateK8s(nodeVersion *updatev1alpha1.NodeVersion, newClusterVersion string, components components.Components) (corev1.ConfigMap, error) {
|
|
|
|
configMap, err := internalk8s.ConstructK8sComponentsCM(components, newClusterVersion)
|
|
|
|
if err != nil {
|
|
|
|
return corev1.ConfigMap{}, fmt.Errorf("constructing k8s-components ConfigMap: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := compatibility.IsValidUpgrade(nodeVersion.Spec.KubernetesClusterVersion, newClusterVersion); err != nil {
|
|
|
|
return corev1.ConfigMap{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
u.log.Debugf("Updating local copy of nodeVersion Kubernetes version from %s to %s", nodeVersion.Spec.KubernetesClusterVersion, newClusterVersion)
|
|
|
|
nodeVersion.Spec.KubernetesComponentsReference = configMap.ObjectMeta.Name
|
|
|
|
nodeVersion.Spec.KubernetesClusterVersion = newClusterVersion
|
|
|
|
|
|
|
|
return configMap, nil
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2023-02-09 09:54:12 -05:00
|
|
|
// CurrentImage returns the currently used image version of the cluster.
|
|
|
|
func (u *Upgrader) CurrentImage(ctx context.Context) (string, error) {
|
|
|
|
nodeVersion, err := u.getConstellationVersion(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("getting constellation-version: %w", err)
|
|
|
|
}
|
|
|
|
return nodeVersion.Spec.ImageVersion, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CurrentKubernetesVersion returns the currently used Kubernetes version.
|
|
|
|
func (u *Upgrader) CurrentKubernetesVersion(ctx context.Context) (string, error) {
|
|
|
|
nodeVersion, err := u.getConstellationVersion(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("getting constellation-version: %w", err)
|
|
|
|
}
|
|
|
|
return nodeVersion.Spec.KubernetesClusterVersion, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// getFromConstellationVersion queries the constellation-version object for a given field.
|
|
|
|
func (u *Upgrader) getConstellationVersion(ctx context.Context) (updatev1alpha1.NodeVersion, error) {
|
|
|
|
raw, err := u.dynamicInterface.getCurrent(ctx, "constellation-version")
|
|
|
|
if err != nil {
|
|
|
|
return updatev1alpha1.NodeVersion{}, err
|
|
|
|
}
|
|
|
|
var nodeVersion updatev1alpha1.NodeVersion
|
|
|
|
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(raw.UnstructuredContent(), &nodeVersion); err != nil {
|
|
|
|
return updatev1alpha1.NodeVersion{}, fmt.Errorf("converting unstructured to NodeVersion: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nodeVersion, nil
|
|
|
|
}
|
|
|
|
|
2022-11-15 09:40:49 -05:00
|
|
|
func (u *Upgrader) updateMeasurements(ctx context.Context, newMeasurements measurements.M) error {
|
2023-02-09 09:54:12 -05:00
|
|
|
existingConf, err := u.stableInterface.getCurrentConfigMap(ctx, constants.JoinConfigMap)
|
2022-08-29 10:49:44 -04:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("retrieving current measurements: %w", err)
|
|
|
|
}
|
2023-02-09 09:54:12 -05:00
|
|
|
if _, ok := existingConf.Data[constants.MeasurementsFilename]; !ok {
|
|
|
|
return errors.New("measurements missing from join-config")
|
|
|
|
}
|
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)
|
2023-02-09 09:54:12 -05:00
|
|
|
u.log.Debugf("Triggering measurements config map update now")
|
|
|
|
_, err = u.stableInterface.updateConfigMap(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
|
|
|
|
}
|
|
|
|
|
2023-03-03 03:38:23 -05:00
|
|
|
// upgradeInProgress checks if an upgrade is in progress.
|
2023-02-09 09:54:12 -05:00
|
|
|
// Returns true with errors as it's the "safer" response. If caller does not check err they at least won't update the cluster.
|
2023-03-03 03:38:23 -05:00
|
|
|
func upgradeInProgress(nodeVersion updatev1alpha1.NodeVersion) bool {
|
2023-02-09 09:54:12 -05:00
|
|
|
conditions := nodeVersion.Status.Conditions
|
|
|
|
activeUpgrade := nodeVersion.Status.ActiveClusterVersionUpgrade
|
|
|
|
|
|
|
|
if activeUpgrade {
|
|
|
|
return true
|
2022-08-29 10:49:44 -04:00
|
|
|
}
|
|
|
|
|
2023-02-09 09:54:12 -05:00
|
|
|
for _, condition := range conditions {
|
|
|
|
if condition.Type == updatev1alpha1.ConditionOutdated && condition.Status == metav1.ConditionTrue {
|
|
|
|
return true
|
|
|
|
}
|
2022-08-29 10:49:44 -04:00
|
|
|
}
|
2023-02-09 09:54:12 -05:00
|
|
|
return false
|
|
|
|
}
|
2022-08-29 10:49:44 -04:00
|
|
|
|
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 {
|
2023-02-09 09:54:12 -05:00
|
|
|
getCurrentConfigMap(ctx context.Context, name string) (*corev1.ConfigMap, error)
|
|
|
|
updateConfigMap(ctx context.Context, configMap *corev1.ConfigMap) (*corev1.ConfigMap, error)
|
|
|
|
createConfigMap(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
|
|
|
|
}
|
|
|
|
|
2023-02-09 09:54:12 -05:00
|
|
|
// getCurrent returns a ConfigMap given it's name.
|
|
|
|
func (u *stableClient) getCurrentConfigMap(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{})
|
|
|
|
}
|
|
|
|
|
2023-02-09 09:54:12 -05:00
|
|
|
// update updates the given ConfigMap.
|
|
|
|
func (u *stableClient) updateConfigMap(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
|
|
|
|
2023-02-09 09:54:12 -05:00
|
|
|
func (u *stableClient) createConfigMap(ctx context.Context, configMap *corev1.ConfigMap) (*corev1.ConfigMap, error) {
|
|
|
|
return u.client.CoreV1().ConfigMaps(constants.ConstellationNamespace).Create(ctx, configMap, metav1.CreateOptions{})
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|