Skip to content

Commit 918112b

Browse files
authored
Support affinity group names when deploy VM (#65)
* Support affinity group names when deploy VM * replace affinitygroupnames with affinitygroupids per team discuss * Update unit test to use UUID for affinity group ids * roll back accidently commited local changes * Support affinity group names when deploy VM * replace affinitygroupnames with affinitygroupids per team discuss * Update unit test to use UUID for affinity group ids * roll back accidently commited local changes * use array instead of comma separated string for affinnity group ids * fix merge errors * add new line at file end * rollback - add new line at file end Co-authored-by: Yufei Wang <[email protected]>
1 parent 058e6f0 commit 918112b

8 files changed

+96
-3
lines changed

api/v1alpha3/cloudstackmachine_types.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ type CloudStackMachineSpec struct {
4444
// Optional details map for deployVirtualMachine
4545
Details map[string]string `json:"details,omitempty"`
4646

47+
// Optional affinitygroupids for deployVirtualMachine
48+
// +optional
49+
AffinityGroupIds []string `json:"affinitygroupids,omitempty"`
50+
4751
// The CS specific unique identifier. Of the form: fmt.Sprintf("cloudstack:///%s", CS Machine Id)
4852
// +optional
4953
ProviderID *string `json:"providerID,omitempty"`

api/v1alpha3/cloudstackmachine_webhook_test.go

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,32 @@ var _ = Describe("CloudStackMachine webhook", func() {
7777
})
7878
})
7979

80+
Context("When creating a CloudStackMachine with all attributes", func() {
81+
It("Should succeed", func() {
82+
ctx := context.Background()
83+
cloudStackMachine := &CloudStackMachine{
84+
TypeMeta: metav1.TypeMeta{
85+
APIVersion: "infrastructure.cluster.x-k8s.io/v1alpha3",
86+
Kind: "CloudStackMachine",
87+
},
88+
ObjectMeta: metav1.ObjectMeta{
89+
Name: "test-machine-3",
90+
Namespace: "default",
91+
},
92+
Spec: CloudStackMachineSpec{
93+
IdentityRef: &CloudStackIdentityReference{
94+
Kind: defaultIdentityRefKind,
95+
Name: "IdentitySecret",
96+
},
97+
Template: "Template",
98+
Offering: "Offering",
99+
AffinityGroupIds: []string{"41eeb6e4-946f-4a18-b543-b2184815f1e4"},
100+
},
101+
}
102+
Expect(k8sClient.Create(ctx, cloudStackMachine)).Should(Succeed())
103+
})
104+
})
105+
80106
Context("When creating a CloudStackMachine with missing Offering attribute", func() {
81107
It("Should be rejected by the validating webhooks", func() {
82108
ctx := context.Background()
@@ -159,7 +185,7 @@ var _ = Describe("CloudStackMachine webhook", func() {
159185
Kind: "CloudStackMachine",
160186
},
161187
ObjectMeta: metav1.ObjectMeta{
162-
Name: "test-machine-3",
188+
Name: "test-machine-4",
163189
Namespace: "default",
164190
},
165191
Spec: CloudStackMachineSpec{
@@ -172,6 +198,7 @@ var _ = Describe("CloudStackMachine webhook", func() {
172198
Details: map[string]string{
173199
"memoryOvercommitRatio": "1.2",
174200
},
201+
AffinityGroupIds: []string{"41eeb6e4-946f-4a18-b543-b2184815f1e4"},
175202
},
176203
}
177204
Expect(k8sClient.Create(ctx, cloudStackMachine)).Should(Succeed())
@@ -200,6 +227,10 @@ var _ = Describe("CloudStackMachine webhook", func() {
200227
cloudStackMachine.DeepCopyInto(cloudStackMachineUpdate)
201228
cloudStackMachineUpdate.Spec.IdentityRef.Name = "IdentityConfigMap"
202229
Expect(k8sClient.Update(ctx, cloudStackMachineUpdate).Error()).Should(MatchRegexp(forbiddenRegex, "identityRef\\.Name"))
230+
231+
cloudStackMachine.DeepCopyInto(cloudStackMachineUpdate)
232+
cloudStackMachineUpdate.Spec.AffinityGroupIds = []string{"28b907b8-75a7-4214-bd3d-6c61961fc2af"}
233+
Expect(k8sClient.Update(ctx, cloudStackMachineUpdate)).Should(Succeed())
203234
})
204235
})
205236
})

api/v1alpha3/cloudstackmachinetemplate_webhook_test.go

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,40 @@ var _ = Describe("CloudStackMachineTemplate webhook", func() {
9393
})
9494
})
9595

