From d5d13bf2552a7976e7123251001cabe6d67c05b2 Mon Sep 17 00:00:00 2001 From: Konstantinos Angelopoulos Date: Tue, 23 Aug 2022 16:20:05 +0200 Subject: [PATCH] integration tests --- pkg/apis/gcp/types_infrastructure.go | 2 +- .../infrastructure/templates/main.tpl.tf | 6 +- pkg/internal/infrastructure/terraform.go | 2 +- pkg/internal/infrastructure/terraform_test.go | 56 +++++++++ .../infrastructure/infrastructure_test.go | 112 ++++++++++++------ 5 files changed, 136 insertions(+), 42 deletions(-) diff --git a/pkg/apis/gcp/types_infrastructure.go b/pkg/apis/gcp/types_infrastructure.go index 4c13f8cc6..94cd18e04 100644 --- a/pkg/apis/gcp/types_infrastructure.go +++ b/pkg/apis/gcp/types_infrastructure.go @@ -48,7 +48,7 @@ type NetworkConfig struct { PrivateServiceConnect *PrivateServiceConnectConfig } -// PrivateServiceConnect holds the configuration for Private Service Connect endpoints. +// PrivateServiceConnectConfig holds the configuration for Private Service Connect endpoints. type PrivateServiceConnectConfig struct { // EndpointIP is the IP where the services will be accessible. EndpointIP string diff --git a/pkg/internal/infrastructure/templates/main.tpl.tf b/pkg/internal/infrastructure/templates/main.tpl.tf index aa63fb3e3..1ae2160f7 100644 --- a/pkg/internal/infrastructure/templates/main.tpl.tf +++ b/pkg/internal/infrastructure/templates/main.tpl.tf @@ -299,11 +299,11 @@ output "{{ .outputKeys.subnetInternal }}" { } {{- end }} -{{ if .networks.privateServiceConnect }} -output "{{ .outputKeys.privateServiceConnectIP }}" { +{{- if .networks.privateServiceConnect }} +output "{{ .outputKeys.privateServiceConnectName }}" { value = google_compute_global_forwarding_rule.default.name } -output "{{ .outputKeys.privateServiceConnectName }}" { +output "{{ .outputKeys.privateServiceConnectIP }}" { value = google_compute_global_address.default.address } {{- end }} diff --git a/pkg/internal/infrastructure/terraform.go b/pkg/internal/infrastructure/terraform.go index 8237efbcc..65f40d7f0 100644 --- a/pkg/internal/infrastructure/terraform.go +++ b/pkg/internal/infrastructure/terraform.go @@ -57,7 +57,7 @@ const ( TerraformOutputKeyCloudRouter = "cloud_router" // TerraformOutputKeyPrivateServiceConnectIP is the name of the psc_ip terraform output variable. TerraformOutputKeyPrivateServiceConnectIP = "psc_ip" - // TerraformOutputKeyPrivateServiceConnectIP is the name of the psc_name terraform output variable. + // TerraformOutputKeyPrivateServiceConnectName is the name of the psc_name terraform output variable. TerraformOutputKeyPrivateServiceConnectName = "psc_name" ) diff --git a/pkg/internal/infrastructure/terraform_test.go b/pkg/internal/infrastructure/terraform_test.go index ec4ed466e..7e32cf53f 100644 --- a/pkg/internal/infrastructure/terraform_test.go +++ b/pkg/internal/infrastructure/terraform_test.go @@ -20,6 +20,7 @@ import ( "strconv" mockterraformer "github.com/gardener/gardener/extensions/pkg/terraformer/mock" + "k8s.io/utils/pointer" api "github.com/gardener/gardener-extension-provider-gcp/pkg/apis/gcp" apiv1alpha1 "github.com/gardener/gardener-extension-provider-gcp/pkg/apis/gcp/v1alpha1" @@ -158,6 +159,7 @@ var _ = Describe("Terraform", func() { CloudNATName: cloudNATName, })) }) + It("should return correct state when cloudRouter name is NOT specified", func() { var ( vpcWithoutCloudRouterConfig = &api.InfrastructureConfig{ @@ -452,6 +454,60 @@ var _ = Describe("Terraform", func() { }, })) }) + + It("should correctly compute the terraformer chart values with privateServiceConnect", func() { + var ( + endpointIP = "10.0.0.0" + endpointName = "endpoint" + ) + config.Networks.PrivateServiceConnect = &api.PrivateServiceConnectConfig{ + EndpointIP: endpointIP, + EndpointName: pointer.String(endpointName), + } + values, err := ComputeTerraformerTemplateValues(infra, serviceAccount, config, &podCIDR, true) + Expect(err).To(BeNil()) + Expect(values).To(Equal(map[string]interface{}{ + "google": map[string]interface{}{ + "region": infra.Spec.Region, + "project": projectID, + "enableBeta": true, + }, + "create": map[string]interface{}{ + "vpc": false, + "cloudRouter": false, + "serviceAccount": true, + }, + "vpc": map[string]interface{}{ + "name": strconv.Quote(config.Networks.VPC.Name), + "cloudRouter": map[string]interface{}{ + "name": "cloudrouter", + }, + }, + "clusterName": infra.Namespace, + "networks": map[string]interface{}{ + "workers": config.Networks.Workers, + "internal": config.Networks.Internal, + "cloudNAT": map[string]interface{}{ + "minPortsPerVM": minPortsPerVM, + }, + "privateServiceConnect": map[string]interface{}{ + "endpointName": endpointName, + "address": endpointIP, + }, + }, + "podCIDR": podCIDR, + "outputKeys": map[string]interface{}{ + "vpcName": TerraformerOutputKeyVPCName, + "cloudNAT": TerraformOutputKeyCloudNAT, + "cloudRouter": TerraformOutputKeyCloudRouter, + "serviceAccountEmail": TerraformerOutputKeyServiceAccountEmail, + "subnetNodes": TerraformerOutputKeySubnetNodes, + "subnetInternal": TerraformerOutputKeySubnetInternal, + "privateServiceConnectIP": TerraformOutputKeyPrivateServiceConnectIP, + "privateServiceConnectName": TerraformOutputKeyPrivateServiceConnectName, + }, + })) + }) }) Describe("#StatusFromTerraformState", func() { diff --git a/test/integration/infrastructure/infrastructure_test.go b/test/integration/infrastructure/infrastructure_test.go index c0275b99e..e490f207e 100644 --- a/test/integration/infrastructure/infrastructure_test.go +++ b/test/integration/infrastructure/infrastructure_test.go @@ -45,6 +45,8 @@ import ( "sigs.k8s.io/controller-runtime/pkg/log/zap" "sigs.k8s.io/controller-runtime/pkg/manager" + gcpinternal "github.com/gardener/gardener-extension-provider-gcp/pkg/apis/gcp" + "github.com/gardener/gardener-extension-provider-gcp/pkg/apis/gcp/helper" gcpinstall "github.com/gardener/gardener-extension-provider-gcp/pkg/apis/gcp/install" gcpv1alpha1 "github.com/gardener/gardener-extension-provider-gcp/pkg/apis/gcp/v1alpha1" "github.com/gardener/gardener-extension-provider-gcp/pkg/controller/infrastructure" @@ -57,6 +59,7 @@ const ( workersSubnetCIDR = "10.250.0.0/19" internalSubnetCIDR = "10.250.112.0/22" podCIDR = "100.96.0.0/11" + pscIP = "10.249.0.0" ) var ( @@ -169,8 +172,21 @@ var _ = Describe("Infrastructure tests", func() { framework.RunCleanupActions() }) - It("should successfully create and delete", func() { + // It("should successfully create and delete", func() { + // providerConfig := newProviderConfig(nil) + // + // namespace, err := generateNamespaceName() + // Expect(err).NotTo(HaveOccurred()) + // + // err = runTest(ctx, c, namespace, providerConfig, project, computeService, iamService) + // Expect(err).NotTo(HaveOccurred()) + // }) + + It("should successfully create and delete with privateServiceConnect", func() { providerConfig := newProviderConfig(nil) + providerConfig.Networks.PrivateServiceConnect = &gcpv1alpha1.PrivateServiceConnectConfig{ + EndpointIP: pscIP, + } namespace, err := generateNamespaceName() Expect(err).NotTo(HaveOccurred()) @@ -180,40 +196,40 @@ var _ = Describe("Infrastructure tests", func() { }) }) - Context("with infrastructure that uses existing vpc", func() { - AfterEach(func() { - framework.RunCleanupActions() - }) - - It("should successfully create and delete", func() { - namespace, err := generateNamespaceName() - Expect(err).NotTo(HaveOccurred()) - - networkName := namespace - cloudRouterName := networkName + "-cloud-router" - - err = prepareNewNetwork(ctx, log, project, computeService, networkName, cloudRouterName) - Expect(err).NotTo(HaveOccurred()) - - var cleanupHandle framework.CleanupActionHandle - cleanupHandle = framework.AddCleanupAction(func() { - err := teardownNetwork(ctx, log, project, computeService, networkName, cloudRouterName) - Expect(err).NotTo(HaveOccurred()) - - framework.RemoveCleanupAction(cleanupHandle) - }) - - providerConfig := newProviderConfig(&gcpv1alpha1.VPC{ - Name: networkName, - CloudRouter: &gcpv1alpha1.CloudRouter{ - Name: cloudRouterName, - }, - }) - - err = runTest(ctx, c, namespace, providerConfig, project, computeService, iamService) - Expect(err).NotTo(HaveOccurred()) - }) - }) + // Context("with infrastructure that uses existing vpc", func() { + // AfterEach(func() { + // framework.RunCleanupActions() + // }) + // + // It("should successfully create and delete", func() { + // namespace, err := generateNamespaceName() + // Expect(err).NotTo(HaveOccurred()) + // + // networkName := namespace + // cloudRouterName := networkName + "-cloud-router" + // + // err = prepareNewNetwork(ctx, log, project, computeService, networkName, cloudRouterName) + // Expect(err).NotTo(HaveOccurred()) + // + // var cleanupHandle framework.CleanupActionHandle + // cleanupHandle = framework.AddCleanupAction(func() { + // err := teardownNetwork(ctx, log, project, computeService, networkName, cloudRouterName) + // Expect(err).NotTo(HaveOccurred()) + // + // framework.RemoveCleanupAction(cleanupHandle) + // }) + // + // providerConfig := newProviderConfig(&gcpv1alpha1.VPC{ + // Name: networkName, + // CloudRouter: &gcpv1alpha1.CloudRouter{ + // Name: cloudRouterName, + // }, + // }) + // + // err = runTest(ctx, c, namespace, providerConfig, project, computeService, iamService) + // Expect(err).NotTo(HaveOccurred()) + // }) + // }) }) func runTest( @@ -337,7 +353,14 @@ func runTest( } By("verify infrastructure creation") - verifyCreation(ctx, project, computeService, iamService, infra, providerConfig) + err = c.Get(ctx, client.ObjectKey{ + Namespace: infra.Namespace, + Name: infra.Name, + }, infra) + Expect(err).To(BeNil()) + providerStatus, err := helper.InfrastructureStatusFromRaw(infra.Status.ProviderStatus) + Expect(err).To(BeNil()) + verifyCreation(ctx, project, computeService, iamService, infra, providerConfig, providerStatus) return nil } @@ -487,6 +510,7 @@ func verifyCreation( iamService *iam.Service, infra *extensionsv1alpha1.Infrastructure, providerConfig *gcpv1alpha1.InfrastructureConfig, + providerStatus *gcpinternal.InfrastructureStatus, ) { // service account if !features.ExtensionFeatureGate.Enabled(features.DisableGardenerServiceAccountCreation) { @@ -497,7 +521,6 @@ func verifyCreation( } // network - network, err := computeService.Networks.Get(project, infra.Namespace).Do() Expect(err).NotTo(HaveOccurred()) Expect(network.AutoCreateSubnetworks).To(BeFalse()) @@ -594,6 +617,21 @@ func verifyCreation( Ports: []string{"30000-32767"}, }, })) + + if providerConfig.Networks.PrivateServiceConnect != nil { + Expect(providerStatus.Networks).NotTo(BeNil()) + Expect(providerStatus.Networks.PrivateServiceConnectStatus).NotTo(BeNil()) + Expect(providerStatus.Networks.PrivateServiceConnectStatus.EndpointName).NotTo(BeEmpty()) + fwd, err := computeService.GlobalForwardingRules.Get(project, providerStatus.Networks.PrivateServiceConnectStatus.EndpointName).Do() + Expect(err).To(BeNil()) + Expect(fwd.Network).To(Equal(network.SelfLink)) + + addr, err := computeService.GlobalAddresses.Get(project, infra.Namespace).Do() + + Expect(err).To(BeNil()) + Expect(addr.Address).To(Equal(providerConfig.Networks.PrivateServiceConnect.EndpointIP)) + Expect(addr.AddressType).To(Equal("INTERNAL")) + } } func verifyDeletion(