Skip to content

Commit 3951079

Browse files
authored
fix: remove last-applied-configuration before diff in ssa (#460)
* fix: remove last-apply-configurations before diff in ssa Signed-off-by: Leonardo Luz Almeida <[email protected]> * fix: add tests to validate expected behaviour Signed-off-by: Leonardo Luz Almeida <[email protected]> Signed-off-by: Leonardo Luz Almeida <[email protected]>
1 parent 517c1ff commit 3951079

File tree

5 files changed

+26
-234
lines changed

5 files changed

+26
-234
lines changed

pkg/diff/diff.go

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ import (
2929
kubescheme "github.com/argoproj/gitops-engine/pkg/utils/kube/scheme"
3030
)
3131

32-
const couldNotMarshalErrMsg = "Could not unmarshal to object of type %s: %v"
32+
const (
33+
couldNotMarshalErrMsg = "Could not unmarshal to object of type %s: %v"
34+
AnnotationLastAppliedConfig = "kubectl.kubernetes.io/last-applied-configuration"
35+
)
3336

3437
// Holds diffing result of two resources
3538
type DiffResult struct {
@@ -173,44 +176,49 @@ func structuredMergeDiff(config, live *unstructured.Unstructured, pt *typed.Pars
173176
}
174177

175178
// 6) Apply default values in predicted live
176-
predictedLive, err := applyDefaultValues(mergedCleanLive)
179+
predictedLive, err := normalizeTypedValue(mergedCleanLive)
177180
if err != nil {
178181
return nil, fmt.Errorf("error applying default values in predicted live: %w", err)
179182
}
180183

181184
// 7) Apply default values in live
182-
taintedLive, err := applyDefaultValues(tvLive)
185+
taintedLive, err := normalizeTypedValue(tvLive)
183186
if err != nil {
184187
return nil, fmt.Errorf("error applying default values in live: %w", err)
185188
}
186189

187190
return buildDiffResult(predictedLive, taintedLive), nil
188191
}
189192

190-
func applyDefaultValues(result *typed.TypedValue) ([]byte, error) {
191-
ru := result.AsValue().Unstructured()
193+
// normalizeTypedValue will prepare the given tv so it can be used in diffs by:
194+
// - removing last-applied-configuration annotation
195+
// - applying default values
196+
func normalizeTypedValue(tv *typed.TypedValue) ([]byte, error) {
197+
ru := tv.AsValue().Unstructured()
192198
r, ok := ru.(map[string]interface{})
193199
if !ok {
194200
return nil, fmt.Errorf("error converting result typedValue: expected map got %T", ru)
195201
}
196-
mergedUnstructured := &unstructured.Unstructured{Object: r}
197-
mergedBytes, err := json.Marshal(mergedUnstructured)
202+
resultUn := &unstructured.Unstructured{Object: r}
203+
unstructured.RemoveNestedField(resultUn.Object, "metadata", "annotations", AnnotationLastAppliedConfig)
204+
205+
resultBytes, err := json.Marshal(resultUn)
198206
if err != nil {
199207
return nil, fmt.Errorf("error while marshaling merged unstructured: %w", err)
200208
}
201209

202-
obj, err := scheme.Scheme.New(mergedUnstructured.GroupVersionKind())
210+
obj, err := scheme.Scheme.New(resultUn.GroupVersionKind())
203211
if err == nil {
204-
err := json.Unmarshal(mergedBytes, &obj)
212+
err := json.Unmarshal(resultBytes, &obj)
205213
if err != nil {
206214
return nil, fmt.Errorf("error unmarshaling merged bytes into object: %w", err)
207215
}
208-
mergedBytes, err = patchDefaultValues(mergedBytes, obj)
216+
resultBytes, err = patchDefaultValues(resultBytes, obj)
209217
if err != nil {
210218
return nil, fmt.Errorf("error applying defaults: %w", err)
211219
}
212220
}
213-
return mergedBytes, nil
221+
return resultBytes, nil
214222
}
215223

216224
func buildDiffResult(predictedBytes []byte, liveBytes []byte) *DiffResult {

pkg/diff/diff_test.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -770,14 +770,13 @@ func TestStructuredMergeDiff(t *testing.T) {
770770
assert.NotNil(t, liveSVC.Spec.InternalTrafficPolicy)
771771
assert.Equal(t, "Cluster", string(*predictedSVC.Spec.InternalTrafficPolicy))
772772
assert.Equal(t, "Cluster", string(*liveSVC.Spec.InternalTrafficPolicy))
773+
assert.Empty(t, predictedSVC.Annotations[AnnotationLastAppliedConfig])
774+
assert.Empty(t, liveSVC.Annotations[AnnotationLastAppliedConfig])
773775
})
774776
t.Run("will remove entries in list", func(t *testing.T) {
775777
// given
776778
liveState := StrToUnstructured(testdata.ServiceLiveYAML)
777779
desiredState := StrToUnstructured(testdata.ServiceConfigWith2Ports)
778-
expectedLiveState := StrToUnstructured(testdata.ExpectedServiceLiveWith2PortsYAML)
779-
expectedLiveBytes, err := json.Marshal(expectedLiveState)
780-
require.NoError(t, err)
781780

782781
// when
783782
result, err := structuredMergeDiff(desiredState, liveState, &svcParseType, manager)
@@ -786,15 +785,13 @@ func TestStructuredMergeDiff(t *testing.T) {
786785
require.NoError(t, err)
787786
assert.NotNil(t, result)
788787
assert.True(t, result.Modified)
789-
assert.Equal(t, string(expectedLiveBytes), string(result.PredictedLive))
788+
svc := YamlToSvc(t, result.PredictedLive)
789+
assert.Len(t, svc.Spec.Ports, 2)
790790
})
791791
t.Run("will remove previously added fields not present in desired state", func(t *testing.T) {
792792
// given
793793
liveState := StrToUnstructured(testdata.LiveServiceWithTypeYAML)
794794
desiredState := StrToUnstructured(testdata.ServiceConfigYAML)
795-
expectedLiveState := StrToUnstructured(testdata.ServiceLiveNoTypeYAML)
796-
expectedLiveBytes, err := json.Marshal(expectedLiveState)
797-
require.NoError(t, err)
798795

799796
// when
800797
result, err := structuredMergeDiff(desiredState, liveState, &svcParseType, manager)
@@ -803,7 +800,8 @@ func TestStructuredMergeDiff(t *testing.T) {
803800
require.NoError(t, err)
804801
assert.NotNil(t, result)
805802
assert.True(t, result.Modified)
806-
assert.Equal(t, string(expectedLiveBytes), string(result.PredictedLive))
803+
svc := YamlToSvc(t, result.PredictedLive)
804+
assert.Equal(t, corev1.ServiceTypeClusterIP, svc.Spec.Type)
807805
})
808806
t.Run("will apply service with multiple ports", func(t *testing.T) {
809807
// given
@@ -818,7 +816,7 @@ func TestStructuredMergeDiff(t *testing.T) {
818816
assert.NotNil(t, result)
819817
assert.True(t, result.Modified)
820818
svc := YamlToSvc(t, result.PredictedLive)
821-
assert.Equal(t, 5, len(svc.Spec.Ports))
819+
assert.Len(t, svc.Spec.Ports, 5)
822820
})
823821
}
824822

pkg/diff/testdata/data.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,9 @@ var (
99
//go:embed smd-service-live.yaml
1010
ServiceLiveYAML string
1111

12-
//go:embed smd-service-live-no-type.yaml
13-
ServiceLiveNoTypeYAML string
14-
1512
//go:embed smd-service-config-2-ports.yaml
1613
ServiceConfigWith2Ports string
1714

18-
//go:embed smd-service-live-expected-2-ports.yaml
19-
ExpectedServiceLiveWith2PortsYAML string
20-
2115
//go:embed smd-service-live-with-type.yaml
2216
LiveServiceWithTypeYAML string
2317

pkg/diff/testdata/smd-service-live-expected-2-ports.yaml

Lines changed: 0 additions & 100 deletions
This file was deleted.

pkg/diff/testdata/smd-service-live-no-type.yaml

Lines changed: 0 additions & 108 deletions
This file was deleted.

0 commit comments

Comments
 (0)