Repository navigation
Expand file tree
/
Copy pathsecret_loader.go
More file actions
63 lines (55 loc) · 1.82 KB
/
Copy pathsecret_loader.go
File metadata and controls
63 lines (55 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package controller
import (
"context"
"log"
"os"
"strings"
"github.com/apache/cloudstack-go/v2/cloudstack"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
)
// LoadCloudStackClientFromK8s attempts to read a Kubernetes Secret containing
// CloudStack credentials and returns an initialized CloudStack client.
// Expected Secret keys: api-key, secret-key, api-url, verify-ssl (optional).
// Environment variables to override default secret name/namespace:
// - CLOUDSTACK_SECRET_NAME (default: cloudstack-credentials)
// - CLOUDSTACK_SECRET_NAMESPACE (default: default)
func LoadCloudStackClientFromK8s() (*cloudstack.CloudStackClient, error) {
secretName := os.Getenv("CLOUDSTACK_SECRET_NAME")
if secretName == "" {
secretName = "cloudstack-credentials"
}
namespace := os.Getenv("CLOUDSTACK_SECRET_NAMESPACE")
if namespace == "" {
namespace = "default"
}
// In-cluster config
cfg, err := rest.InClusterConfig()
if err != nil {
return nil, err
}
clientset, err := kubernetes.NewForConfig(cfg)
if err != nil {
return nil, err
}
secret, err := clientset.CoreV1().Secrets(namespace).Get(context.Background(), secretName, metav1.GetOptions{})
if err != nil {
return nil, err
}
data := secret.Data
apiKey := strings.TrimSpace(string(data["api-key"]))
secretKey := strings.TrimSpace(string(data["secret-key"]))
endpoint := strings.TrimSpace(string(data["api-url"]))
vs := "true"
if b, ok := data["verify-ssl"]; ok {
vs = strings.TrimSpace(string(b))
}
verifySSL := true
if strings.EqualFold(vs, "false") || strings.EqualFold(vs, "0") {
verifySSL = false
}
log.Println("Loaded CloudStack credentials from Kubernetes Secret", secretName, "in namespace", namespace)
client := cloudstack.NewAsyncClient(endpoint, apiKey, secretKey, verifySSL)
return client, nil
}