Skip to content

Commit

Permalink
feat: deploying admin console with the embedded license (#86)
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
ricardomaraschini authored Sep 27, 2023
1 parent e91f7db commit ef4ea79
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 4 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down
27 changes: 27 additions & 0 deletions pkg/addons/adminconsole/adminconsole.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand Down
32 changes: 28 additions & 4 deletions pkg/addons/adminconsole/customize.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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")) {
Expand Down Expand Up @@ -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
}

0 comments on commit ef4ea79

Please sign in to comment.