|
| 1 | +package capi |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + . "github.com/onsi/ginkgo/v2" |
| 7 | + . "github.com/onsi/gomega" |
| 8 | + configv1 "github.com/openshift/api/config/v1" |
| 9 | + mapiv1 "github.com/openshift/api/machine/v1beta1" |
| 10 | + "github.com/openshift/cluster-api-actuator-pkg/pkg/framework" |
| 11 | + "github.com/openshift/cluster-api-actuator-pkg/pkg/framework/gatherer" |
| 12 | + capiinfrastructurev1beta2resourcebuilder "github.com/openshift/cluster-api-actuator-pkg/testutils/resourcebuilder/cluster-api/infrastructure/v1beta2" |
| 13 | + corev1 "k8s.io/api/core/v1" |
| 14 | + awsv1 "sigs.k8s.io/cluster-api-provider-aws/v2/api/v1beta2" |
| 15 | + clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" |
| 16 | + |
| 17 | + "sigs.k8s.io/controller-runtime/pkg/client/config" |
| 18 | + |
| 19 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 20 | + yaml "sigs.k8s.io/yaml" |
| 21 | +) |
| 22 | + |
| 23 | +const ( |
| 24 | + awsMachineTemplateName = "aws-machine-template" |
| 25 | + infrastructureName = "cluster" |
| 26 | + infraAPIVersion = "infrastructure.cluster.x-k8s.io/v1beta1" |
| 27 | +) |
| 28 | + |
| 29 | +var _ = Describe("Cluster API AWS MachineSet", framework.LabelCAPI, framework.LabelDisruptive, Ordered, func() { |
| 30 | + var ( |
| 31 | + cl client.Client |
| 32 | + ctx = context.Background() |
| 33 | + platform configv1.PlatformType |
| 34 | + clusterName string |
| 35 | + oc *gatherer.CLI |
| 36 | + awsMachineTemplate *awsv1.AWSMachineTemplate |
| 37 | + machineSetParams framework.CAPIMachineSetParams |
| 38 | + machineSet *clusterv1.MachineSet |
| 39 | + mapiDefaultProviderSpec *mapiv1.AWSMachineProviderConfig |
| 40 | + err error |
| 41 | + ) |
| 42 | + |
| 43 | + BeforeAll(func() { |
| 44 | + cfg, err := config.GetConfig() |
| 45 | + Expect(err).ToNot(HaveOccurred(), "Failed to GetConfig") |
| 46 | + |
| 47 | + cl, err = client.New(cfg, client.Options{}) |
| 48 | + Expect(err).ToNot(HaveOccurred(), "Failed to create Kubernetes client for test") |
| 49 | + |
| 50 | + infra := &configv1.Infrastructure{} |
| 51 | + infraName := client.ObjectKey{ |
| 52 | + Name: infrastructureName, |
| 53 | + } |
| 54 | + Expect(cl.Get(ctx, infraName, infra)).To(Succeed(), "Failed to get cluster infrastructure object") |
| 55 | + Expect(infra.Status.PlatformStatus).ToNot(BeNil(), "expected the infrastructure Status.PlatformStatus to not be nil") |
| 56 | + clusterName = infra.Status.InfrastructureName |
| 57 | + platform = infra.Status.PlatformStatus.Type |
| 58 | + if platform != configv1.AWSPlatformType { |
| 59 | + Skip("Skipping AWS E2E tests") |
| 60 | + } |
| 61 | + oc, err = framework.NewCLI() |
| 62 | + Expect(err).ToNot(HaveOccurred(), "Failed to new CLI") |
| 63 | + framework.SkipIfNotTechPreviewNoUpgrade(oc, cl) |
| 64 | + _, mapiDefaultProviderSpec = getDefaultAWSMAPIProviderSpec(cl) |
| 65 | + machineSetParams = framework.NewCAPIMachineSetParams( |
| 66 | + "aws-machineset", |
| 67 | + clusterName, |
| 68 | + mapiDefaultProviderSpec.Placement.AvailabilityZone, |
| 69 | + 1, |
| 70 | + corev1.ObjectReference{ |
| 71 | + Kind: "AWSMachineTemplate", |
| 72 | + APIVersion: infraAPIVersion, |
| 73 | + Name: awsMachineTemplateName, |
| 74 | + }, |
| 75 | + ) |
| 76 | + framework.CreateCoreCluster(ctx, cl, clusterName, "AWSCluster") |
| 77 | + }) |
| 78 | + |
| 79 | + AfterEach(func() { |
| 80 | + framework.DeleteCAPIMachineSets(ctx, cl, machineSet) |
| 81 | + framework.WaitForCAPIMachineSetsDeleted(ctx, cl, machineSet) |
| 82 | + framework.DeleteObjects(ctx, cl, awsMachineTemplate) |
| 83 | + }) |
| 84 | + |
| 85 | + //huliu-OCP-51071 - [CAPI] Create machineset with CAPI on aws |
| 86 | + It("should be able to run a machine with a default provider spec", func() { |
| 87 | + awsMachineTemplate = newAWSMachineTemplate(mapiDefaultProviderSpec) |
| 88 | + Expect(cl.Create(ctx, awsMachineTemplate)).To(Succeed(), "Failed to create awsmachinetemplate") |
| 89 | + machineSetParams = framework.UpdateCAPIMachineSetName("aws-machineset-51071", machineSetParams) |
| 90 | + machineSet, err = framework.CreateCAPIMachineSet(ctx, cl, machineSetParams) |
| 91 | + Expect(err).ToNot(HaveOccurred(), "Failed to create CAPI machineset") |
| 92 | + framework.WaitForCAPIMachinesRunning(ctx, cl, machineSet.Name) |
| 93 | + }) |
| 94 | + |
| 95 | + //huliu-OCP-75395 - [CAPI] AWS Placement group support. |
| 96 | + It("should be able to run a machine with cluster placement group", func() { |
| 97 | + awsClient := framework.NewAwsClient(framework.GetCredentialsFromCluster(oc)) |
| 98 | + placementGroupName := clusterName + "pgcluster" |
| 99 | + placementGroupID, err := awsClient.CreatePlacementGroup(placementGroupName, "cluster") |
| 100 | + Expect(err).ToNot(HaveOccurred(), "Failed to create placementgroup") |
| 101 | + Expect(placementGroupID).ToNot(Equal(""), "expected the placementGroupID to not be empty string") |
| 102 | + DeferCleanup(func() { |
| 103 | + _, err = awsClient.DeletePlacementGroup(placementGroupName) |
| 104 | + Expect(err).ToNot(HaveOccurred(), "Failed to delete placementgroup") |
| 105 | + }) |
| 106 | + |
| 107 | + awsMachineTemplate = newAWSMachineTemplate(mapiDefaultProviderSpec) |
| 108 | + awsMachineTemplate.Spec.Template.Spec.PlacementGroupName = placementGroupName |
| 109 | + Expect(cl.Create(ctx, awsMachineTemplate)).To(Succeed(), "Failed to create awsmachinetemplate") |
| 110 | + machineSetParams = framework.UpdateCAPIMachineSetName("aws-machineset-75395", machineSetParams) |
| 111 | + machineSet, err = framework.CreateCAPIMachineSet(ctx, cl, machineSetParams) |
| 112 | + Expect(err).ToNot(HaveOccurred(), "Failed to create CAPI machineset") |
| 113 | + framework.WaitForCAPIMachinesRunning(ctx, cl, machineSet.Name) |
| 114 | + }) |
| 115 | + |
| 116 | + //huliu-OCP-75396 - [CAPI] Creating machines using KMS keys from AWS. |
| 117 | + It("should be able to run a machine using KMS keys", framework.LabelQEOnly, func() { |
| 118 | + awsMachineTemplate = newAWSMachineTemplate(mapiDefaultProviderSpec) |
| 119 | + awskmsClient := framework.NewAwsKmsClient(framework.GetCredentialsFromCluster(oc)) |
| 120 | + key, err := awskmsClient.CreateKey(infrastructureName + " key 75396") |
| 121 | + if err != nil { |
| 122 | + Skip("Create key failed, skip the cases!!") |
| 123 | + } |
| 124 | + defer func() { |
| 125 | + err := awskmsClient.DeleteKey(key) |
| 126 | + Expect(err).ToNot(HaveOccurred(), "Failed to delete the key") |
| 127 | + }() |
| 128 | + |
| 129 | + encryptBool := true |
| 130 | + awsMachineTemplate.Spec.Template.Spec.NonRootVolumes = []awsv1.Volume{ |
| 131 | + { |
| 132 | + DeviceName: "/dev/xvda", |
| 133 | + Size: 140, |
| 134 | + Type: awsv1.VolumeTypeIO1, |
| 135 | + IOPS: 5000, |
| 136 | + Encrypted: &encryptBool, |
| 137 | + EncryptionKey: key, |
| 138 | + }, |
| 139 | + } |
| 140 | + Expect(cl.Create(ctx, awsMachineTemplate)).To(Succeed(), "Failed to create awsmachinetemplate") |
| 141 | + machineSetParams = framework.UpdateCAPIMachineSetName("aws-machineset-75396", machineSetParams) |
| 142 | + machineSet, err = framework.CreateCAPIMachineSet(ctx, cl, machineSetParams) |
| 143 | + Expect(err).ToNot(HaveOccurred(), "Failed to create CAPI machineset") |
| 144 | + framework.WaitForCAPIMachinesRunning(ctx, cl, machineSet.Name) |
| 145 | + }) |
| 146 | +}) |
| 147 | + |
| 148 | +func getDefaultAWSMAPIProviderSpec(cl client.Client) (*mapiv1.MachineSet, *mapiv1.AWSMachineProviderConfig) { |
| 149 | + machineSetList := &mapiv1.MachineSetList{} |
| 150 | + |
| 151 | + Eventually(func() error { |
| 152 | + return cl.List(framework.GetContext(), machineSetList, client.InNamespace(framework.MachineAPINamespace)) |
| 153 | + }, framework.WaitShort, framework.RetryShort).Should(Succeed(), "it should be able to list the MAPI machinesets") |
| 154 | + Expect(machineSetList.Items).ToNot(HaveLen(0), "expected the MAPI machinesets to be present") |
| 155 | + |
| 156 | + machineSet := &machineSetList.Items[0] |
| 157 | + Expect(machineSet.Spec.Template.Spec.ProviderSpec.Value).ToNot(BeNil(), "expected the MAPI machinesets ProviderSpec value to not be nil") |
| 158 | + |
| 159 | + providerSpec := &mapiv1.AWSMachineProviderConfig{} |
| 160 | + Expect(yaml.Unmarshal(machineSet.Spec.Template.Spec.ProviderSpec.Value.Raw, providerSpec)).To(Succeed(), "it should be able to unmarshal the raw yaml into providerSpec") |
| 161 | + |
| 162 | + return machineSet, providerSpec |
| 163 | +} |
| 164 | + |
| 165 | +func newAWSMachineTemplate(mapiProviderSpec *mapiv1.AWSMachineProviderConfig) *awsv1.AWSMachineTemplate { |
| 166 | + By("Creating AWS machine template") |
| 167 | + |
| 168 | + Expect(mapiProviderSpec).ToNot(BeNil(), "expected the mapi ProviderSpec to not be nil") |
| 169 | + Expect(mapiProviderSpec.IAMInstanceProfile).ToNot(BeNil(), "expected the mapi IAMInstanceProfile to not be nil") |
| 170 | + Expect(mapiProviderSpec.IAMInstanceProfile.ID).ToNot(BeNil(), "expected the mapi IAMInstanceProfile.ID to not be nil") |
| 171 | + Expect(mapiProviderSpec.InstanceType).ToNot(BeEmpty(), "expected the mapi InstanceType to not be empty") |
| 172 | + Expect(mapiProviderSpec.Placement.AvailabilityZone).ToNot(BeEmpty(), "expected the mapi Placement.AvailabilityZone to not be empty") |
| 173 | + Expect(mapiProviderSpec.AMI.ID).ToNot(BeNil(), "expected the mapi AMI.ID to not be nil") |
| 174 | + Expect(mapiProviderSpec.SecurityGroups).ToNot(HaveLen(0), "expected the mapi SecurityGroups to be present") |
| 175 | + Expect(mapiProviderSpec.SecurityGroups[0].Filters).ToNot(HaveLen(0), "expected the mapi SecurityGroups[0].Filters to be present") |
| 176 | + Expect(mapiProviderSpec.SecurityGroups[0].Filters[0].Values).ToNot(HaveLen(0), "expected the mapi SecurityGroups[0].Filters[0].Values to be present") |
| 177 | + |
| 178 | + var subnet awsv1.AWSResourceReference |
| 179 | + |
| 180 | + if len(mapiProviderSpec.Subnet.Filters) == 0 { |
| 181 | + subnet = awsv1.AWSResourceReference{ |
| 182 | + ID: mapiProviderSpec.Subnet.ID, |
| 183 | + } |
| 184 | + } else { |
| 185 | + subnet = awsv1.AWSResourceReference{ |
| 186 | + Filters: []awsv1.Filter{ |
| 187 | + { |
| 188 | + Name: "tag:Name", |
| 189 | + Values: mapiProviderSpec.Subnet.Filters[0].Values, |
| 190 | + }, |
| 191 | + }, |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + uncompressedUserData := true |
| 196 | + ami := awsv1.AMIReference{ |
| 197 | + ID: mapiProviderSpec.AMI.ID, |
| 198 | + } |
| 199 | + ignition := &awsv1.Ignition{ |
| 200 | + Version: "3.4", |
| 201 | + StorageType: awsv1.IgnitionStorageTypeOptionUnencryptedUserData, |
| 202 | + } |
| 203 | + additionalSecurityGroups := []awsv1.AWSResourceReference{ |
| 204 | + { |
| 205 | + Filters: []awsv1.Filter{ |
| 206 | + { |
| 207 | + Name: "tag:Name", |
| 208 | + Values: mapiProviderSpec.SecurityGroups[0].Filters[0].Values, |
| 209 | + }, |
| 210 | + }, |
| 211 | + }, |
| 212 | + { |
| 213 | + Filters: []awsv1.Filter{ |
| 214 | + { |
| 215 | + Name: "tag:Name", |
| 216 | + Values: mapiProviderSpec.SecurityGroups[1].Filters[0].Values, |
| 217 | + }, |
| 218 | + }, |
| 219 | + }, |
| 220 | + } |
| 221 | + awsmt := capiinfrastructurev1beta2resourcebuilder. |
| 222 | + AWSMachineTemplate(). |
| 223 | + WithUncompressedUserData(uncompressedUserData). |
| 224 | + WithIAMInstanceProfile(*mapiProviderSpec.IAMInstanceProfile.ID). |
| 225 | + WithInstanceType(mapiProviderSpec.InstanceType). |
| 226 | + WithAMI(ami). |
| 227 | + WithIgnition(ignition). |
| 228 | + WithSubnet(&subnet). |
| 229 | + WithAdditionalSecurityGroups(additionalSecurityGroups). |
| 230 | + WithName(awsMachineTemplateName). |
| 231 | + WithNamespace(framework.ClusterAPINamespace). |
| 232 | + Build() |
| 233 | + |
| 234 | + return awsmt |
| 235 | +} |
0 commit comments