From ee8039453ecb37448dcc485654bc7312f6adf3a0 Mon Sep 17 00:00:00 2001 From: Ricardo Maraschini Date: Tue, 26 Sep 2023 16:38:16 +0200 Subject: [PATCH] feat: deploying admin console with the embedded license if a license exists inside the binary we configure admin console to use it. if no license was found deploys admin console as we used to (without license). --- go.mod | 1 + go.sum | 4 ++++ pkg/addons/adminconsole/adminconsole.go | 27 +++++++++++++++++++++ pkg/addons/adminconsole/customize.go | 32 +++++++++++++++++++++---- 4 files changed, 60 insertions(+), 4 deletions(-) diff --git a/go.mod b/go.mod index 5ba91cd79..9f49be056 100644 --- a/go.mod +++ b/go.mod @@ -14,6 +14,7 @@ require ( github.com/k0sproject/dig v0.2.0 github.com/k0sproject/k0sctl v0.15.5 github.com/k0sproject/rig v0.13.0 + github.com/replicatedhq/kotskinds v0.0.0-20230724164735-f83482cc9cfe github.com/replicatedhq/troubleshoot v0.72.1 github.com/sirupsen/logrus v1.9.3 github.com/stretchr/testify v1.8.4 diff --git a/go.sum b/go.sum index 51e84cdca..42f70b268 100644 --- a/go.sum +++ b/go.sum @@ -298,6 +298,8 @@ github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9 github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 h1:tfuBGBXKqDEevZMzYi5KSi8KkcZtzBcTgAUUtapy0OI= github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572/go.mod h1:9Pwr4B2jHnOSGXyyzV8ROjYa2ojvAY6HCGYYfMoC3Ls= +github.com/go-test/deep v1.1.0 h1:WOcxcdHcvdgThNXjw0t76K42FXTU7HpNQWHpA2HHNlg= +github.com/go-test/deep v1.1.0/go.mod h1:5C2ZWiW0ErCdrYzpqxLbTX7MG14M9iiw8DgHncVwcsE= github.com/gobuffalo/logger v1.0.6 h1:nnZNpxYo0zx+Aj9RfMPBm+x9zAU2OayFh/xrAWi34HU= github.com/gobuffalo/logger v1.0.6/go.mod h1:J31TBEHR1QLV2683OXTAItYIg8pv2JMHnF/quuAbMjs= github.com/gobuffalo/packd v1.0.1 h1:U2wXfRr4E9DH8IdsDLlRFwTZTK7hLfq9qT/QHXGVe/0= @@ -736,6 +738,8 @@ github.com/prometheus/procfs v0.0.3/go.mod h1:4A/X28fw3Fc593LaREMrKMqOKvUAntwMDa github.com/prometheus/procfs v0.11.1 h1:xRC8Iq1yyca5ypa9n1EZnWZkt7dwcoRPQwX/5gwaUuI= github.com/prometheus/procfs v0.11.1/go.mod h1:eesXgaPo1q7lBpVMoMy0ZOFTth9hBn4W/y0/p/ScXhY= github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU= +github.com/replicatedhq/kotskinds v0.0.0-20230724164735-f83482cc9cfe h1:3AJInd06UxzqHmgy8+24CPsT2tYSE0zToJZyuX9q+MA= +github.com/replicatedhq/kotskinds v0.0.0-20230724164735-f83482cc9cfe/go.mod h1:QjhIUu3+OmHZ09u09j3FCoTt8F3BYtQglS+OLmftu9I= github.com/replicatedhq/troubleshoot v0.72.1 h1:PA1kOU6N1PFbxFkizdLEP/FbvhmV1plrHgG2JoxAZw4= github.com/replicatedhq/troubleshoot v0.72.1/go.mod h1:5lp7iXnOUaKY4EPemnwrLiu3HTQhq070epZCJLxoPiE= github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= diff --git a/pkg/addons/adminconsole/adminconsole.go b/pkg/addons/adminconsole/adminconsole.go index fded1e4eb..107f9a62d 100644 --- a/pkg/addons/adminconsole/adminconsole.go +++ b/pkg/addons/adminconsole/adminconsole.go @@ -13,6 +13,7 @@ import ( "helm.sh/helm/v3/pkg/chart/loader" "helm.sh/helm/v3/pkg/cli" "helm.sh/helm/v3/pkg/release" + "sigs.k8s.io/yaml" "github.com/replicatedhq/helmvm/pkg/addons/adminconsole/charts" pb "github.com/replicatedhq/helmvm/pkg/progressbar" @@ -63,6 +64,28 @@ 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 { + license, err := a.customization.license() + if err != nil { + return fmt.Errorf("unable to get license: %w", err) + } + if license == nil { + return nil + } + raw, err := yaml.Marshal(license) + if err != nil { + return fmt.Errorf("unable to marshal license: %w", err) + } + helmValues["automation"] = map[string]interface{}{ + "license": map[string]interface{}{ + "slug": license.Spec.AppSlug, + "data": string(raw), + }, + } + return nil +} + func (a *AdminConsole) Apply(ctx context.Context) error { version, err := a.Latest() if err != nil { @@ -79,6 +102,10 @@ func (a *AdminConsole) Apply(ctx context.Context) error { } defer hfp.Close() + if err := a.addLicenseToHelmValues(); err != nil { + return fmt.Errorf("unable to add license to helm values: %w", err) + } + hchart, err := loader.LoadArchive(hfp) if err != nil { return fmt.Errorf("unable to load chart: %w", err) diff --git a/pkg/addons/adminconsole/customize.go b/pkg/addons/adminconsole/customize.go index 2a7c5feb5..e76502092 100644 --- a/pkg/addons/adminconsole/customize.go +++ b/pkg/addons/adminconsole/customize.go @@ -11,6 +11,7 @@ import ( "os" "runtime" + "github.com/replicatedhq/kotskinds/apis/kots/v1beta1" "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2" "github.com/sirupsen/logrus" corev1 "k8s.io/api/core/v1" @@ -20,15 +21,17 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client/config" "sigs.k8s.io/controller-runtime/pkg/log" "sigs.k8s.io/controller-runtime/pkg/log/zap" + "sigs.k8s.io/yaml" "github.com/replicatedhq/helmvm/pkg/preflights" ) // ParsedSection holds the parsed section from the binary. We only care about the -// application object and whatever HostPreflight we can find. +// application object, whatever HostPreflight we can find, and the app License. type ParsedSection struct { Application []byte HostPreflights [][]byte + License []byte } // AdminConsoleCustomization is a struct that contains the actions to create and update @@ -84,10 +87,12 @@ func (a *AdminConsoleCustomization) processSection(section *elf.Section) (*Parse return nil, fmt.Errorf("unable to copy file out of tar: %w", err) } if bytes.Contains(content.Bytes(), []byte("apiVersion: kots.io/v1beta1")) { - if !bytes.Contains(content.Bytes(), []byte("kind: Application")) { - continue + if bytes.Contains(content.Bytes(), []byte("kind: Application")) { + result.Application = content.Bytes() + } + if bytes.Contains(content.Bytes(), []byte("kind: License")) { + result.License = content.Bytes() } - result.Application = content.Bytes() continue } if bytes.Contains(content.Bytes(), []byte("apiVersion: troubleshoot.sh/v1beta2")) { @@ -188,3 +193,22 @@ func (a *AdminConsoleCustomization) hostPreflights() (*v1beta2.HostPreflightSpec } return all, nil } + +// license reads the kots license from the embedded Kots Application Release. If no license is found, +// returns nil and no error. +func (a *AdminConsoleCustomization) license() (*v1beta1.License, 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 + } + var license v1beta1.License + if err := yaml.Unmarshal(section.License, &license); err != nil { + return nil, fmt.Errorf("failed to unmarshal license: %w", err) + } + return &license, nil +}