-
Notifications
You must be signed in to change notification settings - Fork 7k
fix: fix calculating patch for respect ignore diff feature #17693
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
Merged
alexmt
merged 3 commits into
argoproj:master
from
alexmt:alex/bug/respect-ignore-differences
Apr 3, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -455,3 +455,207 @@ func TestNormalizeTargetResources(t *testing.T) { | |
| assert.Equal(t, 2, len(containers)) | ||
| }) | ||
| } | ||
|
|
||
| func TestNormalizeTargetResourcesWithList(t *testing.T) { | ||
| type fixture struct { | ||
| comparisonResult *comparisonResult | ||
| } | ||
| setupHttpProxy := func(t *testing.T, ignores []v1alpha1.ResourceIgnoreDifferences) *fixture { | ||
| t.Helper() | ||
| dc, err := diff.NewDiffConfigBuilder(). | ||
| WithDiffSettings(ignores, nil, true). | ||
| WithNoCache(). | ||
| Build() | ||
| require.NoError(t, err) | ||
| live := test.YamlToUnstructured(testdata.LiveHTTPProxy) | ||
| target := test.YamlToUnstructured(testdata.TargetHTTPProxy) | ||
| return &fixture{ | ||
| &comparisonResult{ | ||
| reconciliationResult: sync.ReconciliationResult{ | ||
| Live: []*unstructured.Unstructured{live}, | ||
| Target: []*unstructured.Unstructured{target}, | ||
| }, | ||
| diffConfig: dc, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| t.Run("will properly ignore nested fields within arrays", func(t *testing.T) { | ||
| // given | ||
| ignores := []v1alpha1.ResourceIgnoreDifferences{ | ||
| { | ||
| Group: "projectcontour.io", | ||
| Kind: "HTTPProxy", | ||
| JQPathExpressions: []string{".spec.routes[]"}, | ||
| //JSONPointers: []string{"/spec/routes"}, | ||
| }, | ||
| } | ||
| f := setupHttpProxy(t, ignores) | ||
| target := test.YamlToUnstructured(testdata.TargetHTTPProxy) | ||
| f.comparisonResult.reconciliationResult.Target = []*unstructured.Unstructured{target} | ||
|
|
||
| // when | ||
| patchedTargets, err := normalizeTargetResources(f.comparisonResult) | ||
|
|
||
| // then | ||
| require.NoError(t, err) | ||
| require.Equal(t, 1, len(f.comparisonResult.reconciliationResult.Live)) | ||
| require.Equal(t, 1, len(f.comparisonResult.reconciliationResult.Target)) | ||
| require.Equal(t, 1, len(patchedTargets)) | ||
|
|
||
| // live should have 1 entry | ||
| require.Equal(t, 1, len(dig[[]any](f.comparisonResult.reconciliationResult.Live[0].Object, []interface{}{"spec", "routes", 0, "rateLimitPolicy", "global", "descriptors"}))) | ||
| // assert some arbitrary field to show `entries[0]` is not an empty object | ||
| require.Equal(t, "sample-header", dig[string](f.comparisonResult.reconciliationResult.Live[0].Object, []interface{}{"spec", "routes", 0, "rateLimitPolicy", "global", "descriptors", 0, "entries", 0, "requestHeader", "headerName"})) | ||
|
|
||
| // target has 2 entries | ||
| require.Equal(t, 2, len(dig[[]any](f.comparisonResult.reconciliationResult.Target[0].Object, []interface{}{"spec", "routes", 0, "rateLimitPolicy", "global", "descriptors", 0, "entries"}))) | ||
| // assert some arbitrary field to show `entries[0]` is not an empty object | ||
| require.Equal(t, "sample-header", dig[string](f.comparisonResult.reconciliationResult.Target[0].Object, []interface{}{"spec", "routes", 0, "rateLimitPolicy", "global", "descriptors", 0, "entries", 0, "requestHeaderValueMatch", "headers", 0, "name"})) | ||
|
|
||
| // It should be *1* entries in the array | ||
| require.Equal(t, 1, len(dig[[]any](patchedTargets[0].Object, []interface{}{"spec", "routes", 0, "rateLimitPolicy", "global", "descriptors"}))) | ||
| // and it should NOT equal an empty object | ||
| require.Len(t, dig[any](patchedTargets[0].Object, []interface{}{"spec", "routes", 0, "rateLimitPolicy", "global", "descriptors", 0, "entries", 0}), 1) | ||
|
|
||
| }) | ||
| t.Run("will correctly set array entries if new entries have been added", func(t *testing.T) { | ||
| // given | ||
| ignores := []v1alpha1.ResourceIgnoreDifferences{ | ||
| { | ||
| Group: "apps", | ||
| Kind: "Deployment", | ||
| JQPathExpressions: []string{".spec.template.spec.containers[].env[] | select(.name == \"SOME_ENV_VAR\")"}, | ||
| }, | ||
| } | ||
| f := setupHttpProxy(t, ignores) | ||
| live := test.YamlToUnstructured(testdata.LiveDeploymentEnvVarsYaml) | ||
| target := test.YamlToUnstructured(testdata.TargetDeploymentEnvVarsYaml) | ||
| f.comparisonResult.reconciliationResult.Live = []*unstructured.Unstructured{live} | ||
| f.comparisonResult.reconciliationResult.Target = []*unstructured.Unstructured{target} | ||
|
|
||
| // when | ||
| targets, err := normalizeTargetResources(f.comparisonResult) | ||
|
|
||
| // then | ||
| require.NoError(t, err) | ||
| require.Equal(t, 1, len(targets)) | ||
| containers, ok, err := unstructured.NestedSlice(targets[0].Object, "spec", "template", "spec", "containers") | ||
| require.NoError(t, err) | ||
| require.True(t, ok) | ||
| assert.Equal(t, 1, len(containers)) | ||
|
|
||
| ports := containers[0].(map[string]interface{})["ports"].([]interface{}) | ||
| assert.Equal(t, 1, len(ports)) | ||
|
|
||
| env := containers[0].(map[string]interface{})["env"].([]interface{}) | ||
| assert.Equal(t, 3, len(env)) | ||
|
|
||
| first := env[0] | ||
| second := env[1] | ||
| third := env[2] | ||
|
|
||
| // Currently the defined order at this time is the insertion order of the target manifest. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this still true when using SMP? |
||
| assert.Equal(t, "SOME_ENV_VAR", first.(map[string]interface{})["name"]) | ||
| assert.Equal(t, "some_value", first.(map[string]interface{})["value"]) | ||
|
|
||
| assert.Equal(t, "SOME_OTHER_ENV_VAR", second.(map[string]interface{})["name"]) | ||
| assert.Equal(t, "some_other_value", second.(map[string]interface{})["value"]) | ||
|
|
||
| assert.Equal(t, "YET_ANOTHER_ENV_VAR", third.(map[string]interface{})["name"]) | ||
| assert.Equal(t, "yet_another_value", third.(map[string]interface{})["value"]) | ||
| }) | ||
|
|
||
| t.Run("ignore-deployment-image-replicas-changes-additive", func(t *testing.T) { | ||
| // given | ||
|
|
||
| ignores := []v1alpha1.ResourceIgnoreDifferences{ | ||
| { | ||
| Group: "apps", | ||
| Kind: "Deployment", | ||
| JSONPointers: []string{"/spec/replicas"}, | ||
| }, { | ||
| Group: "apps", | ||
| Kind: "Deployment", | ||
| JQPathExpressions: []string{".spec.template.spec.containers[].image"}, | ||
| }, | ||
| } | ||
| f := setupHttpProxy(t, ignores) | ||
| live := test.YamlToUnstructured(testdata.MinimalImageReplicaDeploymentYaml) | ||
| target := test.YamlToUnstructured(testdata.AdditionalImageReplicaDeploymentYaml) | ||
| f.comparisonResult.reconciliationResult.Live = []*unstructured.Unstructured{live} | ||
| f.comparisonResult.reconciliationResult.Target = []*unstructured.Unstructured{target} | ||
|
|
||
| // when | ||
| targets, err := normalizeTargetResources(f.comparisonResult) | ||
|
|
||
| // then | ||
| require.NoError(t, err) | ||
| require.Equal(t, 1, len(targets)) | ||
| metadata, ok, err := unstructured.NestedMap(targets[0].Object, "metadata") | ||
| require.NoError(t, err) | ||
| require.True(t, ok) | ||
| labels, ok := metadata["labels"].(map[string]interface{}) | ||
| require.True(t, ok) | ||
| assert.Equal(t, 2, len(labels)) | ||
| assert.Equal(t, "web", labels["appProcess"]) | ||
|
|
||
| spec, ok, err := unstructured.NestedMap(targets[0].Object, "spec") | ||
| require.NoError(t, err) | ||
| require.True(t, ok) | ||
|
|
||
| assert.Equal(t, int64(1), spec["replicas"]) | ||
|
|
||
| template, ok := spec["template"].(map[string]interface{}) | ||
| require.True(t, ok) | ||
|
|
||
| tMetadata, ok := template["metadata"].(map[string]interface{}) | ||
| require.True(t, ok) | ||
| tLabels, ok := tMetadata["labels"].(map[string]interface{}) | ||
| require.True(t, ok) | ||
| assert.Equal(t, 2, len(tLabels)) | ||
| assert.Equal(t, "web", tLabels["appProcess"]) | ||
|
|
||
| tSpec, ok := template["spec"].(map[string]interface{}) | ||
| require.True(t, ok) | ||
| containers, ok, err := unstructured.NestedSlice(tSpec, "containers") | ||
| require.NoError(t, err) | ||
| require.True(t, ok) | ||
| assert.Equal(t, 1, len(containers)) | ||
|
|
||
| first := containers[0].(map[string]interface{}) | ||
| assert.Equal(t, "alpine:3", first["image"]) | ||
|
|
||
| resources, ok := first["resources"].(map[string]interface{}) | ||
| require.True(t, ok) | ||
| requests, ok := resources["requests"].(map[string]interface{}) | ||
| require.True(t, ok) | ||
| assert.Equal(t, "400m", requests["cpu"]) | ||
|
|
||
| env, ok, err := unstructured.NestedSlice(first, "env") | ||
| require.NoError(t, err) | ||
| require.True(t, ok) | ||
| assert.Equal(t, 1, len(env)) | ||
|
|
||
| env0 := env[0].(map[string]interface{}) | ||
| assert.Equal(t, "EV", env0["name"]) | ||
| assert.Equal(t, "here", env0["value"]) | ||
| }) | ||
| } | ||
|
|
||
| func dig[T any](obj interface{}, path []interface{}) T { | ||
| i := obj | ||
|
|
||
| for _, segment := range path { | ||
| switch segment.(type) { | ||
| case int: | ||
| i = i.([]interface{})[segment.(int)] | ||
| case string: | ||
| i = i.(map[string]interface{})[segment.(string)] | ||
| default: | ||
| panic("invalid path for object") | ||
| } | ||
| } | ||
|
|
||
| return i.(T) | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any value in trying both jqpathexpression and jsonpointer? e.g. Should the test iterate the two options?