Skip to content

Commit ee40fd4

Browse files
committed
Use the last 8 chars of zoneID when generating a failure domain name
Create unit test for v1beta1 conversion
1 parent 05df8c7 commit ee40fd4

File tree

3 files changed

+78
-11
lines changed

3 files changed

+78
-11
lines changed

api/v1beta1/conversion.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
corev1 "k8s.io/api/core/v1"
2323
conv "k8s.io/apimachinery/pkg/conversion"
2424
"sigs.k8s.io/cluster-api-provider-cloudstack/api/v1beta2"
25+
"sigs.k8s.io/cluster-api-provider-cloudstack/controllers"
2526
"sigs.k8s.io/cluster-api-provider-cloudstack/pkg/cloud"
2627
"sigs.k8s.io/controller-runtime/pkg/client"
2728
)
@@ -31,7 +32,7 @@ const DefaultEndpointCredential = "global"
3132
//nolint:golint,revive,stylecheck
3233
func Convert_v1beta1_CloudStackCluster_To_v1beta2_CloudStackCluster(in *CloudStackCluster, out *v1beta2.CloudStackCluster, s conv.Scope) error {
3334
out.ObjectMeta = in.ObjectMeta
34-
failureDomains, err := getFailureDomains(in)
35+
failureDomains, err := GetFailureDomains(in)
3536
if err != nil {
3637
return err
3738
}
@@ -80,8 +81,8 @@ func getZones(csCluster *v1beta2.CloudStackCluster) []Zone {
8081
return zones
8182
}
8283

83-
// getFailureDomains maps v1beta1 zones to v1beta2 failure domains.
84-
func getFailureDomains(csCluster *CloudStackCluster) ([]v1beta2.CloudStackFailureDomainSpec, error) {
84+
// GetFailureDomains maps v1beta1 zones to v1beta2 failure domains.
85+
func GetFailureDomains(csCluster *CloudStackCluster) ([]v1beta2.CloudStackFailureDomainSpec, error) {
8586
var failureDomains []v1beta2.CloudStackFailureDomainSpec
8687
namespace := csCluster.Namespace
8788
for _, zone := range csCluster.Spec.Zones {
@@ -103,7 +104,7 @@ func getFailureDomains(csCluster *CloudStackCluster) ([]v1beta2.CloudStackFailur
103104
Domain: csCluster.Spec.Domain,
104105
Account: csCluster.Spec.Account,
105106
ACSEndpoint: corev1.SecretReference{
106-
Namespace: csCluster.Namespace,
107+
Namespace: namespace,
107108
Name: DefaultEndpointCredential,
108109
},
109110
})
@@ -119,7 +120,7 @@ func getFailureDomains(csCluster *CloudStackCluster) ([]v1beta2.CloudStackFailur
119120
// When upgrading cluster using clusterctl directly, zoneID is fetched directly from kubernetes cluster in cloudstackzones.
120121
func GetDefaultFailureDomainName(namespace string, clusterName string, zoneID string, zoneName string) (string, error) {
121122
if len(zoneID) > 0 {
122-
return zoneID + "-" + clusterName, nil
123+
return WithZoneID(zoneID, clusterName), nil
123124
}
124125

125126
secret, err := GetK8sSecret(DefaultEndpointCredential, namespace)
@@ -130,14 +131,18 @@ func GetDefaultFailureDomainName(namespace string, clusterName string, zoneID st
130131
// try fetch zoneID using zoneName through cloudstack client
131132
zoneID, err = fetchZoneIDUsingCloudStack(secret, zoneName)
132133
if err == nil {
133-
return zoneID + "-" + clusterName, nil
134+
return WithZoneID(zoneID, clusterName), nil
134135
}
135136

136137
zoneID, err = fetchZoneIDUsingK8s(namespace, zoneName)
137138
if err != nil {
138139
return "", nil
139140
}
140-
return zoneID + "-" + clusterName, nil
141+
return WithZoneID(zoneID, clusterName), nil
142+
}
143+
144+
func WithZoneID(zoneID, clusterName string) string {
145+
return controllers.WithClusterSuffix(zoneID[len(zoneID)-8:], clusterName)
141146
}
142147

143148
func fetchZoneIDUsingK8s(namespace string, zoneName string) (string, error) {

api/v1beta1/conversion_test.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
Copyright 2022 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package v1beta1_test
18+
19+
import (
20+
. "github.com/onsi/ginkgo/v2"
21+
. "github.com/onsi/gomega"
22+
infrav1 "sigs.k8s.io/cluster-api-provider-cloudstack/api/v1beta1"
23+
capiv1 "sigs.k8s.io/cluster-api/api/v1beta1"
24+
)
25+
26+
var _ = Describe("Conversion", func() {
27+
BeforeEach(func() { // Reset test vars to initial state.
28+
})
29+
30+
Context("GetFailureDomains function", func() {
31+
It("Return the correct value when the last state update time is known", func() {
32+
csCluster := &infrav1.CloudStackCluster{
33+
Spec: infrav1.CloudStackClusterSpec{
34+
Zones: []infrav1.Zone{
35+
{
36+
Name: "zone-name1",
37+
Network: infrav1.Network{
38+
Name: "network1",
39+
},
40+
},
41+
{
42+
ID: "76472a84-d23f-4e97-b154-ee1b975ed936",
43+
Network: infrav1.Network{
44+
Name: "network1",
45+
},
46+
},
47+
},
48+
ControlPlaneEndpoint: capiv1.APIEndpoint{
49+
Host: "endpoint1",
50+
Port: 443,
51+
},
52+
Account: "account1",
53+
Domain: "domain1",
54+
},
55+
Status: infrav1.CloudStackClusterStatus{},
56+
}
57+
failureDomains, err := infrav1.GetFailureDomains(csCluster)
58+
Ω(err).ShouldNot(HaveOccurred())
59+
Ω(failureDomains).Should(HaveLen(2))
60+
})
61+
})
62+
})

controllers/cloudstackcluster_controller.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ func (r *CloudStackClusterReconciliationRunner) VerifyFailureDomainCRDs() (ctrl.
109109
for _, requiredFd := range r.ReconciliationSubject.Spec.FailureDomains {
110110
found := false
111111
for _, fd := range r.FailureDomains.Items {
112-
requiredFDName := withClusterSuffix(requiredFd.Name, r.CAPICluster.Name)
112+
requiredFDName := WithClusterSuffix(requiredFd.Name, r.CAPICluster.Name)
113113
if requiredFDName == fd.Name {
114114
found = true
115115
if !fd.Status.Ready {
@@ -125,8 +125,8 @@ func (r *CloudStackClusterReconciliationRunner) VerifyFailureDomainCRDs() (ctrl.
125125
return ctrl.Result{}, nil
126126
}
127127

128-
// withClusterSuffix appends a hyphen and the cluster name to a name if not already present.
129-
func withClusterSuffix(name string, clusterName string) string {
128+
// WithClusterSuffix appends a hyphen and the cluster name to a name if not already present.
129+
func WithClusterSuffix(name string, clusterName string) string {
130130
newName := name
131131
if !strings.HasSuffix(name, "-"+clusterName) { // Add cluster name suffix if missing.
132132
newName = name + "-" + clusterName
@@ -138,7 +138,7 @@ func withClusterSuffix(name string, clusterName string) string {
138138
func (r *CloudStackClusterReconciliationRunner) SetFailureDomainsStatusMap() (ctrl.Result, error) {
139139
r.ReconciliationSubject.Status.FailureDomains = clusterv1.FailureDomains{}
140140
for _, fdSpec := range r.ReconciliationSubject.Spec.FailureDomains {
141-
fdSpec.Name = withClusterSuffix(fdSpec.Name, r.CAPICluster.Name)
141+
fdSpec.Name = WithClusterSuffix(fdSpec.Name, r.CAPICluster.Name)
142142
r.ReconciliationSubject.Status.FailureDomains[fdSpec.Name] = clusterv1.FailureDomainSpec{ControlPlane: true}
143143
}
144144
return ctrl.Result{}, nil

0 commit comments

Comments
 (0)