Skip to content

Commit

Permalink
feat: Ignore preserveUnknownFields in Diff from CRD Spec (#6503)
Browse files Browse the repository at this point in the history
Signed-off-by: Subba Reddy Veeramreddy <[email protected]>
  • Loading branch information
subbuv26 authored Jun 22, 2021
1 parent c5b8aa7 commit 7f5c014
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
15 changes: 14 additions & 1 deletion util/settings/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -550,12 +550,14 @@ func (mgr *SettingsManager) GetResourceOverrides() (map[string]v1alpha1.Resource
}

crdGK := "apiextensions.k8s.io/CustomResourceDefinition"
crdPrsvUnkn := "/spec/preserveUnknownFields"

switch diffOptions.IgnoreResourceStatusField {
case "", "crd":
addStatusOverrideToGK(resourceOverrides, crdGK)
log.Info("Ignore status for CustomResourceDefinitions")

addIgnoreDiffItemOverrideToGK(resourceOverrides, crdGK, crdPrsvUnkn)
log.Infof("Ignore '%v' for CustomResourceDefinitions", crdPrsvUnkn)
case "all":
addStatusOverrideToGK(resourceOverrides, "*/*")
log.Info("Ignore status for all objects")
Expand All @@ -582,6 +584,17 @@ func addStatusOverrideToGK(resourceOverrides map[string]v1alpha1.ResourceOverrid
}
}

func addIgnoreDiffItemOverrideToGK(resourceOverrides map[string]v1alpha1.ResourceOverride, groupKind, ignoreItem string) {
if val, ok := resourceOverrides[groupKind]; ok {
val.IgnoreDifferences.JSONPointers = append(val.IgnoreDifferences.JSONPointers, ignoreItem)
resourceOverrides[groupKind] = val
} else {
resourceOverrides[groupKind] = v1alpha1.ResourceOverride{
IgnoreDifferences: v1alpha1.OverrideIgnoreDiff{JSONPointers: []string{ignoreItem}},
}
}
}

func (mgr *SettingsManager) appendResourceOverridesFromSplitKeys(cmData map[string]string, resourceOverrides map[string]v1alpha1.ResourceOverride) error {
for k, v := range cmData {
if !strings.HasPrefix(k, resourceCustomizationsKey) {
Expand Down
12 changes: 8 additions & 4 deletions util/settings/settings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ func TestGetResourceOverrides(t *testing.T) {
ignoreStatus := v1alpha1.ResourceOverride{IgnoreDifferences: v1alpha1.OverrideIgnoreDiff{
JSONPointers: []string{"/status"},
}}
ignoreCRDFields := v1alpha1.ResourceOverride{IgnoreDifferences: v1alpha1.OverrideIgnoreDiff{
JSONPointers: []string{"/status", "/spec/preserveUnknownFields"},
}}
crdGK := "apiextensions.k8s.io/CustomResourceDefinition"

_, settingsManager := fixtures(map[string]string{
Expand Down Expand Up @@ -175,7 +178,7 @@ func TestGetResourceOverrides(t *testing.T) {
// by default, crd status should be ignored
crdOverrides := overrides[crdGK]
assert.NotNil(t, crdOverrides)
assert.Equal(t, ignoreStatus, crdOverrides)
assert.Equal(t, ignoreCRDFields, crdOverrides)

// with value all, status of all objects should be ignored
_, settingsManager = fixtures(map[string]string{
Expand Down Expand Up @@ -208,7 +211,7 @@ func TestGetResourceOverrides(t *testing.T) {
crdOverrides = overrides[crdGK]
assert.NotNil(t, crdOverrides)
assert.Equal(t, v1alpha1.ResourceOverride{IgnoreDifferences: v1alpha1.OverrideIgnoreDiff{
JSONPointers: []string{"/webhooks/0/clientConfig/caBundle", "/status"},
JSONPointers: []string{"/webhooks/0/clientConfig/caBundle", "/status", "/spec/preserveUnknownFields"},
JQPathExpressions: []string{".webhooks[0].clientConfig.caBundle"},
}}, crdOverrides)

Expand Down Expand Up @@ -272,7 +275,7 @@ func TestGetResourceOverrides_with_splitted_keys(t *testing.T) {
overrides, err := settingsManager.GetResourceOverrides()
assert.NoError(t, err)
assert.Equal(t, 5, len(overrides))
assert.Equal(t, 1, len(overrides[crdGK].IgnoreDifferences.JSONPointers))
assert.Equal(t, 2, len(overrides[crdGK].IgnoreDifferences.JSONPointers))
assert.Equal(t, 1, len(overrides["admissionregistration.k8s.io/MutatingWebhookConfiguration"].IgnoreDifferences.JSONPointers))
assert.Equal(t, "foo", overrides["admissionregistration.k8s.io/MutatingWebhookConfiguration"].IgnoreDifferences.JSONPointers[0])
assert.Equal(t, "foo\n", overrides["certmanager.k8s.io/Certificate"].HealthLua)
Expand Down Expand Up @@ -310,8 +313,9 @@ func TestGetResourceOverrides_with_splitted_keys(t *testing.T) {
overrides, err := settingsManager.GetResourceOverrides()
assert.NoError(t, err)
assert.Equal(t, 8, len(overrides))
assert.Equal(t, 1, len(overrides[crdGK].IgnoreDifferences.JSONPointers))
assert.Equal(t, 2, len(overrides[crdGK].IgnoreDifferences.JSONPointers))
assert.Equal(t, "/status", overrides[crdGK].IgnoreDifferences.JSONPointers[0])
assert.Equal(t, "/spec/preserveUnknownFields", overrides[crdGK].IgnoreDifferences.JSONPointers[1])
assert.Equal(t, 1, len(overrides["admissionregistration.k8s.io/MutatingWebhookConfiguration"].IgnoreDifferences.JSONPointers))
assert.Equal(t, "bar", overrides["admissionregistration.k8s.io/MutatingWebhookConfiguration"].IgnoreDifferences.JSONPointers[0])
assert.Equal(t, 1, len(overrides["admissionregistration.k8s.io/MutatingWebhookConfiguration"].KnownTypeFields))
Expand Down

0 comments on commit 7f5c014

Please sign in to comment.