@@ -18,13 +18,11 @@ package v1beta1
18
18
19
19
import (
20
20
"context"
21
- "fmt"
22
21
23
- "github.com/apache/cloudstack-go/v2/cloudstack"
24
- "github.com/pkg/errors"
25
22
corev1 "k8s.io/api/core/v1"
26
23
conv "k8s.io/apimachinery/pkg/conversion"
27
24
"sigs.k8s.io/cluster-api-provider-cloudstack/api/v1beta2"
25
+ "sigs.k8s.io/cluster-api-provider-cloudstack/pkg/cloud"
28
26
"sigs.k8s.io/controller-runtime/pkg/client"
29
27
)
30
28
@@ -124,8 +122,13 @@ func GetDefaultFailureDomainName(namespace string, clusterName string, zoneID st
124
122
return zoneID + "-" + clusterName , nil
125
123
}
126
124
125
+ secret , err := GetK8sSecret (DefaultEndpointCredential , namespace )
126
+ if err != nil {
127
+ return "" , err
128
+ }
129
+
127
130
// try fetch zoneID using zoneName through cloudstack client
128
- zoneID , err : = fetchZoneIDUsingCloudStack (namespace , zoneName )
131
+ zoneID , err = fetchZoneIDUsingCloudStack (secret , zoneName )
129
132
if err == nil {
130
133
return zoneID + "-" + clusterName , nil
131
134
}
@@ -147,41 +150,21 @@ func fetchZoneIDUsingK8s(namespace string, zoneName string) (string, error) {
147
150
return zone .Spec .ID , nil
148
151
}
149
152
150
- func fetchZoneIDUsingCloudStack (namespace string , zoneName string ) (string , error ) {
151
- config , err := GetCloudStackConfig ( namespace )
153
+ func fetchZoneIDUsingCloudStack (secret * corev1. Secret , zoneName string ) (string , error ) {
154
+ client , err := cloud . NewClientFromK8sSecret ( secret , nil )
152
155
if err != nil {
153
156
return "" , err
154
157
}
155
-
156
- csClient := cloudstack .NewAsyncClient (fmt .Sprint (config ["api-url" ]), fmt .Sprint (config ["api-key" ]), fmt .Sprint (config ["secret-key" ]), fmt .Sprint (config ["verify-ssl" ]) == "true" )
157
-
158
- if zoneID , count , err := csClient .Zone .GetZoneID (zoneName ); err != nil {
159
- return "" , err
160
- } else if count != 1 {
161
- return "" , errors .Errorf ("%v zones found for zone name %s" , count , zoneName )
162
- } else {
163
- return zoneID , nil
164
- }
158
+ zone := & v1beta2.CloudStackZoneSpec {Name : zoneName }
159
+ err = client .ResolveZone (zone )
160
+ return zone .ID , err
165
161
}
166
162
167
- func GetCloudStackConfig ( namespace string ) (map [ string ] interface {} , error ) {
163
+ func GetK8sSecret ( name , namespace string ) (* corev1. Secret , error ) {
168
164
endpointCredentials := & corev1.Secret {}
169
- key := client.ObjectKey {Name : DefaultEndpointCredential , Namespace : namespace }
165
+ key := client.ObjectKey {Name : name , Namespace : namespace }
170
166
if err := v1beta2 .K8sClient .Get (context .TODO (), key , endpointCredentials ); err != nil {
171
167
return nil , err
172
168
}
173
-
174
- config := map [string ]interface {}{}
175
- for k , v := range endpointCredentials .Data {
176
- config [k ] = string (v )
177
- }
178
- // TODO change secret parsing manner.
179
- if val , present := config ["verify-ssl" ]; present {
180
- if val == "true" {
181
- config ["verify-ssl" ] = true
182
- } else if val == "false" {
183
- config ["verify-ssl" ] = false
184
- }
185
- }
186
- return config , nil
169
+ return endpointCredentials , nil
187
170
}
0 commit comments