Skip to content

Commit 5630c69

Browse files
Merge pull request #41 from aws/feature/failure-domains-template-offering
Change template and offering to explicitly specify ID or Name
2 parents f2257db + 9a88f4c commit 5630c69

14 files changed

+660
-103
lines changed

.gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
*.dylib
77
bin
88
testbin/*
9-
api/*/zz*generated.deepcopy.go
109

1110
nginx.conf
1211
nginx.conf.bak

api/v1beta1/cloudstackmachine_types.go

+12-2
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ type CloudStackMachineSpec struct {
3838
InstanceID *string `json:"instanceID,omitempty"`
3939

4040
// CloudStack compute offering.
41-
Offering string `json:"offering"`
41+
Offering CloudStackResourceIdentifier `json:"offering"`
4242

4343
// CloudStack template to use.
44-
Template string `json:"template"`
44+
Template CloudStackResourceIdentifier `json:"template"`
4545

4646
// CloudStack ssh key to use.
4747
// +optional
@@ -75,6 +75,16 @@ type CloudStackMachineSpec struct {
7575
IdentityRef *CloudStackIdentityReference `json:"identityRef,omitempty"`
7676
}
7777

78+
type CloudStackResourceIdentifier struct {
79+
// Cloudstack resource ID.
80+
// +optional
81+
ID string `json:"id,omitempty"`
82+
83+
// Cloudstack resource Name
84+
// +optional
85+
Name string `json:"name,omitempty"`
86+
}
87+
7888
// TODO: Review the use of this field/type.
7989
type InstanceState string
8090

api/v1beta1/cloudstackmachine_webhook.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ func (r *CloudStackMachine) ValidateCreate() error {
6363
errorList = append(errorList, field.Forbidden(field.NewPath("spec", "identityRef", "kind"), "must be a Secret"))
6464
}
6565

66-
errorList = webhookutil.EnsureFieldExists(r.Spec.Offering, "Offering", errorList)
67-
errorList = webhookutil.EnsureFieldExists(r.Spec.Template, "Template", errorList)
66+
errorList = webhookutil.EnsureAtLeastOneFieldExists(r.Spec.Offering.ID, r.Spec.Offering.Name, "Offering", errorList)
67+
errorList = webhookutil.EnsureAtLeastOneFieldExists(r.Spec.Template.ID, r.Spec.Template.Name, "Template", errorList)
6868

6969
return webhookutil.AggregateObjErrors(r.GroupVersionKind().GroupKind(), r.Name, errorList)
7070
}
@@ -81,9 +81,9 @@ func (r *CloudStackMachine) ValidateUpdate(old runtime.Object) error {
8181
}
8282
oldSpec := oldMachine.Spec
8383

