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

Fix the kubernetes manifest diff #5154

Merged
merged 7 commits into from
Aug 22, 2024
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
13 changes: 10 additions & 3 deletions pkg/app/piped/platformprovider/kubernetes/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,20 @@
}

key := old.Key.String()
normalized, err := remarshal(new.u)

normalizedOld, err := remarshal(old.u)
if err != nil {
logger.Info("compare manifests directly since it was unable to remarshal old Kubernetes manifest to normalize special fields", zap.Error(err))
return diff.DiffUnstructureds(*old.u, *new.u, key, opts...)
}

normalizedNew, err := remarshal(new.u)
if err != nil {
logger.Info("compare manifests directly since it was unable to remarshal Kubernetes manifest to normalize special fields", zap.Error(err))
logger.Info("compare manifests directly since it was unable to remarshal new Kubernetes manifest to normalize special fields", zap.Error(err))

Check warning on line 72 in pkg/app/piped/platformprovider/kubernetes/diff.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/piped/platformprovider/kubernetes/diff.go#L72

Added line #L72 was not covered by tests
return diff.DiffUnstructureds(*old.u, *new.u, key, opts...)
}

return diff.DiffUnstructureds(*old.u, *normalized, key, opts...)
return diff.DiffUnstructureds(*normalizedOld, *normalizedNew, key, opts...)
}

func DiffList(olds, news []Manifest, logger *zap.Logger, opts ...diff.Option) (*DiffListResult, error) {
Expand Down
165 changes: 165 additions & 0 deletions pkg/app/piped/platformprovider/kubernetes/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,3 +405,168 @@ spec:
})
}
}

func TestNoDiff(t *testing.T) {
t.Parallel()

testcases := []struct {
name string
manifest string
}{
{
name: "limits.memory 1.5Gi",
manifest: `apiVersion: apps/v1
kind: Deployment
metadata:
name: simple
spec:
template:
spec:
containers:
- image: ghcr.io/pipe-cd/helloworld:v0.32.0
name: helloworld
resources:
limits:
memory: 1.5Gi`,
},
{
name: "limits.cpu 1.5",
manifest: `apiVersion: apps/v1
kind: Deployment
metadata:
name: simple
spec:
template:
spec:
containers:
- image: ghcr.io/pipe-cd/helloworld:v0.32.0
name: helloworld
resources:
limits:
cpu: "1.5"`,
},
{
name: "limits.memory 1Gi",
manifest: `apiVersion: apps/v1
kind: Deployment
metadata:
name: simple
spec:
template:
spec:
containers:
- image: ghcr.io/pipe-cd/helloworld:v0.32.0
name: helloworld
resources:
limits:
memory: 1Gi`,
},
{
name: "limits.cpu 1",
manifest: `apiVersion: apps/v1
kind: Deployment
metadata:
name: simple
spec:
template:
spec:
containers:
- image: ghcr.io/pipe-cd/helloworld:v0.32.0
name: helloworld
resources:
limits:
cpu: "1"`,
},
{
name: "requests.memory 1.5Gi",
manifest: `apiVersion: apps/v1
kind: Deployment
metadata:
name: simple
spec:
template:
spec:
containers:
- image: ghcr.io/pipe-cd/helloworld:v0.32.0
name: helloworld
resources:
requests:
memory: 1.5Gi`,
},
{
name: "requests.cpu 1.5",
manifest: `apiVersion: apps/v1
kind: Deployment
metadata:
name: simple
spec:
template:
spec:
containers:
- image: ghcr.io/pipe-cd/helloworld:v0.32.0
name: helloworld
resources:
requests:
cpu: "1.5"`,
},
{
name: "requests.memory 1Gi",
manifest: `apiVersion: apps/v1
kind: Deployment
metadata:
name: simple
spec:
template:
spec:
containers:
- image: ghcr.io/pipe-cd/helloworld:v0.32.0
name: helloworld
resources:
requests:
memory: 1Gi`,
},
{
name: "requests.cpu 1",
manifest: `apiVersion: apps/v1
kind: Deployment
metadata:
name: simple
spec:
template:
spec:
containers:
- image: ghcr.io/pipe-cd/helloworld:v0.32.0
name: helloworld
resources:
requests:
cpu: "1"`,
},
}

for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
manifests, err := ParseManifests(tc.manifest)
require.NoError(t, err)

result, err := DiffList(manifests, manifests, zap.NewNop(), diff.WithEquateEmpty(), diff.WithIgnoreAddingMapKeys(), diff.WithCompareNumberAndNumericString())
require.NoError(t, err)

assert.True(t, result.NoChange())
for _, change := range result.Changes {
t.Log(change.Old.Key, change.New.Key)
for _, node := range change.Diff.Nodes() {
t.Log(node.PathString)
t.Log(node.ValueX)
t.Log(node.ValueY)
t.Log("---")
}
}
for _, add := range result.Adds {
t.Log(add.Key)
}
for _, delete := range result.Deletes {
t.Log(delete.Key)
}
Comment on lines +555 to +569
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[ask] This codes are for checking the diff when result.NoChange() is false?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, and these are logs for debugging, not assertions.

})
}
}
Loading