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

feat: [TKC-3102] [TKC-3109] generation and resource trigger cause #6124

Merged
merged 6 commits into from
Jan 22, 2025
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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ require (
github.com/keygen-sh/jsonapi-go v1.2.1
github.com/keygen-sh/keygen-go/v3 v3.2.0
github.com/kubepug/kubepug v1.7.1
github.com/kubeshop/testkube-operator v1.17.55-0.20250120143637-7fac440aa56e
github.com/kubeshop/testkube-operator v1.17.55-0.20250122143845-e35b2034b520
github.com/minio/minio-go/v7 v7.0.66
github.com/montanaflynn/stats v0.7.1
github.com/moogar0880/problems v0.1.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -336,8 +336,8 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/kubepug/kubepug v1.7.1 h1:LKhfSxS8Y5mXs50v+3Lpyec+cogErDLcV7CMUuiaisw=
github.com/kubepug/kubepug v1.7.1/go.mod h1:lv+HxD0oTFL7ZWjj0u6HKhMbbTIId3eG7aWIW0gyF8g=
github.com/kubeshop/testkube-operator v1.17.55-0.20250120143637-7fac440aa56e h1:S9FRfe+IdDfbNR81NspX2zPWPNUA6XrnrBSvTdRKgYw=
github.com/kubeshop/testkube-operator v1.17.55-0.20250120143637-7fac440aa56e/go.mod h1:P47tw1nKQFufdsZndyq2HG2MSa0zK/lU0XpRfZtEmIk=
github.com/kubeshop/testkube-operator v1.17.55-0.20250122143845-e35b2034b520 h1:OE0GtE9qeyxBUu6ctfpZbftaUngMt9EjaEcDzDUFOns=
github.com/kubeshop/testkube-operator v1.17.55-0.20250122143845-e35b2034b520/go.mod h1:P47tw1nKQFufdsZndyq2HG2MSa0zK/lU0XpRfZtEmIk=
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
github.com/lithammer/fuzzysearch v1.1.8 h1:/HIuJnjHuXS8bKaiTMeeDlW2/AyIWk2brx1V8LFgLN4=
Expand Down
10 changes: 6 additions & 4 deletions pkg/event/kind/webhook/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,7 @@ func (l *WebhookListener) Notify(event testkube.Event) (result testkube.EventRes

uri, err := l.processTemplate("uri", l.Uri, event)
if err != nil {
err = errors.Wrap(err, "webhook uri encode error")
log.Errorw("webhook uri encode error", "error", err)
log.Errorw("uri template processing error", "error", err)
result = testkube.NewFailedEventResult(event.Id, err)
return
}
Expand All @@ -176,15 +175,17 @@ func (l *WebhookListener) Notify(event testkube.Event) (result testkube.EventRes
var data []byte
data, err = l.processTemplate("payload", l.payloadTemplate, event)
if err != nil {
log.Errorw("payload template processing error", "error", err)
result = testkube.NewFailedEventResult(event.Id, err)
return
}

_, err = body.Write(data)
} else {
// clean envs if not requested explicitly by payload template
event.Envs = nil
err = json.NewEncoder(body).Encode(event)
cleanEvent := event
cleanEvent.Envs = nil
err = json.NewEncoder(body).Encode(cleanEvent)
if err == nil && l.payloadObjectField != "" {
data := map[string]string{l.payloadObjectField: body.String()}
body.Reset()
Expand Down Expand Up @@ -212,6 +213,7 @@ func (l *WebhookListener) Notify(event testkube.Event) (result testkube.EventRes
for i := range values {
data, err := l.processTemplate("header", *values[i], event)
if err != nil {
log.Errorw("header template processing error", "error", err)
result = testkube.NewFailedEventResult(event.Id, err)
return
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/keymap/triggers/triggers.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ func getSupportedEvents() map[string][]string {
string(testtrigger.CauseDeploymentImageUpdate),
string(testtrigger.CauseDeploymentScaleUpdate),
string(testtrigger.CauseDeploymentEnvUpdate),
string(testtrigger.CauseDeploymentGenerationModified),
string(testtrigger.CauseDeploymentResourceModified),
}
m[testtrigger.ResourceStatefulSet] = []string{string(testtrigger.EventCreated), string(testtrigger.EventModified), string(testtrigger.EventDeleted)}
m[testtrigger.ResourceDaemonSet] = []string{string(testtrigger.EventCreated), string(testtrigger.EventModified), string(testtrigger.EventDeleted)}
Expand Down
61 changes: 50 additions & 11 deletions pkg/triggers/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package triggers
import (
"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,19 +14,57 @@ 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)

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)
}
if !cmp.Equal(oldContainer.Env, newContainer.Env) {
causes = append(causes, testtrigger.CauseDeploymentEnvUpdate)
}

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

oldContainers := make(map[string]corev1.Container)
for _, o := range old {
oldContainers[o.Name] = o
}

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

if !cmp.Equal(oldContainers, newContainers) {
causes = append(causes, testtrigger.CauseDeploymentContainersModified)
}

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

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

return causes
}
Loading