Skip to content

Commit fdb9734

Browse files
committed
fix: v1beta1 FailureDomain NPE
Signed-off-by: Suhas Agasthya <suhasagasthya@gmail.com>
1 parent b89d369 commit fdb9734

File tree

1 file changed

+6
-50
lines changed

1 file changed

+6
-50
lines changed

api/v1beta1/conversion.go

Lines changed: 6 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,14 @@ limitations under the License.
1717
package v1beta1
1818

1919
import (
20-
"context"
2120
"errors"
21+
"fmt"
22+
"strings"
2223

2324
corev1 "k8s.io/api/core/v1"
2425
machineryconversion "k8s.io/apimachinery/pkg/conversion"
25-
"sigs.k8s.io/controller-runtime/pkg/client"
2626

2727
infrav1 "sigs.k8s.io/cluster-api-provider-cloudstack/api/v1beta3"
28-
"sigs.k8s.io/cluster-api-provider-cloudstack/pkg/cloud"
2928
)
3029

3130
const DefaultEndpointCredential = "global"
@@ -137,56 +136,13 @@ func GetFailureDomains(csCluster *CloudStackCluster) ([]infrav1.CloudStackFailur
137136
// method to get zoneID by calling cloudstack API.
138137
// When upgrading cluster using clusterctl directly, zoneID is fetched directly from kubernetes cluster in cloudstackzones.
139138
func GetDefaultFailureDomainName(namespace string, zoneID string, zoneName string) (string, error) {
140-
if len(zoneID) > 0 {
139+
if len(strings.TrimSpace(zoneID)) > 0 {
141140
return zoneID, nil
142141
}
143142

144-
secret, err := GetK8sSecret(DefaultEndpointCredential, namespace)
145-
if err != nil {
146-
return "", err
147-
}
148-
149-
// try fetch zoneID using zoneName through cloudstack client.
150-
zoneID, err = fetchZoneIDUsingCloudStack(secret, zoneName)
151-
if err == nil {
152-
return zoneID, nil
153-
}
154-
155-
zoneID, err = fetchZoneIDUsingK8s(namespace, zoneName)
156-
if err != nil {
157-
return "", err
158-
}
159-
160-
return zoneID, nil
161-
}
162-
163-
func fetchZoneIDUsingK8s(namespace string, zoneName string) (string, error) {
164-
zone := &CloudStackZone{}
165-
key := client.ObjectKey{Name: zoneName, Namespace: namespace}
166-
if err := infrav1.K8sClient.Get(context.TODO(), key, zone); err != nil {
167-
return "", err
168-
}
169-
170-
return zone.Spec.ID, nil
171-
}
172-
173-
func fetchZoneIDUsingCloudStack(secret *corev1.Secret, zoneName string) (string, error) {
174-
client, err := cloud.NewClientFromK8sSecret(secret, nil)
175-
if err != nil {
176-
return "", err
177-
}
178-
zone := &infrav1.CloudStackZoneSpec{Name: zoneName}
179-
err = client.ResolveZone(zone)
180-
181-
return zone.ID, err
182-
}
183-
184-
func GetK8sSecret(name, namespace string) (*corev1.Secret, error) {
185-
endpointCredentials := &corev1.Secret{}
186-
key := client.ObjectKey{Name: name, Namespace: namespace}
187-
if err := infrav1.K8sClient.Get(context.TODO(), key, endpointCredentials); err != nil {
188-
return nil, err
143+
if len(strings.TrimSpace(zoneName)) > 0 {
144+
return strings.ToLower(zoneName), nil
189145
}
190146

191-
return endpointCredentials, nil
147+
return "", fmt.Errorf("cannot derive failure domain name: both zone ID and zone name are empty (namespace=%s)", namespace)
192148
}

0 commit comments

Comments
 (0)