Skip to content

Commit

Permalink
controller: fix flavor apply proper order (#143)
Browse files Browse the repository at this point in the history
* controller: fix flavor apply proper order

* controller: change default flavor apply order

* controller: add more tests for flavor merge
  • Loading branch information
morpheu authored Jan 23, 2024
1 parent 9b286b0 commit 08c1f49
Show file tree
Hide file tree
Showing 2 changed files with 190 additions and 7 deletions.
14 changes: 7 additions & 7 deletions controllers/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
}

Expand All @@ -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
}
Expand Down
183 changes: 183 additions & 0 deletions controllers/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
},
},
},
},
Expand Down Expand Up @@ -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
},
},
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 08c1f49

Please sign in to comment.