From 229443bcb6c945bbff6f1264023a5f55f97773c0 Mon Sep 17 00:00:00 2001 From: Ricardo Maraschini Date: Wed, 20 Dec 2023 10:44:08 +0100 Subject: [PATCH] feat: setting kots app version for deployment (#233) stop deploying latest and inform kots which version should be deployed based on the release version label. --- Makefile | 2 +- pkg/addons/adminconsole/adminconsole.go | 16 ++++++---- pkg/customization/customization.go | 41 ++++++++++++++++++++++--- 3 files changed, 48 insertions(+), 11 deletions(-) diff --git a/Makefile b/Makefile index 386cd4c2f..16207aee2 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ ARCH := $(shell uname -m) APP_NAME = embedded-cluster ADMIN_CONSOLE_CHART_URL = oci://registry.replicated.com/library ADMIN_CONSOLE_CHART_NAME = admin-console -ADMIN_CONSOLE_CHART_VERSION = 1.104.7-build.1 +ADMIN_CONSOLE_CHART_VERSION = 1.104.7-build.2 ADMIN_CONSOLE_IMAGE_OVERRIDE = ADMIN_CONSOLE_MIGRATIONS_IMAGE_OVERRIDE = EMBEDDED_OPERATOR_CHART_URL = oci://registry.replicated.com/library diff --git a/pkg/addons/adminconsole/adminconsole.go b/pkg/addons/adminconsole/adminconsole.go index 058fa347a..444bb5c45 100644 --- a/pkg/addons/adminconsole/adminconsole.go +++ b/pkg/addons/adminconsole/adminconsole.go @@ -96,8 +96,8 @@ func (a *AdminConsole) HostPreflights() (*v1beta2.HostPreflightSpec, error) { return a.customization.HostPreflights() } -// addLicenseToHelmValues adds the embedded license to the helm values. -func (a *AdminConsole) addLicenseToHelmValues() error { +// addLicenseAndVersionToHelmValues adds the embedded license to the helm values. +func (a *AdminConsole) addLicenseAndVersionToHelmValues() error { license, err := a.customization.License() if err != nil { return fmt.Errorf("unable to get license: %w", err) @@ -109,7 +109,14 @@ func (a *AdminConsole) addLicenseToHelmValues() error { if err != nil { return fmt.Errorf("unable to marshal license: %w", err) } + var appVersion string + if release, err := a.customization.ChannelRelease(); err != nil { + return fmt.Errorf("unable to get channel release: %w", err) + } else if release != nil { + appVersion = release.VersionLabel + } helmValues["automation"] = map[string]interface{}{ + "appVersionLabel": appVersion, "license": map[string]interface{}{ "slug": license.Spec.AppSlug, "data": string(raw), @@ -186,18 +193,15 @@ func (a *AdminConsole) addKotsApplicationToHelmValues() error { // GenerateHelmConfig generates the helm config for the adminconsole and writes the charts to // the disk. func (a *AdminConsole) GenerateHelmConfig(onlyDefaults bool) ([]v1beta1.Chart, []v1beta1.Repository, error) { - if !onlyDefaults { if err := a.addPasswordToHelmValues(); err != nil { return nil, nil, fmt.Errorf("unable to add password to helm values: %w", err) } - if err := a.addKotsApplicationToHelmValues(); err != nil { return nil, nil, fmt.Errorf("unable to add kots app to helm values: %w", err) } } - - if err := a.addLicenseToHelmValues(); err != nil { + if err := a.addLicenseAndVersionToHelmValues(); err != nil { return nil, nil, fmt.Errorf("unable to add license to helm values: %w", err) } values, err := yaml.Marshal(helmValues) diff --git a/pkg/customization/customization.go b/pkg/customization/customization.go index e2bf62b15..ccab908ca 100644 --- a/pkg/customization/customization.go +++ b/pkg/customization/customization.go @@ -13,11 +13,17 @@ import ( embeddedclusterv1beta1 "github.com/replicatedhq/embedded-cluster-operator/api/v1beta1" "github.com/replicatedhq/kotskinds/apis/kots/v1beta1" "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2" - "sigs.k8s.io/yaml" + "gopkg.in/yaml.v2" + kyaml "sigs.k8s.io/yaml" "github.com/replicatedhq/embedded-cluster/pkg/preflights" ) +// ChannelRelease contains information about a specific app release inside a channel. +type ChannelRelease struct { + VersionLabel string `yaml:"versionLabel"` +} + // ParsedSection holds the parsed section from the binary. We only care about the // application object, whatever HostPreflight we can find, and the app License. type ParsedSection struct { @@ -25,6 +31,7 @@ type ParsedSection struct { HostPreflights [][]byte License []byte EmbeddedClusterConfig []byte + ChannelRelease []byte } // AdminConsole is a struct that contains the actions to create and update the admin @@ -105,6 +112,10 @@ func (a AdminConsole) processSection(section *elf.Section) (*ParsedSection, erro result.EmbeddedClusterConfig = content.Bytes() continue } + if bytes.Contains(content.Bytes(), []byte("# channel release object")) { + result.ChannelRelease = content.Bytes() + continue + } } } @@ -145,7 +156,7 @@ func (a AdminConsole) License() (*v1beta1.License, error) { return nil, nil } var license v1beta1.License - if err := yaml.Unmarshal(section.License, &license); err != nil { + if err := kyaml.Unmarshal(section.License, &license); err != nil { return nil, fmt.Errorf("failed to unmarshal license: %w", err) } return &license, nil @@ -178,14 +189,36 @@ func (a AdminConsole) EmbeddedClusterConfig() (*embeddedclusterv1beta1.Config, e } else if section == nil { return nil, nil } - rawcfg := section.EmbeddedClusterConfig if rawcfg == nil { return nil, nil } var cfg embeddedclusterv1beta1.Config - if err := yaml.Unmarshal(rawcfg, &cfg); err != nil { + if err := kyaml.Unmarshal(rawcfg, &cfg); err != nil { return nil, fmt.Errorf("unable to unmarshal embedded cluster config: %w", err) } return &cfg, nil } + +// ChannelRelease reads the embedded channel release object. If no channel release is found, +// returns nil and no error. +func (a AdminConsole) ChannelRelease() (*ChannelRelease, error) { + if runtime.GOOS != "linux" { + return nil, nil + } + section, err := a.ExtractCustomization() + if err != nil { + return nil, err + } else if section == nil { + return nil, nil + } + raw := section.ChannelRelease + if raw == nil { + return nil, nil + } + var release ChannelRelease + if err := yaml.Unmarshal(raw, &release); err != nil { + return nil, fmt.Errorf("unable to unmarshal channel release: %w", err) + } + return &release, nil +}