Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🤖 Sync from open-cluster-management-io/policy-generator-plugin: #136 #43

Merged
merged 2 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
18 changes: 12 additions & 6 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 @@ -205,9 +205,15 @@ func (m *manifestPatcher) ApplyPatches() ([]map[string]interface{}, error) {
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
5 changes: 5 additions & 0 deletions internal/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -1703,6 +1703,11 @@ func (p *Plugin) generateSelector(
resolvedSelectorsLS.MatchExpressions = append(resolvedSelectorsLS.MatchExpressions, lsReq)
}

// Sort the MatchExpressions to make the result deterministic
sort.Slice(resolvedSelectorsLS.MatchExpressions, func(i, j int) bool {
return resolvedSelectorsLS.MatchExpressions[i].Key < resolvedSelectorsLS.MatchExpressions[j].Key
})

resolved, err := runtime.DefaultUnstructuredConverter.ToUnstructured(&resolvedSelectorsLS)
if err != nil {
panic(err)
Expand Down