From fdb887376fb31eef07e0ef2fd733b8c9689081a9 Mon Sep 17 00:00:00 2001 From: Paulo Sousa Date: Mon, 22 Jan 2024 11:12:35 -0300 Subject: [PATCH 1/3] controller: fix flavor apply proper order --- controllers/controller.go | 2 +- controllers/controller_test.go | 59 ++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/controllers/controller.go b/controllers/controller.go index cc9f50cf8..3e740da8b 100644 --- a/controllers/controller.go +++ b/controllers/controller.go @@ -212,7 +212,7 @@ func mergeInstanceWithFlavor(instance *v1alpha1.RpaasInstance, flavor v1alpha1.R return nil } - mergedInstanceSpec, err := mergeInstance(*flavor.Spec.InstanceTemplate, instance.Spec) + mergedInstanceSpec, err := mergeInstance(instance.Spec, *flavor.Spec.InstanceTemplate) if err != nil { return err } diff --git a/controllers/controller_test.go b/controllers/controller_test.go index dc53d5607..01ec49c05 100644 --- a/controllers/controller_test.go +++ b/controllers/controller_test.go @@ -590,6 +590,65 @@ func TestReconcileRpaasInstance_getRpaasInstance(t *testing.T) { return i }, }, + + "when there are multiple flavors and last on overrides ingress annotations": { + resources: []runtime.Object{ + &v1alpha1.RpaasFlavor{ + ObjectMeta: metav1.ObjectMeta{ + Name: "flavor-a", + Namespace: "default", + }, + Spec: v1alpha1.RpaasFlavorSpec{ + InstanceTemplate: &v1alpha1.RpaasInstanceSpec{ + Service: &nginxv1alpha1.NginxService{ + Annotations: map[string]string{ + "rpaas.extensions.tsuru.io/custom-annotation-svc": "custom svc annotation", + }, + }, + Ingress: &nginxv1alpha1.NginxIngress{ + Annotations: map[string]string{ + "rpaas.extensions.tsuru.io/custom-annotation-ingress": "foo", + }, + }, + }, + }, + }, + &v1alpha1.RpaasFlavor{ + ObjectMeta: metav1.ObjectMeta{ + Name: "flavor-b", + Namespace: "default", + }, + Spec: v1alpha1.RpaasFlavorSpec{ + InstanceTemplate: &v1alpha1.RpaasInstanceSpec{ + Ingress: &nginxv1alpha1.NginxIngress{ + Annotations: map[string]string{ + "rpaas.extensions.tsuru.io/custom-annotation-ingress": "bar", + "another.example.com/blah": "bleh", + }, + }, + }, + }, + }, + }, + instance: func(i *v1alpha1.RpaasInstance) *v1alpha1.RpaasInstance { + i.Spec.Flavors = []string{"flavor-a", "flavor-b"} + return i + }, + expected: func(i *v1alpha1.RpaasInstance) *v1alpha1.RpaasInstance { + i.Spec.Service = &nginxv1alpha1.NginxService{ + Annotations: map[string]string{ + "rpaas.extensions.tsuru.io/custom-annotation-svc": "custom svc annotation", + }, + } + i.Spec.Ingress = &nginxv1alpha1.NginxIngress{ + Annotations: map[string]string{ + "rpaas.extensions.tsuru.io/custom-annotation-ingress": "bar", + "another.example.com/blah": "bleh", + }, + } + return i + }, + }, } for name, tt := range tests { From 25278c06c4ceef2f89a23ed129a60cadbd84fe5f Mon Sep 17 00:00:00 2001 From: Paulo Sousa Date: Mon, 22 Jan 2024 16:02:58 -0300 Subject: [PATCH 2/3] controller: change default flavor apply order --- controllers/controller.go | 12 ++--- controllers/controller_test.go | 98 ++++++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+), 6 deletions(-) diff --git a/controllers/controller.go b/controllers/controller.go index 3e740da8b..967dea7a0 100644 --- a/controllers/controller.go +++ b/controllers/controller.go @@ -174,6 +174,12 @@ func (r *RpaasInstanceReconciler) mergeInstanceWithFlavors(ctx context.Context, return nil, err } + for _, defaultFlavor := range defaultFlavors { + if err := mergeInstanceWithFlavor(instance, defaultFlavor); err != nil { + return nil, err + } + } + for _, flavorName := range instance.Spec.Flavors { flavorObjectKey := types.NamespacedName{ Name: flavorName, @@ -198,12 +204,6 @@ func (r *RpaasInstanceReconciler) mergeInstanceWithFlavors(ctx context.Context, } } - for _, defaultFlavor := range defaultFlavors { - if err := mergeInstanceWithFlavor(instance, defaultFlavor); err != nil { - return nil, err - } - } - return instance, nil } diff --git a/controllers/controller_test.go b/controllers/controller_test.go index 01ec49c05..53d083a67 100644 --- a/controllers/controller_test.go +++ b/controllers/controller_test.go @@ -649,6 +649,104 @@ func TestReconcileRpaasInstance_getRpaasInstance(t *testing.T) { return i }, }, + + "when there are multiple flavors and one of them is default": { + resources: []runtime.Object{ + &v1alpha1.RpaasFlavor{ + ObjectMeta: metav1.ObjectMeta{ + Name: "default", + Namespace: "default", + }, + Spec: v1alpha1.RpaasFlavorSpec{ + Default: true, + InstanceTemplate: &v1alpha1.RpaasInstanceSpec{ + Service: &nginxv1alpha1.NginxService{ + Annotations: map[string]string{ + "rpaas.extensions.tsuru.io/custom-annotation-svc": "custom svc annotation", + }, + }, + + PodTemplate: nginxv1alpha1.NginxPodTemplateSpec{ + ServiceAccountName: "my-service-account", + Annotations: map[string]string{ + "prometheus.io/path": "/status", + }, + }, + }, + }, + }, + &v1alpha1.RpaasFlavor{ + ObjectMeta: metav1.ObjectMeta{ + Name: "flavor-a", + Namespace: "default", + }, + Spec: v1alpha1.RpaasFlavorSpec{ + InstanceTemplate: &v1alpha1.RpaasInstanceSpec{ + PodTemplate: nginxv1alpha1.NginxPodTemplateSpec{ + Annotations: map[string]string{ + "logging.tsuru.io/sample": "0.5", + }, + }, + }, + }, + }, + &v1alpha1.RpaasFlavor{ + ObjectMeta: metav1.ObjectMeta{ + Name: "flavor-b", + Namespace: "default", + }, + Spec: v1alpha1.RpaasFlavorSpec{ + InstanceTemplate: &v1alpha1.RpaasInstanceSpec{ + PodTemplate: nginxv1alpha1.NginxPodTemplateSpec{ + ServiceAccountName: "flavor-b-service-account", + }, + Ingress: &nginxv1alpha1.NginxIngress{ + Annotations: map[string]string{ + "ingress.tsuru.io": "flavor-b", + }, + }, + }, + }, + }, + &v1alpha1.RpaasFlavor{ + ObjectMeta: metav1.ObjectMeta{ + Name: "flavor-c", + Namespace: "default", + }, + Spec: v1alpha1.RpaasFlavorSpec{ + InstanceTemplate: &v1alpha1.RpaasInstanceSpec{ + PodTemplate: nginxv1alpha1.NginxPodTemplateSpec{ + ServiceAccountName: "flavor-c-service-account", + }, + }, + }, + }, + }, + instance: func(i *v1alpha1.RpaasInstance) *v1alpha1.RpaasInstance { + i.Spec.Flavors = []string{"flavor-a", "flavor-b", "flavor-c"} + return i + }, + expected: func(i *v1alpha1.RpaasInstance) *v1alpha1.RpaasInstance { + i.Spec.Service = &nginxv1alpha1.NginxService{ + Annotations: map[string]string{ + "rpaas.extensions.tsuru.io/custom-annotation-svc": "custom svc annotation", + }, + } + i.Spec.PodTemplate = nginxv1alpha1.NginxPodTemplateSpec{ + ServiceAccountName: "flavor-c-service-account", + Annotations: map[string]string{ + "prometheus.io/path": "/status", + "logging.tsuru.io/sample": "0.5", + }, + } + i.Spec.Ingress = &nginxv1alpha1.NginxIngress{ + Annotations: map[string]string{ + "ingress.tsuru.io": "flavor-b", + }, + } + return i + }, + }, } for name, tt := range tests { From 449357be8f38b896df1d777978b4803bb163d6fc Mon Sep 17 00:00:00 2001 From: Paulo Sousa Date: Mon, 22 Jan 2024 18:23:26 -0300 Subject: [PATCH 3/3] controller: add more tests for flavor merge --- controllers/controller_test.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/controllers/controller_test.go b/controllers/controller_test.go index 53d083a67..d4cb700d4 100644 --- a/controllers/controller_test.go +++ b/controllers/controller_test.go @@ -498,11 +498,17 @@ func TestReconcileRpaasInstance_getRpaasInstance(t *testing.T) { Namespace: "default", }, Spec: v1alpha1.RpaasFlavorSpec{ + Default: true, InstanceTemplate: &v1alpha1.RpaasInstanceSpec{ DNS: &v1alpha1.DNSConfig{ Zone: "apps.example.com", TTL: func(n int32) *int32 { return &n }(300), }, + PodTemplate: nginxv1alpha1.NginxPodTemplateSpec{ + Annotations: map[string]string{ + "rpaas.extensions.tsuru.io/is-default-pod-annotation": "true", + }, + }, }, }, }, @@ -530,6 +536,11 @@ func TestReconcileRpaasInstance_getRpaasInstance(t *testing.T) { Zone: "apps.test", TTL: func(n int32) *int32 { return &n }(30), } + i.Spec.PodTemplate = nginxv1alpha1.NginxPodTemplateSpec{ + Annotations: map[string]string{ + "rpaas.extensions.tsuru.io/is-default-pod-annotation": "true", + }, + } return i }, }, @@ -721,6 +732,21 @@ func TestReconcileRpaasInstance_getRpaasInstance(t *testing.T) { }, }, }, + &v1alpha1.RpaasFlavor{ + ObjectMeta: metav1.ObjectMeta{ + Name: "flavor-d", + Namespace: "default", + }, + Spec: v1alpha1.RpaasFlavorSpec{ + InstanceTemplate: &v1alpha1.RpaasInstanceSpec{ + PodTemplate: nginxv1alpha1.NginxPodTemplateSpec{ + Annotations: map[string]string{ + "donotuse.tsuru.io": "flavor-d", + }, + }, + }, + }, + }, }, instance: func(i *v1alpha1.RpaasInstance) *v1alpha1.RpaasInstance { i.Spec.Flavors = []string{"flavor-a", "flavor-b", "flavor-c"}