Skip to content

Commit

Permalink
Use Strategic Merge and JSON 6902 patches
Browse files Browse the repository at this point in the history
Signed-off-by: Hidde Beydals <hello@hidde.co>
  • Loading branch information
hiddeco committed Feb 3, 2021
1 parent 17a6bff commit f2bb23d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
32 changes: 32 additions & 0 deletions controllers/kustomization_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package controllers

import (
"crypto/sha1"
"encoding/json"
"fmt"
"io/ioutil"
"os"
Expand All @@ -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"
)

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f2bb23d

Please sign in to comment.