Skip to content

Commit

Permalink
Avoid use of scale when autoscale is enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
wpjunior committed Nov 4, 2021
1 parent a337495 commit 639377d
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
5 changes: 5 additions & 0 deletions internal/pkg/rpaas/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,11 @@ func (m *k8sRpaasManager) Scale(ctx context.Context, instanceName string, replic
if err != nil {
return err
}

if instance.Spec.Autoscale != nil {
return ValidationError{Msg: "cannot scale manual with autoscaler configured, please update autoscale settings"}
}

originalInstance := instance.DeepCopy()
if replicas < 0 {
return ValidationError{Msg: fmt.Sprintf("invalid replicas number: %d", replicas)}
Expand Down
68 changes: 68 additions & 0 deletions internal/pkg/rpaas/k8s_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3959,6 +3959,74 @@ func Test_k8sRpaasManager_UpdateAutoscale(t *testing.T) {
}
}

func Test_k8sRpaasManager_Scale(t *testing.T) {
scheme := runtime.NewScheme()
corev1.AddToScheme(scheme)
v1alpha1.SchemeBuilder.AddToScheme(scheme)
nginxv1alpha1.SchemeBuilder.AddToScheme(scheme)

instance1 := newEmptyRpaasInstance()
instance1.Spec.Autoscale = &v1alpha1.RpaasInstanceAutoscaleSpec{
MaxReplicas: 3,
MinReplicas: pointerToInt32(1),
TargetCPUUtilizationPercentage: pointerToInt32(70),
}

instance2 := newEmptyRpaasInstance()
instance2.Name = "another-instance"
instance2.Spec.Autoscale = nil

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

testCases := []struct {
instance string
assertion func(*testing.T, error, *k8sRpaasManager)
}{
{
instance: "my-invalid-instance",
assertion: func(t *testing.T, err error, m *k8sRpaasManager) {
assert.Error(t, NotFoundError{
Msg: `instance not found`,
}, err)
},
},
{
instance: "my-instance",
assertion: func(t *testing.T, err error, m *k8sRpaasManager) {
assert.Error(t, err)
assert.Equal(t, "cannot scale manual with autoscaler configured, please update autoscale settings", err.Error())

instance := v1alpha1.RpaasInstance{}
err = m.cli.Get(context.Background(), types.NamespacedName{Name: "my-instance", Namespace: getServiceName()}, &instance)
require.NoError(t, err)

assert.NotNil(t, instance.Spec.Autoscale)
},
},
{
instance: "another-instance",
assertion: func(t *testing.T, err error, m *k8sRpaasManager) {
assert.NoError(t, err)

instance := v1alpha1.RpaasInstance{}
err = m.cli.Get(context.Background(), types.NamespacedName{Name: "another-instance", Namespace: getServiceName()}, &instance)
require.NoError(t, err)

assert.Equal(t, int32(30), *instance.Spec.Replicas)
},
},
}

for _, tt := range testCases {
t.Run(tt.instance, func(t *testing.T) {
manager := &k8sRpaasManager{cli: fake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(resources...).Build()}
err := manager.Scale(context.Background(), tt.instance, 30)
tt.assertion(t, err, manager)
})
}

}

func Test_k8sRpaasManager_DeleteAutoscale(t *testing.T) {
scheme := runtime.NewScheme()
corev1.AddToScheme(scheme)
Expand Down

0 comments on commit 639377d

Please sign in to comment.