Add support for multiple values files to create hr

Signed-off-by: Stefan Prodan <stefan.prodan@gmail.com>
pull/866/head
Stefan Prodan 4 years ago
parent 5416c19b2e
commit 3dd574ee51
No known key found for this signature in database
GPG Key ID: 3299AEB0E4085BAF

@ -18,6 +18,7 @@ package main
import ( import (
"context" "context"
"encoding/json"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
@ -62,11 +63,12 @@ var createHelmReleaseCmd = &cobra.Command{
--source=Bucket/podinfo \ --source=Bucket/podinfo \
--chart=./charts/podinfo --chart=./charts/podinfo
# Create a HelmRelease with values from a local YAML file # Create a HelmRelease with values from local YAML files
flux create hr podinfo \ flux create hr podinfo \
--source=HelmRepository/podinfo \ --source=HelmRepository/podinfo \
--chart=podinfo \ --chart=podinfo \
--values=./my-values.yaml --values=./my-values1.yaml \
--values=./my-values2.yaml
# Create a HelmRelease with values from a Kubernetes secret # Create a HelmRelease with values from a Kubernetes secret
kubectl -n app create secret generic my-secret-values \ kubectl -n app create secret generic my-secret-values \
@ -105,7 +107,7 @@ type helmReleaseFlags struct {
chart string chart string
chartVersion string chartVersion string
targetNamespace string targetNamespace string
valuesFile string valuesFile []string
valuesFrom flags.HelmReleaseValuesFrom valuesFrom flags.HelmReleaseValuesFrom
saName string saName string
} }
@ -120,7 +122,7 @@ func init() {
createHelmReleaseCmd.Flags().StringArrayVar(&helmReleaseArgs.dependsOn, "depends-on", nil, "HelmReleases that must be ready before this release can be installed, supported formats '<name>' and '<namespace>/<name>'") createHelmReleaseCmd.Flags().StringArrayVar(&helmReleaseArgs.dependsOn, "depends-on", nil, "HelmReleases that must be ready before this release can be installed, supported formats '<name>' and '<namespace>/<name>'")
createHelmReleaseCmd.Flags().StringVar(&helmReleaseArgs.targetNamespace, "target-namespace", "", "namespace to install this release, defaults to the HelmRelease namespace") createHelmReleaseCmd.Flags().StringVar(&helmReleaseArgs.targetNamespace, "target-namespace", "", "namespace to install this release, defaults to the HelmRelease namespace")
createHelmReleaseCmd.Flags().StringVar(&helmReleaseArgs.saName, "service-account", "", "the name of the service account to impersonate when reconciling this HelmRelease") createHelmReleaseCmd.Flags().StringVar(&helmReleaseArgs.saName, "service-account", "", "the name of the service account to impersonate when reconciling this HelmRelease")
createHelmReleaseCmd.Flags().StringVar(&helmReleaseArgs.valuesFile, "values", "", "local path to the values.yaml file") createHelmReleaseCmd.Flags().StringArrayVar(&helmReleaseArgs.valuesFile, "values", nil, "local path to values.yaml files")
createHelmReleaseCmd.Flags().Var(&helmReleaseArgs.valuesFrom, "values-from", helmReleaseArgs.valuesFrom.Description()) createHelmReleaseCmd.Flags().Var(&helmReleaseArgs.valuesFrom, "values-from", helmReleaseArgs.valuesFrom.Description())
createCmd.AddCommand(createHelmReleaseCmd) createCmd.AddCommand(createHelmReleaseCmd)
} }
@ -175,18 +177,37 @@ func createHelmReleaseCmdRun(cmd *cobra.Command, args []string) error {
helmRelease.Spec.ServiceAccountName = helmReleaseArgs.saName helmRelease.Spec.ServiceAccountName = helmReleaseArgs.saName
} }
if helmReleaseArgs.valuesFile != "" { if len(helmReleaseArgs.valuesFile) > 0 {
data, err := ioutil.ReadFile(helmReleaseArgs.valuesFile) var valuesMap map[string]interface{}
for _, v := range helmReleaseArgs.valuesFile {
data, err := ioutil.ReadFile(v)
if err != nil { if err != nil {
return fmt.Errorf("reading values from %s failed: %w", helmReleaseArgs.valuesFile, err) return fmt.Errorf("reading values from %s failed: %w", v, err)
} }
json, err := yaml.YAMLToJSON(data) jsonBytes, err := yaml.YAMLToJSON(data)
if err != nil { if err != nil {
return fmt.Errorf("converting values to JSON from %s failed: %w", helmReleaseArgs.valuesFile, err) return fmt.Errorf("converting values to JSON from %s failed: %w", v, err)
} }
helmRelease.Spec.Values = &apiextensionsv1.JSON{Raw: json} jsonMap := make(map[string]interface{})
if err := json.Unmarshal(jsonBytes, &jsonMap); err != nil {
return fmt.Errorf("unmarshaling values from %s failed: %w", v, err)
}
if valuesMap == nil {
valuesMap = jsonMap
} else {
valuesMap = utils.MergeMaps(valuesMap, jsonMap)
}
}
jsonRaw, err := json.Marshal(valuesMap)
if err != nil {
return fmt.Errorf("marshaling values failed: %w", err)
}
helmRelease.Spec.Values = &apiextensionsv1.JSON{Raw: jsonRaw}
} }
if helmReleaseArgs.valuesFrom.String() != "" { if helmReleaseArgs.valuesFrom.String() != "" {

@ -32,11 +32,12 @@ flux create helmrelease [name] [flags]
--source=Bucket/podinfo \ --source=Bucket/podinfo \
--chart=./charts/podinfo --chart=./charts/podinfo
# Create a HelmRelease with values from a local YAML file # Create a HelmRelease with values from local YAML files
flux create hr podinfo \ flux create hr podinfo \
--source=HelmRepository/podinfo \ --source=HelmRepository/podinfo \
--chart=podinfo \ --chart=podinfo \
--values=./my-values.yaml --values=./my-values1.yaml \
--values=./my-values2.yaml
# Create a HelmRelease with values from a Kubernetes secret # Create a HelmRelease with values from a Kubernetes secret
kubectl -n app create secret generic my-secret-values \ kubectl -n app create secret generic my-secret-values \
@ -78,7 +79,7 @@ flux create helmrelease [name] [flags]
--service-account string the name of the service account to impersonate when reconciling this HelmRelease --service-account string the name of the service account to impersonate when reconciling this HelmRelease
--source helmChartSource source that contains the chart in the format '<kind>/<name>', where kind must be one of: (HelmRepository, GitRepository, Bucket) --source helmChartSource source that contains the chart in the format '<kind>/<name>', where kind must be one of: (HelmRepository, GitRepository, Bucket)
--target-namespace string namespace to install this release, defaults to the HelmRelease namespace --target-namespace string namespace to install this release, defaults to the HelmRelease namespace
--values string local path to the values.yaml file --values stringArray local path to values.yaml files
--values-from helmReleaseValuesFrom Kubernetes object reference that contains the values.yaml data key in the format '<kind>/<name>', where kind must be one of: (Secret, ConfigMap) --values-from helmReleaseValuesFrom Kubernetes object reference that contains the values.yaml data key in the format '<kind>/<name>', where kind must be one of: (Secret, ConfigMap)
``` ```

@ -393,3 +393,24 @@ func ValidateComponents(components []string) error {
return nil return nil
} }
// TODO(stefan): move this to fluxcd/pkg
// taken from: https://github.com/fluxcd/helm-controller/blob/main/internal/util/util.go
func MergeMaps(a, b map[string]interface{}) map[string]interface{} {
out := make(map[string]interface{}, len(a))
for k, v := range a {
out[k] = v
}
for k, v := range b {
if v, ok := v.(map[string]interface{}); ok {
if bv, ok := out[k]; ok {
if bv, ok := bv.(map[string]interface{}); ok {
out[k] = MergeMaps(bv, v)
continue
}
}
}
out[k] = v
}
return out
}

Loading…
Cancel
Save