|
| 1 | +package integration |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/google/uuid" |
| 7 | + testutils "github.com/kong/kong-operator/pkg/utils/test" |
| 8 | + "github.com/kong/kong-operator/test" |
| 9 | + "github.com/kong/kong-operator/test/helpers" |
| 10 | + "github.com/kong/kong-operator/test/helpers/deploy" |
| 11 | + konnectv1alpha1 "github.com/kong/kubernetes-configuration/v2/api/konnect/v1alpha1" |
| 12 | + konnectv1alpha2 "github.com/kong/kubernetes-configuration/v2/api/konnect/v1alpha2" |
| 13 | + "github.com/stretchr/testify/assert" |
| 14 | + "github.com/stretchr/testify/require" |
| 15 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 16 | + k8stypes "k8s.io/apimachinery/pkg/types" |
| 17 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 18 | +) |
| 19 | + |
| 20 | +func TestKonnectExtensionKonnectControlPlaneNotFound(t *testing.T) { |
| 21 | + ns, _ := helpers.SetupTestEnv(t, GetCtx(), GetEnv()) |
| 22 | + |
| 23 | + // Let's generate a unique test ID that we can refer to in Konnect entities. |
| 24 | + // Using only the first 8 characters of the UUID to keep the ID short enough for Konnect to accept it as a part |
| 25 | + // of an entity name. |
| 26 | + testID := uuid.NewString()[:8] |
| 27 | + t.Logf("Running Konnect extensions test with ID: %s", testID) |
| 28 | + |
| 29 | + // Create an APIAuth for test. |
| 30 | + clientNamespaced := client.NewNamespacedClient(GetClients().MgrClient, ns.Name) |
| 31 | + |
| 32 | + konnectExtension := deploy.KonnectExtension( |
| 33 | + t, ctx, clientNamespaced, |
| 34 | + deploy.WithKonnectExtensionKonnectNamespacedRefControlPlaneRef(&konnectv1alpha2.KonnectGatewayControlPlane{ |
| 35 | + ObjectMeta: metav1.ObjectMeta{ |
| 36 | + Name: "controlplane-not-found", |
| 37 | + Namespace: ns.Name, |
| 38 | + }, |
| 39 | + }), |
| 40 | + ) |
| 41 | + |
| 42 | + t.Logf("Waiting for KonnectExtension %s/%s to have expected conditions set to False", konnectExtension.Namespace, konnectExtension.Name) |
| 43 | + require.EventuallyWithT(t, func(t *assert.CollectT) { |
| 44 | + ok, msg := checkKonnectExtensionConditions(t, |
| 45 | + konnectExtension, |
| 46 | + helpers.CheckAllConditionsFalse, |
| 47 | + konnectv1alpha1.ControlPlaneRefValidConditionType, |
| 48 | + konnectv1alpha2.KonnectExtensionReadyConditionType) |
| 49 | + assert.Truef(t, ok, "condition check failed: %s, conditions: %+v", msg, konnectExtension.Status.Conditions) |
| 50 | + }, testutils.ObjectUpdateTimeout, testutils.ObjectUpdateTick) |
| 51 | +} |
| 52 | + |
| 53 | +func TestKonnectExtensionControlPlaneRotation(t *testing.T) { |
| 54 | + t.Skip("TODO: adapt to ControlPlane v2alpha1 https://github.com/kong/kong-operator/issues/1730") |
| 55 | + |
| 56 | + ns, _ := helpers.SetupTestEnv(t, GetCtx(), GetEnv()) |
| 57 | + |
| 58 | + // Let's generate a unique test ID that we can refer to in Konnect entities. |
| 59 | + // Using only the first 8 characters of the UUID to keep the ID short enough for Konnect to accept it as a part |
| 60 | + // of an entity name. |
| 61 | + testID := uuid.NewString()[:8] |
| 62 | + t.Logf("Running Konnect extensions test with ID: %s", testID) |
| 63 | + |
| 64 | + // Create an APIAuth for test. |
| 65 | + clientNamespaced := client.NewNamespacedClient(GetClients().MgrClient, ns.Name) |
| 66 | + |
| 67 | + authCfg := deploy.KonnectAPIAuthConfiguration(t, GetCtx(), clientNamespaced, |
| 68 | + deploy.WithTestIDLabel(testID), |
| 69 | + func(obj client.Object) { |
| 70 | + authCfg := obj.(*konnectv1alpha1.KonnectAPIAuthConfiguration) |
| 71 | + authCfg.Spec.Type = konnectv1alpha1.KonnectAPIAuthTypeToken |
| 72 | + authCfg.Spec.Token = test.KonnectAccessToken() |
| 73 | + authCfg.Spec.ServerURL = test.KonnectServerURL() |
| 74 | + }, |
| 75 | + ) |
| 76 | + |
| 77 | + // Create a Konnect control plane for the KonnectExtension to attach to. |
| 78 | + cp := deploy.KonnectGatewayControlPlane(t, GetCtx(), clientNamespaced, authCfg, |
| 79 | + deploy.WithTestIDLabel(testID), |
| 80 | + ) |
| 81 | + |
| 82 | + t.Logf("Waiting for Konnect ID to be assigned to ControlPlane %s/%s", cp.Namespace, cp.Name) |
| 83 | + require.EventuallyWithT(t, func(t *assert.CollectT) { |
| 84 | + err := GetClients().MgrClient.Get(GetCtx(), k8stypes.NamespacedName{Name: cp.Name, Namespace: cp.Namespace}, cp) |
| 85 | + require.NoError(t, err) |
| 86 | + assertKonnectEntityProgrammed(t, cp) |
| 87 | + }, testutils.ObjectUpdateTimeout, testutils.ObjectUpdateTick) |
| 88 | + |
| 89 | + konnectExtension := deploy.KonnectExtension( |
| 90 | + t, ctx, clientNamespaced, |
| 91 | + deploy.WithKonnectExtensionKonnectNamespacedRefControlPlaneRef(cp), |
| 92 | + ) |
| 93 | + |
| 94 | + t.Logf("Waiting for KonnectExtension %s/%s to have expected conditions set to True", konnectExtension.Namespace, konnectExtension.Name) |
| 95 | + require.EventuallyWithT(t, func(t *assert.CollectT) { |
| 96 | + ok, msg := checkKonnectExtensionConditions(t, |
| 97 | + konnectExtension, |
| 98 | + helpers.CheckAllConditionsTrue, |
| 99 | + konnectv1alpha1.ControlPlaneRefValidConditionType, |
| 100 | + konnectv1alpha1.DataPlaneCertificateProvisionedConditionType, |
| 101 | + konnectv1alpha2.KonnectExtensionReadyConditionType) |
| 102 | + assert.Truef(t, ok, "condition check failed: %s, conditions: %+v", msg, konnectExtension.Status.Conditions) |
| 103 | + }, testutils.ObjectUpdateTimeout, testutils.ObjectUpdateTick) |
| 104 | + |
| 105 | + t.Logf("waiting for status.konnect and status.dataPlaneClientAuth to be set for KonnectExtension %s/%s", konnectExtension.Namespace, konnectExtension.Name) |
| 106 | + require.EventuallyWithT(t, |
| 107 | + checkKonnectExtensionStatus(konnectExtension, cp.GetKonnectID(), ""), |
| 108 | + testutils.ObjectUpdateTimeout, testutils.ObjectUpdateTick) |
| 109 | + |
| 110 | + t.Logf("deleting Konnect control plane %s/%s", cp.Namespace, cp.Name) |
| 111 | + deleteObjectAndWaitForDeletionFn(t, cp.DeepCopy())() |
| 112 | + |
| 113 | + // Create a Konnect control plane for the KonnectExtension to attach to. |
| 114 | + cp = deploy.KonnectGatewayControlPlane(t, GetCtx(), clientNamespaced, authCfg, |
| 115 | + deploy.WithTestIDLabel(testID), |
| 116 | + deploy.WithName(cp.Name), // Reuse the same name to ensure the KonnectExtension is recreated with the same name. |
| 117 | + ) |
| 118 | + t.Cleanup(deleteObjectAndWaitForDeletionFn(t, cp.DeepCopy())) |
| 119 | + |
| 120 | + t.Logf("Waiting for Konnect ID to be assigned to ControlPlane %s/%s", cp.Namespace, cp.Name) |
| 121 | + require.EventuallyWithT(t, func(t *assert.CollectT) { |
| 122 | + err := GetClients().MgrClient.Get(GetCtx(), k8stypes.NamespacedName{Name: cp.Name, Namespace: cp.Namespace}, cp) |
| 123 | + require.NoError(t, err) |
| 124 | + assertKonnectEntityProgrammed(t, cp) |
| 125 | + }, testutils.ObjectUpdateTimeout, testutils.ObjectUpdateTick) |
| 126 | + |
| 127 | + t.Logf("Waiting for KonnectExtension %s/%s to have expected conditions set to True", konnectExtension.Namespace, konnectExtension.Name) |
| 128 | + require.EventuallyWithT(t, func(t *assert.CollectT) { |
| 129 | + ok, msg := checkKonnectExtensionConditions(t, |
| 130 | + konnectExtension, |
| 131 | + helpers.CheckAllConditionsTrue, |
| 132 | + konnectv1alpha1.ControlPlaneRefValidConditionType, |
| 133 | + konnectv1alpha1.DataPlaneCertificateProvisionedConditionType, |
| 134 | + konnectv1alpha2.KonnectExtensionReadyConditionType) |
| 135 | + assert.Truef(t, ok, "condition check failed: %s, conditions: %+v", msg, konnectExtension.Status.Conditions) |
| 136 | + }, testutils.ObjectUpdateTimeout, testutils.ObjectUpdateTick) |
| 137 | + |
| 138 | + t.Logf("waiting for status.konnect and status.dataPlaneClientAuth to be properly updated for KonnectExtension %s/%s", konnectExtension.Namespace, konnectExtension.Name) |
| 139 | + require.EventuallyWithT(t, |
| 140 | + checkKonnectExtensionStatus(konnectExtension, cp.GetKonnectID(), ""), |
| 141 | + testutils.ObjectUpdateTimeout, testutils.ObjectUpdateTick) |
| 142 | + |
| 143 | + // delete the KonnectExtension first to avoid the ControlPlane gets deleted first and |
| 144 | + // the KonnectExtension gets stuck in deletion. |
| 145 | + deleteObjectAndWaitForDeletionFn(t, konnectExtension.DeepCopy())() |
| 146 | +} |
0 commit comments