84-
errorList = webhookutil.EnsureStringFieldsAreEqual(r.Spec.Offering, oldSpec.Offering, "offering", errorList)
84+
errorList = webhookutil.EnsureBothFieldsAreEqual(r.Spec.Offering.ID, r.Spec.Offering.Name, oldSpec.Offering.ID, oldSpec.Offering.Name, "offering", errorList)
8585
errorList = webhookutil.EnsureStringFieldsAreEqual(r.Spec.SSHKey, oldSpec.SSHKey, "sshkey", errorList)
86-
errorList = webhookutil.EnsureStringFieldsAreEqual(r.Spec.Template, oldSpec.Template, "template", errorList)
86+
errorList = webhookutil.EnsureBothFieldsAreEqual(r.Spec.Template.ID, r.Spec.Template.Name, oldSpec.Template.ID, oldSpec.Template.Name, "template", errorList)
8787
errorList = webhookutil.EnsureStringStringMapFieldsAreEqual(&r.Spec.Details, &oldSpec.Details, "details", errorList)
8888
if r.Spec.IdentityRef != nil && oldSpec.IdentityRef != nil {
8989
errorList = webhookutil.EnsureStringFieldsAreEqual(

api/v1beta1/cloudstackmachine_webhook_test.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package v1beta1_test
1818

1919
import (
2020
"context"
21+
"github.com/aws/cluster-api-provider-cloudstack/api/v1beta1"
2122

2223
"github.com/aws/cluster-api-provider-cloudstack/test/dummies"
2324
. "github.com/onsi/ginkgo"
@@ -41,13 +42,13 @@ var _ = Describe("CloudStackMachine webhook", func() {
4142
})
4243

4344
It("should reject a CloudStackMachine with missing Offering attribute", func() {
44-
dummies.CSMachine1.Spec.Offering = ""
45+
dummies.CSMachine1.Spec.Offering = v1beta1.CloudStackResourceIdentifier{ID: "", Name: ""}
4546
Expect(k8sClient.Create(ctx, dummies.CSMachine1)).
4647
Should(MatchError(MatchRegexp(requiredRegex, "Offering")))
4748
})
4849

4950
It("should reject a CloudStackMachine with missing Template attribute", func() {
50-
dummies.CSMachine1.Spec.Template = ""
51+
dummies.CSMachine1.Spec.Template = v1beta1.CloudStackResourceIdentifier{ID: "", Name: ""}
5152
Expect(k8sClient.Create(ctx, dummies.CSMachine1)).
5253
Should(MatchError(MatchRegexp(requiredRegex, "Template")))
5354
})
@@ -65,13 +66,13 @@ var _ = Describe("CloudStackMachine webhook", func() {
6566
})
6667

6768
It("should reject VM offering updates to the CloudStackMachine", func() {
68-
dummies.CSMachine1.Spec.Offering = "ArbitraryUpdateOffering"
69+
dummies.CSMachine1.Spec.Offering = v1beta1.CloudStackResourceIdentifier{Name: "ArbitraryUpdateOffering"}
6970
Ω(k8sClient.Update(ctx, dummies.CSMachine1)).
7071
Should(MatchError(MatchRegexp(forbiddenRegex, "offering")))
7172
})
7273

7374
It("should reject VM template updates to the CloudStackMachine", func() {
74-
dummies.CSMachine1.Spec.Template = "ArbitraryUpdateTemplate"
75+
dummies.CSMachine1.Spec.Template = v1beta1.CloudStackResourceIdentifier{Name: "ArbitraryUpdateTemplate"}
7576
Ω(k8sClient.Update(ctx, dummies.CSMachine1)).
7677
Should(MatchError(MatchRegexp(forbiddenRegex, "template")))
7778
})

api/v1beta1/cloudstackmachinetemplate_webhook.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ func (r *CloudStackMachineTemplate) ValidateCreate() error {
7676
"AffinityGroupIDs cannot be specified when Affinity is specified as anything but `no`"))
7777
}
7878

79-
errorList = webhookutil.EnsureFieldExists(spec.Offering, "Offering", errorList)
80-
errorList = webhookutil.EnsureFieldExists(spec.Template, "Template", errorList)
79+
errorList = webhookutil.EnsureAtLeastOneFieldExists(spec.Offering.ID, spec.Offering.Name, "Offering", errorList)
80+
errorList = webhookutil.EnsureAtLeastOneFieldExists(spec.Template.ID, spec.Template.Name, "Template", errorList)
8181

8282
return webhookutil.AggregateObjErrors(r.GroupVersionKind().GroupKind(), r.Name, errorList)
8383
}
@@ -96,9 +96,9 @@ func (r *CloudStackMachineTemplate) ValidateUpdate(old runtime.Object) error {
9696
oldSpec := oldMachineTemplate.Spec.Spec.Spec
9797

9898
errorList := field.ErrorList(nil)
99-
errorList = webhookutil.EnsureStringFieldsAreEqual(spec.Offering, oldSpec.Offering, "offering", errorList)
99+
errorList = webhookutil.EnsureBothFieldsAreEqual(spec.Offering.ID, spec.Offering.Name, oldSpec.Offering.ID, oldSpec.Offering.Name, "offering", errorList)
100100
errorList = webhookutil.EnsureStringFieldsAreEqual(spec.SSHKey, oldSpec.SSHKey, "sshkey", errorList)
101-
errorList = webhookutil.EnsureStringFieldsAreEqual(spec.Template, oldSpec.Template, "template", errorList)
101+
errorList = webhookutil.EnsureBothFieldsAreEqual(spec.Template.ID, spec.Template.Name, oldSpec.Template.ID, oldSpec.Template.Name, "template", errorList)
102102
errorList = webhookutil.EnsureStringStringMapFieldsAreEqual(&spec.Details, &oldSpec.Details, "details", errorList)
103103
errorList = webhookutil.EnsureStringFieldsAreEqual(spec.Affinity, oldSpec.Affinity, "affinity", errorList)
104104

api/v1beta1/cloudstackmachinetemplate_webhook_test.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package v1beta1_test
1818

1919
import (
2020
"context"
21+
"github.com/aws/cluster-api-provider-cloudstack/api/v1beta1"
2122

2223
"github.com/aws/cluster-api-provider-cloudstack/test/dummies"
2324
. "github.com/onsi/ginkgo"
@@ -41,13 +42,13 @@ var _ = Describe("CloudStackMachineTemplate webhook", func() {
4142
})
4243

4344
It("Should reject a CloudStackMachineTemplate when missing the VM Offering attribute", func() {
44-
dummies.CSMachineTemplate1.Spec.Spec.Spec.Offering = ""
45+
dummies.CSMachineTemplate1.Spec.Spec.Spec.Offering = v1beta1.CloudStackResourceIdentifier{Name: "", ID: ""}
4546
Expect(k8sClient.Create(ctx, dummies.CSMachineTemplate1)).
4647
Should(MatchError(MatchRegexp(requiredRegex, "Offering")))
4748
})
4849

4950
It("Should reject a CloudStackMachineTemplate when missing the VM Template attribute", func() {
50-
dummies.CSMachineTemplate1.Spec.Spec.Spec.Template = ""
51+
dummies.CSMachineTemplate1.Spec.Spec.Spec.Template = v1beta1.CloudStackResourceIdentifier{Name: "", ID: ""}
5152
Expect(k8sClient.Create(ctx, dummies.CSMachineTemplate1)).
5253
Should(MatchError(MatchRegexp(requiredRegex, "Template")))
5354
})
@@ -65,13 +66,13 @@ var _ = Describe("CloudStackMachineTemplate webhook", func() {
6566
})
6667

6768
It("should reject VM template updates to the CloudStackMachineTemplate", func() {
68-
dummies.CSMachineTemplate1.Spec.Spec.Spec.Template = "ArbitraryUpdateTemplate"
69+
dummies.CSMachineTemplate1.Spec.Spec.Spec.Template = v1beta1.CloudStackResourceIdentifier{Name: "ArbitraryUpdateTemplate"}
6970
Ω(k8sClient.Update(ctx, dummies.CSMachineTemplate1)).
7071
Should(MatchError(MatchRegexp(forbiddenRegex, "template")))
7172
})
7273

7374
It("should reject VM offering updates to the CloudStackMachineTemplate", func() {
74-
dummies.CSMachineTemplate1.Spec.Spec.Spec.Offering = "Offering2"
75+
dummies.CSMachineTemplate1.Spec.Spec.Spec.Offering = v1beta1.CloudStackResourceIdentifier{Name: "Offering2"}
7576
Ω(k8sClient.Update(ctx, dummies.CSMachineTemplate1)).
7677
Should(MatchError(MatchRegexp(forbiddenRegex, "offering")))
7778
})

0 commit comments

Comments
 (0)