Skip to content

Commit

Permalink
Add validation to prevent to unset creationOnly flavor
Browse files Browse the repository at this point in the history
  • Loading branch information
wpjunior committed Sep 6, 2021
1 parent 67654bc commit 92beec6
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 9 deletions.
33 changes: 25 additions & 8 deletions internal/pkg/rpaas/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,14 +269,15 @@ func (m *k8sRpaasManager) CreateInstance(ctx context.Context, args CreateArgs) e
}

func (m *k8sRpaasManager) UpdateInstance(ctx context.Context, instanceName string, args UpdateInstanceArgs) error {
if err := m.validateUpdateInstanceArgs(ctx, args); err != nil {
instance, err := m.GetInstance(ctx, instanceName)
if err != nil {
return err
}

instance, err := m.GetInstance(ctx, instanceName)
if err != nil {
if err := m.validateUpdateInstanceArgs(ctx, instance, args); err != nil {
return err
}

originalInstance := instance.DeepCopy()
if args.Plan != "" && args.Plan != instance.Spec.PlanName {
plan, err := m.getPlan(ctx, args.Plan)
Expand Down Expand Up @@ -1317,22 +1318,22 @@ func (m *k8sRpaasManager) validateCreate(ctx context.Context, args CreateArgs) e
return ConflictError{Msg: fmt.Sprintf("rpaas instance named %q already exists", args.Name)}
}

if err = m.validateFlavors(ctx, args.Flavors(), true); err != nil {
if err = m.validateFlavors(ctx, nil, args.Flavors()); err != nil {
return err
}

return nil
}

func (m *k8sRpaasManager) validateUpdateInstanceArgs(ctx context.Context, args UpdateInstanceArgs) error {
if err := m.validateFlavors(ctx, args.Flavors(), false); err != nil {
func (m *k8sRpaasManager) validateUpdateInstanceArgs(ctx context.Context, instance *v1alpha1.RpaasInstance, args UpdateInstanceArgs) error {
if err := m.validateFlavors(ctx, instance, args.Flavors()); err != nil {
return err
}

return nil
}

func (m *k8sRpaasManager) validateFlavors(ctx context.Context, flavors []string, creation bool) error {
func (m *k8sRpaasManager) validateFlavors(ctx context.Context, instance *v1alpha1.RpaasInstance, flavors []string) error {
encountered := map[string]bool{}
allFlavors, err := m.getFlavors(ctx)
if err != nil {
Expand All @@ -1345,7 +1346,7 @@ func (m *k8sRpaasManager) validateFlavors(ctx context.Context, flavors []string,
return ValidationError{Msg: fmt.Sprintf("flavor %q not found", f)}
}

if flavorObj.Spec.CreationOnly && !creation {
if flavorObj.Spec.CreationOnly && instance != nil {
return ValidationError{Msg: fmt.Sprintf("flavor %q can used only in the creation of instance", f)}
}

Expand All @@ -1356,6 +1357,22 @@ func (m *k8sRpaasManager) validateFlavors(ctx context.Context, flavors []string,
encountered[f] = true
}

if instance == nil {
return nil
}

for _, f := range instance.Spec.Flavors {
flavorObj := m.selectFlavor(ctx, allFlavors, f)

if flavorObj == nil {
continue
}

if flavorObj.Spec.CreationOnly && !encountered[f] {
return ValidationError{Msg: fmt.Sprintf("flavor %q can unset, cause it is a creation only flavor", f)}
}
}

return nil
}

Expand Down
25 changes: 24 additions & 1 deletion internal/pkg/rpaas/k8s_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3607,6 +3607,12 @@ func Test_k8sRpaasManager_UpdateInstance(t *testing.T) {
}
instance1.Spec.PlanName = "plan1"

instance2 := newEmptyRpaasInstance()
instance2.Name = "instance2"
instance2.Labels = labelsForRpaasInstance(instance1.Name)
instance2.Spec.PlanName = "plan1"
instance2.Spec.Flavors = []string{"feature-create-only"}

podLabels := mergeMap(instance1.Labels, map[string]string{"pod-label-1": "v1"})

instance1.Spec.PodTemplate = nginxv1alpha1.NginxPodTemplateSpec{
Expand Down Expand Up @@ -3647,7 +3653,7 @@ func Test_k8sRpaasManager_UpdateInstance(t *testing.T) {
},
}

resources := []runtime.Object{instance1, plan1, plan2, creationOnlyFlavor}
resources := []runtime.Object{instance1, instance2, plan1, plan2, creationOnlyFlavor}

tests := []struct {
name string
Expand Down Expand Up @@ -3685,6 +3691,23 @@ func Test_k8sRpaasManager_UpdateInstance(t *testing.T) {
assert.Equal(t, `flavor "feature-create-only" can used only in the creation of instance`, err.Error())
},
},
{
name: "when tries to remove a creationOnly flavor",
instance: "instance2",
args: UpdateInstanceArgs{
Description: "Another description",
Plan: "plan2",
Tags: []string{},
Team: "team-two",
Parameters: map[string]interface{}{
"lb-name": "my-instance.example",
},
},
assertion: func(t *testing.T, err error, instance *v1alpha1.RpaasInstance) {
require.Error(t, err)
assert.Equal(t, `flavor "feature-create-only" can unset, cause it is a creation only flavor`, err.Error())
},
},
{
name: "when successfully updating an instance",
instance: "instance1",
Expand Down

0 comments on commit 92beec6

Please sign in to comment.