From 2aee04e3f8387dc8cfd3c201fd91d03081980c8d Mon Sep 17 00:00:00 2001 From: Florian Bacher Date: Mon, 14 Nov 2022 10:52:39 +0100 Subject: [PATCH] pr review: added function for generating name of workload of an app Signed-off-by: Florian Bacher --- .../api/v1alpha1/keptnappversion_types.go | 4 ++ .../v1alpha1/keptnappversion_types_test.go | 41 +++++++++++++++++++ .../keptnworkloadinstance/controller.go | 2 +- 3 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 operator/api/v1alpha1/keptnappversion_types_test.go diff --git a/operator/api/v1alpha1/keptnappversion_types.go b/operator/api/v1alpha1/keptnappversion_types.go index a7e21a7ae0..0695dbb9f0 100644 --- a/operator/api/v1alpha1/keptnappversion_types.go +++ b/operator/api/v1alpha1/keptnappversion_types.go @@ -355,3 +355,7 @@ func (a KeptnAppVersion) GetSpanAttributes() []attribute.KeyValue { common.AppNamespace.String(a.Namespace), } } + +func (v KeptnAppVersion) GetWorkloadNameOfApp(workloadName string) string { + return fmt.Sprintf("%s-%s", v.Spec.AppName, workloadName) +} diff --git a/operator/api/v1alpha1/keptnappversion_types_test.go b/operator/api/v1alpha1/keptnappversion_types_test.go new file mode 100644 index 0000000000..68f4c47396 --- /dev/null +++ b/operator/api/v1alpha1/keptnappversion_types_test.go @@ -0,0 +1,41 @@ +package v1alpha1 + +import ( + "testing" +) + +func TestKeptnAppVersion_GetWorkloadNameOfApp(t *testing.T) { + type fields struct { + Spec KeptnAppVersionSpec + } + type args struct { + workloadName string + } + tests := []struct { + name string + fields fields + args args + want string + }{ + { + name: "", + fields: fields{ + Spec: KeptnAppVersionSpec{AppName: "my-app"}, + }, + args: args{ + workloadName: "my-workload", + }, + want: "my-app-my-workload", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + v := KeptnAppVersion{ + Spec: tt.fields.Spec, + } + if got := v.GetWorkloadNameOfApp(tt.args.workloadName); got != tt.want { + t.Errorf("GetWorkloadNameOfApp() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/operator/controllers/keptnworkloadinstance/controller.go b/operator/controllers/keptnworkloadinstance/controller.go index 251a08fb1a..9f7dbe773e 100644 --- a/operator/controllers/keptnworkloadinstance/controller.go +++ b/operator/controllers/keptnworkloadinstance/controller.go @@ -299,7 +299,7 @@ func getLatestAppVersion(apps *klcv1alpha1.KeptnAppVersionList, wli *klcv1alpha1 for _, app := range apps.Items { if app.Spec.AppName == wli.Spec.AppName { for _, appWorkload := range app.Spec.Workloads { - if appWorkload.Version == wli.Spec.Version && fmt.Sprintf("%s-%s", app.Spec.AppName, appWorkload.Name) == wli.Spec.WorkloadName { + if appWorkload.Version == wli.Spec.Version && app.GetWorkloadNameOfApp(appWorkload.Name) == wli.Spec.WorkloadName { workloadFound = true newVersion, err := version.NewVersion(app.Spec.Version) if err != nil {