96+
Context("When creating a CloudStackMachineTemplate with all attributes", func() {
97+
It("Should succeed", func() {
98+
ctx := context.Background()
99+
cloudStackMachineTemplate := &CloudStackMachineTemplate{
100+
TypeMeta: metav1.TypeMeta{
101+
APIVersion: "infrastructure.cluster.x-k8s.io/v1alpha3",
102+
Kind: "CloudStackMachineTemplate",
103+
},
104+
ObjectMeta: metav1.ObjectMeta{
105+
Name: "test-machinetemplate-3",
106+
Namespace: "default",
107+
},
108+
Spec: CloudStackMachineTemplateSpec{
109+
Spec: CloudStackMachineTemplateResource{
110+
ObjectMeta: metav1.ObjectMeta{
111+
Name: "test-machinetemplateresource",
112+
Namespace: "default",
113+
},
114+
Spec: CloudStackMachineSpec{
115+
IdentityRef: &CloudStackIdentityReference{
116+
Kind: defaultIdentityRefKind,
117+
Name: "IdentitySecret",
118+
},
119+
Template: "Template",
120+
Offering: "Offering",
121+
AffinityGroupIds: []string{"41eeb6e4-946f-4a18-b543-b2184815f1e4"},
122+
},
123+
},
124+
},
125+
}
126+
Expect(k8sClient.Create(ctx, cloudStackMachineTemplate)).Should(Succeed())
127+
})
128+
})
129+
96130
Context("When creating a CloudStackMachineTemplate with missing Offering attribute", func() {
97131
It("Should be rejected by the validating webhooks", func() {
98132
ctx := context.Background()
@@ -199,7 +233,7 @@ var _ = Describe("CloudStackMachineTemplate webhook", func() {
199233
Kind: "CloudStackMachineTemplate",
200234
},
201235
ObjectMeta: metav1.ObjectMeta{
202-
Name: "test-machinetemplate-3",
236+
Name: "test-machinetemplate-4",
203237
Namespace: "default",
204238
},
205239
Spec: CloudStackMachineTemplateSpec{
@@ -218,6 +252,7 @@ var _ = Describe("CloudStackMachineTemplate webhook", func() {
218252
Details: map[string]string{
219253
"memoryOvercommitRatio": "1.2",
220254
},
255+
AffinityGroupIds: []string{"41eeb6e4-946f-4a18-b543-b2184815f1e4"},
221256
},
222257
},
223258
},
@@ -248,6 +283,10 @@ var _ = Describe("CloudStackMachineTemplate webhook", func() {
248283
cloudStackMachineTemplate.DeepCopyInto(cloudStackMachineTemplateUpdate)
249284
cloudStackMachineTemplateUpdate.Spec.Spec.Spec.IdentityRef.Name = "IdentityConfigMap"
250285
Expect(k8sClient.Update(ctx, cloudStackMachineTemplateUpdate).Error()).Should(MatchRegexp(forbiddenRegex, "identityRef\\.Name"))
286+
287+
cloudStackMachineTemplate.DeepCopyInto(cloudStackMachineTemplateUpdate)
288+
cloudStackMachineTemplateUpdate.Spec.Spec.Spec.AffinityGroupIds = []string{"28b907b8-75a7-4214-bd3d-6c61961fc2ag"}
289+
Expect(k8sClient.Update(ctx, cloudStackMachineTemplateUpdate)).Should(Succeed())
251290
})
252291
})
253292
})

api/v1alpha3/zz_generated.deepcopy.go

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/crd/bases/infrastructure.cluster.x-k8s.io_cloudstackmachines.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ spec:
6161
spec:
6262
description: CloudStackMachineSpec defines the desired state of CloudStackMachine
6363
properties:
64+
affinitygroupids:
65+
description: Optional affinitygroupids for deployVirtualMachine
66+
items:
67+
type: string
68+
type: array
6469
details:
6570
additionalProperties:
6671
type: string

config/crd/bases/infrastructure.cluster.x-k8s.io_cloudstackmachinetemplates.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ spec:
4747
description: CloudStackMachineSpec defines the desired state of
4848
CloudStackMachine
4949
properties:
50+
affinitygroupids:
51+
description: Optional affinitygroupids for deployVirtualMachine
52+
items:
53+
type: string
54+
type: array
5055
details:
5156
additionalProperties:
5257
type: string

config/default/manager_image_patch.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ spec:
1717
volumes:
1818
- name: cloud-config
1919
secret:
20-
secretName: capc-cloudstack-secret
20+
secretName: capc-cloudstack-secret

pkg/cloud/instance.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ func (c *client) GetOrCreateVMInstance(
108108
setIfNotEmpty(csMachine.Name, p.SetDisplayname)
109109
setIfNotEmpty(csMachine.Spec.SSHKey, p.SetKeypair)
110110
setIfNotEmpty(userData, p.SetUserdata)
111+
112+
if len(csMachine.Spec.AffinityGroupIds) > 0 {
113+
p.SetAffinitygroupids(csMachine.Spec.AffinityGroupIds)
114+
}
111115
// If this VM instance is a control plane, consider setting it's IP.
112116
_, isControlPlanceMachine := machine.ObjectMeta.Labels["cluster.x-k8s.io/control-plane"]
113117
if isControlPlanceMachine && csCluster.Status.NetworkType == NetworkTypeShared {

0 commit comments

Comments
 (0)