From 08c1f49626cd5b39ac10fbd95efc8985f7102c56 Mon Sep 17 00:00:00 2001 From: Paulo Sousa Date: Tue, 23 Jan 2024 09:37:44 -0300 Subject: [PATCH] controller: fix flavor apply proper order (#143) * controller: fix flavor apply proper order * controller: change default flavor apply order * controller: add more tests for flavor merge --- controllers/controller.go | 14 +-- controllers/controller_test.go | 183 +++++++++++++++++++++++++++++++++ 2 files changed, 190 insertions(+), 7 deletions(-) diff --git a/controllers/controller.go b/controllers/controller.go index cc9f50cf8..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 } @@ -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..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 }, }, @@ -590,6 +601,178 @@ 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 + }, + }, + + "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", + }, + }, + }, + }, + &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"} + 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 {