Skip to content

Commit a195930

Browse files
committed
Added test for preemptible and GPU instances
refractor for onHost constant values
1 parent e128242 commit a195930

File tree

1 file changed

+94
-10
lines changed

1 file changed

+94
-10
lines changed

pkg/capi/gcp.go

+94-10
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ import (
2020
)
2121

2222
const (
23-
infraAPIVersion = "infrastructure.cluster.x-k8s.io/v1beta1"
24-
gcpMachineTemplateName = "gcp-machine-template"
23+
infraAPIVersion = "infrastructure.cluster.x-k8s.io/v1beta1"
24+
gcpMachineTemplateName = "gcp-machine-template"
25+
OnHostMaintenanceTerminate = "Terminate"
26+
OnHostMaintenanceMigrate = "Migrate"
2527
)
2628

2729
var (
@@ -77,7 +79,9 @@ var _ = Describe("Cluster API GCP MachineSet", framework.LabelCAPI, framework.La
7779
Expect(mapiProviderSpec).ToNot(BeNil())
7880
gcpMachineTemplate = createGCPMachineTemplate(mapiProviderSpec)
7981
gcpMachineTemplate.Spec.Template.Spec.RootDeviceType = &expectedDiskType
80-
*gcpMachineTemplate.Spec.Template.Spec.OnHostMaintenance = gcpv1.HostMaintenancePolicyTerminate
82+
mapiProviderSpec.OnHostMaintenance = OnHostMaintenanceTerminate
83+
gcpMachineTemplate.Spec.Template.Spec.OnHostMaintenance = (*gcpv1.HostMaintenancePolicy)(&mapiProviderSpec.OnHostMaintenance)
84+
8185
if err := cl.Create(ctx, gcpMachineTemplate); err != nil && !apierrors.IsAlreadyExists(err) {
8286
Expect(err).ToNot(HaveOccurred())
8387
}
@@ -107,7 +111,8 @@ var _ = Describe("Cluster API GCP MachineSet", framework.LabelCAPI, framework.La
107111
mapiProviderSpec := getGCPMAPIProviderSpec(cl)
108112
Expect(mapiProviderSpec).ToNot(BeNil())
109113
gcpMachineTemplate = createGCPMachineTemplate(mapiProviderSpec)
110-
*gcpMachineTemplate.Spec.Template.Spec.OnHostMaintenance = gcpv1.HostMaintenancePolicyMigrate
114+
mapiProviderSpec.OnHostMaintenance = OnHostMaintenanceMigrate
115+
gcpMachineTemplate.Spec.Template.Spec.OnHostMaintenance = (*gcpv1.HostMaintenancePolicy)(&mapiProviderSpec.OnHostMaintenance)
111116
gcpMachineTemplate.Spec.Template.Spec.ShieldedInstanceConfig = &gcpv1.GCPShieldedInstanceConfig{
112117
SecureBoot: enableSecureBoot,
113118
VirtualizedTrustedPlatformModule: enableVtpm,
@@ -167,7 +172,7 @@ var _ = Describe("Cluster API GCP MachineSet", framework.LabelCAPI, framework.La
167172
gcpMachineTemplate.Spec.Template.Spec.InstanceType = "n2d-standard-4"
168173

169174
if *confidentialCompute == gcpv1.ConfidentialComputePolicyEnabled {
170-
mapiProviderSpec.OnHostMaintenance = "Terminate"
175+
mapiProviderSpec.OnHostMaintenance = OnHostMaintenanceTerminate
171176
gcpMachineTemplate.Spec.Template.Spec.OnHostMaintenance = (*gcpv1.HostMaintenancePolicy)(&mapiProviderSpec.OnHostMaintenance)
172177
} else {
173178
mapiProviderSpec.OnHostMaintenance = "Migrate"
@@ -206,6 +211,86 @@ var _ = Describe("Cluster API GCP MachineSet", framework.LabelCAPI, framework.La
206211
Entry("Confidential Compute enabled", &confidentialComputeEnabled),
207212
Entry("Confidential Compute disabled", &confidentialComputeDisabled),
208213
)
214+
// OCP-74732 GPU machine can be provisioned successfully by capi machineset
215+
// author: [email protected]
216+
It("should provision GPU machine successfully", func() {
217+
mapiProviderSpec := getGCPMAPIProviderSpec(cl)
218+
Expect(mapiProviderSpec).ToNot(BeNil())
219+
gcpMachineTemplate = createGCPMachineTemplate(mapiProviderSpec)
220+
gcpMachineTemplate.Spec.Template.Spec.InstanceType = "a2-highgpu-1g"
221+
mapiProviderSpec.OnHostMaintenance = OnHostMaintenanceTerminate
222+
gcpMachineTemplate.Spec.Template.Spec.OnHostMaintenance = (*gcpv1.HostMaintenancePolicy)(&mapiProviderSpec.OnHostMaintenance)
223+
224+
if err := cl.Create(ctx, gcpMachineTemplate); err != nil && !apierrors.IsAlreadyExists(err) {
225+
Expect(err).ToNot(HaveOccurred())
226+
}
227+
228+
By("Creating a MachineSet for GPU machine")
229+
machineSet, err = framework.CreateCAPIMachineSet(ctx, cl, framework.NewCAPIMachineSetParams(
230+
"gcp-machineset-gpu-74732",
231+
clusterName,
232+
mapiProviderSpec.Zone,
233+
1,
234+
corev1.ObjectReference{
235+
Kind: "GCPMachineTemplate",
236+
APIVersion: infraAPIVersion,
237+
Name: gcpMachineTemplateName,
238+
},
239+
))
240+
Expect(err).ToNot(HaveOccurred(), "Failed to create CAPI MachineSet with GPU instanceType")
241+
242+
framework.WaitForCAPIMachinesRunning(ctx, cl, machineSet.Name)
243+
244+
By("Verifying the GPU machinetype configuration on the created GCP MachineTemplate")
245+
createdTemplate := &gcpv1.GCPMachineTemplate{}
246+
Expect(cl.Get(framework.GetContext(), client.ObjectKey{
247+
Namespace: framework.ClusterAPINamespace,
248+
Name: gcpMachineTemplateName,
249+
}, createdTemplate)).To(Succeed())
250+
var machineType = createdTemplate.Spec.Template.Spec.InstanceType
251+
Expect(machineType).To(Equal("a2-highgpu-1g"))
252+
},
253+
)
254+
// OCP-75792 Preemptible machines can be provisioned successfully by capi machineset
255+
// author: [email protected]
256+
It("should provision Preemptible machine successfully", func() {
257+
mapiProviderSpec := getGCPMAPIProviderSpec(cl)
258+
Expect(mapiProviderSpec).ToNot(BeNil())
259+
gcpMachineTemplate = createGCPMachineTemplate(mapiProviderSpec)
260+
gcpMachineTemplate.Spec.Template.Spec.Preemptible = true
261+
mapiProviderSpec.OnHostMaintenance = OnHostMaintenanceTerminate
262+
gcpMachineTemplate.Spec.Template.Spec.OnHostMaintenance = (*gcpv1.HostMaintenancePolicy)(&mapiProviderSpec.OnHostMaintenance)
263+
264+
if err := cl.Create(ctx, gcpMachineTemplate); err != nil && !apierrors.IsAlreadyExists(err) {
265+
Expect(err).ToNot(HaveOccurred())
266+
}
267+
268+
By("Creating a MachineSet for preeemptible machine")
269+
machineSet, err = framework.CreateCAPIMachineSet(ctx, cl, framework.NewCAPIMachineSetParams(
270+
"gcp-machineset-preemptible-75792",
271+
clusterName,
272+
mapiProviderSpec.Zone,
273+
1,
274+
corev1.ObjectReference{
275+
Kind: "GCPMachineTemplate",
276+
APIVersion: infraAPIVersion,
277+
Name: gcpMachineTemplateName,
278+
},
279+
))
280+
Expect(err).ToNot(HaveOccurred(), "Failed to create CAPI MachineSet with preemptible instanceType")
281+
282+
framework.WaitForCAPIMachinesRunning(ctx, cl, machineSet.Name)
283+
284+
By("Verifying the preemptible machinetype configuration on the created GCP MachineTemplate")
285+
createdTemplate := &gcpv1.GCPMachineTemplate{}
286+
Expect(cl.Get(framework.GetContext(), client.ObjectKey{
287+
Namespace: framework.ClusterAPINamespace,
288+
Name: gcpMachineTemplateName,
289+
}, createdTemplate)).To(Succeed())
290+
var preemptible = createdTemplate.Spec.Template.Spec.Preemptible
291+
Expect(preemptible).To(Equal(true))
292+
})
293+
209294
})
210295

211296
func getGCPMAPIProviderSpec(cl client.Client) *mapiv1.GCPMachineProviderSpec {
@@ -246,11 +331,10 @@ func createGCPMachineTemplate(mapiProviderSpec *mapiv1.GCPMachineProviderSpec) *
246331
ipForwardingDisabled := gcpv1.IPForwardingDisabled
247332

248333
gcpMachineSpec := gcpv1.GCPMachineSpec{
249-
RootDeviceSize: mapiProviderSpec.Disks[0].SizeGB,
250-
InstanceType: mapiProviderSpec.MachineType,
251-
OnHostMaintenance: (*gcpv1.HostMaintenancePolicy)(&mapiProviderSpec.OnHostMaintenance),
252-
Image: &mapiProviderSpec.Disks[0].Image,
253-
Subnet: &mapiProviderSpec.NetworkInterfaces[0].Subnetwork,
334+
RootDeviceSize: mapiProviderSpec.Disks[0].SizeGB,
335+
InstanceType: mapiProviderSpec.MachineType,
336+
Image: &mapiProviderSpec.Disks[0].Image,
337+
Subnet: &mapiProviderSpec.NetworkInterfaces[0].Subnetwork,
254338
ServiceAccount: &gcpv1.ServiceAccount{
255339
Email: mapiProviderSpec.ServiceAccounts[0].Email,
256340
Scopes: mapiProviderSpec.ServiceAccounts[0].Scopes,

0 commit comments

Comments
 (0)