Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: apply embedded unsupported overrides #114

Merged
merged 4 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/AlecAivazis/survey/v2 v2.3.7
github.com/canonical/lxd v0.0.0-20230814092713-c77ee90f5032
github.com/creack/pty v1.1.18
github.com/ghodss/yaml v1.0.0
github.com/google/go-containerregistry v0.14.0
github.com/google/uuid v1.3.1
github.com/gosimple/slug v1.13.1
Expand Down Expand Up @@ -72,7 +73,7 @@ require (
github.com/docker/go-units v0.5.0 // indirect
github.com/docker/libtrust v0.0.0-20160708172513-aabc10ec26b7 // indirect
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
github.com/evanphx/json-patch v5.6.0+incompatible // indirect
github.com/evanphx/json-patch v5.6.0+incompatible
github.com/evanphx/json-patch/v5 v5.7.0 // indirect
github.com/exponent-io/jsonpath v0.0.0-20151013193312-d6023ce2651d // indirect
github.com/fatih/color v1.15.0 // indirect
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,7 @@ github.com/gabriel-vasile/mimetype v1.4.2/go.mod h1:zApsH/mKG4w07erKIaJPFiX0Tsq9
github.com/garyburd/redigo v0.0.0-20150301180006-535138d7bcd7/go.mod h1:NR3MbYisc3/PwhQ00EMzDiPmrwpPxAn5GI05/YaO1SY=
github.com/getsentry/raven-go v0.0.0-20180121060056-563b81fc02b7/go.mod h1:KungGk8q33+aIAZUIVWZDr2OfAEBsO49PX4NzFV5kcQ=
github.com/ghodss/yaml v0.0.0-20150909031657-73d445a93680/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/globalsign/mgo v0.0.0-20180905125535-1ca0a4f7cbcb/go.mod h1:xkRDCp4j0OGD1HRkm4kmhM+pmpv3AKq5SU7GMg4oO/Q=
github.com/globalsign/mgo v0.0.0-20181015135952-eeefdecb41b8/go.mod h1:xkRDCp4j0OGD1HRkm4kmhM+pmpv3AKq5SU7GMg4oO/Q=
Expand Down
14 changes: 10 additions & 4 deletions pkg/addons/adminconsole/adminconsole.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ var helmValues = map[string]interface{}{
},
}

