forked from shapeblue/cloudstack-csi-driver
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsyncer.go
More file actions
111 lines (97 loc) · 2.68 KB
/
syncer.go
File metadata and controls
111 lines (97 loc) · 2.68 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Package syncer provides the logic used by command line tool cloudstack-csi-sc-syncer.
//
// It provides functions to synchronize CloudStack disk offerings
// to Kubernetes storage classes.
package syncer
import (
"context"
"fmt"
"strings"
"github.com/apache/cloudstack-go/v2/cloudstack"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"github.com/cloudstack/cloudstack-csi-driver/pkg/cloud"
)
// Config holds the syncer tool configuration.
type Config struct {
Agent string
CloudStackConfig string
KubeConfig string
Label string
NamePrefix string
Delete bool
VolumeExpansion bool
}
// Syncer has a function Run which synchronizes CloudStack
// disk offerings to Kubernetes Storage classes.
type Syncer interface {
Run(context.Context) error
}
// syncer is Syncer implementation.
type syncer struct {
k8sClient *kubernetes.Clientset
csClient *cloudstack.CloudStackClient
labelsSet labels.Set
namePrefix string
delete bool
volumeExpansion bool
}
func createK8sClient(kubeconfig, agent string) (*kubernetes.Clientset, error) {
var config *rest.Config
var err error
if kubeconfig == "-" {
config, err = rest.InClusterConfig()
if err != nil {
return nil, err
}
} else {
config, err = clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
return nil, err
}
}
config.UserAgent = agent
return kubernetes.NewForConfig(config)
}
func createCloudStackClient(cloudstackconfig string) (*cloudstack.CloudStackClient, error) {
config, err := cloud.ReadConfig(cloudstackconfig)
if err != nil {
return nil, err
}
client := cloudstack.NewAsyncClient(config.APIURL, config.APIKey, config.SecretKey, config.VerifySSL)
return client, nil
}
func createLabelsSet(label string) labels.Set {
m := make(map[string]string)
if len(label) > 0 {
parts := strings.SplitN(label, "=", 2)
key := parts[0]
value := ""
if len(parts) > 1 {
value = parts[1]
}
m[key] = value
}
return m
}
// New creates a new Syncer instance.
func New(config Config) (Syncer, error) {
k8sClient, err := createK8sClient(config.KubeConfig, config.Agent)
if err != nil {
return nil, fmt.Errorf("cannot create Kubernetes client: %w", err)
}
csClient, err := createCloudStackClient(config.CloudStackConfig)
if err != nil {
return nil, fmt.Errorf("cannot create CloudStack client: %w", err)
}
return syncer{
k8sClient: k8sClient,
csClient: csClient,
labelsSet: createLabelsSet(config.Label),
namePrefix: config.NamePrefix,
delete: config.Delete,
volumeExpansion: config.VolumeExpansion,
}, nil
}