diff --git a/kong2kic/global_plugin.go b/kong2kic/global_plugin.go index 0dfbc6c93..feda9ca71 100644 --- a/kong2kic/global_plugin.go +++ b/kong2kic/global_plugin.go @@ -29,6 +29,7 @@ func populateKICKongClusterPlugins(content *file.Content, file *KICContent) erro kongClusterPlugin.APIVersion = ConfigurationKongHQv1 kongClusterPlugin.Kind = KongClusterPluginKind kongClusterPlugin.Annotations = map[string]string{IngressClass: ClassName} + kongClusterPlugin.Labels = map[string]string{GlobalLabel: GlobalLabelValue} if plugin.Name != nil { kongClusterPlugin.PluginName = *plugin.Name kongClusterPlugin.Name = calculateSlug(*plugin.Name) diff --git a/kong2kic/kong2kic_integration_test.go b/kong2kic/kong2kic_integration_test.go index b6df6a354..3ceb40d21 100644 --- a/kong2kic/kong2kic_integration_test.go +++ b/kong2kic/kong2kic_integration_test.go @@ -33,7 +33,7 @@ import ( ) func Test_deployManifests(t *testing.T) { - versions := []string{"2.12", "3.0", "3.1", "3.2", "3.3"} + versions := []string{"2.12", "3.0", "3.1", "3.2", "3.3", "3.4", "3.5"} for _, version := range versions { t.Run("KIC Version "+version, func(t *testing.T) { t.Parallel() @@ -75,6 +75,29 @@ func Test_deployManifests(t *testing.T) { dynamicClient, err := dynamic.NewForConfig(config) require.NoError(t, err) + t.Log("creating Gateway resource for HTTPRoutes") + gatewayGVR, gatewayClassGVR, err := createGatewayResources(t, dynamicClient, kindToResource) + require.NoError(t, err) + defer func() { + // Delete Gateway first + err := dynamicClient.Resource(gatewayGVR). + Namespace(apiv1.NamespaceDefault). + Delete(context.TODO(), "kong", metav1.DeleteOptions{}) + if err != nil { + t.Logf("failed to delete Gateway: %v", err) + } else { + t.Log("deleted Gateway: kong") + } + // Then delete GatewayClass + err = dynamicClient.Resource(gatewayClassGVR). + Delete(context.TODO(), "kong", metav1.DeleteOptions{}) + if err != nil { + t.Logf("failed to delete GatewayClass: %v", err) + } else { + t.Log("deleted GatewayClass: kong") + } + }() + t.Log("deploying manifests to the cluster") err = deployManifestsToClusterForVersion(t, dynamicClient, kindToResource, version) require.NoError(t, err) @@ -190,10 +213,82 @@ func deployGatewayAPICRDs(t *testing.T, config *rest.Config) (*clientset.Clients } // Wait for CRDs to be available - time.Sleep(2 * time.Second) + time.Sleep(1 * time.Second) return clientset, nil } +// Helper function to create Gateway and GatewayClass resources +func createGatewayResources( + t *testing.T, + dynamicClient dynamic.Interface, + kindToResource map[string]string, +) (schema.GroupVersionResource, schema.GroupVersionResource, error) { + // Create GatewayClass first + gatewayClassManifest := ` +apiVersion: gateway.networking.k8s.io/v1 +kind: GatewayClass +metadata: + name: kong +spec: + controllerName: konghq.com/kic-gateway-controller +` + gatewayClass := &unstructured.Unstructured{} + err := yaml.Unmarshal([]byte(gatewayClassManifest), gatewayClass) + if err != nil { + return schema.GroupVersionResource{}, schema.GroupVersionResource{}, err + } + + gatewayClassGVR, err := getGroupVersionResource(gatewayClass, kindToResource) + if err != nil { + return schema.GroupVersionResource{}, schema.GroupVersionResource{}, err + } + + _, err = dynamicClient.Resource(gatewayClassGVR). + Create(context.TODO(), gatewayClass, metav1.CreateOptions{}) + if err != nil { + return schema.GroupVersionResource{}, schema.GroupVersionResource{}, err + } + t.Logf("created GatewayClass: %s", gatewayClass.GetName()) + + // Then create Gateway + gatewayManifest := ` +apiVersion: gateway.networking.k8s.io/v1 +kind: Gateway +metadata: + name: kong +spec: + gatewayClassName: kong + listeners: + - name: proxy + port: 80 + protocol: HTTP +` + gateway := &unstructured.Unstructured{} + err = yaml.Unmarshal([]byte(gatewayManifest), gateway) + if err != nil { + return schema.GroupVersionResource{}, schema.GroupVersionResource{}, err + } + + gatewayGVR, err := getGroupVersionResource(gateway, kindToResource) + if err != nil { + return schema.GroupVersionResource{}, schema.GroupVersionResource{}, err + } + + setNamespaceIfNeeded(gateway) + + _, err = dynamicClient.Resource(gatewayGVR). + Namespace(gateway.GetNamespace()). + Create(context.TODO(), gateway, metav1.CreateOptions{}) + if err != nil { + return schema.GroupVersionResource{}, schema.GroupVersionResource{}, err + } + t.Logf("created Gateway: %s in Namespace: %s", gateway.GetName(), gateway.GetNamespace()) + + // Wait for the Gateway to be ready + time.Sleep(1 * time.Second) + return gatewayGVR, gatewayClassGVR, nil +} + // Helper function to get Kind to Resource mapping func getKindToResourceMap(clientset *clientset.Clientset) (map[string]string, error) { kindToResource := make(map[string]string) diff --git a/kong2kic/testdata/global-plugin-output-expected.yaml b/kong2kic/testdata/global-plugin-output-expected.yaml index 2ecdc5fb1..659133a73 100644 --- a/kong2kic/testdata/global-plugin-output-expected.yaml +++ b/kong2kic/testdata/global-plugin-output-expected.yaml @@ -10,6 +10,8 @@ metadata: annotations: konghq.com/tags: cloudops-managed,metrics kubernetes.io/ingress.class: kong + labels: + global: "true" name: prometheus plugin: prometheus --- \ No newline at end of file diff --git a/kong2kic/utils.go b/kong2kic/utils.go index 28be4e350..88f1db4d5 100644 --- a/kong2kic/utils.go +++ b/kong2kic/utils.go @@ -47,6 +47,8 @@ const ( ConfigurationKongHQv1beta1 = "configuration.konghq.com/v1beta1" GatewayAPIVersionV1 = "gateway.networking.k8s.io/v1" GatewayAPIVersionV1Beta1 = "gateway.networking.k8s.io/v1beta1" + GlobalLabel = "global" + GlobalLabelValue = "true" HTTPRouteKind = "HTTPRoute" IngressAPIVersion = "networking.k8s.io/v1" IngressClass = "kubernetes.io/ingress.class"