Skip to content

Commit

Permalink
use ValuesBlock for cilium appInstall as well
Browse files Browse the repository at this point in the history
Signed-off-by: Simon Bein <simontheleg@gmail.com>
  • Loading branch information
SimonTheLeg committed Jun 19, 2024
1 parent 5977f09 commit b7f3397
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 14 deletions.
17 changes: 17 additions & 0 deletions pkg/apis/apps.kubermatic/v1/application_definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ limitations under the License.
package v1

import (
"encoding/json"
"fmt"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/yaml"
)

const (
Expand Down Expand Up @@ -261,3 +263,18 @@ func (ad *ApplicationDefinitionSpec) GetDefaultValues() ([]byte, error) {
}
return nil, nil
}

// GetParsedDefaultValues parses the values either from the DefaultValues or DefaultValuesBlock field.
// Will return an error if both fields are set.
func (ai *ApplicationDefinitionSpec) GetParsedDefaultValues() (map[string]interface{}, error) {
values := make(map[string]interface{})
if len(ai.DefaultValues.Raw) > 0 && string(ai.DefaultValues.Raw) != "{}" && ai.DefaultValuesBlock != "" {
return nil, fmt.Errorf("the fields DefaultValues and DefaultValuesBlock cannot be used simultaneously. Please delete one of them.")
}
if len(ai.DefaultValues.Raw) > 0 && string(ai.DefaultValues.Raw) != "{}" {
err := json.Unmarshal(ai.DefaultValues.Raw, &values)
return values, err
}
err := yaml.Unmarshal([]byte(ai.DefaultValuesBlock), &values)
return values, err
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ import (
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/record"
"sigs.k8s.io/controller-runtime/pkg/builder"
Expand All @@ -49,6 +48,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/yaml"
)

const (
Expand Down Expand Up @@ -292,12 +292,10 @@ func (r *Reconciler) parseAppDefDefaultValues(ctx context.Context, cluster *kube
if err := r.Client.Get(ctx, types.NamespacedName{Name: cluster.Spec.CNIPlugin.Type.String()}, appDef); err != nil {
return ctrlruntimeclient.IgnoreNotFound(err)
}
if appDef.Spec.DefaultValues != nil {
if len(appDef.Spec.DefaultValues.Raw) > 0 {
if err := json.Unmarshal(appDef.Spec.DefaultValues.Raw, &values); err != nil {
return fmt.Errorf("failed to unmarshal ApplicationDefinition default values: %w", err)
}
}

values, err := appDef.Spec.GetParsedDefaultValues()
if err != nil {
return fmt.Errorf("failed to unmarshal ApplicationDefinition default values: %w", err)
}
return nil
}
Expand Down Expand Up @@ -344,11 +342,9 @@ func ApplicationInstallationReconciler(cluster *kubermaticv1.Cluster, overwriteR
}

// Unmarshal existing values
values := make(map[string]any)
if len(app.Spec.Values.Raw) > 0 {
if err := json.Unmarshal(app.Spec.Values.Raw, &values); err != nil {
return app, fmt.Errorf("failed to unmarshal CNI values: %w", err)
}
values, err := app.Spec.GetParsedValues()
if err != nil {
return app, fmt.Errorf("failed to unmarshal CNI values: %w", err)
}

// If (and only if) existing values is empty, use the initial values
Expand All @@ -370,11 +366,11 @@ func ApplicationInstallationReconciler(cluster *kubermaticv1.Cluster, overwriteR
}

// Set new values
rawValues, err := json.Marshal(values)
rawValues, err := yaml.Marshal(values)
if err != nil {
return app, fmt.Errorf("failed to marshall CNI values: %w", err)
}
app.Spec.Values = runtime.RawExtension{Raw: rawValues}
app.Spec.ValuesBlock = string(rawValues)

return app, nil
}
Expand Down

0 comments on commit b7f3397

Please sign in to comment.