@@ -19,6 +19,8 @@ package v1beta1
1919import (
2020 "context"
2121 "errors"
22+ "fmt"
23+ "strings"
2224
2325 corev1 "k8s.io/api/core/v1"
2426 machineryconversion "k8s.io/apimachinery/pkg/conversion"
@@ -137,13 +139,13 @@ func GetFailureDomains(csCluster *CloudStackCluster) ([]infrav1.CloudStackFailur
137139// method to get zoneID by calling cloudstack API.
138140// When upgrading cluster using clusterctl directly, zoneID is fetched directly from kubernetes cluster in cloudstackzones.
139141func GetDefaultFailureDomainName (namespace string , zoneID string , zoneName string ) (string , error ) {
140- if len (zoneID ) > 0 {
142+ if len (strings . TrimSpace ( zoneID ) ) > 0 {
141143 return zoneID , nil
142144 }
143145
144146 secret , err := GetK8sSecret (DefaultEndpointCredential , namespace )
145147 if err != nil {
146- return "" , err
148+ return "" , fmt . Errorf ( "failed to get secret for namespace %s: %w" , namespace , err )
147149 }
148150
149151 // try fetch zoneID using zoneName through cloudstack client.
@@ -161,6 +163,9 @@ func GetDefaultFailureDomainName(namespace string, zoneID string, zoneName strin
161163}
162164
163165func fetchZoneIDUsingK8s (namespace string , zoneName string ) (string , error ) {
166+ if infrav1 .K8sClient == nil {
167+ return "" , fmt .Errorf ("k8s client not initialized" )
168+ }
164169 zone := & CloudStackZone {}
165170 key := client.ObjectKey {Name : zoneName , Namespace : namespace }
166171 if err := infrav1 .K8sClient .Get (context .TODO (), key , zone ); err != nil {
@@ -182,11 +187,13 @@ func fetchZoneIDUsingCloudStack(secret *corev1.Secret, zoneName string) (string,
182187}
183188
184189func GetK8sSecret (name , namespace string ) (* corev1.Secret , error ) {
190+ if infrav1 .K8sClient == nil {
191+ return nil , fmt .Errorf ("k8s client not initialized" )
192+ }
185193 endpointCredentials := & corev1.Secret {}
186194 key := client.ObjectKey {Name : name , Namespace : namespace }
187195 if err := infrav1 .K8sClient .Get (context .TODO (), key , endpointCredentials ); err != nil {
188196 return nil , err
189197 }
190-
191198 return endpointCredentials , nil
192199}
0 commit comments