Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix stop/start bug with HPA enabled components #905

Merged
merged 1 commit into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions charts/radix-operator/Chart.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
apiVersion: v2
name: radix-operator
version: 1.20.3
appVersion: 1.40.3
version: 1.20.4
appVersion: 1.40.4
kubeVersion: ">=1.24.0"
description: Radix Operator
keywords:
Expand Down
105 changes: 46 additions & 59 deletions pkg/apis/deployment/deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1603,6 +1603,52 @@ func TestObjectSynced_DeploymentReplicasFromCurrentDeploymentWhenHPAEnabled(t *t
assert.Equal(t, int32(1), *comp1.Spec.Replicas)
}

func TestObjectSynced_StopAndStartDeploymentWhenHPAEnabled(t *testing.T) {
tu, client, kubeUtil, radixclient, prometheusclient, _ := setupTest()
defer teardownTest()
envNamespace := utils.GetEnvironmentNamespace("anyapp", "test")

// Initial sync creating deployments should use replicas from spec
_, err := applyDeploymentWithSync(tu, client, kubeUtil, radixclient, prometheusclient, utils.ARadixDeployment().
WithDeploymentName("deployment1").
WithAppName("anyapp").
WithEnvironment("test").
WithComponents(
utils.NewDeployComponentBuilder().WithName("comp1").WithReplicas(pointers.Ptr(1)).WithHorizontalScaling(pointers.Ptr(int32(2)), int32(4), nil, nil),
))
require.NoError(t, err)

comp1, _ := client.AppsV1().Deployments(envNamespace).Get(context.TODO(), "comp1", metav1.GetOptions{})
assert.Equal(t, int32(2), *comp1.Spec.Replicas)

// Resync existing RD with replicas 0 (stop) should set deployment replicas to 0
err = applyDeploymentUpdateWithSync(tu, client, kubeUtil, radixclient, prometheusclient, utils.ARadixDeployment().
WithDeploymentName("deployment1").
WithAppName("anyapp").
WithEnvironment("test").
WithComponents(
utils.NewDeployComponentBuilder().WithName("comp1").WithReplicas(pointers.Ptr(0)).WithHorizontalScaling(pointers.Ptr(int32(1)), int32(4), nil, nil),
))
require.NoError(t, err)

comp1, _ = client.AppsV1().Deployments(envNamespace).Get(context.TODO(), "comp1", metav1.GetOptions{})
assert.Equal(t, int32(0), *comp1.Spec.Replicas)

// Resync existing RD with replicas set back to original value (start) should use replicas from spec
err = applyDeploymentUpdateWithSync(tu, client, kubeUtil, radixclient, prometheusclient, utils.ARadixDeployment().
WithDeploymentName("deployment1").
WithAppName("anyapp").
WithEnvironment("test").
WithComponents(
utils.NewDeployComponentBuilder().WithName("comp1").WithReplicas(pointers.Ptr(1)).WithHorizontalScaling(pointers.Ptr(int32(2)), int32(4), nil, nil),
))
require.NoError(t, err)

comp1, _ = client.AppsV1().Deployments(envNamespace).Get(context.TODO(), "comp1", metav1.GetOptions{})
assert.Equal(t, int32(2), *comp1.Spec.Replicas)

}

