-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
349 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,252 @@ | ||
package capi | ||
|
||
import ( | ||
"context" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
configv1 "github.com/openshift/api/config/v1" | ||
mapiv1 "github.com/openshift/api/machine/v1beta1" | ||
"github.com/openshift/cluster-api-actuator-pkg/pkg/framework" | ||
"github.com/openshift/cluster-api-actuator-pkg/pkg/framework/gatherer" | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
awsv1 "sigs.k8s.io/cluster-api-provider-aws/v2/api/v1beta2" | ||
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" | ||
|
||
"sigs.k8s.io/controller-runtime/pkg/client/config" | ||
|
||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
yaml "sigs.k8s.io/yaml" | ||
) | ||
|
||
const ( | ||
awsMachineTemplateName = "aws-machine-template" | ||
infrastructureName = "cluster" | ||
infraAPIVersion = "infrastructure.cluster.x-k8s.io/v1beta1" | ||
) | ||
|
||
var ( | ||
cl client.Client | ||
ctx = context.Background() | ||
platform configv1.PlatformType | ||
clusterName string | ||
oc *gatherer.CLI | ||
) | ||
|
||
var _ = Describe("Cluster API AWS MachineSet", framework.LabelCAPI, framework.LabelDisruptive, Ordered, func() { | ||
var ( | ||
awsMachineTemplate *awsv1.AWSMachineTemplate | ||
machineSetParams framework.CAPIMachineSetParams | ||
machineSet *clusterv1.MachineSet | ||
mapiDefaultProviderSpec *mapiv1.AWSMachineProviderConfig | ||
deleted bool | ||
err error | ||
) | ||
|
||
BeforeAll(func() { | ||
cfg, err := config.GetConfig() | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
cl, err = client.New(cfg, client.Options{}) | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
infra := &configv1.Infrastructure{} | ||
infraName := client.ObjectKey{ | ||
Name: infrastructureName, | ||
} | ||
Expect(cl.Get(ctx, infraName, infra)).To(Succeed()) | ||
Expect(infra.Status.PlatformStatus).ToNot(BeNil()) | ||
clusterName = infra.Status.InfrastructureName | ||
platform = infra.Status.PlatformStatus.Type | ||
deleted = false | ||
oc, _ = framework.NewCLI() | ||
framework.SkipIfNotTechPreviewNoUpgrade(oc, cl) | ||
_, mapiDefaultProviderSpec = getDefaultAWSMAPIProviderSpec(cl) | ||
machineSetParams = framework.NewCAPIMachineSetParams( | ||
"aws-machineset", | ||
clusterName, | ||
mapiDefaultProviderSpec.Placement.AvailabilityZone, | ||
1, | ||
corev1.ObjectReference{ | ||
Kind: "AWSMachineTemplate", | ||
APIVersion: infraAPIVersion, | ||
Name: awsMachineTemplateName, | ||
}, | ||
) | ||
|
||
if platform != configv1.AWSPlatformType { | ||
Skip("Skipping AWS E2E tests") | ||
} | ||
|
||
framework.CreateCoreCluster(ctx, cl, clusterName, "AWSCluster") | ||
}) | ||
|
||
BeforeEach(func() { | ||
deleted = false | ||
}) | ||
|
||
AfterEach(func() { | ||
if platform != configv1.AWSPlatformType { | ||
// Because AfterEach always runs, even when tests are skipped, we have to | ||
// explicitly skip it here for other platforms. | ||
Skip("Skipping AWS E2E tests") | ||
} | ||
if !deleted { | ||
framework.DeleteCAPIMachineSets(ctx, cl, machineSet) | ||
framework.WaitForCAPIMachineSetsDeleted(ctx, cl, machineSet) | ||
framework.DeleteObjects(ctx, cl, awsMachineTemplate) | ||
} | ||
}) | ||
|
||
//huliu-OCP-51071 - [CAPI] Create machineset with CAPI on aws | ||
It("should be able to run a machine with a default provider spec", func() { | ||
awsMachineTemplate = newAWSMachineTemplate(mapiDefaultProviderSpec) | ||
if err = cl.Create(ctx, awsMachineTemplate); err != nil { | ||
Expect(err).ToNot(HaveOccurred()) | ||
} | ||
machineSetParams = framework.UpdateCAPIMachineSetName("aws-machineset-51071", machineSetParams) | ||
machineSet, err = framework.CreateCAPIMachineSet(ctx, cl, machineSetParams) | ||
Expect(err).ToNot(HaveOccurred(), "Failed to create CAPI machineset") | ||
framework.WaitForCAPIMachinesRunning(ctx, cl, machineSet.Name) | ||
}) | ||
|
||
//huliu-OCP-75395 - [CAPI] AWS Placement group support. | ||
It("should be able to run a machine with cluster placement group", func() { | ||
awsClient := framework.CreateAWSClient(oc) | ||
placementGroupName := clusterName + "pgcluster" | ||
placementGroupID, err := awsClient.CreatePlacementGroup(placementGroupName, "cluster") | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(placementGroupID).ToNot(Equal("")) | ||
defer func() { | ||
framework.DeleteCAPIMachineSets(ctx, cl, machineSet) | ||
framework.WaitForCAPIMachineSetsDeleted(ctx, cl, machineSet) | ||
framework.DeleteObjects(ctx, cl, awsMachineTemplate) | ||
deleted = true | ||
_, err = awsClient.DeletePlacementGroup(placementGroupName) | ||
Expect(err).ToNot(HaveOccurred()) | ||
}() | ||
|
||
awsMachineTemplate = newAWSMachineTemplate(mapiDefaultProviderSpec) | ||
awsMachineTemplate.Spec.Template.Spec.PlacementGroupName = placementGroupName | ||
if err = cl.Create(ctx, awsMachineTemplate); err != nil { | ||
Expect(err).ToNot(HaveOccurred()) | ||
} | ||
machineSetParams = framework.UpdateCAPIMachineSetName("aws-machineset-75395", machineSetParams) | ||
machineSet, err = framework.CreateCAPIMachineSet(ctx, cl, machineSetParams) | ||
Expect(err).ToNot(HaveOccurred(), "Failed to create CAPI machineset") | ||
framework.WaitForCAPIMachinesRunning(ctx, cl, machineSet.Name) | ||
}) | ||
|
||
//huliu-OCP-75396 - [CAPI] Creating machines using KMS keys from AWS. | ||
It("should be able to run a machine using KMS keys", func() { | ||
awsMachineTemplate = newAWSMachineTemplate(mapiDefaultProviderSpec) | ||
region := mapiDefaultProviderSpec.Placement.Region | ||
if region != "us-east-1" && region != "us-east-2" { | ||
Skip("Region is " + region + ", skip this test scenario because we only created kms key in us-east-1/us-east-2 region") | ||
} | ||
var key string | ||
switch region { | ||
case "us-east-1": | ||
key = "arn:aws:kms:us-east-1:301721915996:key/c471ec83-cfaf-41a2-9241-d9e99c4da344" | ||
case "us-east-2": | ||
key = "arn:aws:kms:us-east-2:301721915996:key/c228ef83-df2c-4151-84c4-d9f39f39a972" | ||
} | ||
|
||
encryptBool := true | ||
awsMachineTemplate.Spec.Template.Spec.NonRootVolumes = []awsv1.Volume{ | ||
{ | ||
DeviceName: "/dev/xvda", | ||
Size: 140, | ||
Type: awsv1.VolumeTypeIO1, | ||
IOPS: 5000, | ||
Encrypted: &encryptBool, | ||
EncryptionKey: key, | ||
}, | ||
} | ||
if err := cl.Create(ctx, awsMachineTemplate); err != nil { | ||
Expect(err).ToNot(HaveOccurred()) | ||
} | ||
machineSetParams = framework.UpdateCAPIMachineSetName("aws-machineset-75396", machineSetParams) | ||
machineSet, err = framework.CreateCAPIMachineSet(ctx, cl, machineSetParams) | ||
Expect(err).ToNot(HaveOccurred(), "Failed to create CAPI machineset") | ||
framework.WaitForCAPIMachinesRunning(ctx, cl, machineSet.Name) | ||
}) | ||
}) | ||
|
||
func getDefaultAWSMAPIProviderSpec(cl client.Client) (*mapiv1.MachineSet, *mapiv1.AWSMachineProviderConfig) { | ||
machineSetList := &mapiv1.MachineSetList{} | ||
Expect(cl.List(ctx, machineSetList, client.InNamespace(framework.MachineAPINamespace))).To(Succeed()) | ||
|
||
Expect(machineSetList.Items).ToNot(HaveLen(0)) | ||
machineSet := &machineSetList.Items[0] | ||
Expect(machineSet.Spec.Template.Spec.ProviderSpec.Value).ToNot(BeNil()) | ||
|
||
providerSpec := &mapiv1.AWSMachineProviderConfig{} | ||
Expect(yaml.Unmarshal(machineSet.Spec.Template.Spec.ProviderSpec.Value.Raw, providerSpec)).To(Succeed()) | ||
|
||
return machineSet, providerSpec | ||
} | ||
|
||
func newAWSMachineTemplate(mapiProviderSpec *mapiv1.AWSMachineProviderConfig) *awsv1.AWSMachineTemplate { | ||
By("Creating AWS machine template") | ||
|
||
Expect(mapiProviderSpec).ToNot(BeNil()) | ||
Expect(mapiProviderSpec.IAMInstanceProfile).ToNot(BeNil()) | ||
Expect(mapiProviderSpec.IAMInstanceProfile.ID).ToNot(BeNil()) | ||
Expect(mapiProviderSpec.InstanceType).ToNot(BeEmpty()) | ||
Expect(mapiProviderSpec.Placement.AvailabilityZone).ToNot(BeEmpty()) | ||
Expect(mapiProviderSpec.AMI.ID).ToNot(BeNil()) | ||
Expect(mapiProviderSpec.Subnet.Filters).ToNot(HaveLen(0)) | ||
Expect(mapiProviderSpec.Subnet.Filters[0].Values).ToNot(HaveLen(0)) | ||
Expect(mapiProviderSpec.SecurityGroups).ToNot(HaveLen(0)) | ||
Expect(mapiProviderSpec.SecurityGroups[0].Filters).ToNot(HaveLen(0)) | ||
Expect(mapiProviderSpec.SecurityGroups[0].Filters[0].Values).ToNot(HaveLen(0)) | ||
|
||
uncompressedUserData := true | ||
|
||
awsMachineSpec := awsv1.AWSMachineSpec{ | ||
UncompressedUserData: &uncompressedUserData, | ||
IAMInstanceProfile: *mapiProviderSpec.IAMInstanceProfile.ID, | ||
InstanceType: mapiProviderSpec.InstanceType, | ||
AMI: awsv1.AMIReference{ | ||
ID: mapiProviderSpec.AMI.ID, | ||
}, | ||
Ignition: &awsv1.Ignition{ | ||
Version: "3.4", | ||
StorageType: awsv1.IgnitionStorageTypeOptionUnencryptedUserData, | ||
}, | ||
Subnet: &awsv1.AWSResourceReference{ | ||
Filters: []awsv1.Filter{ | ||
{ | ||
Name: "tag:Name", | ||
Values: mapiProviderSpec.Subnet.Filters[0].Values, | ||
}, | ||
}, | ||
}, | ||
AdditionalSecurityGroups: []awsv1.AWSResourceReference{ | ||
{ | ||
Filters: []awsv1.Filter{ | ||
{ | ||
Name: "tag:Name", | ||
Values: mapiProviderSpec.SecurityGroups[0].Filters[0].Values, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
awsMachineTemplate := &awsv1.AWSMachineTemplate{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: awsMachineTemplateName, | ||
Namespace: framework.ClusterAPINamespace, | ||
}, | ||
Spec: awsv1.AWSMachineTemplateSpec{ | ||
Template: awsv1.AWSMachineTemplateResource{ | ||
Spec: awsMachineSpec, | ||
}, | ||
}, | ||
} | ||
|
||
return awsMachineTemplate | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters