Skip to content

Commit

Permalink
fix: container causes
Browse files Browse the repository at this point in the history
Signed-off-by: Vladislav Sukhin <[email protected]>
  • Loading branch information
vsukhin committed Jan 21, 2025
1 parent 6ff21f5 commit b89709b
Showing 1 changed file with 53 additions and 12 deletions.
65 changes: 53 additions & 12 deletions pkg/triggers/diff.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package triggers

import (
"maps"

"github.com/google/go-cmp/cmp"
apps_v1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"

"github.com/kubeshop/testkube-operator/pkg/validation/tests/v1/testtrigger"
)
Expand All @@ -13,26 +16,64 @@ func diffDeployments(old, new *apps_v1.Deployment) []testtrigger.Cause {
if *old.Spec.Replicas != *new.Spec.Replicas {
causes = append(causes, testtrigger.CauseDeploymentScaleUpdate)
}
for _, newContainer := range new.Spec.Template.Spec.Containers {
oldContainer := findContainer(old.Spec.Template.Spec.Containers, newContainer.Name)
if oldContainer == nil {
causes = append(causes, testtrigger.CauseDeploymentContainersModified)
continue
}
if oldContainer.Image != newContainer.Image {
causes = append(causes, testtrigger.CauseDeploymentImageUpdate)
}
if !cmp.Equal(oldContainer.Env, newContainer.Env) {
causes = append(causes, testtrigger.CauseDeploymentEnvUpdate)

containerCauses := append(diffContainers(old.Spec.Template.Spec.InitContainers, new.Spec.Template.Spec.InitContainers),
diffContainers(old.Spec.Template.Spec.Containers, new.Spec.Template.Spec.Containers)...)

unique := make(map[testtrigger.Cause]struct{})
for _, containerCause := range containerCauses {
if _, ok := unique[containerCause]; !ok {
unique[containerCause] = struct{}{}
causes = append(causes, containerCause)
}
break
}

if old.Generation != new.Generation {
causes = append(causes, testtrigger.CauseDeploymentGenerationModified)
}

if old.ResourceVersion != new.ResourceVersion {
causes = append(causes, testtrigger.CauseDeploymentResourceModified)
}

return causes
}

func diffContainers(old, new []corev1.Container) []testtrigger.Cause {
var causes []testtrigger.Cause
oldNames := make(map[string]struct{})
oldContainers := make(map[string]corev1.Container)
for _, o := range old {
oldNames[o.Name] = struct{}{}
oldContainers[o.Name] = o
}

newNames := make(map[string]struct{})
newContainers := make(map[string]corev1.Container)
for _, n := range new {
newNames[n.Name] = struct{}{}
newContainers[n.Name] = n
}

if !maps.Equal(oldNames, newNames) {
causes = append(causes, testtrigger.CauseDeploymentContainersModified)
}

for name, newContainer := range newContainers {
if oldContainer, ok := oldContainers[name]; ok {
if !cmp.Equal(oldContainer, newContainer) {
causes = append(causes, testtrigger.CauseDeploymentContainersModified)
}

if oldContainer.Image != newContainer.Image {
causes = append(causes, testtrigger.CauseDeploymentImageUpdate)
}

if !cmp.Equal(oldContainer.Env, newContainer.Env) {
causes = append(causes, testtrigger.CauseDeploymentEnvUpdate)
}
}
}

return causes
}

0 comments on commit b89709b

Please sign in to comment.