Skip to content

Commit 05df8c7

Browse files
committed
Use NewClientFromK8sSecret in conversion
1 parent e8f2e2c commit 05df8c7

File tree

1 file changed

+15
-32
lines changed

1 file changed

+15
-32
lines changed

api/v1beta1/conversion.go

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,11 @@ package v1beta1
1818

1919
import (
2020
"context"
21-
"fmt"
2221

23-
"github.com/apache/cloudstack-go/v2/cloudstack"
24-
"github.com/pkg/errors"
2522
corev1 "k8s.io/api/core/v1"
2623
conv "k8s.io/apimachinery/pkg/conversion"
2724
"sigs.k8s.io/cluster-api-provider-cloudstack/api/v1beta2"
25+
"sigs.k8s.io/cluster-api-provider-cloudstack/pkg/cloud"
2826
"sigs.k8s.io/controller-runtime/pkg/client"
2927
)
3028

@@ -124,8 +122,13 @@ func GetDefaultFailureDomainName(namespace string, clusterName string, zoneID st
124122
return zoneID + "-" + clusterName, nil
125123
}
126124

125+
secret, err := GetK8sSecret(DefaultEndpointCredential, namespace)
126+
if err != nil {
127+
return "", err
128+
}
129+
127130
// try fetch zoneID using zoneName through cloudstack client
128-
zoneID, err := fetchZoneIDUsingCloudStack(namespace, zoneName)
131+
zoneID, err = fetchZoneIDUsingCloudStack(secret, zoneName)
129132
if err == nil {
130133
return zoneID + "-" + clusterName, nil
131134
}
@@ -147,41 +150,21 @@ func fetchZoneIDUsingK8s(namespace string, zoneName string) (string, error) {
147150
return zone.Spec.ID, nil
148151
}
149152

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)
152155
if err != nil {
153156
return "", err
154157
}
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
165161
}
166162

167-
func GetCloudStackConfig(namespace string) (map[string]interface{}, error) {
163+
func GetK8sSecret(name, namespace string) (*corev1.Secret, error) {
168164
endpointCredentials := &corev1.Secret{}
169-
key := client.ObjectKey{Name: DefaultEndpointCredential, Namespace: namespace}
165+
key := client.ObjectKey{Name: name, Namespace: namespace}
170166
if err := v1beta2.K8sClient.Get(context.TODO(), key, endpointCredentials); err != nil {
171167
return nil, err
172168
}
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
187170
}

0 commit comments

Comments
 (0)