Skip to content

Commit

Permalink
Merge pull request #66 from openshift-cherrypick-robot/cherry-pick-64…
Browse files Browse the repository at this point in the history
…-to-release-4.11

[release-4.11] Bug 2107564: Add E2E for GCP
  • Loading branch information
openshift-merge-robot committed Aug 25, 2022
2 parents 1a88f55 + 4784864 commit 06d77ef
Show file tree
Hide file tree
Showing 210 changed files with 6,051 additions and 1,514 deletions.
4 changes: 2 additions & 2 deletions cmd/cluster-capi-operator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,15 +234,15 @@ func setupInfraClusterReconciler(mgr manager.Manager, platform configv1.Platform
klog.Error(err, "unable to create controller", "controller", "AWSCluster")
os.Exit(1)
}
case configv1.GCPPlatformType:
case configv1.AzurePlatformType:
if err := (&cluster.GenericInfraClusterReconciler{
ClusterOperatorStatusClient: getClusterOperatorStatusClient(mgr, "cluster-capi-operator-infra-cluster-resource-controller"),
InfraCluster: &azurev1.AzureCluster{},
}).SetupWithManager(mgr); err != nil {
klog.Error(err, "unable to create controller", "controller", "AzureCluster")
os.Exit(1)
}
case configv1.AzurePlatformType:
case configv1.GCPPlatformType:
if err := (&cluster.GenericInfraClusterReconciler{
ClusterOperatorStatusClient: getClusterOperatorStatusClient(mgr, "cluster-capi-operator-infra-cluster-resource-controller"),
InfraCluster: &gcpv1.GCPCluster{},
Expand Down
1 change: 1 addition & 0 deletions e2e/aws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ var _ = Describe("Cluster API AWS MachineSet", Ordered, func() {
machineSet = framework.CreateMachineSet(cl, framework.NewMachineSetParams(
"aws-machineset",
clusterName,
"",
1,
corev1.ObjectReference{
Kind: "AWSMachineTemplate",
Expand Down
2 changes: 2 additions & 0 deletions e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/kubernetes/scheme"
awsv1 "sigs.k8s.io/cluster-api-provider-aws/api/v1beta1"
gcpv1 "sigs.k8s.io/cluster-api-provider-gcp/api/v1beta1"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
runtimeclient "sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/config"
Expand All @@ -32,6 +33,7 @@ var (
func init() {
utilruntime.Must(configv1.Install(scheme.Scheme))
utilruntime.Must(awsv1.AddToScheme(scheme.Scheme))
utilruntime.Must(gcpv1.AddToScheme(scheme.Scheme))
utilruntime.Must(clusterv1.AddToScheme(scheme.Scheme))
utilruntime.Must(mapiv1.AddToScheme(scheme.Scheme))
}
Expand Down
8 changes: 7 additions & 1 deletion e2e/framework/machineset.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ import (
type machineSetParams struct {
msName string
clusterName string
failureDomain string
replicas int32
infrastructureRef corev1.ObjectReference
}

// NewMachineSetParams returns a new machineSetParams object.
func NewMachineSetParams(msName, clusterName string, replicas int32, infrastructureRef corev1.ObjectReference) machineSetParams {
func NewMachineSetParams(msName, clusterName, failureDomain string, replicas int32, infrastructureRef corev1.ObjectReference) machineSetParams {
Expect(msName).ToNot(BeEmpty())
Expect(clusterName).ToNot(BeEmpty())
Expect(infrastructureRef.APIVersion).ToNot(BeEmpty())
Expand All @@ -33,6 +34,7 @@ func NewMachineSetParams(msName, clusterName string, replicas int32, infrastruct
clusterName: clusterName,
replicas: replicas,
infrastructureRef: infrastructureRef,
failureDomain: failureDomain,
}
}

Expand Down Expand Up @@ -75,6 +77,10 @@ func CreateMachineSet(cl client.Client, params machineSetParams) *clusterv1.Mach
},
}

if params.failureDomain != "" {
ms.Spec.Template.Spec.FailureDomain = &params.failureDomain
}

Expect(cl.Create(ctx, ms)).To(Succeed())
return ms
}
Expand Down
181 changes: 181 additions & 0 deletions e2e/gcp_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
package e2e

import (
"fmt"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
gcpv1 "sigs.k8s.io/cluster-api-provider-gcp/api/v1beta1"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/controller-runtime/pkg/client"
yaml "sigs.k8s.io/yaml"

configv1 "github.com/openshift/api/config/v1"
mapiv1 "github.com/openshift/api/machine/v1beta1"
"github.com/openshift/cluster-capi-operator/e2e/framework"
)

const (
gcpMachineTemplateName = "gcp-machine-template"
)

var _ = Describe("Cluster API GCP MachineSet", Ordered, func() {
var gcpMachineTemplate *gcpv1.GCPMachineTemplate
var machineSet *clusterv1.MachineSet
var mapiMachineSpec *mapiv1.GCPMachineProviderSpec

BeforeAll(func() {
if platform != configv1.GCPPlatformType {
Skip("Skipping GCP E2E tests")
}
framework.CreateCoreCluster(cl, clusterName, "GCPCluster")
mapiMachineSpec = getGCPMAPIProviderSpec(cl)
createGCPCluster(cl, mapiMachineSpec)
})

AfterEach(func() {
framework.DeleteMachineSets(cl, machineSet)
framework.WaitForMachineSetsDeleted(cl, machineSet)
framework.DeleteObjects(cl, gcpMachineTemplate)
})

It("should be able to run a machine", func() {
gcpMachineTemplate = createGCPMachineTemplate(cl, mapiMachineSpec)

machineSet = framework.CreateMachineSet(cl, framework.NewMachineSetParams(
"gcp-machineset",
clusterName,
mapiMachineSpec.Zone,
1,
corev1.ObjectReference{
Kind: "GCPMachineTemplate",
APIVersion: infraAPIVersion,
Name: gcpMachineTemplateName,
},
))

framework.WaitForMachineSet(cl, machineSet.Name)
})
})

func getGCPMAPIProviderSpec(cl client.Client) *mapiv1.GCPMachineProviderSpec {
machineSetList := &mapiv1.MachineSetList{}
Expect(cl.List(ctx, machineSetList, client.InNamespace(framework.MAPINamespace))).To(Succeed())

Expect(machineSetList.Items).ToNot(HaveLen(0))
machineSet := machineSetList.Items[0]
Expect(machineSet.Spec.Template.Spec.ProviderSpec.Value).ToNot(BeNil())

providerSpec := &mapiv1.GCPMachineProviderSpec{}
Expect(yaml.Unmarshal(machineSet.Spec.Template.Spec.ProviderSpec.Value.Raw, providerSpec)).To(Succeed())

return providerSpec
}

func createGCPCluster(cl client.Client, mapiProviderSpec *mapiv1.GCPMachineProviderSpec) *gcpv1.GCPCluster {
By("Creating GCP cluster")

gcpCluster := &gcpv1.GCPCluster{
ObjectMeta: metav1.ObjectMeta{
Name: clusterName,
Namespace: framework.CAPINamespace,
},
Spec: gcpv1.GCPClusterSpec{
Network: gcpv1.NetworkSpec{
Name: &mapiProviderSpec.NetworkInterfaces[0].Network,
},
Region: mapiProviderSpec.Region,
Project: mapiProviderSpec.ProjectID,
},
}

if err := cl.Create(ctx, gcpCluster); err != nil && !apierrors.IsAlreadyExists(err) {
Expect(err).ToNot(HaveOccurred())
}

Eventually(func() (bool, error) {
patchedGCPCluster := &gcpv1.GCPCluster{}
err := cl.Get(ctx, client.ObjectKeyFromObject(gcpCluster), patchedGCPCluster)
if err != nil {
return false, err
}

if patchedGCPCluster.Annotations == nil {
return false, nil
}

if _, ok := patchedGCPCluster.Annotations[clusterv1.ManagedByAnnotation]; !ok {
return false, nil
}

return patchedGCPCluster.Status.Ready, nil
}, framework.WaitShort).Should(BeTrue())

return gcpCluster
}

func createGCPMachineTemplate(cl client.Client, mapiProviderSpec *mapiv1.GCPMachineProviderSpec) *gcpv1.GCPMachineTemplate {
By("Creating GCP machine template")

Expect(mapiProviderSpec).ToNot(BeNil())
Expect(mapiProviderSpec.Disks).ToNot(BeNil())
Expect(len(mapiProviderSpec.Disks)).To(BeNumerically(">", 0))
Expect(mapiProviderSpec.Disks[0].Type).ToNot(BeEmpty())
Expect(mapiProviderSpec.MachineType).ToNot(BeEmpty())
Expect(mapiProviderSpec.NetworkInterfaces).ToNot(BeNil())
Expect(len(mapiProviderSpec.NetworkInterfaces)).To(BeNumerically(">", 0))
Expect(mapiProviderSpec.NetworkInterfaces[0].Subnetwork).ToNot(BeEmpty())
Expect(mapiProviderSpec.ServiceAccounts).ToNot(BeNil())
Expect(mapiProviderSpec.ServiceAccounts[0].Email).ToNot(BeEmpty())
Expect(mapiProviderSpec.ServiceAccounts[0].Scopes).ToNot(BeNil())
Expect(len(mapiProviderSpec.ServiceAccounts)).To(BeNumerically(">", 0))
Expect(mapiProviderSpec.Tags).ToNot(BeNil())
Expect(len(mapiProviderSpec.Tags)).To(BeNumerically(">", 0))

var rootDeviceType gcpv1.DiskType
switch mapiProviderSpec.Disks[0].Type {
case "pd-standard":
rootDeviceType = gcpv1.PdStandardDiskType
case "pd-ssd":
rootDeviceType = gcpv1.PdSsdDiskType
case "local-ssd":
rootDeviceType = gcpv1.LocalSsdDiskType
}

ipForwardingDisabled := gcpv1.IPForwardingDisabled
gcpMachineSpec := gcpv1.GCPMachineSpec{
RootDeviceType: &rootDeviceType,
RootDeviceSize: mapiProviderSpec.Disks[0].SizeGB,
InstanceType: mapiProviderSpec.MachineType,
Image: &mapiProviderSpec.Disks[0].Image,
Subnet: &mapiProviderSpec.NetworkInterfaces[0].Subnetwork,
ServiceAccount: &gcpv1.ServiceAccount{
Email: mapiProviderSpec.ServiceAccounts[0].Email,
Scopes: mapiProviderSpec.ServiceAccounts[0].Scopes,
},
AdditionalNetworkTags: mapiProviderSpec.Tags,
AdditionalLabels: gcpv1.Labels{fmt.Sprintf("kubernetes-io-cluster-%s", clusterName): "owned"},
IPForwarding: &ipForwardingDisabled,
}

gcpMachineTemplate := &gcpv1.GCPMachineTemplate{
ObjectMeta: metav1.ObjectMeta{
Name: gcpMachineTemplateName,
Namespace: framework.CAPINamespace,
},
Spec: gcpv1.GCPMachineTemplateSpec{
Template: gcpv1.GCPMachineTemplateResource{
Spec: gcpMachineSpec,
},
},
}

if err := cl.Create(ctx, gcpMachineTemplate); err != nil && !apierrors.IsAlreadyExists(err) {
Expect(err).ToNot(HaveOccurred())
}

return gcpMachineTemplate
}
18 changes: 9 additions & 9 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.18
require (
github.com/gobuffalo/flect v0.2.4
github.com/onsi/ginkgo/v2 v2.1.3
github.com/onsi/gomega v1.18.1
github.com/onsi/gomega v1.19.0
github.com/openshift/api v0.0.0-20220222102030-354aa98a475c
github.com/openshift/library-go v0.0.0-20220221165938-535fc9bdb13b
github.com/pkg/errors v0.9.1
Expand All @@ -18,12 +18,12 @@ require (
k8s.io/component-base v0.24.0
k8s.io/klog/v2 v2.60.1
k8s.io/utils v0.0.0-20220210201930-3a6ce19ff2f9
sigs.k8s.io/cluster-api v1.1.1
sigs.k8s.io/cluster-api v1.1.4
sigs.k8s.io/cluster-api-operator v0.0.0-20220221190000-3f8fd689a585
sigs.k8s.io/cluster-api-provider-aws v1.3.0
sigs.k8s.io/cluster-api-provider-azure v1.2.1
sigs.k8s.io/cluster-api-provider-gcp v1.0.2
sigs.k8s.io/controller-runtime v0.11.1
sigs.k8s.io/cluster-api-provider-gcp v1.1.0
sigs.k8s.io/controller-runtime v0.11.2
sigs.k8s.io/yaml v1.3.0
)

Expand Down Expand Up @@ -56,7 +56,7 @@ require (
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/gnostic v0.5.7-v3refs // indirect
github.com/google/go-cmp v0.5.7 // indirect
github.com/google/go-cmp v0.5.8 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/imdario/mergo v0.3.12 // indirect
Expand All @@ -72,15 +72,15 @@ require (
github.com/prometheus/common v0.32.1 // indirect
github.com/prometheus/procfs v0.7.3 // indirect
golang.org/x/crypto v0.0.0-20220214200702-86341886e292 // indirect
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd // indirect
golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8 // indirect
golang.org/x/sys v0.0.0-20220209214540-3681064d5158 // indirect
golang.org/x/net v0.0.0-20220607020251-c690dde0001d // indirect
golang.org/x/oauth2 v0.0.0-20220608161450-d0670ef3b1eb // indirect
golang.org/x/sys v0.0.0-20220610221304-9f5ed59c137d // indirect
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/time v0.0.0-20220210224613-90d013bbcef8 // indirect
gomodules.xyz/jsonpatch/v2 v2.2.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.27.1 // indirect
google.golang.org/protobuf v1.28.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
k8s.io/kube-openapi v0.0.0-20220328201542-3ee0da9b0b42 // indirect
Expand Down
Loading

0 comments on commit 06d77ef

Please sign in to comment.