2022-11-24 10:39:33 -05:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
|
|
|
package helm
|
|
|
|
|
|
|
|
import (
|
2022-12-19 10:52:15 -05:00
|
|
|
"context"
|
2022-11-24 10:39:33 -05:00
|
|
|
"fmt"
|
2022-12-19 10:52:15 -05:00
|
|
|
"strings"
|
|
|
|
"time"
|
2022-11-24 10:39:33 -05:00
|
|
|
|
2023-01-31 06:12:19 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/compatibility"
|
2022-12-19 10:52:15 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/config"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/constants"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/deploy/helm"
|
2022-12-19 02:08:46 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/file"
|
2023-01-04 07:55:10 -05:00
|
|
|
"github.com/pkg/errors"
|
2022-12-19 02:08:46 -05:00
|
|
|
"github.com/spf13/afero"
|
2022-12-22 06:30:04 -05:00
|
|
|
"golang.org/x/mod/semver"
|
2022-11-24 10:39:33 -05:00
|
|
|
"helm.sh/helm/v3/pkg/action"
|
2022-12-19 10:52:15 -05:00
|
|
|
"helm.sh/helm/v3/pkg/chart"
|
2022-11-24 10:39:33 -05:00
|
|
|
"helm.sh/helm/v3/pkg/cli"
|
2023-01-04 07:55:10 -05:00
|
|
|
"helm.sh/helm/v3/pkg/release"
|
2022-12-19 02:08:46 -05:00
|
|
|
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
|
|
|
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
|
|
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
2022-11-24 10:39:33 -05:00
|
|
|
)
|
|
|
|
|
2023-01-04 07:55:10 -05:00
|
|
|
const (
|
|
|
|
// AllowDestructive is a named bool to signal that destructive actions have been confirmed by the user.
|
|
|
|
AllowDestructive = true
|
|
|
|
// DenyDestructive is a named bool to signal that destructive actions have not been confirmed by the user yet.
|
|
|
|
DenyDestructive = false
|
|
|
|
)
|
|
|
|
|
2022-11-24 10:39:33 -05:00
|
|
|
// Client handles interaction with helm.
|
|
|
|
type Client struct {
|
2022-12-19 02:08:46 -05:00
|
|
|
config *action.Configuration
|
|
|
|
kubectl crdClient
|
|
|
|
fs file.Handler
|
2023-01-04 07:55:10 -05:00
|
|
|
actions actionWrapper
|
2022-12-19 02:08:46 -05:00
|
|
|
log debugLog
|
2022-11-24 10:39:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewClient returns a new initializes client for the namespace Client.
|
2022-12-19 02:08:46 -05:00
|
|
|
func NewClient(client crdClient, kubeConfigPath, helmNamespace string, log debugLog) (*Client, error) {
|
2022-11-24 10:39:33 -05:00
|
|
|
settings := cli.New()
|
|
|
|
settings.KubeConfig = kubeConfigPath // constants.AdminConfFilename
|
|
|
|
|
|
|
|
actionConfig := &action.Configuration{}
|
2022-12-19 10:52:15 -05:00
|
|
|
if err := actionConfig.Init(settings.RESTClientGetter(), helmNamespace, "secret", log.Debugf); err != nil {
|
2022-11-24 10:39:33 -05:00
|
|
|
return nil, fmt.Errorf("initializing config: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-12-19 02:08:46 -05:00
|
|
|
fileHandler := file.NewHandler(afero.NewOsFs())
|
|
|
|
|
|
|
|
kubeconfig, err := fileHandler.Read(kubeConfigPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("reading gce config: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := client.Initialize(kubeconfig); err != nil {
|
|
|
|
return nil, fmt.Errorf("initializing kubectl: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-01-04 07:55:10 -05:00
|
|
|
return &Client{kubectl: client, fs: fileHandler, actions: actions{config: actionConfig}, log: log}, nil
|
2022-11-24 10:39:33 -05:00
|
|
|
}
|
|
|
|
|
2022-12-22 06:30:04 -05:00
|
|
|
// Upgrade runs a helm-upgrade on all deployments that are managed via Helm.
|
|
|
|
// If the CLI receives an interrupt signal it will cancel the context.
|
|
|
|
// Canceling the context will prompt helm to abort and roll back the ongoing upgrade.
|
2023-01-04 07:55:10 -05:00
|
|
|
func (c *Client) Upgrade(ctx context.Context, config *config.Config, timeout time.Duration, allowDestructive bool) error {
|
|
|
|
if err := c.upgradeRelease(ctx, timeout, ciliumPath, ciliumReleaseName, false, allowDestructive); err != nil {
|
2022-12-22 06:30:04 -05:00
|
|
|
return fmt.Errorf("upgrading cilium: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-01-04 07:55:10 -05:00
|
|
|
if err := c.upgradeRelease(ctx, timeout, certManagerPath, certManagerReleaseName, false, allowDestructive); err != nil {
|
2022-12-22 06:30:04 -05:00
|
|
|
return fmt.Errorf("upgrading cert-manager: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-01-04 07:55:10 -05:00
|
|
|
if err := c.upgradeRelease(ctx, timeout, conOperatorsPath, conOperatorsReleaseName, true, allowDestructive); err != nil {
|
2022-12-22 06:30:04 -05:00
|
|
|
return fmt.Errorf("upgrading constellation operators: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-01-04 07:55:10 -05:00
|
|
|
if err := c.upgradeRelease(ctx, timeout, conServicesPath, conServicesReleaseName, false, allowDestructive); err != nil {
|
2022-12-22 06:30:04 -05:00
|
|
|
return fmt.Errorf("upgrading constellation-services: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-01-31 06:12:19 -05:00
|
|
|
// Versions queries the cluster for running versions and returns a map of releaseName -> version.
|
|
|
|
func (c *Client) Versions() (string, error) {
|
|
|
|
serviceVersion, err := c.currentVersion(conServicesReleaseName)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("getting constellation-services version: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return compatibility.EnsurePrefixV(serviceVersion), nil
|
|
|
|
}
|
|
|
|
|
2022-12-22 06:30:04 -05:00
|
|
|
// currentVersion returns the version of the currently installed helm release.
|
|
|
|
func (c *Client) currentVersion(release string) (string, error) {
|
2023-01-04 07:55:10 -05:00
|
|
|
rel, err := c.actions.listAction(release)
|
2022-11-24 10:39:33 -05:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(rel) == 0 {
|
|
|
|
return "", fmt.Errorf("release %s not found", release)
|
|
|
|
}
|
|
|
|
if len(rel) > 1 {
|
|
|
|
return "", fmt.Errorf("multiple releases found for %s", release)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rel[0] == nil || rel[0].Chart == nil || rel[0].Chart.Metadata == nil {
|
|
|
|
return "", fmt.Errorf("received invalid release %s", release)
|
|
|
|
}
|
|
|
|
|
|
|
|
return rel[0].Chart.Metadata.Version, nil
|
|
|
|
}
|
2022-12-19 10:52:15 -05:00
|
|
|
|
2023-01-04 07:55:10 -05:00
|
|
|
// ErrConfirmationMissing signals that an action requires user confirmation.
|
|
|
|
var ErrConfirmationMissing = errors.New("action requires user confirmation")
|
|
|
|
|
2022-12-22 06:30:04 -05:00
|
|
|
func (c *Client) upgradeRelease(
|
2023-01-04 07:55:10 -05:00
|
|
|
ctx context.Context, timeout time.Duration, chartPath, releaseName string, hasCRDs bool, allowDestructive bool,
|
2022-12-22 06:30:04 -05:00
|
|
|
) error {
|
|
|
|
chart, err := loadChartsDir(helmFS, chartPath)
|
2022-12-19 10:52:15 -05:00
|
|
|
if err != nil {
|
2022-12-22 06:30:04 -05:00
|
|
|
return fmt.Errorf("loading chart: %w", err)
|
2022-12-19 10:52:15 -05:00
|
|
|
}
|
2022-12-22 06:30:04 -05:00
|
|
|
currentVersion, err := c.currentVersion(releaseName)
|
2022-12-19 10:52:15 -05:00
|
|
|
if err != nil {
|
2022-12-22 06:30:04 -05:00
|
|
|
return fmt.Errorf("getting current version: %w", err)
|
2022-12-19 10:52:15 -05:00
|
|
|
}
|
2022-12-22 06:30:04 -05:00
|
|
|
c.log.Debugf("Current %s version: %s", releaseName, currentVersion)
|
|
|
|
c.log.Debugf("New %s version: %s", releaseName, chart.Metadata.Version)
|
2022-12-19 10:52:15 -05:00
|
|
|
|
2022-12-22 06:30:04 -05:00
|
|
|
if !isUpgrade(currentVersion, chart.Metadata.Version) {
|
|
|
|
c.log.Debugf(
|
|
|
|
"Skipping upgrade of %s: new version (%s) is not an upgrade for current version (%s)",
|
|
|
|
releaseName, chart.Metadata.Version, currentVersion,
|
|
|
|
)
|
|
|
|
return nil
|
2022-12-19 10:52:15 -05:00
|
|
|
}
|
|
|
|
|
2023-01-04 07:55:10 -05:00
|
|
|
if releaseName == certManagerReleaseName && !allowDestructive {
|
|
|
|
return ErrConfirmationMissing
|
|
|
|
}
|
|
|
|
|
2022-12-22 06:30:04 -05:00
|
|
|
if hasCRDs {
|
|
|
|
if err := c.updateCRDs(ctx, chart); err != nil {
|
|
|
|
return fmt.Errorf("updating CRDs: %w", err)
|
|
|
|
}
|
2022-12-19 10:52:15 -05:00
|
|
|
}
|
2022-12-22 06:30:04 -05:00
|
|
|
values, err := c.prepareValues(chart, releaseName)
|
2022-12-19 10:52:15 -05:00
|
|
|
if err != nil {
|
2022-12-22 06:30:04 -05:00
|
|
|
return fmt.Errorf("preparing values: %w", err)
|
2022-12-19 10:52:15 -05:00
|
|
|
}
|
|
|
|
|
2022-12-22 06:30:04 -05:00
|
|
|
c.log.Debugf("Upgrading %s from %s to %s", releaseName, currentVersion, chart.Metadata.Version)
|
2023-01-04 07:55:10 -05:00
|
|
|
err = c.actions.upgradeAction(ctx, releaseName, chart, values, timeout)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2022-12-19 10:52:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-02-14 10:41:19 -05:00
|
|
|
// prepareValues returns a values map as required for helm-upgrade.
|
2022-12-19 10:52:15 -05:00
|
|
|
// It imitates the behaviour of helm's reuse-values flag by fetching the current values from the cluster
|
|
|
|
// and merging the fetched values with the locally found values.
|
|
|
|
// This is done to ensure that new values (from upgrades of the local files) end up in the cluster.
|
|
|
|
// reuse-values does not ensure this.
|
|
|
|
func (c *Client) prepareValues(chart *chart.Chart, releaseName string) (map[string]any, error) {
|
|
|
|
// Ensure installCRDs is set for cert-manager chart.
|
|
|
|
if releaseName == certManagerReleaseName {
|
|
|
|
chart.Values["installCRDs"] = true
|
|
|
|
}
|
2023-01-04 07:55:10 -05:00
|
|
|
values, err := c.actions.getValues(releaseName)
|
2022-12-19 10:52:15 -05:00
|
|
|
if err != nil {
|
2023-01-04 07:55:10 -05:00
|
|
|
return nil, fmt.Errorf("getting values for %s: %w", releaseName, err)
|
2022-12-19 10:52:15 -05:00
|
|
|
}
|
|
|
|
return helm.MergeMaps(chart.Values, values), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetValues queries the cluster for the values of the given release.
|
|
|
|
func (c *Client) GetValues(release string) (map[string]any, error) {
|
|
|
|
client := action.NewGetValues(c.config)
|
|
|
|
// Version corresponds to the releases revision. Specifying a Version <= 0 yields the latest release.
|
|
|
|
client.Version = 0
|
|
|
|
values, err := client.Run(release)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("getting values for %s: %w", release, err)
|
|
|
|
}
|
|
|
|
return values, nil
|
|
|
|
}
|
|
|
|
|
2022-12-22 06:30:04 -05:00
|
|
|
// updateCRDs walks through the dependencies of the given chart and applies
|
2022-12-19 10:52:15 -05:00
|
|
|
// the files in the dependencie's 'crds' folder.
|
|
|
|
// This function is NOT recursive!
|
2022-12-22 06:30:04 -05:00
|
|
|
func (c *Client) updateCRDs(ctx context.Context, chart *chart.Chart) error {
|
2022-12-19 10:52:15 -05:00
|
|
|
for _, dep := range chart.Dependencies() {
|
|
|
|
for _, crdFile := range dep.Files {
|
|
|
|
if strings.HasPrefix(crdFile.Name, "crds/") {
|
2022-12-22 06:30:04 -05:00
|
|
|
c.log.Debugf("Updating crd: %s", crdFile.Name)
|
2022-12-19 02:08:46 -05:00
|
|
|
err := c.kubectl.ApplyCRD(ctx, crdFile.Data)
|
2022-12-19 10:52:15 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-12-22 06:30:04 -05:00
|
|
|
// isUpgrade returns true if the new version is greater than the current version.
|
|
|
|
// Versions should adhere to the semver spec, but this function will prefix the versions with 'v' if they don't.
|
|
|
|
func isUpgrade(currentVersion, newVersion string) bool {
|
|
|
|
if !strings.HasPrefix(currentVersion, "v") {
|
|
|
|
currentVersion = "v" + currentVersion
|
|
|
|
}
|
|
|
|
if !strings.HasPrefix(newVersion, "v") {
|
|
|
|
newVersion = "v" + newVersion
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the current version is not a valid semver,
|
|
|
|
// we cant compare it to the new version.
|
|
|
|
// -> We can't upgrade.
|
|
|
|
if !semver.IsValid(currentVersion) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if semver.Compare(currentVersion, newVersion) < 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-12-19 10:52:15 -05:00
|
|
|
type debugLog interface {
|
|
|
|
Debugf(format string, args ...any)
|
|
|
|
Sync()
|
|
|
|
}
|
2022-12-19 02:08:46 -05:00
|
|
|
|
|
|
|
type crdClient interface {
|
|
|
|
Initialize(kubeconfig []byte) error
|
|
|
|
ApplyCRD(ctx context.Context, rawCRD []byte) error
|
|
|
|
GetCRDs(ctx context.Context) ([]apiextensionsv1.CustomResourceDefinition, error)
|
|
|
|
GetCRs(ctx context.Context, gvr schema.GroupVersionResource) ([]unstructured.Unstructured, error)
|
|
|
|
}
|
2023-01-04 07:55:10 -05:00
|
|
|
|
|
|
|
type actionWrapper interface {
|
|
|
|
listAction(release string) ([]*release.Release, error)
|
|
|
|
getValues(release string) (map[string]any, error)
|
|
|
|
upgradeAction(ctx context.Context, releaseName string, chart *chart.Chart, values map[string]any, timeout time.Duration) error
|
|
|
|
}
|
|
|
|
|
|
|
|
type actions struct {
|
|
|
|
config *action.Configuration
|
|
|
|
}
|
|
|
|
|
|
|
|
// listAction execute a List action by wrapping helm's action package.
|
|
|
|
// It creates the action, runs it at returns results and errors.
|
|
|
|
func (a actions) listAction(release string) ([]*release.Release, error) {
|
|
|
|
action := action.NewList(a.config)
|
|
|
|
action.Filter = release
|
|
|
|
return action.Run()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a actions) getValues(release string) (map[string]any, error) {
|
|
|
|
client := action.NewGetValues(a.config)
|
|
|
|
// Version corresponds to the releases revision. Specifying a Version <= 0 yields the latest release.
|
|
|
|
client.Version = 0
|
|
|
|
return client.Run(release)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a actions) upgradeAction(ctx context.Context, releaseName string, chart *chart.Chart, values map[string]any, timeout time.Duration) error {
|
|
|
|
action := action.NewUpgrade(a.config)
|
|
|
|
action.Atomic = true
|
|
|
|
action.Namespace = constants.HelmNamespace
|
|
|
|
action.ReuseValues = false
|
|
|
|
action.Timeout = timeout
|
|
|
|
if _, err := action.RunWithContext(ctx, releaseName, chart, values); err != nil {
|
|
|
|
return fmt.Errorf("upgrading %s: %w", releaseName, err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|