|
| 1 | +package integration |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/samber/lo" |
| 7 | + "github.com/stretchr/testify/require" |
| 8 | + appsv1 "k8s.io/api/apps/v1" |
| 9 | + corev1 "k8s.io/api/core/v1" |
| 10 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 11 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 12 | + |
| 13 | + "github.com/kong/gateway-operator/pkg/consts" |
| 14 | + testutils "github.com/kong/gateway-operator/pkg/utils/test" |
| 15 | + "github.com/kong/gateway-operator/test/helpers" |
| 16 | + |
| 17 | + operatorv1beta1 "github.com/kong/kubernetes-configuration/api/gateway-operator/v1beta1" |
| 18 | +) |
| 19 | + |
| 20 | +// TestDataPlaneScaleSubresource tests the scale subresource of the DataPlane CRD. |
| 21 | +// It verifies that when a deployment is restarted using kubectl rollout restart, |
| 22 | +// the new ReplicaSet maintains the correct replica count. |
| 23 | +func TestDataPlaneScaleSubresource(t *testing.T) { |
| 24 | + t.Parallel() |
| 25 | + |
| 26 | + namespace, cleaner := helpers.SetupTestEnv(t, GetCtx(), GetEnv()) |
| 27 | + clients := GetClients() |
| 28 | + |
| 29 | + t.Log("deploying dataplane resource with 2 replicas") |
| 30 | + dataplane := &operatorv1beta1.DataPlane{ |
| 31 | + ObjectMeta: metav1.ObjectMeta{ |
| 32 | + Namespace: namespace.Name, |
| 33 | + GenerateName: "dataplane-scale-test-", |
| 34 | + }, |
| 35 | + Spec: operatorv1beta1.DataPlaneSpec{ |
| 36 | + DataPlaneOptions: operatorv1beta1.DataPlaneOptions{ |
| 37 | + Deployment: operatorv1beta1.DataPlaneDeploymentOptions{ |
| 38 | + DeploymentOptions: operatorv1beta1.DeploymentOptions{ |
| 39 | + Replicas: lo.ToPtr(int32(2)), // Set initial replica count to 2 |
| 40 | + PodTemplateSpec: &corev1.PodTemplateSpec{ |
| 41 | + ObjectMeta: metav1.ObjectMeta{ |
| 42 | + Labels: map[string]string{ |
| 43 | + "label-a": "value-a", |
| 44 | + "label-x": "value-x", |
| 45 | + }, |
| 46 | + }, |
| 47 | + Spec: corev1.PodSpec{ |
| 48 | + Containers: []corev1.Container{ |
| 49 | + { |
| 50 | + Env: []corev1.EnvVar{ |
| 51 | + { |
| 52 | + Name: "TEST_ENV", |
| 53 | + Value: "test", |
| 54 | + }, |
| 55 | + }, |
| 56 | + Name: consts.DataPlaneProxyContainerName, |
| 57 | + Image: helpers.GetDefaultDataPlaneImage(), |
| 58 | + }, |
| 59 | + }, |
| 60 | + }, |
| 61 | + }, |
| 62 | + }, |
| 63 | + }, |
| 64 | + Network: operatorv1beta1.DataPlaneNetworkOptions{ |
| 65 | + Services: &operatorv1beta1.DataPlaneServices{ |
| 66 | + Ingress: &operatorv1beta1.DataPlaneServiceOptions{ |
| 67 | + ServiceOptions: operatorv1beta1.ServiceOptions{ |
| 68 | + Annotations: map[string]string{ |
| 69 | + "purpose": "scale", |
| 70 | + }, |
| 71 | + }, |
| 72 | + }, |
| 73 | + }, |
| 74 | + }, |
| 75 | + }, |
| 76 | + }, |
| 77 | + } |
| 78 | + |
| 79 | + dataplaneClient := clients.OperatorClient.GatewayOperatorV1beta1().DataPlanes(namespace.Name) |
| 80 | + dataplane, err := dataplaneClient.Create(GetCtx(), dataplane, metav1.CreateOptions{}) |
| 81 | + require.NoError(t, err) |
| 82 | + cleaner.Add(dataplane) |
| 83 | + |
| 84 | + dataplaneName := client.ObjectKeyFromObject(dataplane) |
| 85 | + |
| 86 | + t.Log("verifying dataplane gets marked ready") |
| 87 | + require.Eventually(t, testutils.DataPlaneIsReady(t, GetCtx(), dataplaneName, clients.OperatorClient), testutils.GatewayReadyTimeLimit, testutils.ObjectUpdateTick) |
| 88 | + |
| 89 | + t.Log("verifying dataplane has 2 ready replicas") |
| 90 | + require.Eventually(t, testutils.DataPlaneHasNReadyPods(t, GetCtx(), dataplaneName, clients, 2), testutils.GatewayReadyTimeLimit, testutils.ObjectUpdateTick) |
| 91 | + |
| 92 | + // Get the deployment created by the dataplane controller |
| 93 | + var deployment appsv1.Deployment |
| 94 | + require.Eventually(t, testutils.DataPlaneHasActiveDeployment(t, GetCtx(), dataplaneName, &deployment, client.MatchingLabels{ |
| 95 | + consts.GatewayOperatorManagedByLabel: consts.DataPlaneManagedLabelValue, |
| 96 | + }, clients), testutils.GatewayReadyTimeLimit, testutils.ObjectUpdateTick) |
| 97 | + |
| 98 | + // Test scaling through the scale subresource |
| 99 | + t.Log("testing scaling through scale subresource") |
| 100 | + require.Eventually(t, |
| 101 | + testutils.DataPlaneUpdateEventually(t, GetCtx(), dataplaneName, clients, func(dp *operatorv1beta1.DataPlane) { |
| 102 | + dp.Spec.Deployment.Replicas = lo.ToPtr(int32(3)) // Scale up to 3 replicas |
| 103 | + }), |
| 104 | + testutils.GatewayReadyTimeLimit, testutils.ObjectUpdateTick) |
| 105 | + |
| 106 | + t.Log("verifying dataplane scales to 3 replicas") |
| 107 | + require.Eventually(t, testutils.DataPlaneHasNReadyPods(t, GetCtx(), dataplaneName, clients, 3), testutils.GatewayReadyTimeLimit, testutils.ObjectUpdateTick) |
| 108 | +} |
0 commit comments