Skip to content

Commit

Permalink
Fix validation of flavor with creationOnly option
Browse files Browse the repository at this point in the history
  • Loading branch information
wpjunior committed Jan 19, 2024
1 parent 80079da commit 2f8a538
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
3 changes: 2 additions & 1 deletion internal/pkg/rpaas/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -1259,6 +1259,7 @@ func (m *k8sRpaasManager) validatePlan(ctx context.Context, updatedPlan string)
}

func (m *k8sRpaasManager) validateFlavors(ctx context.Context, instance *v1alpha1.RpaasInstance, flavors []string) error {
isCreation := instance == nil
encountered := map[string]struct{}{}
for _, f := range flavors {
if _, duplicated := encountered[f]; duplicated {
Expand All @@ -1285,7 +1286,7 @@ func (m *k8sRpaasManager) validateFlavors(ctx context.Context, instance *v1alpha
return &ValidationError{Msg: fmt.Sprintf("flavor %q not found", f)}
}

if flavorObj.Spec.CreationOnly {
if flavorObj.Spec.CreationOnly && !isCreation {
return &ValidationError{Msg: fmt.Sprintf("flavor %q can used only in the creation of instance", f)}
}
}
Expand Down
60 changes: 60 additions & 0 deletions internal/pkg/rpaas/k8s_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2563,6 +2563,16 @@ func Test_k8sRpaasManager_CreateInstance(t *testing.T) {
Description: "aaaaa",
},
},
&v1alpha1.RpaasFlavor{
ObjectMeta: metav1.ObjectMeta{
Name: "feature-create-only",
Namespace: getServiceName(),
},
Spec: v1alpha1.RpaasFlavorSpec{
CreationOnly: true,
Description: "aaaaa",
},
},
}
one := int32(1)
tests := []struct {
Expand Down Expand Up @@ -2989,6 +2999,56 @@ func Test_k8sRpaasManager_CreateInstance(t *testing.T) {
},
},
},
{
name: "with flavor allowed just in creation",
args: CreateArgs{Name: "r1", Team: "t1", Parameters: map[string]interface{}{"flavors": map[string]interface{}{"0": "feature-create-only"}}},
expected: v1alpha1.RpaasInstance{
TypeMeta: metav1.TypeMeta{
Kind: "RpaasInstance",
APIVersion: "extensions.tsuru.io/v1alpha1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "r1",
Namespace: "rpaasv2",
ResourceVersion: "1",
Annotations: map[string]string{
"rpaas.extensions.tsuru.io/tags": "",
"rpaas.extensions.tsuru.io/description": "",
"rpaas.extensions.tsuru.io/team-owner": "t1",
},
Labels: map[string]string{
"rpaas.extensions.tsuru.io/service-name": "rpaasv2",
"rpaas.extensions.tsuru.io/instance-name": "r1",
"rpaas.extensions.tsuru.io/team-owner": "t1",
"rpaas_service": "rpaasv2",
"rpaas_instance": "r1",
},
},
Spec: v1alpha1.RpaasInstanceSpec{
Replicas: &one,
PlanName: "plan1",
Flavors: []string{"feature-create-only"},
Service: &nginxv1alpha1.NginxService{
Labels: map[string]string{
"rpaas.extensions.tsuru.io/service-name": "rpaasv2",
"rpaas.extensions.tsuru.io/instance-name": "r1",
"rpaas.extensions.tsuru.io/team-owner": "t1",
"rpaas_service": "rpaasv2",
"rpaas_instance": "r1",
},
},
PodTemplate: nginxv1alpha1.NginxPodTemplateSpec{
Labels: map[string]string{
"rpaas.extensions.tsuru.io/service-name": "rpaasv2",
"rpaas.extensions.tsuru.io/instance-name": "r1",
"rpaas.extensions.tsuru.io/team-owner": "t1",
"rpaas_service": "rpaasv2",
"rpaas_instance": "r1",
},
},
},
},
},
{
name: "with team affinity",
args: CreateArgs{Name: "r1", Team: "team-one"},
Expand Down

0 comments on commit 2f8a538

Please sign in to comment.