Skip to content

Commit

Permalink
bug: merge chart values before comparing (#82)
Browse files Browse the repository at this point in the history
* combine charts earlier so we can compare the final values

* add k0s cluster object to unit tests

* add unit tests for drift/no drift with overridden values

---------

Co-authored-by: Andrew Lavery <[email protected]>
  • Loading branch information
danj-replicated and laverya authored Jan 30, 2024
1 parent c8cf3ce commit f7bf508
Show file tree
Hide file tree
Showing 2 changed files with 224 additions and 13 deletions.
25 changes: 13 additions & 12 deletions controllers/installation_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,11 +286,24 @@ func (r *InstallationReconciler) ReconcileHelmCharts(ctx context.Context, in *v1

combinedConfigs := mergeHelmConfigs(meta, in)

// fetch the current clusterconfig
var clusterconfig k0sv1beta1.ClusterConfig
if err := r.Get(ctx, client.ObjectKey{Name: "k0s", Namespace: "kube-system"}, &clusterconfig); err != nil {
return fmt.Errorf("failed to get cluster config: %w", err)
}

finalChartList, err := generateDesiredCharts(meta, clusterconfig, combinedConfigs)
if err != nil {
return err
}
combinedConfigs.Charts = finalChartList

// detect drift between the cluster config and the installer metadata
var installedCharts k0shelm.ChartList
if err := r.List(ctx, &installedCharts); err != nil {
return fmt.Errorf("failed to list installed charts: %w", err)
}

chartErrors, chartDrift, err := detectChartDrift(combinedConfigs, installedCharts)
if err != nil {
return fmt.Errorf("failed to check chart drift: %w", err)
Expand Down Expand Up @@ -320,19 +333,7 @@ func (r *InstallationReconciler) ReconcileHelmCharts(ctx context.Context, in *v1
return nil
}

// fetch the current clusterconfig
var clusterconfig k0sv1beta1.ClusterConfig
if err := r.Get(ctx, client.ObjectKey{Name: "k0s", Namespace: "kube-system"}, &clusterconfig); err != nil {
return fmt.Errorf("failed to get cluster config: %w", err)
}

finalChartList, err := generateDesiredCharts(meta, clusterconfig, combinedConfigs)
if err != nil {
return err
}

// Replace the current chart configs with the new chart configs
combinedConfigs.Charts = finalChartList
clusterconfig.Spec.Extensions.Helm = combinedConfigs
in.Status.SetState(v1beta1.InstallationStateAddonsInstalling, "Installing addons")
//Update the clusterconfig
Expand Down
212 changes: 211 additions & 1 deletion controllers/installation_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,17 @@ func TestInstallationReconciler_ReconcileHelmCharts(t *testing.T) {
Spec: k0shelmv1beta1.ChartSpec{ReleaseName: "extchart"},
Status: k0shelmv1beta1.ChartStatus{Version: "2"},
},
&k0sv1beta1.ClusterConfig{
ObjectMeta: metav1.ObjectMeta{
Name: "k0s",
Namespace: "kube-system",
},
Spec: &k0sv1beta1.ClusterSpec{
Extensions: &k0sv1beta1.ClusterExtensions{
Helm: &k0sv1beta1.HelmExtensions{},
},
},
},
},
},
},
Expand Down Expand Up @@ -168,6 +179,17 @@ func TestInstallationReconciler_ReconcileHelmCharts(t *testing.T) {
Spec: k0shelmv1beta1.ChartSpec{ReleaseName: "extchart"},
Status: k0shelmv1beta1.ChartStatus{Version: "2", Error: "exterror"},
},
&k0sv1beta1.ClusterConfig{
ObjectMeta: metav1.ObjectMeta{
Name: "k0s",
Namespace: "kube-system",
},
Spec: &k0sv1beta1.ClusterSpec{
Extensions: &k0sv1beta1.ClusterExtensions{
Helm: &k0sv1beta1.HelmExtensions{},
},
},
},
},
},
},
Expand Down Expand Up @@ -205,7 +227,19 @@ func TestInstallationReconciler_ReconcileHelmCharts(t *testing.T) {
},
},
fields: fields{
State: []runtime.Object{},
State: []runtime.Object{
&k0sv1beta1.ClusterConfig{
ObjectMeta: metav1.ObjectMeta{
Name: "k0s",
Namespace: "kube-system",
},
Spec: &k0sv1beta1.ClusterSpec{
Extensions: &k0sv1beta1.ClusterExtensions{
Helm: &k0sv1beta1.HelmExtensions{},
},
},
},
},
},
},
{
Expand Down Expand Up @@ -270,6 +304,182 @@ func TestInstallationReconciler_ReconcileHelmCharts(t *testing.T) {
},
},
},
{
name: "k8s install completed, good version, overridden values, both types of charts, no drift",
in: v1beta1.Installation{
Status: v1beta1.InstallationStatus{State: v1beta1.InstallationStateKubernetesInstalled},
Spec: v1beta1.InstallationSpec{
Config: &v1beta1.ConfigSpec{
Version: "goodver",
Extensions: v1beta1.Extensions{
Helm: &k0sv1beta1.HelmExtensions{
Charts: []k0sv1beta1.Chart{
{
Name: "extchart",
Version: "2",
},
},
},
},
},
},
},
out: v1beta1.InstallationStatus{
State: v1beta1.InstallationStateInstalled,
Reason: "Addons upgraded",
},
releaseMeta: release.Meta{
Configs: &k0sv1beta1.HelmExtensions{
Charts: []k0sv1beta1.Chart{
{
Name: "metachart",
Version: "1",
Values: `
abc: xyz
password: overridden`,
},
},
},
Protected: map[string][]string{
"metachart": {"password"},
},
},
fields: fields{
State: []runtime.Object{
&k0shelmv1beta1.Chart{
ObjectMeta: metav1.ObjectMeta{
Name: "metachart",
},
Spec: k0shelmv1beta1.ChartSpec{
ReleaseName: "metachart",
Values: `abc: xyz
password: original`,
},
Status: k0shelmv1beta1.ChartStatus{Version: "1"},
},
&k0shelmv1beta1.Chart{
ObjectMeta: metav1.ObjectMeta{
Name: "extchart",
},
Spec: k0shelmv1beta1.ChartSpec{ReleaseName: "extchart"},
Status: k0shelmv1beta1.ChartStatus{Version: "2"},
},
&k0sv1beta1.ClusterConfig{
ObjectMeta: metav1.ObjectMeta{
Name: "k0s",
Namespace: "kube-system",
},
Spec: &k0sv1beta1.ClusterSpec{
Extensions: &k0sv1beta1.ClusterExtensions{
Helm: &k0sv1beta1.HelmExtensions{
Charts: []k0sv1beta1.Chart{
{
Name: "metachart",
Version: "1",
Values: `
abc: xyz
password: original`,
},
{
Name: "extchart",
Version: "2",
},
},
},
},
},
},
},
},
},
{
name: "k8s install completed, good version, overridden values, both types of charts, values drift",
in: v1beta1.Installation{
Status: v1beta1.InstallationStatus{State: v1beta1.InstallationStateKubernetesInstalled},
Spec: v1beta1.InstallationSpec{
Config: &v1beta1.ConfigSpec{
Version: "goodver",
Extensions: v1beta1.Extensions{
Helm: &k0sv1beta1.HelmExtensions{
Charts: []k0sv1beta1.Chart{
{
Name: "extchart",
Version: "2",
},
},
},
},
},
},
},
out: v1beta1.InstallationStatus{
State: v1beta1.InstallationStateAddonsInstalling,
Reason: "Installing addons",
},
releaseMeta: release.Meta{
Configs: &k0sv1beta1.HelmExtensions{
Charts: []k0sv1beta1.Chart{
{
Name: "metachart",
Version: "1",
Values: `
abc: xyz
password: overridden`,
},
},
},
Protected: map[string][]string{
"metachart": {"password"},
},
},
fields: fields{
State: []runtime.Object{
&k0shelmv1beta1.Chart{
ObjectMeta: metav1.ObjectMeta{
Name: "metachart",
},
Spec: k0shelmv1beta1.ChartSpec{
ReleaseName: "metachart",
Values: `abc: original
password: original`,
},
Status: k0shelmv1beta1.ChartStatus{Version: "1"},
},
&k0shelmv1beta1.Chart{
ObjectMeta: metav1.ObjectMeta{
Name: "extchart",
},
Spec: k0shelmv1beta1.ChartSpec{ReleaseName: "extchart"},
Status: k0shelmv1beta1.ChartStatus{Version: "2"},
},
&k0sv1beta1.ClusterConfig{
ObjectMeta: metav1.ObjectMeta{
Name: "k0s",
Namespace: "kube-system",
},
Spec: &k0sv1beta1.ClusterSpec{
Extensions: &k0sv1beta1.ClusterExtensions{
Helm: &k0sv1beta1.HelmExtensions{
Charts: []k0sv1beta1.Chart{
{
Name: "metachart",
Version: "1",
Values: `
abc: original
password: original`,
},
{
Name: "extchart",
Version: "2",
},
},
},
},
},
},
},
},
},
}
for _, tt := range tests {

Expand Down

0 comments on commit f7bf508

Please sign in to comment.