func TestObjectSynced_DeploymentRevisionHistoryLimit(t *testing.T) {
tu, client, kubeUtil, radixclient, prometheusclient, _ := setupTest()
defer teardownTest()
Expand Down Expand Up @@ -2575,65 +2621,6 @@ func TestHistoryLimit_IsBroken_FixedAmountOfDeployments(t *testing.T) {
teardownTest()
}

// func TestObjectUpdated_WithIngressConfig_AnnotationIsPutOnIngresses(t *testing.T) {
// tu, client, kubeUtil, radixclient, prometheusclient, _ := setupTest()
//
// // Setup
// client.CoreV1().ConfigMaps(corev1.NamespaceDefault).Create(
// context.TODO(),
// &corev1.ConfigMap{
// ObjectMeta: metav1.ObjectMeta{
// Name: ingressConfigurationMap,
// Namespace: corev1.NamespaceDefault,
// },
// Data: map[string]string{
// "ingressConfiguration": testIngressConfiguration,
// },
// },
// metav1.CreateOptions{})
//
// os.Setenv(defaults.ActiveClusternameEnvironmentVariable, clusterName)
// applyDeploymentWithSync(tu, client, kubeUtil, radixclient, prometheusclient, utils.ARadixDeployment().
// WithAppName("any-app").
// WithEnvironment("dev").
// WithComponents(
// utils.NewDeployComponentBuilder().
// WithName("frontend").
// WithPort("http", 8080).
// WithPublicPort("http").
// WithDNSAppAlias(true).
// WithIngressConfiguration("non-existing")))
//
// applyDeploymentWithSync(tu, client, kubeUtil, radixclient, prometheusclient, utils.ARadixDeployment().
// WithAppName("any-app-2").
// WithEnvironment("dev").
// WithComponents(
// utils.NewDeployComponentBuilder().
// WithName("frontend").
// WithPort("http", 8080).
// WithPublicPort("http").
// WithDNSAppAlias(true).
// WithIngressConfiguration("socket")))
//
// // Test
// ingresses, _ := client.NetworkingV1().Ingresses(utils.GetEnvironmentNamespace("any-app", "dev")).List(context.TODO(), metav1.ListOptions{})
// appAliasIngress := getIngressByName("any-app-url-alias", ingresses)
// clusterSpecificIngress := getIngressByName("frontend", ingresses)
// activeClusterIngress := getIngressByName("frontend-active-cluster-url-alias", ingresses)
// assert.Equal(t, 1, len(appAliasIngress.ObjectMeta.Annotations))
// assert.Equal(t, 1, len(clusterSpecificIngress.ObjectMeta.Annotations))
// assert.Equal(t, 1, len(activeClusterIngress.ObjectMeta.Annotations))
//
// ingresses, _ = client.NetworkingV1().Ingresses(utils.GetEnvironmentNamespace("any-app-2", "dev")).List(context.TODO(), metav1.ListOptions{})
// appAliasIngress = getIngressByName("any-app-2-url-alias", ingresses)
// clusterSpecificIngress = getIngressByName("frontend", ingresses)
// activeClusterIngress = getIngressByName("frontend-active-cluster-url-alias", ingresses)
// assert.Equal(t, 4, len(appAliasIngress.ObjectMeta.Annotations))
// assert.Equal(t, 4, len(clusterSpecificIngress.ObjectMeta.Annotations))
// assert.Equal(t, 4, len(activeClusterIngress.ObjectMeta.Annotations))
//
// }

func TestHPAConfig(t *testing.T) {
tu, client, kubeUtil, radixclient, prometheusclient, _ := setupTest()
defer teardownTest()
Expand Down
9 changes: 6 additions & 3 deletions pkg/apis/deployment/kubedeployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,12 @@ func (deploy *Deployment) getDesiredUpdatedDeploymentConfig(deployComponent v1.R
err := deploy.setDesiredDeploymentProperties(deployComponent, desiredDeployment)

// When HPA is enabled for a component, the HPA controller will scale the Deployment up/down by changing Replicas
// We must keep this value
if hs := deployComponent.GetHorizontalScaling(); hs != nil {
desiredDeployment.Spec.Replicas = currentDeployment.Spec.Replicas
// We must keep this value as long as replicas >= 0.
// Current replicas will be 0 if the component was previously stopped (replicas set explicitly to 0)
if hs := deployComponent.GetHorizontalScaling(); hs != nil && !isComponentStopped(deployComponent) {
if replicas := currentDeployment.Spec.Replicas; replicas != nil && *replicas > 0 {
desiredDeployment.Spec.Replicas = currentDeployment.Spec.Replicas
}
}

return desiredDeployment, err
Expand Down