diff --git a/cmd/diff.go b/cmd/diff.go index 020bc6f1..c2238704 100644 --- a/cmd/diff.go +++ b/cmd/diff.go @@ -52,6 +52,7 @@ func NewDiffCmd(globalCfg *config.GlobalImpl) *cobra.Command { f.StringArrayVar(&diffOptions.Suppress, "suppress", nil, "suppress specified Kubernetes objects in the output. Can be provided multiple times. For example: --suppress KeycloakClient --suppress VaultSecret") f.BoolVar(&diffOptions.ReuseValues, "reuse-values", false, `Override helmDefaults.reuseValues "helm diff upgrade --install --reuse-values"`) f.BoolVar(&diffOptions.ResetValues, "reset-values", false, `Override helmDefaults.reuseValues "helm diff upgrade --install --reset-values"`) + f.BoolVar(&diffOptions.TakeOwnership, "take-ownership", false, "add --take-ownership flag to helm") f.StringVar(&diffOptions.PostRenderer, "post-renderer", "", `pass --post-renderer to "helm template" or "helm upgrade --install"`) f.StringArrayVar(&diffOptions.PostRendererArgs, "post-renderer-args", nil, `pass --post-renderer-args to "helm template" or "helm upgrade --install"`) f.StringArrayVar(&diffOptions.SuppressOutputLineRegex, "suppress-output-line-regex", nil, "a list of regex patterns to suppress output lines from the diff output") diff --git a/go.mod b/go.mod index ad888f36..93bae27f 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/helmfile/helmfile -go 1.24 +go 1.24.2 require ( dario.cat/mergo v1.0.1 @@ -30,7 +30,7 @@ require ( golang.org/x/term v0.31.0 gopkg.in/yaml.v2 v2.4.0 helm.sh/helm/v3 v3.17.3 - k8s.io/apimachinery v0.32.4 + k8s.io/apimachinery v0.33.0 ) require ( @@ -99,11 +99,11 @@ require ( golang.org/x/oauth2 v0.28.0 // indirect golang.org/x/sys v0.32.0 // indirect golang.org/x/text v0.23.0 // indirect - golang.org/x/time v0.8.0 // indirect + golang.org/x/time v0.9.0 // indirect google.golang.org/api v0.215.0 // indirect google.golang.org/genproto v0.0.0-20241118233622-e639e219e697 // indirect google.golang.org/grpc v1.68.0 // indirect - google.golang.org/protobuf v1.36.1 // indirect + google.golang.org/protobuf v1.36.5 // indirect gopkg.in/ini.v1 v1.67.0 // indirect sigs.k8s.io/json v0.0.0-20241010143419-9aa6b5e7a4b3 // indirect sigs.k8s.io/yaml v1.4.0 // indirect @@ -222,9 +222,8 @@ require ( github.com/gogo/protobuf v1.3.2 // indirect github.com/golang-jwt/jwt/v5 v5.2.2 // indirect github.com/google/btree v1.0.1 // indirect - github.com/google/gnostic-models v0.6.8 // indirect + github.com/google/gnostic-models v0.6.9 // indirect github.com/google/go-jsonnet v0.20.0 // indirect - github.com/google/gofuzz v1.2.0 // indirect github.com/google/s2a-go v0.1.8 // indirect github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect github.com/googleapis/enterprise-certificate-proxy v0.3.4 // indirect @@ -316,11 +315,12 @@ require ( k8s.io/client-go v0.32.3 // indirect k8s.io/component-base v0.32.2 // indirect k8s.io/klog/v2 v2.130.1 // indirect - k8s.io/kube-openapi v0.0.0-20241105132330-32ad38e42d3f // indirect + k8s.io/kube-openapi v0.0.0-20250318190949-c8a335a9a2ff // indirect k8s.io/kubectl v0.32.2 // indirect k8s.io/utils v0.0.0-20241104100929-3ea5e8cea738 // indirect oras.land/oras-go v1.2.5 // indirect sigs.k8s.io/kustomize/api v0.18.0 // indirect sigs.k8s.io/kustomize/kyaml v0.18.1 // indirect - sigs.k8s.io/structured-merge-diff/v4 v4.4.2 // indirect + sigs.k8s.io/randfill v1.0.0 // indirect + sigs.k8s.io/structured-merge-diff/v4 v4.6.0 // indirect ) diff --git a/go.sum b/go.sum index 3819c587..8bbdcf0f 100644 --- a/go.sum +++ b/go.sum @@ -1080,8 +1080,8 @@ github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ github.com/google/btree v1.0.1 h1:gK4Kx5IaGY9CD5sPJ36FHiBJ6ZXl0kilRiiCj+jdYp4= github.com/google/btree v1.0.1/go.mod h1:xXMiIv4Fb/0kKde4SpL7qlzvu5cMJDRkFDxJfI9uaxA= github.com/google/flatbuffers v2.0.8+incompatible/go.mod h1:1AeVuKshWv4vARoZatz6mlQ0JxURH0Kv5+zNeJKJCa8= -github.com/google/gnostic-models v0.6.8 h1:yo/ABAfM5IMRsS1VnXjTBvUb61tFIHozhlYvRgGre9I= -github.com/google/gnostic-models v0.6.8/go.mod h1:5n7qKqH0f5wFt+aWF8CW6pZLLNOfYuF5OpfBSENuI8U= +github.com/google/gnostic-models v0.6.9 h1:MU/8wDLif2qCXZmzncUQ/BOfxWfthHi63KqpoNbWqVw= +github.com/google/gnostic-models v0.6.9/go.mod h1:CiWsm0s6BSQd1hRn8/QmxqB6BesYcbSZxsz9b0KuDBw= github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= @@ -1444,8 +1444,8 @@ github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6L github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc= github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= -github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8= -github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4= +github.com/rogpeppe/go-internal v1.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR38lUII= +github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWNMNyH2VO9fmH0o= github.com/rubenv/sql-migrate v1.7.1 h1:f/o0WgfO/GqNuVg+6801K/KW3WdDSupzSjDYODmiUq4= github.com/rubenv/sql-migrate v1.7.1/go.mod h1:Ob2Psprc0/3ggbM6wCzyYVFFuc6FyZrb2AS+ezLDFb4= github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= @@ -1932,8 +1932,8 @@ golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxb golang.org/x/time v0.0.0-20220922220347-f3bd1da661af/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.1.0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.3.0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= -golang.org/x/time v0.8.0 h1:9i3RxcPv3PZnitoVGMPDKZSq1xW1gK1Xy3ArNOGZfEg= -golang.org/x/time v0.8.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM= +golang.org/x/time v0.9.0 h1:EsRrnYcQiGH+5FfbgvV4AP7qEZstoyrHB0DzarOQ4ZY= +golang.org/x/time v0.9.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM= golang.org/x/tools v0.0.0-20180525024113-a5b4c53f6e8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= @@ -2285,8 +2285,8 @@ google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqw google.golang.org/protobuf v1.29.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= google.golang.org/protobuf v1.30.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= -google.golang.org/protobuf v1.36.1 h1:yBPeRvTftaleIgM3PZ/WBIZ7XM/eEYAaEyCwvyjq/gk= -google.golang.org/protobuf v1.36.1/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE= +google.golang.org/protobuf v1.36.5 h1:tPhr+woSbjfYvY6/GPufUoYizxw1cF/yFoxJ2fmpwlM= +google.golang.org/protobuf v1.36.5/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE= gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= @@ -2328,8 +2328,8 @@ k8s.io/api v0.32.3 h1:Hw7KqxRusq+6QSplE3NYG4MBxZw1BZnq4aP4cJVINls= k8s.io/api v0.32.3/go.mod h1:2wEDTXADtm/HA7CCMD8D8bK4yuBUptzaRhYcYEEYA3k= k8s.io/apiextensions-apiserver v0.32.2 h1:2YMk285jWMk2188V2AERy5yDwBYrjgWYggscghPCvV4= k8s.io/apiextensions-apiserver v0.32.2/go.mod h1:GPwf8sph7YlJT3H6aKUWtd0E+oyShk/YHWQHf/OOgCA= -k8s.io/apimachinery v0.32.4 h1:8EEksaxA7nd7xWJkkwLDN4SvWS5ot9g6Z/VZb3ju25I= -k8s.io/apimachinery v0.32.4/go.mod h1:GpHVgxoKlTxClKcteaeuF1Ul/lDVb74KpZcxcmLDElE= +k8s.io/apimachinery v0.33.0 h1:1a6kHrJxb2hs4t8EE5wuR/WxKDwGN1FKH3JvDtA0CIQ= +k8s.io/apimachinery v0.33.0/go.mod h1:BHW0YOu7n22fFv/JkYOEfkUYNRN0fj0BlvMFWA7b+SM= k8s.io/cli-runtime v0.32.2 h1:aKQR4foh9qeyckKRkNXUccP9moxzffyndZAvr+IXMks= k8s.io/cli-runtime v0.32.2/go.mod h1:a/JpeMztz3xDa7GCyyShcwe55p8pbcCVQxvqZnIwXN8= k8s.io/client-go v0.32.3 h1:RKPVltzopkSgHS7aS98QdscAgtgah/+zmpAogooIqVU= @@ -2338,8 +2338,8 @@ k8s.io/component-base v0.32.2 h1:1aUL5Vdmu7qNo4ZsE+569PV5zFatM9hl+lb3dEea2zU= k8s.io/component-base v0.32.2/go.mod h1:PXJ61Vx9Lg+P5mS8TLd7bCIr+eMJRQTyXe8KvkrvJq0= k8s.io/klog/v2 v2.130.1 h1:n9Xl7H1Xvksem4KFG4PYbdQCQxqc/tTUyrgXaOhHSzk= k8s.io/klog/v2 v2.130.1/go.mod h1:3Jpz1GvMt720eyJH1ckRHK1EDfpxISzJ7I9OYgaDtPE= -k8s.io/kube-openapi v0.0.0-20241105132330-32ad38e42d3f h1:GA7//TjRY9yWGy1poLzYYJJ4JRdzg3+O6e8I+e+8T5Y= -k8s.io/kube-openapi v0.0.0-20241105132330-32ad38e42d3f/go.mod h1:R/HEjbvWI0qdfb8viZUeVZm0X6IZnxAydC7YU42CMw4= +k8s.io/kube-openapi v0.0.0-20250318190949-c8a335a9a2ff h1:/usPimJzUKKu+m+TE36gUyGcf03XZEP0ZIKgKj35LS4= +k8s.io/kube-openapi v0.0.0-20250318190949-c8a335a9a2ff/go.mod h1:5jIi+8yX4RIb8wk3XwBo5Pq2ccx4FP10ohkbSKCZoK8= k8s.io/kubectl v0.32.2 h1:TAkag6+XfSBgkqK9I7ZvwtF0WVtUAvK8ZqTt+5zi1Us= k8s.io/kubectl v0.32.2/go.mod h1:+h/NQFSPxiDZYX/WZaWw9fwYezGLISP0ud8nQKg+3g8= k8s.io/utils v0.0.0-20241104100929-3ea5e8cea738 h1:M3sRQVHv7vB20Xc2ybTt7ODCeFj6JSWYFzOFnYeS6Ro= @@ -2390,7 +2390,10 @@ sigs.k8s.io/kustomize/api v0.18.0 h1:hTzp67k+3NEVInwz5BHyzc9rGxIauoXferXyjv5lWPo sigs.k8s.io/kustomize/api v0.18.0/go.mod h1:f8isXnX+8b+SGLHQ6yO4JG1rdkZlvhaCf/uZbLVMb0U= sigs.k8s.io/kustomize/kyaml v0.18.1 h1:WvBo56Wzw3fjS+7vBjN6TeivvpbW9GmRaWZ9CIVmt4E= sigs.k8s.io/kustomize/kyaml v0.18.1/go.mod h1:C3L2BFVU1jgcddNBE1TxuVLgS46TjObMwW5FT9FcjYo= -sigs.k8s.io/structured-merge-diff/v4 v4.4.2 h1:MdmvkGuXi/8io6ixD5wud3vOLwc1rj0aNqRlpuvjmwA= -sigs.k8s.io/structured-merge-diff/v4 v4.4.2/go.mod h1:N8f93tFZh9U6vpxwRArLiikrE5/2tiu1w1AGfACIGE4= +sigs.k8s.io/randfill v0.0.0-20250304075658-069ef1bbf016/go.mod h1:XeLlZ/jmk4i1HRopwe7/aU3H5n1zNUcX6TM94b3QxOY= +sigs.k8s.io/randfill v1.0.0 h1:JfjMILfT8A6RbawdsK2JXGBR5AQVfd+9TbzrlneTyrU= +sigs.k8s.io/randfill v1.0.0/go.mod h1:XeLlZ/jmk4i1HRopwe7/aU3H5n1zNUcX6TM94b3QxOY= +sigs.k8s.io/structured-merge-diff/v4 v4.6.0 h1:IUA9nvMmnKWcj5jl84xn+T5MnlZKThmUW1TdblaLVAc= +sigs.k8s.io/structured-merge-diff/v4 v4.6.0/go.mod h1:dDy58f92j70zLsuZVuUX5Wp9vtxXpaZnkPGWeqDfCps= sigs.k8s.io/yaml v1.4.0 h1:Mk1wCc2gy/F0THH0TAp1QYyJNzRm2KCLy3o5ASXVI5E= sigs.k8s.io/yaml v1.4.0/go.mod h1:Ejl7/uTz7PSA4eKMyQCUTnhZYNmLIl+5c2lQPGR2BPY= diff --git a/pkg/app/app.go b/pkg/app/app.go index 614c9bf7..cf624bf8 100644 --- a/pkg/app/app.go +++ b/pkg/app/app.go @@ -1390,6 +1390,7 @@ func (a *App) apply(r *Run, c ApplyConfigProvider) (bool, bool, []error) { PostRendererArgs: c.PostRendererArgs(), SkipSchemaValidation: c.SkipSchemaValidation(), SuppressOutputLineRegex: c.SuppressOutputLineRegex(), + TakeOwnership: c.TakeOwnership(), } infoMsg, releasesToBeUpdated, releasesToBeReinstalled, releasesToBeDeleted, errs := r.diff(false, detailedExitCode, c, diffOpts) @@ -1643,6 +1644,7 @@ func (a *App) diff(r *Run, c DiffConfigProvider) (*string, bool, bool, []error) PostRendererArgs: c.PostRendererArgs(), SkipSchemaValidation: c.SkipSchemaValidation(), SuppressOutputLineRegex: c.SuppressOutputLineRegex(), + TakeOwnership: c.TakeOwnership(), } filtered := &Run{ diff --git a/pkg/app/config.go b/pkg/app/config.go index 34dff76d..e699d86f 100644 --- a/pkg/app/config.go +++ b/pkg/app/config.go @@ -157,6 +157,7 @@ type DiffConfigProvider interface { NoColor() bool Context() int DiffOutput() string + TakeOwnership() bool concurrencyConfig valuesControlMode diff --git a/pkg/app/diff_test.go b/pkg/app/diff_test.go index 2e60b3df..18dde7fc 100644 --- a/pkg/app/diff_test.go +++ b/pkg/app/diff_test.go @@ -46,6 +46,7 @@ type diffConfig struct { skipSchemaValidation bool reuseValues bool logger *zap.SugaredLogger + takeOwnership bool } func (a diffConfig) Args() string { @@ -183,6 +184,9 @@ func (a diffConfig) SkipSchemaValidation() bool { func (a diffConfig) SuppressOutputLineRegex() []string { return a.suppressOutputLineRegex } +func (a diffConfig) TakeOwnership() bool { + return a.takeOwnership +} func TestDiff(t *testing.T) { type flags struct { diff --git a/pkg/config/apply.go b/pkg/config/apply.go index 7cfc6d3c..81a9a195 100644 --- a/pkg/config/apply.go +++ b/pkg/config/apply.go @@ -269,6 +269,7 @@ func (a *ApplyImpl) TakeOwnership() bool { return a.ApplyOptions.TakeOwnership } +// SyncReleaseLabels returns the SyncReleaseLabels. func (a *ApplyImpl) SyncReleaseLabels() bool { return a.ApplyOptions.SyncReleaseLabels } diff --git a/pkg/config/diff.go b/pkg/config/diff.go index ac8018b9..d8830fad 100644 --- a/pkg/config/diff.go +++ b/pkg/config/diff.go @@ -49,6 +49,8 @@ type DiffOptions struct { // SuppressOutputLineRegex is a list of regexes to suppress output lines SuppressOutputLineRegex []string SkipSchemaValidation bool + // TakeOwnership is true if the ownership should be taken + TakeOwnership bool } // NewDiffOptions creates a new Apply @@ -201,6 +203,12 @@ func (t *DiffImpl) SuppressOutputLineRegex() []string { return t.DiffOptions.SuppressOutputLineRegex } +// SkipSchemaValidation returns the SkipSchemaValidation. func (t *DiffImpl) SkipSchemaValidation() bool { return t.DiffOptions.SkipSchemaValidation } + +// TakeOwnership returns the TakeOwnership. +func (t *DiffImpl) TakeOwnership() bool { + return t.DiffOptions.TakeOwnership +} diff --git a/pkg/maputil/maputil.go b/pkg/maputil/maputil.go index bf0c5c7a..e5cf925b 100644 --- a/pkg/maputil/maputil.go +++ b/pkg/maputil/maputil.go @@ -19,7 +19,7 @@ func CastKeysToStrings(s any) (map[string]any, error) { return nil, fmt.Errorf("unexpected type of key in map: expected string, got %T: value=%v, map=%v", typedK, typedK, src) } - castedV, err := recursivelyStringifyMapKey(v) + castedV, err := RecursivelyStringifyMapKey(v) if err != nil { return nil, err } @@ -28,7 +28,7 @@ func CastKeysToStrings(s any) (map[string]any, error) { } case map[string]any: for k, v := range src { - castedV, err := recursivelyStringifyMapKey(v) + castedV, err := RecursivelyStringifyMapKey(v) if err != nil { return nil, err } @@ -39,7 +39,7 @@ func CastKeysToStrings(s any) (map[string]any, error) { return new, nil } -func recursivelyStringifyMapKey(v any) (any, error) { +func RecursivelyStringifyMapKey(v any) (any, error) { var castedV any switch typedV := v.(type) { case map[any]any, map[string]any: @@ -51,7 +51,7 @@ func recursivelyStringifyMapKey(v any) (any, error) { case []any: a := []any{} for i := range typedV { - res, err := recursivelyStringifyMapKey(typedV[i]) + res, err := RecursivelyStringifyMapKey(typedV[i]) if err != nil { return nil, err } diff --git a/pkg/state/helmx.go b/pkg/state/helmx.go index 00d29cac..1a46b1d5 100644 --- a/pkg/state/helmx.go +++ b/pkg/state/helmx.go @@ -54,7 +54,22 @@ func formatLabels(labels map[string]string) string { // append labels flags to helm flags, starting from helm v3.13.0 func (st *HelmState) appendLabelsFlags(flags []string, helm helmexec.Interface, release *ReleaseSpec, syncReleaseLabels bool) []string { - if helm.IsVersionAtLeast("3.13.0") && (syncReleaseLabels || release.SyncReleaseLabels) { + if !helm.IsVersionAtLeast("3.13.0") { + return flags + } + isSyncReleaseLabels := false + switch { + // Check if SyncReleaseLabels is true in the release spec. + case release.SyncReleaseLabels != nil && *release.SyncReleaseLabels: + isSyncReleaseLabels = true + // Check if syncReleaseLabels argument is true. + case syncReleaseLabels: + isSyncReleaseLabels = true + // Check if SyncReleaseLabels is true in HelmDefaults. + case st.HelmDefaults.SyncReleaseLabels != nil && *st.HelmDefaults.SyncReleaseLabels: + isSyncReleaseLabels = true + } + if isSyncReleaseLabels { labels := formatLabels(release.Labels) if labels != "" { flags = append(flags, "--labels", labels) @@ -203,13 +218,17 @@ func (st *HelmState) appendHideNotesFlags(flags []string, helm helmexec.Interfac } // append take-ownership flags to helm flags -func (st *HelmState) appendTakeOwnershipFlags(flags []string, helm helmexec.Interface, ops *SyncOpts) []string { +func (st *HelmState) appendTakeOwnershipFlagsForUpgrade(flags []string, helm helmexec.Interface, release *ReleaseSpec, takeOwnership bool) []string { // see https://github.com/helm/helm/releases/tag/v3.17.0 if !helm.IsVersionAtLeast("3.17.0") { return flags } switch { - case ops.TakeOwnership: + case release.TakeOwnership != nil && *release.TakeOwnership: + flags = append(flags, "--take-ownership") + case takeOwnership: + flags = append(flags, "--take-ownership") + case st.HelmDefaults.TakeOwnership != nil && *st.HelmDefaults.TakeOwnership: flags = append(flags, "--take-ownership") } return flags diff --git a/pkg/state/helmx_test.go b/pkg/state/helmx_test.go index 76f4300d..90bfb83e 100644 --- a/pkg/state/helmx_test.go +++ b/pkg/state/helmx_test.go @@ -387,12 +387,13 @@ func TestAppendHideNotesFlags(t *testing.T) { } } -func TestAppendTakeOwnershipFlags(t *testing.T) { +func TestAppendTakeOwnershipFlagsForUpgrade(t *testing.T) { type args struct { flags []string helm helmexec.Interface helmSpec HelmSpec opt *SyncOpts + release *ReleaseSpec expected []string } tests := []struct { @@ -402,8 +403,9 @@ func TestAppendTakeOwnershipFlags(t *testing.T) { { name: "no take-ownership when helm less than 3.17.0", args: args{ - flags: []string{}, - helm: testutil.NewVersionHelmExec("3.16.0"), + flags: []string{}, + release: &ReleaseSpec{}, + helm: testutil.NewVersionHelmExec("3.16.0"), opt: &SyncOpts{ TakeOwnership: true, }, @@ -413,21 +415,34 @@ func TestAppendTakeOwnershipFlags(t *testing.T) { { name: "take-ownership from cmd flag", args: args{ - flags: []string{}, - helm: testutil.NewVersionHelmExec("3.17.0"), + flags: []string{}, + helm: testutil.NewVersionHelmExec("3.17.0"), + release: &ReleaseSpec{}, opt: &SyncOpts{ TakeOwnership: true, }, expected: []string{"--take-ownership"}, }, }, + { + name: "take-ownership from release", + args: args{ + flags: []string{}, + helm: testutil.NewVersionHelmExec("3.17.0"), + release: &ReleaseSpec{ + TakeOwnership: &[]bool{true}[0], + }, + opt: &SyncOpts{}, + expected: []string{"--take-ownership"}, + }, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { st := &HelmState{} st.HelmDefaults = tt.args.helmSpec - got := st.appendTakeOwnershipFlags(tt.args.flags, tt.args.helm, tt.args.opt) - require.Equalf(t, tt.args.expected, got, "appendTakeOwnershipFlags() = %v, want %v", got, tt.args.expected) + got := st.appendTakeOwnershipFlagsForUpgrade(tt.args.flags, tt.args.helm, tt.args.release, tt.args.opt.TakeOwnership) + require.Equalf(t, tt.args.expected, got, "appendTakeOwnershipFlagsForUpgrade() = %v, want %v", got, tt.args.expected) }) } } diff --git a/pkg/state/state.go b/pkg/state/state.go index 8216b806..9b121851 100644 --- a/pkg/state/state.go +++ b/pkg/state/state.go @@ -204,11 +204,13 @@ type HelmSpec struct { // PlainHttp is true if the remote charte should be fetched using HTTP and not HTTPS PlainHttp bool `yaml:"plainHttp,omitempty"` // Wait, if set to true, will wait until all resources are deleted before mark delete command as successful - DeleteWait bool `yaml:"deleteWait"` + DeleteWait bool `yaml:"deleteWait,omitempty"` // Timeout is the time in seconds to wait for helmfile delete command (default 300) - DeleteTimeout int `yaml:"deleteTimeout"` + DeleteTimeout int `yaml:"deleteTimeout,omitempty"` // SyncReleaseLabels is true if the release labels should be synced with the helmfile labels - SyncReleaseLabels bool `yaml:"syncReleaseLabels"` + SyncReleaseLabels *bool `yaml:"syncReleaseLabels,omitempty"` + // TakeOwnership is true if the helmfile should take ownership of the release + TakeOwnership *bool `yaml:"takeOwnership,omitempty"` } // RepositorySpec that defines values for a helm repo @@ -414,7 +416,9 @@ type ReleaseSpec struct { // Timeout is the time in seconds to wait for helmfile delete command (default 300) DeleteTimeout *int `yaml:"deleteTimeout,omitempty"` // SyncReleaseLabels is true if the release labels should be synced with the helmfile labels - SyncReleaseLabels bool `yaml:"syncReleaseLabels"` + SyncReleaseLabels *bool `yaml:"syncReleaseLabels,omitempty"` + // TakeOwnership is true if the release should take ownership of the resources + TakeOwnership *bool `yaml:"takeOwnership,omitempty"` } func (r *Inherits) UnmarshalYAML(unmarshal func(any) error) error { @@ -2142,6 +2146,7 @@ type DiffOpts struct { PostRendererArgs []string SuppressOutputLineRegex []string SkipSchemaValidation bool + TakeOwnership bool } func (o *DiffOpts) Apply(opts *DiffOpts) { @@ -2899,9 +2904,11 @@ func (st *HelmState) flagsForUpgrade(helm helmexec.Interface, release *ReleaseSp postRenderer := "" syncReleaseLabels := false + takeOwnership := false if opt != nil { postRenderer = opt.PostRenderer syncReleaseLabels = opt.SyncReleaseLabels + takeOwnership = opt.TakeOwnership } flags = st.appendConnectionFlags(flags, release) @@ -2930,7 +2937,7 @@ func (st *HelmState) flagsForUpgrade(helm helmexec.Interface, release *ReleaseSp flags = st.appendHideNotesFlags(flags, helm, opt) // append take-ownership flag - flags = st.appendTakeOwnershipFlags(flags, helm, opt) + flags = st.appendTakeOwnershipFlagsForUpgrade(flags, helm, release, takeOwnership) flags = st.appendExtraSyncFlags(flags, opt) @@ -3071,6 +3078,17 @@ func (st *HelmState) flagsForDiff(helm helmexec.Interface, release *ReleaseSpec, flags = st.appendSuppressOutputLineRegexFlags(flags, release, suppressOutputLineRegex) } + takeOwnership := false + if opt != nil { + takeOwnership = opt.TakeOwnership + } + + var err error + flags, err = st.appendTakeOwnershipFlagsForDiff(flags, release, takeOwnership) + if err != nil { + return nil, nil, err + } + common, files, err := st.namespaceAndValuesFlags(helm, release, workerIndex) if err != nil { return nil, files, err @@ -3079,6 +3097,33 @@ func (st *HelmState) flagsForDiff(helm helmexec.Interface, release *ReleaseSpec, return append(flags, common...), files, nil } +func (st *HelmState) appendTakeOwnershipFlagsForDiff(flags []string, release *ReleaseSpec, takeOwnership bool) ([]string, error) { + settings := cli.New() + isAppendTakeOwnership := false + + switch { + case release.TakeOwnership != nil && *release.TakeOwnership: + isAppendTakeOwnership = true + case takeOwnership: + isAppendTakeOwnership = true + case st.HelmDefaults.TakeOwnership != nil && *st.HelmDefaults.TakeOwnership: + isAppendTakeOwnership = true + } + if isAppendTakeOwnership { + diffVersion, err := helmexec.GetPluginVersion("diff", settings.PluginsDirectory) + if err != nil { + return flags, err + } + dv, _ := semver.NewVersion("v3.11.0") + + if diffVersion.LessThan(dv) { + return flags, fmt.Errorf("take-ownership is not supported by helm-diff plugin version %s, please use at least v3.11.0", diffVersion) + } + flags = append(flags, "--take-ownership") + } + return flags, nil +} + func (st *HelmState) appendChartVersionFlags(flags []string, release *ReleaseSpec) []string { if release.Version != "" { flags = append(flags, "--version", release.Version) diff --git a/pkg/state/temp_test.go b/pkg/state/temp_test.go index 03da0c09..8cfc3c10 100644 --- a/pkg/state/temp_test.go +++ b/pkg/state/temp_test.go @@ -38,39 +38,39 @@ func TestGenerateID(t *testing.T) { run(testcase{ subject: "baseline", release: ReleaseSpec{Name: "foo", Chart: "incubator/raw"}, - want: "foo-values-57cf65699f", + want: "foo-values-5859bbd547", }) run(testcase{ subject: "different bytes content", release: ReleaseSpec{Name: "foo", Chart: "incubator/raw"}, data: []byte(`{"k":"v"}`), - want: "foo-values-78ccf5c7cb", + want: "foo-values-56876c54b8", }) run(testcase{ subject: "different map content", release: ReleaseSpec{Name: "foo", Chart: "incubator/raw"}, data: map[string]any{"k": "v"}, - want: "foo-values-f94dd98bb", + want: "foo-values-5ccd486f8b", }) run(testcase{ subject: "different chart", release: ReleaseSpec{Name: "foo", Chart: "stable/envoy"}, - want: "foo-values-6d977c9b54", + want: "foo-values-6f477bcd58", }) run(testcase{ subject: "different name", release: ReleaseSpec{Name: "bar", Chart: "incubator/raw"}, - want: "bar-values-5bc44889dd", + want: "bar-values-86d77b4c7b", }) run(testcase{ subject: "specific ns", release: ReleaseSpec{Name: "foo", Chart: "incubator/raw", Namespace: "myns"}, - want: "myns-foo-values-589f97d65d", + want: "myns-foo-values-68d8d6f46", }) for id, n := range ids { diff --git a/pkg/tmpl/context_funcs.go b/pkg/tmpl/context_funcs.go index e16fd9ef..c0f1111d 100644 --- a/pkg/tmpl/context_funcs.go +++ b/pkg/tmpl/context_funcs.go @@ -307,14 +307,14 @@ func ToYaml(v any) (string, error) { return string(data), nil } -func FromYaml(str string) (Values, error) { - m := map[string]any{} +func FromYaml(str string) (any, error) { + var m any if err := yaml.Unmarshal([]byte(str), &m); err != nil { return nil, fmt.Errorf("%s, offending yaml: %s", err, str) } - m, err := maputil.CastKeysToStrings(m) + m, err := maputil.RecursivelyStringifyMapKey(m) if err != nil { return nil, fmt.Errorf("%s, offending yaml: %s", err, str) } diff --git a/pkg/tmpl/context_funcs_test.go b/pkg/tmpl/context_funcs_test.go index d1141d8e..58ac5bd0 100644 --- a/pkg/tmpl/context_funcs_test.go +++ b/pkg/tmpl/context_funcs_test.go @@ -200,7 +200,88 @@ func TestToYaml(t *testing.T) { require.Equal(t, expected, actual) } -func testFromYaml(t *testing.T, goccyGoYaml bool, expected Values) { +func testFromYamlObject(t *testing.T) { + raw := `foo: + bar: BAR +` + + actual, err := FromYaml(raw) + require.NoError(t, err) + require.Equal( + t, + map[string]any{ + "foo": map[string]any{ + "bar": "BAR", + }, + }, + actual, + ) +} + +func testFromYamlArray(t *testing.T) { + raw := `- foo +- bar: BAR +` + + actual, err := FromYaml(raw) + require.NoError(t, err) + require.Equal( + t, + []any{ + "foo", + map[string]any{ + "bar": "BAR", + }, + }, + actual, + ) +} + +func testFromYamlString(t *testing.T) { + raw := `foo +` + + actual, err := FromYaml(raw) + require.NoError(t, err) + require.Equal(t, "foo", actual) +} + +func testFromYamlNumber(t *testing.T) { + raw := `1234 +` + + actual, err := FromYaml(raw) + require.NoError(t, err) + + switch a := actual.(type) { + case int: + require.Equal(t, 1234, a) + case uint64: + require.Equal(t, uint64(1234), a) + default: + t.Errorf("unexpected type: %T", a) + } +} + +func testFromYamlBoolean(t *testing.T) { + raw := `true +` + + actual, err := FromYaml(raw) + require.NoError(t, err) + require.Equal(t, true, actual) +} + +func testFromYamlNull(t *testing.T) { + raw := `null +` + + actual, err := FromYaml(raw) + require.NoError(t, err) + require.Equal(t, nil, actual) +} + +func testFromYaml(t *testing.T, goccyGoYaml bool) { t.Helper() v := runtime.GoccyGoYaml @@ -209,39 +290,26 @@ func testFromYaml(t *testing.T, goccyGoYaml bool, expected Values) { runtime.GoccyGoYaml = v }) - raw := `foo: - bar: BAR -` - actual, err := FromYaml(raw) - require.NoError(t, err) - require.Equal(t, expected, actual) + t.Run("test unmarshalling object", testFromYamlObject) + + t.Run("test unmarshalling array", testFromYamlArray) + + t.Run("test unmarshalling string", testFromYamlString) + + t.Run("test unmarshalling number", testFromYamlNumber) + + t.Run("test unmarshalling boolean", testFromYamlBoolean) + + t.Run("test unmarshalling null", testFromYamlNull) } func TestFromYaml(t *testing.T) { t.Run("with goccy/go-yaml", func(t *testing.T) { - testFromYaml( - t, - true, - // nolint: unconvert - Values(map[string]any{ - "foo": map[string]any{ - "bar": "BAR", - }, - }), - ) + testFromYaml(t, true) }) t.Run("with gopkg.in/yaml.v2", func(t *testing.T) { - testFromYaml( - t, - false, - // nolint: unconvert - Values(map[string]any{ - "foo": map[string]any{ - "bar": "BAR", - }, - }), - ) + testFromYaml(t, false) }) } diff --git a/test/e2e/template/helmfile/testdata/snapshot/issue_2098_release_template_needs/output.yaml b/test/e2e/template/helmfile/testdata/snapshot/issue_2098_release_template_needs/output.yaml index c7d30230..25dd4428 100644 --- a/test/e2e/template/helmfile/testdata/snapshot/issue_2098_release_template_needs/output.yaml +++ b/test/e2e/template/helmfile/testdata/snapshot/issue_2098_release_template_needs/output.yaml @@ -19,7 +19,6 @@ releases: name: default-shared-resources namespace: default service: shared-resources - syncReleaseLabels: false - chart: aservo/util version: 0.0.1 needs: @@ -31,10 +30,8 @@ releases: name: default-release-resources namespace: default service: release-resources - syncReleaseLabels: false templates: defaults: name: default-{{ .Release.Labels.service }} namespace: default - syncReleaseLabels: false renderedvalues: {}