// AdminConsole manages the admin console helm chart installation.
type AdminConsole struct {
customization customization.AdminConsoleCustomization
customization customization.AdminConsole
namespace string
useprompt bool
config v1beta1.ClusterConfig
Expand Down Expand Up @@ -67,6 +68,7 @@ func (a *AdminConsole) askPassword() (string, error) {

}

// Version returns the embedded admin console version.
func (a *AdminConsole) Version() (map[string]string, error) {
latest, err := a.Latest()
if err != nil {
Expand All @@ -76,7 +78,7 @@ func (a *AdminConsole) Version() (map[string]string, error) {

}

// HostPreflight returns the host preflight objects found inside the adminconsole
// HostPreflights returns the host preflight objects found inside the adminconsole
// or as part of the embedded kots release (customization).
func (a *AdminConsole) HostPreflights() (*v1beta2.HostPreflightSpec, error) {
return a.customization.HostPreflights()
Expand Down Expand Up @@ -202,7 +204,7 @@ func (a *AdminConsole) GenerateHelmConfig() ([]v1beta1.Chart, error) {
helmValues["password"] = pass
}

cust, err := customization.AdminConsoleCustomization{}.ExtractCustomization()
cust, err := customization.AdminConsole{}.ExtractCustomization()
if err == nil {
if cust != nil && cust.Application != nil {
helmValues["kotsApplication"] = string(cust.Application)
Expand All @@ -228,6 +230,7 @@ func (a *AdminConsole) GenerateHelmConfig() ([]v1beta1.Chart, error) {

}

// WriteChartFile writes the adminconsole chart to the disk.
func (a *AdminConsole) WriteChartFile(version string) error {

chartfile, err := a.GetChartFileName()
Expand Down Expand Up @@ -255,6 +258,7 @@ func (a *AdminConsole) WriteChartFile(version string) error {
return nil
}

// GetChartFileName returns the name of the adminconsole chart.
func (a *AdminConsole) GetChartFileName() (string, error) {
latest, err := a.Latest()
if err != nil {
Expand All @@ -275,6 +279,7 @@ func (a *AdminConsole) GetChartFileName() (string, error) {
return "", fmt.Errorf("unable to find adminconsole chart file")
}

// Latest returns the latest version of the adminconsole chart.
func (a *AdminConsole) Latest() (string, error) {
logrus.Infof("Finding latest Admin Console addon version")
files, err := charts.FS.ReadDir(".")
Expand Down Expand Up @@ -304,11 +309,12 @@ func (a *AdminConsole) Latest() (string, error) {
return latest, nil
}

// New creates a new AdminConsole object.
func New(ns string, useprompt bool, config v1beta1.ClusterConfig) (*AdminConsole, error) {
return &AdminConsole{
namespace: ns,
useprompt: useprompt,
customization: customization.AdminConsoleCustomization{},
customization: customization.AdminConsole{},
config: config,
}, nil
}
75 changes: 73 additions & 2 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"strconv"
"strings"

jsonpatch "github.com/evanphx/json-patch"
fmtconvert "github.com/ghodss/yaml"
"github.com/k0sproject/dig"
k0sconfig "github.com/k0sproject/k0s/pkg/apis/v1beta1"
"github.com/k0sproject/k0sctl/pkg/apis/k0sctl.k0sproject.io/v1beta1"
Expand All @@ -22,6 +24,7 @@ import (
"sigs.k8s.io/yaml"

"github.com/replicatedhq/helmvm/pkg/addons"
"github.com/replicatedhq/helmvm/pkg/customization"
"github.com/replicatedhq/helmvm/pkg/defaults"
"github.com/replicatedhq/helmvm/pkg/infra"
"github.com/replicatedhq/helmvm/pkg/prompts"
Expand Down Expand Up @@ -56,9 +59,23 @@ func SetUploadBinary(config *v1beta1.Cluster) {

// RenderClusterConfig renders a cluster configuration interactively.
func RenderClusterConfig(ctx context.Context, nodes []infra.Node, multi bool) (*v1beta1.Cluster, error) {
embconfig, err := customization.AdminConsole{}.EmbeddedClusterConfig()
if err != nil {
return nil, fmt.Errorf("unable to get embedded cluster config: %w", err)
}
if multi {
return renderMultiNodeConfig(ctx, nodes)
cfg, err := renderMultiNodeConfig(ctx, nodes)
if err != nil {
return nil, fmt.Errorf("unable to render multi-node config: %w", err)
}
applyEmbeddedUnsupportedOverrides(cfg, embconfig)
return cfg, nil
}
cfg, err := renderSingleNodeConfig(ctx)
if err != nil {
return nil, fmt.Errorf("unable to render single-node config: %w", err)
}
applyEmbeddedUnsupportedOverrides(cfg, embconfig)
return renderSingleNodeConfig(ctx)
}

Expand Down Expand Up @@ -229,7 +246,7 @@ func renderMultiNodeConfig(ctx context.Context, nodes []infra.Node) (*v1beta1.Cl
}
} else {
for _, node := range nodes {
hostcfg := HostConfigFromInfraNode(node)
hostcfg := hostConfigFromInfraNode(node)
hosts = append(hosts, hostcfg.render())
}
}
Expand Down Expand Up @@ -396,3 +413,57 @@ func updateHostFiles(host *cluster.Host, bundleDir string) error {
}
return nil
}

// UnsupportedConfigOverrides is a auxiliary struct for parsing the unsupported overrides
// as provided in the Kots release. XXX This should eventually become a CRD.
type UnsupportedConfigOverrides struct {
Spec struct {
UnsupportedOverrides struct {
K0s *cluster.K0s `yaml:"k0s"`
} `yaml:"unsupportedOverrides"`
} `yaml:"spec"`
}

// applyEmbeddedUnsupportedOverrides applies the custom configuration to the cluster config.
func applyEmbeddedUnsupportedOverrides(config *v1beta1.Cluster, embconfig []byte) error {
if embconfig == nil {
return nil
}
var overrides UnsupportedConfigOverrides
if err := yaml.Unmarshal(embconfig, &overrides); err != nil {
return fmt.Errorf("unable to parse cluster config overrides: %w", err)
}
if overrides.Spec.UnsupportedOverrides.K0s == nil {
return nil
}
origConfigBytes, err := yaml.Marshal(overrides.Spec.UnsupportedOverrides.K0s.Config)
if err != nil {
return fmt.Errorf("unable to marshal cluster config overrides: %w", err)
}
newConfigBytes, err := yaml.Marshal(config.Spec.K0s.Config)
if err != nil {
return fmt.Errorf("unable to marshal original cluster config: %w", err)
}
original, err := fmtconvert.YAMLToJSON(newConfigBytes)
if err != nil {
return fmt.Errorf("unable to convert cluster config overrides to json: %w", err)
}
target, err := fmtconvert.YAMLToJSON(origConfigBytes)
if err != nil {
return fmt.Errorf("unable to convert original cluster config to json: %w", err)
}
result, err := jsonpatch.MergePatch(original, target)
if err != nil {
return fmt.Errorf("unable to create patch configuration: %w", err)
}
newConfigBytes, err = fmtconvert.JSONToYAML(result)
if err != nil {
return fmt.Errorf("unable to convert patched configuration to json: %w", err)
}
var newK0sConfig dig.Mapping
if err := yaml.Unmarshal(newConfigBytes, &newK0sConfig); err != nil {
return fmt.Errorf("unable to unmarshal patched cluster config: %w", err)
}
config.Spec.K0s.Config = newK0sConfig
return nil
}
54 changes: 54 additions & 0 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package config

import (
"embed"
"path"
"strings"
"testing"

"github.com/k0sproject/k0sctl/pkg/apis/k0sctl.k0sproject.io/v1beta1"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v2"
)

//go:embed testdata/*yaml
var testData embed.FS

func Test_applyUnsupportedOverrides(t *testing.T) {
type test struct {
Name string
Config string `yaml:"config"`
Override string `yaml:"override"`
Expected string `yaml:"expected"`
}
entries, err := testData.ReadDir("testdata")
assert.NoError(t, err)
var tests []test
for _, entry := range entries {
if !strings.HasPrefix(entry.Name(), "override") {
continue
}
fpath := path.Join("testdata", entry.Name())
data, err := testData.ReadFile(fpath)
assert.NoError(t, err)
var onetest test
err = yaml.Unmarshal(data, &onetest)
assert.NoError(t, err)
onetest.Name = fpath
tests = append(tests, onetest)
}
for _, tt := range tests {
t.Run(tt.Name, func(t *testing.T) {
var config v1beta1.Cluster
err := yaml.Unmarshal([]byte(tt.Config), &config)
assert.NoError(t, err)
err = applyEmbeddedUnsupportedOverrides(&config, []byte(tt.Override))
assert.NoError(t, err)
result, err := yaml.Marshal(config)
assert.NoError(t, err)
resultString := strings.TrimSpace(string(result))
expectedString := strings.TrimSpace(string(tt.Expected))
assert.Equal(t, resultString, expectedString)
})
}
}
2 changes: 1 addition & 1 deletion pkg/config/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (h *hostcfg) testConnection() error {

// HostConfigFromInfraNode returns a new hostcfg object with properties copied
// from the given infra.Node.
func HostConfigFromInfraNode(node infra.Node) hostcfg {
func hostConfigFromInfraNode(node infra.Node) hostcfg {
return hostcfg{
Address: node.Address,
Role: node.Role,
Expand Down
67 changes: 67 additions & 0 deletions pkg/config/testdata/override-change-name.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
config: |-
apiVersion: k0sctl.k0sproject.io/v1beta1
kind: Cluster
metadata:
name: helmvm
spec:
hosts:
- ssh:
address: 18.235.234.142
user: ubuntu
port: 22
keyPath: /home/user/.ssh/key.pem
role: controller+worker
installFlags:
- --disable-components konnectivity-server
noTaints: true
k0s:
version: v1.27.5+k0s.0
dynamicConfig: false
config:
apiVersion: k0s.k0sproject.io/v1beta1
kind: ClusterConfig
metadata:
name: helmvm
spec:
network:
provider: calico
telemetry:
enabled: false
override: |-
apiVersion: embeddedcluster.replicated.com/v1beta1
kind: Config
spec:
unsupportedOverrides:
k0s:
config:
metadata:
name: foo
expected: |-
apiVersion: k0sctl.k0sproject.io/v1beta1
kind: Cluster
metadata:
name: helmvm
spec:
hosts:
- ssh:
address: 18.235.234.142
user: ubuntu
port: 22
keyPath: /home/user/.ssh/key.pem
role: controller+worker
installFlags:
- --disable-components konnectivity-server
noTaints: true
k0s:
version: v1.27.5+k0s.0
dynamicConfig: false
config:
apiVersion: k0s.k0sproject.io/v1beta1
kind: ClusterConfig
metadata:
name: foo
spec:
network:
provider: calico
telemetry:
enabled: false
Loading
Loading