Skip to content

Commit

Permalink
Fix PatchesStrategicMerge deprecation warning
Browse files Browse the repository at this point in the history
  • Loading branch information
dhaiducek committed Dec 8, 2023
1 parent f21abdf commit 615b5d3
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 15 deletions.
6 changes: 4 additions & 2 deletions examples/input-kustomize/base/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- nginx-pod.yaml
labels:
- pairs:
app: myapp
- pairs:
app: myapp
2 changes: 2 additions & 0 deletions examples/input-kustomize/dev/kustomization.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ../base
namePrefix: dev-
Expand Down
3 changes: 2 additions & 1 deletion examples/input-kustomize/kustomization.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- dev
- prod

2 changes: 2 additions & 0 deletions examples/input-kustomize/prod/kustomization.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ../base
namePrefix: prod-
Expand Down
15 changes: 11 additions & 4 deletions examples/kustomization.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

generators:
- ./policyGenerator.yaml
- ./policyGenerator-kustomize.yaml
patchesStrategicMerge:
- input/patch.yaml
commonLabels:
custom: myApp

patches:
- path: input/patch.yaml

labels:
- includeSelectors: true
pairs:
custom: myApp
19 changes: 12 additions & 7 deletions internal/patches.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,8 @@ func (m *manifestPatcher) ApplyPatches() ([]map[string]interface{}, error) {
}

kustomizationYAMLFile := map[string][]interface{}{
"resources": {},
"patchesStrategicMerge": {},
"resources": {},
"patches": {},
}

options := []struct {
Expand All @@ -185,7 +185,7 @@ func (m *manifestPatcher) ApplyPatches() ([]map[string]interface{}, error) {
objects []map[string]interface{}
}{
{"manifest", "resources", m.manifests},
{"patch", "patchesStrategicMerge", m.patches},
{"patch", "patches", m.patches},
}
for _, option := range options {
for i, object := range option.objects {
Expand All @@ -204,10 +204,15 @@ func (m *manifestPatcher) ApplyPatches() ([]map[string]interface{}, error) {
if err != nil {
return nil, fmt.Errorf(errTemplate, option.optionType, err)
}

kustomizationYAMLFile[option.kustomizeKey] = append(
kustomizationYAMLFile[option.kustomizeKey], manifestFileName,
)
if (option.kustomizeKey != "patches") {
kustomizationYAMLFile[option.kustomizeKey] = append(
kustomizationYAMLFile[option.kustomizeKey], manifestFileName,
)
} else {
kustomizationYAMLFile[option.kustomizeKey] = append(
kustomizationYAMLFile[option.kustomizeKey], map[string]interface{}{"path": manifestFileName},
)
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion internal/patches_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,8 @@ func TestApplyPatchesInvalidPatch(t *testing.T) {
patcher := manifestPatcher{manifests: manifests, patches: patches}
_, err := patcher.ApplyPatches()

expected := "failed to apply the patch(es) to the manifest(s) using Kustomize: no matches " +
expected := "failed to apply the patch(es) to the manifest(s) using Kustomize: no resource " +
"matches strategic merge patch \"ToasterOven.v1.[noGrp]/configmap2.default\": no matches " +
"for Id ToasterOven.v1.[noGrp]/configmap2.default; failed to find unique target for patch " +
"ToasterOven.v1.[noGrp]/configmap2.default"
assertEqual(t, err.Error(), expected)
Expand Down

0 comments on commit 615b5d3

Please sign in to comment.