diff --git a/controllers/kustomization_generator.go b/controllers/kustomization_generator.go index bf81600d7..307bb7da1 100644 --- a/controllers/kustomization_generator.go +++ b/controllers/kustomization_generator.go @@ -18,6 +18,7 @@ package controllers import ( "crypto/sha1" + "encoding/json" "fmt" "io/ioutil" "os" @@ -32,6 +33,8 @@ import ( kustypes "sigs.k8s.io/kustomize/api/types" "sigs.k8s.io/yaml" + "github.com/fluxcd/pkg/apis/kustomize" + kustomizev1 "github.com/fluxcd/kustomize-controller/api/v1beta1" ) @@ -96,6 +99,21 @@ func (kg *KustomizeGenerator) WriteFile(dirPath string) (string, error) { kus.Namespace = kg.kustomization.Spec.TargetNamespace } + for _, m := range kg.kustomization.Spec.PatchesStrategicMerge { + kus.PatchesStrategicMerge = append(kus.PatchesStrategicMerge, kustypes.PatchStrategicMerge(m.String())) + } + + for _, m := range kg.kustomization.Spec.PatchesJSON6902 { + patch, err := json.Marshal(m.Patch) + if err != nil { + return "", err + } + kus.PatchesJson6902 = append(kus.PatchesJson6902, kustypes.Patch{ + Patch: string(patch), + Target: adaptSelector(&m.Target), + }) + } + for _, image := range kg.kustomization.Spec.Images { newImage := kustypes.Image{ Name: image.Name, @@ -279,6 +297,20 @@ func (kg *KustomizeGenerator) generateLabelTransformer(checksum, dirPath string) return nil } +func adaptSelector(selector *kustomize.Selector) (output *kustypes.Selector) { + if selector != nil { + output = &kustypes.Selector{} + output.Gvk.Group = selector.Group + output.Gvk.Kind = selector.Kind + output.Gvk.Version = selector.Version + output.Name = selector.Name + output.Namespace = selector.Namespace + output.LabelSelector = selector.LabelSelector + output.AnnotationSelector = selector.AnnotationSelector + } + return +} + // buildKustomization wraps krusty.MakeKustomizer with the following settings: // - disable kyaml due to critical bugs like: // - https://github.com/kubernetes-sigs/kustomize/issues/3446 diff --git a/go.mod b/go.mod index 365539dcf..67b8722c4 100644 --- a/go.mod +++ b/go.mod @@ -7,6 +7,7 @@ replace github.com/fluxcd/kustomize-controller/api => ./api require ( github.com/cyphar/filepath-securejoin v0.2.2 github.com/fluxcd/kustomize-controller/api v0.7.4 + github.com/fluxcd/pkg/apis/kustomize v0.0.1 github.com/fluxcd/pkg/apis/meta v0.7.0 github.com/fluxcd/pkg/runtime v0.8.0 github.com/fluxcd/pkg/testserver v0.0.2