From e745e16c94b98ea293185855563fe907ea45774f Mon Sep 17 00:00:00 2001 From: Ethan Mosbaugh Date: Fri, 16 Aug 2024 09:24:51 -0700 Subject: [PATCH 1/5] feat(helm): add option to disable helm upgrade force flag Signed-off-by: Ethan Mosbaugh --- docs/helm-charts.md | 19 ++++++++------- inttest/addons/addons_test.go | 24 +++++++++++-------- pkg/apis/helm/v1beta1/chart_types.go | 15 ++++++------ pkg/apis/k0s/v1beta1/extensions.go | 4 +++- .../controller/extensions_controller.go | 2 ++ pkg/helm/helm.go | 4 ++-- .../_crds/helm/helm.k0sproject.io_charts.yaml | 2 ++ .../k0s/k0s.k0sproject.io_clusterconfigs.yaml | 5 ++++ 8 files changed, 46 insertions(+), 29 deletions(-) diff --git a/docs/helm-charts.md b/docs/helm-charts.md index a27167f2a61a..34de9eef5513 100644 --- a/docs/helm-charts.md +++ b/docs/helm-charts.md @@ -35,15 +35,16 @@ It is possible to customize the timeout by using the `timeout' field. ### Chart configuration -| Field | Default value | Description | -|-----------|---------------|----------------------------------------------------------------------| -| name | - | Release name | -| chartname | - | chartname in form "repository/chartname" or path to tgz file | -| version | - | version to install | -| timeout | - | timeout to wait for release install | -| values | - | yaml as a string, custom chart values | -| namespace | - | namespace to install chart into | -| order | 0 | order to apply manifest. For equal values, alphanum ordering is used | +| Field | Default value | Description | +|---------------------|---------------|-------------------------------------------------------------------------------------| +| name | - | Release name | +| chartname | - | chartname in form "repository/chartname" or path to tgz file | +| version | - | version to install | +| timeout | - | timeout to wait for release install | +| values | - | yaml as a string, custom chart values | +| namespace | - | namespace to install chart into | +| disableForceUpgrade | false | disables the use of the "helm upgrade --force" flag when upgrading the the chart | +| order | 0 | order to apply manifest. For equal values, alphanum ordering is used | ## Example diff --git a/inttest/addons/addons_test.go b/inttest/addons/addons_test.go index 3df29c2a3d1c..50100d0dcd58 100644 --- a/inttest/addons/addons_test.go +++ b/inttest/addons/addons_test.go @@ -369,17 +369,19 @@ func (as *AddonsSuite) doTestAddonUpdate(addonName string, values map[string]int Name: "testChartUpdate", Template: chartCrdTemplate, Data: struct { - Name string - ChartName string - Values string - Version string - TargetNS string + Name string + ChartName string + Values string + Version string + TargetNS string + DisableForceUpgrade bool }{ - Name: "test-addon", - ChartName: "ealenn/echo-server", - Values: string(valuesBytes), - Version: "0.5.0", - TargetNS: "default", + Name: "test-addon", + ChartName: "ealenn/echo-server", + Values: string(valuesBytes), + Version: "0.5.0", + TargetNS: "default", + DisableForceUpgrade: true, }, } buf := bytes.NewBuffer([]byte{}) @@ -424,6 +426,7 @@ spec: version: "0.0.1" values: "" namespace: kube-system + disableForceUpgrade: true ` // TODO: this actually duplicates logic from the controller code @@ -443,4 +446,5 @@ spec: {{ .Values | nindent 4 }} version: {{ .Version }} namespace: {{ .TargetNS }} + disableForceUpgrade: {{ .DisableForceUpgrade }} ` diff --git a/pkg/apis/helm/v1beta1/chart_types.go b/pkg/apis/helm/v1beta1/chart_types.go index 3c63f75f7290..d8c7a4cde68d 100644 --- a/pkg/apis/helm/v1beta1/chart_types.go +++ b/pkg/apis/helm/v1beta1/chart_types.go @@ -27,13 +27,14 @@ import ( // ChartSpec defines the desired state of Chart type ChartSpec struct { - ChartName string `json:"chartName,omitempty"` - ReleaseName string `json:"releaseName,omitempty"` - Values string `json:"values,omitempty"` - Version string `json:"version,omitempty"` - Namespace string `json:"namespace,omitempty"` - Timeout string `json:"timeout,omitempty"` - Order int `json:"order,omitempty"` + ChartName string `json:"chartName,omitempty"` + ReleaseName string `json:"releaseName,omitempty"` + Values string `json:"values,omitempty"` + Version string `json:"version,omitempty"` + Namespace string `json:"namespace,omitempty"` + Timeout string `json:"timeout,omitempty"` + DisableForceUpgrade bool `json:"disableForceUpgrade,omitempty"` + Order int `json:"order,omitempty"` } // YamlValues returns values as map diff --git a/pkg/apis/k0s/v1beta1/extensions.go b/pkg/apis/k0s/v1beta1/extensions.go index 591c6e4e74bf..faee3e3ba4de 100644 --- a/pkg/apis/k0s/v1beta1/extensions.go +++ b/pkg/apis/k0s/v1beta1/extensions.go @@ -114,7 +114,9 @@ type Chart struct { // Timeout specifies the timeout for how long to wait for the chart installation to finish. // A duration string is a sequence of decimal numbers, each with optional fraction and a unit suffix, such as "300ms" or "2h45m". Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h". Timeout metav1.Duration `json:"timeout,omitempty"` - Order int `json:"order,omitempty"` + // DisableForceUpgrade disables the the use of the "helm upgrade --force" flag when upgrading the the chart. + DisableForceUpgrade bool `json:"disableForceUpgrade,omitempty"` + Order int `json:"order,omitempty"` } // Validate performs validation diff --git a/pkg/component/controller/extensions_controller.go b/pkg/component/controller/extensions_controller.go index 52d84972be0c..e9249f1397de 100644 --- a/pkg/component/controller/extensions_controller.go +++ b/pkg/component/controller/extensions_controller.go @@ -300,6 +300,7 @@ func (cr *ChartReconciler) updateOrInstallChart(ctx context.Context, chart helmv chart.Status.Namespace, chart.Spec.YamlValues(), timeout, + !chart.Spec.DisableForceUpgrade, ) if err != nil { return fmt.Errorf("can't reconcile upgrade for %q: %w", chart.GetName(), err) @@ -372,6 +373,7 @@ spec: {{ .Values | nindent 4 }} version: {{ .Version }} namespace: {{ .TargetNS }} + disableForceUpgrade: {{ .DisableForceUpgrade }} ` const finalizerName = "helm.k0sproject.io/uninstall-helm-release" diff --git a/pkg/helm/helm.go b/pkg/helm/helm.go index f26997ce086a..938303b84e39 100644 --- a/pkg/helm/helm.go +++ b/pkg/helm/helm.go @@ -264,7 +264,7 @@ func (hc *Commands) InstallChart(ctx context.Context, chartName string, version // UpgradeChart upgrades a helm chart. // InstallChart, UpgradeChart and UninstallRelease(releaseName are *NOT* thread-safe -func (hc *Commands) UpgradeChart(ctx context.Context, chartName string, version string, releaseName string, namespace string, values map[string]interface{}, timeout time.Duration) (*release.Release, error) { +func (hc *Commands) UpgradeChart(ctx context.Context, chartName string, version string, releaseName string, namespace string, values map[string]interface{}, timeout time.Duration, force bool) (*release.Release, error) { cfg, err := hc.getActionCfg(namespace) if err != nil { return nil, fmt.Errorf("can't create action configuration: %w", err) @@ -274,7 +274,7 @@ func (hc *Commands) UpgradeChart(ctx context.Context, chartName string, version upgrade.Wait = true upgrade.WaitForJobs = true upgrade.Install = true - upgrade.Force = true + upgrade.Force = force upgrade.Atomic = true upgrade.Timeout = timeout chartDir, err := hc.locateChart(chartName, version) diff --git a/static/_crds/helm/helm.k0sproject.io_charts.yaml b/static/_crds/helm/helm.k0sproject.io_charts.yaml index 517a675506ab..23f773336356 100644 --- a/static/_crds/helm/helm.k0sproject.io_charts.yaml +++ b/static/_crds/helm/helm.k0sproject.io_charts.yaml @@ -41,6 +41,8 @@ spec: properties: chartName: type: string + disableForceUpgrade: + type: boolean namespace: type: string order: diff --git a/static/_crds/k0s/k0s.k0sproject.io_clusterconfigs.yaml b/static/_crds/k0s/k0s.k0sproject.io_clusterconfigs.yaml index 92752afc7e1e..8f1a7ba510fe 100644 --- a/static/_crds/k0s/k0s.k0sproject.io_clusterconfigs.yaml +++ b/static/_crds/k0s/k0s.k0sproject.io_clusterconfigs.yaml @@ -101,6 +101,11 @@ spec: chartname: minLength: 1 type: string + disableForceUpgrade: + description: DisableForceUpgrade disables the the use + of the "helm upgrade --force" flag when upgrading + the the chart. + type: boolean name: maxLength: 53 minLength: 1 From 339b4d4900afaae96f0d9f7d805fede5641d7140 Mon Sep 17 00:00:00 2001 From: Ethan Mosbaugh Date: Wed, 21 Aug 2024 09:56:21 -0700 Subject: [PATCH 2/5] change from disableForceUpgrade to forceUpgrade Signed-off-by: Ethan Mosbaugh --- docs/helm-charts.md | 20 ++-- inttest/addons/addons_test.go | 31 +++--- pkg/apis/helm/v1beta1/chart_types.go | 23 +++-- .../helm/v1beta1/zz_generated.deepcopy.go | 7 +- pkg/apis/k0s/v1beta1/extensions.go | 8 +- pkg/apis/k0s/v1beta1/zz_generated.deepcopy.go | 13 ++- .../controller/extensions_controller.go | 42 ++++---- .../controller/extensions_controller_test.go | 95 +++++++++++++++++++ .../_crds/helm/helm.k0sproject.io_charts.yaml | 2 +- .../k0s/k0s.k0sproject.io_clusterconfigs.yaml | 9 +- 10 files changed, 191 insertions(+), 59 deletions(-) diff --git a/docs/helm-charts.md b/docs/helm-charts.md index 34de9eef5513..618a58272965 100644 --- a/docs/helm-charts.md +++ b/docs/helm-charts.md @@ -35,16 +35,16 @@ It is possible to customize the timeout by using the `timeout' field. ### Chart configuration -| Field | Default value | Description | -|---------------------|---------------|-------------------------------------------------------------------------------------| -| name | - | Release name | -| chartname | - | chartname in form "repository/chartname" or path to tgz file | -| version | - | version to install | -| timeout | - | timeout to wait for release install | -| values | - | yaml as a string, custom chart values | -| namespace | - | namespace to install chart into | -| disableForceUpgrade | false | disables the use of the "helm upgrade --force" flag when upgrading the the chart | -| order | 0 | order to apply manifest. For equal values, alphanum ordering is used | +| Field | Default value | Description | +|--------------|---------------|----------------------------------------------------------------------------------------| +| name | - | Release name | +| chartname | - | chartname in form "repository/chartname" or path to tgz file | +| version | - | version to install | +| timeout | - | timeout to wait for release install | +| values | - | yaml as a string, custom chart values | +| namespace | - | namespace to install chart into | +| forceUpgrade | true | when set to false, disables the use of the "--force" flag when upgrading the the chart | +| order | 0 | order to apply manifest. For equal values, alphanum ordering is used | ## Example diff --git a/inttest/addons/addons_test.go b/inttest/addons/addons_test.go index 50100d0dcd58..d9e209498f96 100644 --- a/inttest/addons/addons_test.go +++ b/inttest/addons/addons_test.go @@ -37,6 +37,7 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/util/wait" k8s "k8s.io/client-go/kubernetes" + "k8s.io/utils/ptr" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" crlog "sigs.k8s.io/controller-runtime/pkg/log" @@ -369,19 +370,19 @@ func (as *AddonsSuite) doTestAddonUpdate(addonName string, values map[string]int Name: "testChartUpdate", Template: chartCrdTemplate, Data: struct { - Name string - ChartName string - Values string - Version string - TargetNS string - DisableForceUpgrade bool + Name string + ChartName string + Values string + Version string + TargetNS string + ForceUpgrade *bool }{ - Name: "test-addon", - ChartName: "ealenn/echo-server", - Values: string(valuesBytes), - Version: "0.5.0", - TargetNS: "default", - DisableForceUpgrade: true, + Name: "test-addon", + ChartName: "ealenn/echo-server", + Values: string(valuesBytes), + Version: "0.5.0", + TargetNS: "default", + ForceUpgrade: ptr.To(false), }, } buf := bytes.NewBuffer([]byte{}) @@ -426,7 +427,7 @@ spec: version: "0.0.1" values: "" namespace: kube-system - disableForceUpgrade: true + forceUpgrade: false ` // TODO: this actually duplicates logic from the controller code @@ -446,5 +447,7 @@ spec: {{ .Values | nindent 4 }} version: {{ .Version }} namespace: {{ .TargetNS }} - disableForceUpgrade: {{ .DisableForceUpgrade }} +{{- if ne .ForceUpgrade nil }} + forceUpgrade: {{ .ForceUpgrade }} +{{- end }} ` diff --git a/pkg/apis/helm/v1beta1/chart_types.go b/pkg/apis/helm/v1beta1/chart_types.go index d8c7a4cde68d..b0469b51da0e 100644 --- a/pkg/apis/helm/v1beta1/chart_types.go +++ b/pkg/apis/helm/v1beta1/chart_types.go @@ -27,14 +27,14 @@ import ( // ChartSpec defines the desired state of Chart type ChartSpec struct { - ChartName string `json:"chartName,omitempty"` - ReleaseName string `json:"releaseName,omitempty"` - Values string `json:"values,omitempty"` - Version string `json:"version,omitempty"` - Namespace string `json:"namespace,omitempty"` - Timeout string `json:"timeout,omitempty"` - DisableForceUpgrade bool `json:"disableForceUpgrade,omitempty"` - Order int `json:"order,omitempty"` + ChartName string `json:"chartName,omitempty"` + ReleaseName string `json:"releaseName,omitempty"` + Values string `json:"values,omitempty"` + Version string `json:"version,omitempty"` + Namespace string `json:"namespace,omitempty"` + Timeout string `json:"timeout,omitempty"` + ForceUpgrade *bool `json:"forceUpgrade,omitempty"` + Order int `json:"order,omitempty"` } // YamlValues returns values as map @@ -55,6 +55,13 @@ func (cs ChartSpec) HashValues() string { return fmt.Sprintf("%x", h.Sum(nil)) } +// ShouldForceUpgrade returns true if the chart should be force upgraded +func (cs ChartSpec) ShouldForceUpgrade() bool { + // This defaults to true when not explicitly set to false. + // Better have this the other way round in the next API version. + return cs.ForceUpgrade == nil || *cs.ForceUpgrade +} + // ChartStatus defines the observed state of Chart type ChartStatus struct { ReleaseName string `json:"releaseName,omitempty"` diff --git a/pkg/apis/helm/v1beta1/zz_generated.deepcopy.go b/pkg/apis/helm/v1beta1/zz_generated.deepcopy.go index 447f2669c4c1..c6b29424a13e 100644 --- a/pkg/apis/helm/v1beta1/zz_generated.deepcopy.go +++ b/pkg/apis/helm/v1beta1/zz_generated.deepcopy.go @@ -29,7 +29,7 @@ func (in *Chart) DeepCopyInto(out *Chart) { *out = *in out.TypeMeta = in.TypeMeta in.ObjectMeta.DeepCopyInto(&out.ObjectMeta) - out.Spec = in.Spec + in.Spec.DeepCopyInto(&out.Spec) out.Status = in.Status } @@ -86,6 +86,11 @@ func (in *ChartList) DeepCopyObject() runtime.Object { // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *ChartSpec) DeepCopyInto(out *ChartSpec) { *out = *in + if in.ForceUpgrade != nil { + in, out := &in.ForceUpgrade, &out.ForceUpgrade + *out = new(bool) + **out = **in + } } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ChartSpec. diff --git a/pkg/apis/k0s/v1beta1/extensions.go b/pkg/apis/k0s/v1beta1/extensions.go index faee3e3ba4de..c88bd207072f 100644 --- a/pkg/apis/k0s/v1beta1/extensions.go +++ b/pkg/apis/k0s/v1beta1/extensions.go @@ -114,9 +114,11 @@ type Chart struct { // Timeout specifies the timeout for how long to wait for the chart installation to finish. // A duration string is a sequence of decimal numbers, each with optional fraction and a unit suffix, such as "300ms" or "2h45m". Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h". Timeout metav1.Duration `json:"timeout,omitempty"` - // DisableForceUpgrade disables the the use of the "helm upgrade --force" flag when upgrading the the chart. - DisableForceUpgrade bool `json:"disableForceUpgrade,omitempty"` - Order int `json:"order,omitempty"` + // When set to false, disables the use of the "--force" flag when upgrading the the chart (default: true). + // +kubebuilder:default=true + // +optional + ForceUpgrade *bool `json:"forceUpgrade,omitempty"` + Order int `json:"order,omitempty"` } // Validate performs validation diff --git a/pkg/apis/k0s/v1beta1/zz_generated.deepcopy.go b/pkg/apis/k0s/v1beta1/zz_generated.deepcopy.go index df19e82d6a45..fe50d9ca9860 100644 --- a/pkg/apis/k0s/v1beta1/zz_generated.deepcopy.go +++ b/pkg/apis/k0s/v1beta1/zz_generated.deepcopy.go @@ -130,6 +130,11 @@ func (in *CalicoImageSpec) DeepCopy() *CalicoImageSpec { func (in *Chart) DeepCopyInto(out *Chart) { *out = *in out.Timeout = in.Timeout + if in.ForceUpgrade != nil { + in, out := &in.ForceUpgrade, &out.ForceUpgrade + *out = new(bool) + **out = **in + } } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Chart. @@ -147,7 +152,9 @@ func (in ChartsSettings) DeepCopyInto(out *ChartsSettings) { { in := &in *out = make(ChartsSettings, len(*in)) - copy(*out, *in) + for i := range *in { + (*in)[i].DeepCopyInto(&(*out)[i]) + } } } @@ -609,7 +616,9 @@ func (in *HelmExtensions) DeepCopyInto(out *HelmExtensions) { if in.Charts != nil { in, out := &in.Charts, &out.Charts *out = make(ChartsSettings, len(*in)) - copy(*out, *in) + for i := range *in { + (*in)[i].DeepCopyInto(&(*out)[i]) + } } } diff --git a/pkg/component/controller/extensions_controller.go b/pkg/component/controller/extensions_controller.go index e9249f1397de..8331622296eb 100644 --- a/pkg/component/controller/extensions_controller.go +++ b/pkg/component/controller/extensions_controller.go @@ -116,24 +116,13 @@ func (ec *ExtensionsController) reconcileHelmExtensions(helmSpec *k0sv1beta1.Hel fileName := chartManifestFileName(&chart) fileNamesToKeep = append(fileNamesToKeep, fileName) - tw := templatewriter.TemplateWriter{ - Path: filepath.Join(ec.manifestsDir, fileName), - Name: "addon_crd_manifest", - Template: chartCrdTemplate, - Data: struct { - k0sv1beta1.Chart - Finalizer string - }{ - Chart: chart, - Finalizer: finalizerName, - }, - } - if err := tw.Write(); err != nil { + path, err := ec.writeChartManifestFile(chart, fileName) + if err != nil { errs = append(errs, fmt.Errorf("can't write file for Helm chart manifest %q: %w", chart.ChartName, err)) continue } - ec.L.Infof("Wrote Helm chart manifest file %q", tw.Path) + ec.L.Infof("Wrote Helm chart manifest file %q", path) } if err := filepath.WalkDir(ec.manifestsDir, func(path string, entry fs.DirEntry, err error) error { @@ -162,6 +151,25 @@ func (ec *ExtensionsController) reconcileHelmExtensions(helmSpec *k0sv1beta1.Hel return errors.Join(errs...) } +func (ec *ExtensionsController) writeChartManifestFile(chart k0sv1beta1.Chart, fileName string) (string, error) { + tw := templatewriter.TemplateWriter{ + Path: filepath.Join(ec.manifestsDir, fileName), + Name: "addon_crd_manifest", + Template: chartCrdTemplate, + Data: struct { + k0sv1beta1.Chart + Finalizer string + }{ + Chart: chart, + Finalizer: finalizerName, + }, + } + if err := tw.Write(); err != nil { + return "", err + } + return tw.Path, nil +} + // Determines the file name to use when storing a chart as a manifest on disk. func chartManifestFileName(c *k0sv1beta1.Chart) string { return fmt.Sprintf("%d_helm_extension_%s.yaml", c.Order, c.Name) @@ -300,7 +308,7 @@ func (cr *ChartReconciler) updateOrInstallChart(ctx context.Context, chart helmv chart.Status.Namespace, chart.Spec.YamlValues(), timeout, - !chart.Spec.DisableForceUpgrade, + chart.Spec.ShouldForceUpgrade(), ) if err != nil { return fmt.Errorf("can't reconcile upgrade for %q: %w", chart.GetName(), err) @@ -373,7 +381,9 @@ spec: {{ .Values | nindent 4 }} version: {{ .Version }} namespace: {{ .TargetNS }} - disableForceUpgrade: {{ .DisableForceUpgrade }} +{{- if ne .ForceUpgrade nil }} + forceUpgrade: {{ .ForceUpgrade }} +{{- end }} ` const finalizerName = "helm.k0sproject.io/uninstall-helm-release" diff --git a/pkg/component/controller/extensions_controller_test.go b/pkg/component/controller/extensions_controller_test.go index 499c68f9fdfb..94d46fe9fb85 100644 --- a/pkg/component/controller/extensions_controller_test.go +++ b/pkg/component/controller/extensions_controller_test.go @@ -17,11 +17,17 @@ limitations under the License. package controller import ( + "os" + "strings" "testing" + "time" "github.com/k0sproject/k0s/pkg/apis/helm/v1beta1" k0sv1beta1 "github.com/k0sproject/k0s/pkg/apis/k0s/v1beta1" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/utils/ptr" ) func TestChartNeedsUpgrade(t *testing.T) { @@ -162,3 +168,92 @@ func TestChartManifestFileName(t *testing.T) { assert.Equal(t, chartManifestFileName(&chart2), "2_helm_extension_release.yaml") assert.True(t, isChartManifestFileName("0_helm_extension_release.yaml")) } + +func TestExtensionsController_writeChartManifestFile(t *testing.T) { + type args struct { + chart k0sv1beta1.Chart + fileName string + } + tests := []struct { + name string + args args + want string + }{ + { + name: "forceUpgrade is nil should omit from manifest", + args: args{ + chart: k0sv1beta1.Chart{ + Name: "release", + ChartName: "k0s/chart", + Version: "0.0.1", + Values: "values", + TargetNS: "default", + Timeout: metav1.Duration{Duration: 5 * time.Minute}, + }, + fileName: "0_helm_extension_release.yaml", + }, + want: `apiVersion: helm.k0sproject.io/v1beta1 +kind: Chart +metadata: + name: k0s-addon-chart-release + namespace: "kube-system" + finalizers: + - helm.k0sproject.io/uninstall-helm-release +spec: + chartName: k0s/chart + releaseName: release + timeout: 5m0s + values: | + + values + version: 0.0.1 + namespace: default +`, + }, + { + name: "forceUpgrade is false should be included in manifest", + args: args{ + chart: k0sv1beta1.Chart{ + Name: "release", + ChartName: "k0s/chart", + Version: "0.0.1", + Values: "values", + TargetNS: "default", + Timeout: metav1.Duration{Duration: 5 * time.Minute}, + ForceUpgrade: ptr.To(false), + }, + fileName: "0_helm_extension_release.yaml", + }, + want: `apiVersion: helm.k0sproject.io/v1beta1 +kind: Chart +metadata: + name: k0s-addon-chart-release + namespace: "kube-system" + finalizers: + - helm.k0sproject.io/uninstall-helm-release +spec: + chartName: k0s/chart + releaseName: release + timeout: 5m0s + values: | + + values + version: 0.0.1 + namespace: default + forceUpgrade: false +`, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + ec := &ExtensionsController{ + manifestsDir: t.TempDir(), + } + path, err := ec.writeChartManifestFile(tt.args.chart, tt.args.fileName) + require.NoError(t, err) + contents, err := os.ReadFile(path) + require.NoError(t, err) + assert.Equal(t, strings.TrimSpace(tt.want), strings.TrimSpace(string(contents))) + }) + } +} diff --git a/static/_crds/helm/helm.k0sproject.io_charts.yaml b/static/_crds/helm/helm.k0sproject.io_charts.yaml index 23f773336356..3620b7c35b65 100644 --- a/static/_crds/helm/helm.k0sproject.io_charts.yaml +++ b/static/_crds/helm/helm.k0sproject.io_charts.yaml @@ -41,7 +41,7 @@ spec: properties: chartName: type: string - disableForceUpgrade: + forceUpgrade: type: boolean namespace: type: string diff --git a/static/_crds/k0s/k0s.k0sproject.io_clusterconfigs.yaml b/static/_crds/k0s/k0s.k0sproject.io_clusterconfigs.yaml index 8f1a7ba510fe..499da637c6e9 100644 --- a/static/_crds/k0s/k0s.k0sproject.io_clusterconfigs.yaml +++ b/static/_crds/k0s/k0s.k0sproject.io_clusterconfigs.yaml @@ -101,10 +101,11 @@ spec: chartname: minLength: 1 type: string - disableForceUpgrade: - description: DisableForceUpgrade disables the the use - of the "helm upgrade --force" flag when upgrading - the the chart. + forceUpgrade: + default: true + description: 'When set to false, disables the use of + the "--force" flag when upgrading the the chart (default: + true).' type: boolean name: maxLength: 53 From 27b816d4720616c39d903637ee65b19b069866c3 Mon Sep 17 00:00:00 2001 From: Ethan Mosbaugh Date: Wed, 4 Sep 2024 09:09:13 -0700 Subject: [PATCH 3/5] add go doc and kubebuilder markers for crd validation to helmv1beta1 type Signed-off-by: Ethan Mosbaugh --- pkg/apis/helm/v1beta1/chart_types.go | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/pkg/apis/helm/v1beta1/chart_types.go b/pkg/apis/helm/v1beta1/chart_types.go index b0469b51da0e..79e2e5a781c7 100644 --- a/pkg/apis/helm/v1beta1/chart_types.go +++ b/pkg/apis/helm/v1beta1/chart_types.go @@ -27,14 +27,17 @@ import ( // ChartSpec defines the desired state of Chart type ChartSpec struct { - ChartName string `json:"chartName,omitempty"` - ReleaseName string `json:"releaseName,omitempty"` - Values string `json:"values,omitempty"` - Version string `json:"version,omitempty"` - Namespace string `json:"namespace,omitempty"` - Timeout string `json:"timeout,omitempty"` - ForceUpgrade *bool `json:"forceUpgrade,omitempty"` - Order int `json:"order,omitempty"` + ChartName string `json:"chartName,omitempty"` + ReleaseName string `json:"releaseName,omitempty"` + Values string `json:"values,omitempty"` + Version string `json:"version,omitempty"` + Namespace string `json:"namespace,omitempty"` + Timeout string `json:"timeout,omitempty"` + // ForceUpgrade when set to false, disables the use of the "--force" flag when upgrading the the chart (default: true). + // +kubebuilder:default=true + // +optional + ForceUpgrade *bool `json:"forceUpgrade,omitempty"` + Order int `json:"order,omitempty"` } // YamlValues returns values as map From 60aaf75173301bcff6023b01dbdde4e90cd8c201 Mon Sep 17 00:00:00 2001 From: Ethan Mosbaugh Date: Wed, 4 Sep 2024 09:10:41 -0700 Subject: [PATCH 4/5] fix go doc string prefix with variable name Signed-off-by: Ethan Mosbaugh --- pkg/apis/k0s/v1beta1/extensions.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/apis/k0s/v1beta1/extensions.go b/pkg/apis/k0s/v1beta1/extensions.go index c88bd207072f..d412027b1d01 100644 --- a/pkg/apis/k0s/v1beta1/extensions.go +++ b/pkg/apis/k0s/v1beta1/extensions.go @@ -114,7 +114,7 @@ type Chart struct { // Timeout specifies the timeout for how long to wait for the chart installation to finish. // A duration string is a sequence of decimal numbers, each with optional fraction and a unit suffix, such as "300ms" or "2h45m". Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h". Timeout metav1.Duration `json:"timeout,omitempty"` - // When set to false, disables the use of the "--force" flag when upgrading the the chart (default: true). + // ForceUpgrade when set to false, disables the use of the "--force" flag when upgrading the the chart (default: true). // +kubebuilder:default=true // +optional ForceUpgrade *bool `json:"forceUpgrade,omitempty"` From ffce6750bfe394fcccc417815e79d12fa77a94bd Mon Sep 17 00:00:00 2001 From: Ethan Mosbaugh Date: Thu, 5 Sep 2024 11:50:10 -0700 Subject: [PATCH 5/5] regenerate crds Signed-off-by: Ethan Mosbaugh --- static/_crds/helm/helm.k0sproject.io_charts.yaml | 3 +++ static/_crds/k0s/k0s.k0sproject.io_clusterconfigs.yaml | 6 +++--- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/static/_crds/helm/helm.k0sproject.io_charts.yaml b/static/_crds/helm/helm.k0sproject.io_charts.yaml index 3620b7c35b65..98ea00310323 100644 --- a/static/_crds/helm/helm.k0sproject.io_charts.yaml +++ b/static/_crds/helm/helm.k0sproject.io_charts.yaml @@ -42,6 +42,9 @@ spec: chartName: type: string forceUpgrade: + default: true + description: 'ForceUpgrade when set to false, disables the use of + the "--force" flag when upgrading the the chart (default: true).' type: boolean namespace: type: string diff --git a/static/_crds/k0s/k0s.k0sproject.io_clusterconfigs.yaml b/static/_crds/k0s/k0s.k0sproject.io_clusterconfigs.yaml index 499da637c6e9..46637c6b3e5b 100644 --- a/static/_crds/k0s/k0s.k0sproject.io_clusterconfigs.yaml +++ b/static/_crds/k0s/k0s.k0sproject.io_clusterconfigs.yaml @@ -103,9 +103,9 @@ spec: type: string forceUpgrade: default: true - description: 'When set to false, disables the use of - the "--force" flag when upgrading the the chart (default: - true).' + description: 'ForceUpgrade when set to false, disables + the use of the "--force" flag when upgrading the the + chart (default: true).' type: boolean name: maxLength: 53