Skip to content

Commit

Permalink
fix: Server side diff now works correctly with fields removal (#640)
Browse files Browse the repository at this point in the history
* fix: Server side diff now works correctly with some fields removal

Helps with argoproj/argo-cd#20792

Removed and modified sets may only contain the fields that changed, not including key fields like "name". This can cause merge to fail, since it expects those fields to be present if they are present in the predicted live.
Fortunately, we can inspect the set and derive the key fields necessary. Then they can be added to the set and used during a merge.
Also, have a new test which fails before the fix, but passes now.

Failure of the new test before the fix
```
            	Error:      	Received unexpected error:
            	            	error removing non config mutations for resource Deployment/nginx-deployment: error reverting webhook removed fields in predicted live resource: .spec.template.spec.containers: element 0: associative list with keys has an element that omits key field "name" (and doesn't have default value)
            	Test:       	TestServerSideDiff/will_test_removing_some_field_with_undoing_changes_done_by_webhook
```

Signed-off-by: Andrii Korotkov <[email protected]>

* Use new version of structured merge diff with a new option

Signed-off-by: Andrii Korotkov <[email protected]>

* Add DCO

Signed-off-by: Andrii Korotkov <[email protected]>

* Try to fix sonar exclusions config

Signed-off-by: Andrii Korotkov <[email protected]>

---------

Signed-off-by: Andrii Korotkov <[email protected]>
  • Loading branch information
andrii-korotkov-verkada authored Dec 11, 2024
1 parent 0371401 commit 8849c3f
Show file tree
Hide file tree
Showing 9 changed files with 362 additions and 5 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ require (
k8s.io/kube-openapi v0.0.0-20240228011516-70dd3763d340
k8s.io/kubectl v0.31.2
k8s.io/kubernetes v1.31.0
sigs.k8s.io/structured-merge-diff/v4 v4.4.3
sigs.k8s.io/structured-merge-diff/v4 v4.4.4-0.20241211184406-7bf59b3d70ee
sigs.k8s.io/yaml v1.4.0
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ sigs.k8s.io/kustomize/api v0.17.2 h1:E7/Fjk7V5fboiuijoZHgs4aHuexi5Y2loXlVOAVAG5g
sigs.k8s.io/kustomize/api v0.17.2/go.mod h1:UWTz9Ct+MvoeQsHcJ5e+vziRRkwimm3HytpZgIYqye0=
sigs.k8s.io/kustomize/kyaml v0.17.1 h1:TnxYQxFXzbmNG6gOINgGWQt09GghzgTP6mIurOgrLCQ=
sigs.k8s.io/kustomize/kyaml v0.17.1/go.mod h1:9V0mCjIEYjlXuCdYsSXvyoy2BTsLESH7TlGV81S282U=
sigs.k8s.io/structured-merge-diff/v4 v4.4.3 h1:sCP7Vv3xx/CWIuTPVN38lUPx0uw0lcLfzaiDa8Ja01A=
sigs.k8s.io/structured-merge-diff/v4 v4.4.3/go.mod h1:N8f93tFZh9U6vpxwRArLiikrE5/2tiu1w1AGfACIGE4=
sigs.k8s.io/structured-merge-diff/v4 v4.4.4-0.20241211184406-7bf59b3d70ee h1:ipT2c6nEOdAfBwiwW1oI0mkrlPabbXEFmJBrg6B+OR8=
sigs.k8s.io/structured-merge-diff/v4 v4.4.4-0.20241211184406-7bf59b3d70ee/go.mod h1:N8f93tFZh9U6vpxwRArLiikrE5/2tiu1w1AGfACIGE4=
sigs.k8s.io/yaml v1.4.0 h1:Mk1wCc2gy/F0THH0TAp1QYyJNzRm2KCLy3o5ASXVI5E=
sigs.k8s.io/yaml v1.4.0/go.mod h1:Ejl7/uTz7PSA4eKMyQCUTnhZYNmLIl+5c2lQPGR2BPY=
4 changes: 2 additions & 2 deletions pkg/diff/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ func removeWebhookMutation(predictedLive, live *unstructured.Unstructured, gvkPa
}

if comparison.Modified != nil && !comparison.Modified.Empty() {
liveModValues := typedLive.ExtractItems(comparison.Modified)
liveModValues := typedLive.ExtractItems(comparison.Modified, typed.WithAppendKeyFields())
// revert modified fields not owned by any manager
typedPredictedLive, err = typedPredictedLive.Merge(liveModValues)
if err != nil {
Expand All @@ -267,7 +267,7 @@ func removeWebhookMutation(predictedLive, live *unstructured.Unstructured, gvkPa
}

if comparison.Removed != nil && !comparison.Removed.Empty() {
liveRmValues := typedLive.ExtractItems(comparison.Removed)
liveRmValues := typedLive.ExtractItems(comparison.Removed, typed.WithAppendKeyFields())
// revert removed fields not owned by any manager
typedPredictedLive, err = typedPredictedLive.Merge(liveRmValues)
if err != nil {
Expand Down
25 changes: 25 additions & 0 deletions pkg/diff/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -933,6 +933,31 @@ func TestServerSideDiff(t *testing.T) {
assert.Empty(t, liveSVC.Annotations[AnnotationLastAppliedConfig])
assert.Empty(t, predictedSVC.Labels["event"])
})

t.Run("will test removing some field with undoing changes done by webhook", func(t *testing.T) {
// given
t.Parallel()
liveState := StrToUnstructured(testdata.Deployment2LiveYAML)
desiredState := StrToUnstructured(testdata.Deployment2ConfigYAML)
opts := buildOpts(testdata.Deployment2PredictedLiveJSONSSD)

// when
result, err := serverSideDiff(desiredState, liveState, opts...)

// then
require.NoError(t, err)
assert.NotNil(t, result)
assert.True(t, result.Modified)
predictedDeploy := YamlToDeploy(t, result.PredictedLive)
liveDeploy := YamlToDeploy(t, result.NormalizedLive)
assert.Len(t, predictedDeploy.Spec.Template.Spec.Containers, 1)
assert.Len(t, liveDeploy.Spec.Template.Spec.Containers, 1)
assert.Equal(t, "500m", predictedDeploy.Spec.Template.Spec.Containers[0].Resources.Requests.Cpu().String())
assert.Equal(t, "512Mi", predictedDeploy.Spec.Template.Spec.Containers[0].Resources.Requests.Memory().String())
assert.Equal(t, "500m", liveDeploy.Spec.Template.Spec.Containers[0].Resources.Requests.Cpu().String())
assert.Equal(t, "512Mi", liveDeploy.Spec.Template.Spec.Containers[0].Resources.Requests.Memory().String())
})

t.Run("will include mutation webhook modifications", func(t *testing.T) {
// given
t.Parallel()
Expand Down
9 changes: 9 additions & 0 deletions pkg/diff/testdata/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ var (
//go:embed smd-deploy-config.yaml
DeploymentConfigYAML string

//go:embed smd-deploy2-live.yaml
Deployment2LiveYAML string

//go:embed smd-deploy2-config.yaml
Deployment2ConfigYAML string

//go:embed smd-deploy2-predicted-live.json
Deployment2PredictedLiveJSONSSD string

// OpenAPIV2Doc is a binary representation of the openapi
// document available in a given k8s instance. To update
// this file the following commands can be executed:
Expand Down
36 changes: 36 additions & 0 deletions pkg/diff/testdata/smd-deploy2-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: missing
applications.argoproj.io/app-name: nginx
something-else: bla
name: nginx-deployment
namespace: default
spec:
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
applications.argoproj.io/app-name: nginx
spec:
containers:
- image: 'nginx:1.23.1'
imagePullPolicy: Never
livenessProbe:
exec:
command:
- cat
- non-existent-file
initialDelaySeconds: 5
periodSeconds: 180
name: nginx
ports:
- containerPort: 8081
protocol: UDP
- containerPort: 80
protocol: TCP
161 changes: 161 additions & 0 deletions pkg/diff/testdata/smd-deploy2-live.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
apiVersion: apps/v1
kind: Deployment
metadata:
annotations:
deployment.kubernetes.io/revision: '1'
creationTimestamp: '2022-09-18T23:50:25Z'
generation: 1
labels:
app: missing
applications.argoproj.io/app-name: nginx
something-else: bla
managedFields:
- apiVersion: apps/v1
fieldsType: FieldsV1
fieldsV1:
'f:metadata':
'f:labels':
'f:app': {}
'f:applications.argoproj.io/app-name': {}
'f:something-else': {}
'f:spec':
'f:replicas': {}
'f:selector': {}
'f:template':
'f:metadata':
'f:labels':
'f:app': {}
'f:applications.argoproj.io/app-name': {}
'f:spec':
'f:containers':
'k:{"name":"nginx"}':
.: {}
'f:image': {}
'f:imagePullPolicy': {}
'f:livenessProbe':
'f:exec':
'f:command': {}
'f:initialDelaySeconds': {}
'f:periodSeconds': {}
'f:name': {}
'f:ports':
'k:{"containerPort":80,"protocol":"TCP"}':
.: {}
'f:containerPort': {}
'f:protocol': {}
'f:resources':
'f:requests':
'f:cpu': {}
'f:memory': {}
manager: argocd-controller
operation: Apply
time: '2022-09-18T23:50:25Z'
- apiVersion: apps/v1
fieldsType: FieldsV1
fieldsV1:
'f:metadata':
'f:annotations':
.: {}
'f:deployment.kubernetes.io/revision': {}
'f:status':
'f:availableReplicas': {}
'f:conditions':
.: {}
'k:{"type":"Available"}':
.: {}
'f:lastTransitionTime': {}
'f:lastUpdateTime': {}
'f:message': {}
'f:reason': {}
'f:status': {}
'f:type': {}
'k:{"type":"Progressing"}':
.: {}
'f:lastTransitionTime': {}
'f:lastUpdateTime': {}
'f:message': {}
'f:reason': {}
'f:status': {}
'f:type': {}
'f:observedGeneration': {}
'f:readyReplicas': {}
'f:replicas': {}
'f:updatedReplicas': {}
manager: kube-controller-manager
operation: Update
subresource: status
time: '2022-09-23T18:30:59Z'
name: nginx-deployment
namespace: default
resourceVersion: '7492752'
uid: 731f7434-d3d9-47fa-b179-d9368a84f7c9
spec:
progressDeadlineSeconds: 600
replicas: 2
revisionHistoryLimit: 10
selector:
matchLabels:
app: nginx
strategy:
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
type: RollingUpdate
template:
metadata:
creationTimestamp: null
labels:
app: nginx
applications.argoproj.io/app-name: nginx
spec:
containers:
- image: 'nginx:1.23.1'
imagePullPolicy: Never
livenessProbe:
exec:
command:
- cat
- non-existent-file
failureThreshold: 3
initialDelaySeconds: 5
periodSeconds: 180
successThreshold: 1
timeoutSeconds: 1
name: nginx
ports:
- containerPort: 80
protocol: TCP
- containerPort: 8080
protocol: TCP
- containerPort: 8081
protocol: UDP
resources:
requests:
memory: 512Mi
cpu: 500m
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
dnsPolicy: ClusterFirst
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
terminationGracePeriodSeconds: 30
status:
availableReplicas: 2
conditions:
- lastTransitionTime: '2022-09-18T23:50:25Z'
lastUpdateTime: '2022-09-18T23:50:26Z'
message: ReplicaSet "nginx-deployment-6d68ff5f86" has successfully progressed.
reason: NewReplicaSetAvailable
status: 'True'
type: Progressing
- lastTransitionTime: '2022-09-23T18:30:59Z'
lastUpdateTime: '2022-09-23T18:30:59Z'
message: Deployment has minimum availability.
reason: MinimumReplicasAvailable
status: 'True'
type: Available
observedGeneration: 1
readyReplicas: 2
replicas: 2
updatedReplicas: 2
Loading

0 comments on commit 8849c3f

Please sign in to comment.