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"
|
2023-03-03 03:38:23 -05:00
|
|
|
"errors"
|
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-04-13 10:55:12 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/semver"
|
2023-02-14 12:04:58 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/versions"
|
2022-12-19 02:08:46 -05:00
|
|
|
"github.com/spf13/afero"
|
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
|
|
|
|
)
|
|
|
|
|
2023-03-24 06:51:18 -04:00
|
|
|
// ErrConfirmationMissing signals that an action requires user confirmation.
|
|
|
|
var ErrConfirmationMissing = errors.New("action requires user confirmation")
|
|
|
|
|
2023-03-03 06:43:33 -05:00
|
|
|
// Client handles interaction with helm and the cluster.
|
2022-11-24 10:39:33 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-06-21 09:49:42 -04:00
|
|
|
func (c *Client) shouldUpgrade(releaseName, newVersion string, force bool) error {
|
2023-03-20 09:49:04 -04:00
|
|
|
currentVersion, err := c.currentVersion(releaseName)
|
2023-03-03 06:43:48 -05:00
|
|
|
if err != nil {
|
2023-04-13 10:55:12 -04:00
|
|
|
return fmt.Errorf("getting version for %s: %w", releaseName, err)
|
2023-03-03 06:43:48 -05:00
|
|
|
}
|
2023-03-20 09:49:04 -04:00
|
|
|
c.log.Debugf("Current %s version: %s", releaseName, currentVersion)
|
2023-04-13 10:55:12 -04:00
|
|
|
c.log.Debugf("New %s version: %s", releaseName, newVersion)
|
2023-03-20 09:49:04 -04:00
|
|
|
|
|
|
|
// This may break for cert-manager or cilium if we decide to upgrade more than one minor version at a time.
|
|
|
|
// Leaving it as is since it is not clear to me what kind of sanity check we could do.
|
2023-06-21 09:49:42 -04:00
|
|
|
if !force {
|
|
|
|
if err := compatibility.IsValidUpgrade(currentVersion, newVersion); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-03-03 06:43:48 -05:00
|
|
|
}
|
2023-04-13 10:55:12 -04:00
|
|
|
// at this point we conclude that the release should be upgraded. check that this CLI supports the upgrade.
|
|
|
|
if releaseName == constellationOperatorsInfo.releaseName || releaseName == constellationServicesInfo.releaseName {
|
|
|
|
if compatibility.EnsurePrefixV(constants.VersionInfo()) != compatibility.EnsurePrefixV(newVersion) {
|
|
|
|
return fmt.Errorf("this CLI only supports microservice version %s for upgrading", constants.VersionInfo())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c.log.Debugf("Upgrading %s from %s to %s", releaseName, currentVersion, newVersion)
|
2023-03-20 09:49:04 -04:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2023-03-03 06:43:48 -05:00
|
|
|
|
2023-03-20 09:49:04 -04: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-06-21 09:49:42 -04:00
|
|
|
func (c *Client) Upgrade(ctx context.Context, config *config.Config, timeout time.Duration, allowDestructive, force bool, upgradeID string) error {
|
2023-03-03 03:38:23 -05:00
|
|
|
upgradeErrs := []error{}
|
2023-03-20 09:49:04 -04:00
|
|
|
upgradeReleases := []*chart.Chart{}
|
|
|
|
|
|
|
|
for _, info := range []chartInfo{ciliumInfo, certManagerInfo, constellationOperatorsInfo, constellationServicesInfo} {
|
|
|
|
chart, err := loadChartsDir(helmFS, info.path)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("loading chart: %w", err)
|
|
|
|
}
|
2023-04-13 10:55:12 -04:00
|
|
|
|
|
|
|
// define target version the chart is upgraded to
|
|
|
|
var upgradeVersion string
|
2023-03-20 09:49:04 -04:00
|
|
|
if info == constellationOperatorsInfo || info == constellationServicesInfo {
|
|
|
|
// ensure that the services chart has the same version as the CLI
|
|
|
|
updateVersions(chart, compatibility.EnsurePrefixV(constants.VersionInfo()))
|
2023-04-13 10:55:12 -04:00
|
|
|
upgradeVersion = config.MicroserviceVersion
|
|
|
|
} else {
|
|
|
|
upgradeVersion = chart.Metadata.Version
|
2023-03-20 09:49:04 -04:00
|
|
|
}
|
|
|
|
|
2023-06-30 10:46:05 -04:00
|
|
|
var invalidUpgrade *compatibility.InvalidUpgradeError
|
2023-06-21 09:49:42 -04:00
|
|
|
err = c.shouldUpgrade(info.releaseName, upgradeVersion, force)
|
2023-03-20 09:49:04 -04:00
|
|
|
switch {
|
|
|
|
case errors.As(err, &invalidUpgrade):
|
|
|
|
upgradeErrs = append(upgradeErrs, fmt.Errorf("skipping %s upgrade: %w", info.releaseName, err))
|
|
|
|
case err != nil:
|
2023-02-03 05:05:42 -05:00
|
|
|
return fmt.Errorf("should upgrade %s: %w", info.releaseName, err)
|
2023-03-20 09:49:04 -04:00
|
|
|
case err == nil:
|
|
|
|
upgradeReleases = append(upgradeReleases, chart)
|
2023-07-03 09:13:36 -04:00
|
|
|
|
|
|
|
// Check if installing/upgrading the chart could be destructive
|
|
|
|
// If so, we don't want to perform any actions,
|
|
|
|
// unless the user confirms it to be OK.
|
|
|
|
if !allowDestructive &&
|
|
|
|
info.chartName == certManagerInfo.chartName {
|
|
|
|
return ErrConfirmationMissing
|
|
|
|
}
|
2023-03-20 09:49:04 -04:00
|
|
|
}
|
2022-12-22 06:30:04 -05:00
|
|
|
}
|
|
|
|
|
2023-03-20 09:49:04 -04:00
|
|
|
if len(upgradeReleases) == 0 {
|
|
|
|
return errors.Join(upgradeErrs...)
|
2022-12-22 06:30:04 -05:00
|
|
|
}
|
|
|
|
|
2023-06-21 03:22:32 -04:00
|
|
|
crds, err := c.backupCRDs(ctx, upgradeID)
|
2023-03-20 09:49:04 -04:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("creating CRD backup: %w", err)
|
|
|
|
}
|
2023-06-21 03:22:32 -04:00
|
|
|
if err := c.backupCRs(ctx, crds, upgradeID); err != nil {
|
2023-03-20 09:49:04 -04:00
|
|
|
return fmt.Errorf("creating CR backup: %w", err)
|
2022-12-22 06:30:04 -05:00
|
|
|
}
|
|
|
|
|
2023-03-20 09:49:04 -04:00
|
|
|
for _, chart := range upgradeReleases {
|
2023-07-03 09:13:36 -04:00
|
|
|
err = c.upgradeRelease(ctx, timeout, config, chart)
|
2023-03-20 09:49:04 -04:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("upgrading %s: %w", chart.Metadata.Name, err)
|
|
|
|
}
|
2022-12-22 06:30:04 -05:00
|
|
|
}
|
|
|
|
|
2023-03-03 03:38:23 -05:00
|
|
|
return errors.Join(upgradeErrs...)
|
2022-12-22 06:30:04 -05:00
|
|
|
}
|
|
|
|
|
2023-01-31 06:12:19 -05:00
|
|
|
// Versions queries the cluster for running versions and returns a map of releaseName -> version.
|
2023-03-24 06:51:18 -04:00
|
|
|
func (c *Client) Versions() (ServiceVersions, error) {
|
|
|
|
ciliumVersion, err := c.currentVersion(ciliumInfo.releaseName)
|
|
|
|
if err != nil {
|
|
|
|
return ServiceVersions{}, fmt.Errorf("getting %s version: %w", ciliumInfo.releaseName, err)
|
|
|
|
}
|
|
|
|
certManagerVersion, err := c.currentVersion(certManagerInfo.releaseName)
|
|
|
|
if err != nil {
|
|
|
|
return ServiceVersions{}, fmt.Errorf("getting %s version: %w", certManagerInfo.releaseName, err)
|
|
|
|
}
|
|
|
|
operatorsVersion, err := c.currentVersion(constellationOperatorsInfo.releaseName)
|
|
|
|
if err != nil {
|
|
|
|
return ServiceVersions{}, fmt.Errorf("getting %s version: %w", constellationOperatorsInfo.releaseName, err)
|
|
|
|
}
|
|
|
|
servicesVersion, err := c.currentVersion(constellationServicesInfo.releaseName)
|
2023-01-31 06:12:19 -05:00
|
|
|
if err != nil {
|
2023-03-24 06:51:18 -04:00
|
|
|
return ServiceVersions{}, fmt.Errorf("getting %s version: %w", constellationServicesInfo.releaseName, err)
|
2023-01-31 06:12:19 -05:00
|
|
|
}
|
|
|
|
|
2023-03-24 06:51:18 -04:00
|
|
|
return ServiceVersions{
|
|
|
|
cilium: compatibility.EnsurePrefixV(ciliumVersion),
|
|
|
|
certManager: compatibility.EnsurePrefixV(certManagerVersion),
|
|
|
|
constellationOperators: compatibility.EnsurePrefixV(operatorsVersion),
|
|
|
|
constellationServices: compatibility.EnsurePrefixV(servicesVersion),
|
|
|
|
}, nil
|
2023-01-31 06:12:19 -05:00
|
|
|
}
|
|
|
|
|
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-03-24 06:51:18 -04:00
|
|
|
// ServiceVersions bundles the versions of all services that are part of Constellation.
|
|
|
|
type ServiceVersions struct {
|
|
|
|
cilium string
|
|
|
|
certManager string
|
|
|
|
constellationOperators string
|
|
|
|
constellationServices string
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewServiceVersions returns a new ServiceVersions struct.
|
|
|
|
func NewServiceVersions(cilium, certManager, constellationOperators, constellationServices string) ServiceVersions {
|
|
|
|
return ServiceVersions{
|
|
|
|
cilium: cilium,
|
|
|
|
certManager: certManager,
|
|
|
|
constellationOperators: constellationOperators,
|
|
|
|
constellationServices: constellationServices,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cilium returns the version of the Cilium release.
|
|
|
|
func (s ServiceVersions) Cilium() string {
|
|
|
|
return s.cilium
|
|
|
|
}
|
|
|
|
|
|
|
|
// CertManager returns the version of the cert-manager release.
|
|
|
|
func (s ServiceVersions) CertManager() string {
|
|
|
|
return s.certManager
|
|
|
|
}
|
|
|
|
|
|
|
|
// ConstellationOperators returns the version of the constellation-operators release.
|
|
|
|
func (s ServiceVersions) ConstellationOperators() string {
|
|
|
|
return s.constellationOperators
|
|
|
|
}
|
|
|
|
|
|
|
|
// ConstellationServices returns the version of the constellation-services release.
|
|
|
|
func (s ServiceVersions) ConstellationServices() string {
|
|
|
|
return s.constellationServices
|
|
|
|
}
|
2023-01-04 07:55:10 -05:00
|
|
|
|
2022-12-22 06:30:04 -05:00
|
|
|
func (c *Client) upgradeRelease(
|
2023-07-03 09:13:36 -04:00
|
|
|
ctx context.Context, timeout time.Duration, conf *config.Config, chart *chart.Chart,
|
2022-12-22 06:30:04 -05:00
|
|
|
) error {
|
2023-02-14 12:04:58 -05:00
|
|
|
// We need to load all values that can be statically loaded before merging them with the cluster
|
|
|
|
// values. Otherwise the templates are not rendered correctly.
|
2023-07-12 06:42:51 -04:00
|
|
|
k8sVersion, err := versions.NewValidK8sVersion(conf.KubernetesVersion, false)
|
2023-02-14 12:04:58 -05:00
|
|
|
if err != nil {
|
2023-06-05 03:13:02 -04:00
|
|
|
return fmt.Errorf("validating k8s version: %s", conf.KubernetesVersion)
|
2023-02-14 12:04:58 -05:00
|
|
|
}
|
|
|
|
loader := NewLoader(conf.GetProvider(), k8sVersion)
|
2023-03-20 09:49:04 -04:00
|
|
|
|
2023-02-14 12:04:58 -05:00
|
|
|
var values map[string]any
|
2023-03-20 09:49:04 -04:00
|
|
|
var releaseName string
|
|
|
|
|
|
|
|
switch chart.Metadata.Name {
|
|
|
|
case ciliumInfo.chartName:
|
|
|
|
releaseName = ciliumInfo.releaseName
|
2023-02-14 12:04:58 -05:00
|
|
|
values, err = loader.loadCiliumValues()
|
2023-04-13 10:55:12 -04:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("loading values: %w", err)
|
|
|
|
}
|
2023-03-20 09:49:04 -04:00
|
|
|
case certManagerInfo.chartName:
|
|
|
|
releaseName = certManagerInfo.releaseName
|
2023-02-14 12:04:58 -05:00
|
|
|
values = loader.loadCertManagerValues()
|
2023-03-20 09:49:04 -04:00
|
|
|
case constellationOperatorsInfo.chartName:
|
|
|
|
releaseName = constellationOperatorsInfo.releaseName
|
2023-02-14 12:04:58 -05:00
|
|
|
values, err = loader.loadOperatorsValues()
|
2023-04-13 10:55:12 -04:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("loading values: %w", err)
|
|
|
|
}
|
2023-03-20 09:49:04 -04:00
|
|
|
|
|
|
|
if err := c.updateCRDs(ctx, chart); err != nil {
|
|
|
|
return fmt.Errorf("updating CRDs: %w", err)
|
|
|
|
}
|
|
|
|
case constellationServicesInfo.chartName:
|
|
|
|
releaseName = constellationServicesInfo.releaseName
|
2023-02-14 12:04:58 -05:00
|
|
|
values, err = loader.loadConstellationServicesValues()
|
2023-04-13 10:55:12 -04:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("loading values: %w", err)
|
2023-03-21 05:04:48 -04:00
|
|
|
}
|
2023-03-23 04:53:23 -04:00
|
|
|
|
2023-05-03 05:11:53 -04:00
|
|
|
if err := c.applyMigrations(ctx, releaseName, values, conf); err != nil {
|
2023-04-13 10:55:12 -04:00
|
|
|
return fmt.Errorf("applying migrations: %w", err)
|
2023-03-23 04:53:23 -04:00
|
|
|
}
|
2023-02-14 12:04:58 -05:00
|
|
|
default:
|
2023-03-20 09:49:04 -04:00
|
|
|
return fmt.Errorf("unknown chart name: %s", chart.Metadata.Name)
|
2023-02-14 12:04:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
values, err = c.prepareValues(values, 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
|
|
|
}
|
|
|
|
|
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-04-13 10:55:12 -04:00
|
|
|
// applyMigrations checks the from version and applies the necessary migrations.
|
|
|
|
// The function assumes the caller has verified that our version drift restriction is not violated,
|
|
|
|
// Currently, this is done during config validation.
|
2023-05-03 05:11:53 -04:00
|
|
|
func (c *Client) applyMigrations(ctx context.Context, releaseName string, values map[string]any, conf *config.Config) error {
|
2023-04-13 10:55:12 -04:00
|
|
|
current, err := c.currentVersion(releaseName)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("getting %s version: %w", releaseName, err)
|
|
|
|
}
|
|
|
|
currentV, err := semver.New(current)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("parsing current version: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-07-10 08:03:45 -04:00
|
|
|
if currentV.Major == 2 && currentV.Minor == 8 {
|
|
|
|
// Rename/change the following function to implement any necessary migrations.
|
|
|
|
return migrateFrom2_8(ctx, values, conf, c.kubectl)
|
2023-04-13 10:55:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-10 08:03:45 -04:00
|
|
|
// migrateFrom2_8 is currently a no-op that is kept for documentation purposes.
|
|
|
|
// If you have to implement the function please make sure to update the below comment to your situation.
|
|
|
|
// migrateFrom2_8 applies the necessary migrations for upgrading from v2.8.x to v2.9.x.
|
|
|
|
// migrateFrom2_8 should be applied for v2.8.x --> v2.9.x.
|
|
|
|
// migrateFrom2_8 should NOT be applied for v2.8.0 --> v2.9.x.
|
|
|
|
func migrateFrom2_8(_ context.Context, _ map[string]any, _ *config.Config, _ crdClient) error {
|
2023-04-13 10:55:12 -04: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.
|
2023-02-14 12:04:58 -05:00
|
|
|
func (c *Client) prepareValues(localValues map[string]any, releaseName string) (map[string]any, error) {
|
2022-12-19 10:52:15 -05:00
|
|
|
// Ensure installCRDs is set for cert-manager chart.
|
2023-03-20 09:49:04 -04:00
|
|
|
if releaseName == certManagerInfo.releaseName {
|
2023-02-14 12:04:58 -05:00
|
|
|
localValues["installCRDs"] = true
|
2022-12-19 10:52:15 -05:00
|
|
|
}
|
2023-02-14 12:04:58 -05:00
|
|
|
clusterValues, 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
|
|
|
}
|
2023-02-14 12:04:58 -05:00
|
|
|
|
|
|
|
return helm.MergeMaps(clusterValues, localValues), nil
|
2022-12-19 10:52:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|