-
Notifications
You must be signed in to change notification settings - Fork 267
/
cloudstack.go
189 lines (163 loc) Β· 5.53 KB
/
cloudstack.go
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package api
import (
"fmt"
"os"
"strings"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
anywherev1 "github.com/aws/eks-anywhere/pkg/api/v1alpha1"
"github.com/aws/eks-anywhere/pkg/cluster"
)
type CloudStackConfig struct {
datacenterConfig *anywherev1.CloudStackDatacenterConfig
machineConfigs map[string]*anywherev1.CloudStackMachineConfig
}
type CloudStackFiller func(config CloudStackConfig)
// CloudStackToConfigFiller transforms a set of CloudStackFiller's in a single ClusterConfigFiller.
func CloudStackToConfigFiller(fillers ...CloudStackFiller) ClusterConfigFiller {
return func(c *cluster.Config) {
updateCloudStack(c, fillers...)
}
}
func updateCloudStack(config *cluster.Config, fillers ...CloudStackFiller) {
cc := CloudStackConfig{
datacenterConfig: config.CloudStackDatacenter,
machineConfigs: config.CloudStackMachineConfigs,
}
for _, f := range fillers {
f(cc)
}
}
func WithCloudStackComputeOfferingForAllMachines(value string) CloudStackFiller {
return func(config CloudStackConfig) {
for _, m := range config.machineConfigs {
m.Spec.ComputeOffering.Name = value
}
}
}
func WithCloudStackAz(az anywherev1.CloudStackAvailabilityZone) CloudStackFiller {
return func(config CloudStackConfig) {
config.datacenterConfig.Spec.AvailabilityZones = append(config.datacenterConfig.Spec.AvailabilityZones, az)
}
}
func RemoveCloudStackAzs() CloudStackFiller {
return func(config CloudStackConfig) {
config.datacenterConfig.Spec.AvailabilityZones = make([]anywherev1.CloudStackAvailabilityZone, 0)
}
}
func WithCloudStackAffinityGroupIds(value []string) CloudStackFiller {
return func(config CloudStackConfig) {
for _, m := range config.machineConfigs {
m.Spec.AffinityGroupIds = value
}
}
}
func WithUserCustomDetails(value map[string]string) CloudStackFiller {
return func(config CloudStackConfig) {
for _, m := range config.machineConfigs {
m.Spec.UserCustomDetails = value
}
}
}
func WithSymlinks(value map[string]string) CloudStackFiller {
return func(config CloudStackConfig) {
for _, m := range config.machineConfigs {
m.Spec.Symlinks = value
}
}
}
func WithCloudStackTemplateForAllMachines(value string) CloudStackFiller {
return func(config CloudStackConfig) {
for _, m := range config.machineConfigs {
m.Spec.Template.Name = value
}
}
}
func WithCloudStackConfigNamespace(ns string) CloudStackFiller {
return func(config CloudStackConfig) {
config.datacenterConfig.Namespace = ns
for _, m := range config.machineConfigs {
m.Namespace = ns
}
}
}
func WithCloudStackSSHAuthorizedKey(value string) CloudStackFiller {
return func(config CloudStackConfig) {
for _, m := range config.machineConfigs {
if len(m.Spec.Users) == 0 {
m.Spec.Users = []anywherev1.UserConfiguration{{Name: "capc"}}
}
m.Spec.Users[0].SshAuthorizedKeys = []string{value}
}
}
}
func WithCloudStackDomain(value string) CloudStackFiller {
return func(config CloudStackConfig) {
for _, az := range config.datacenterConfig.Spec.AvailabilityZones {
az.Domain = value
}
}
}
func WithCloudStackAccount(value string) CloudStackFiller {
return func(config CloudStackConfig) {
for _, az := range config.datacenterConfig.Spec.AvailabilityZones {
az.Account = value
}
}
}
// WithCloudStackCredentialsRef returns a CloudStackFiller that updates the edentialsRef of all availability zones.
func WithCloudStackCredentialsRef(value string) CloudStackFiller {
return func(config CloudStackConfig) {
zones := []anywherev1.CloudStackAvailabilityZone{}
for _, az := range config.datacenterConfig.Spec.AvailabilityZones {
az.CredentialsRef = value
zones = append(zones, az)
}
config.datacenterConfig.Spec.AvailabilityZones = zones
}
}
func WithCloudStackStringFromEnvVar(envVar string, opt func(string) CloudStackFiller) CloudStackFiller {
return opt(os.Getenv(envVar))
}
func WithCloudStackAzFromEnvVars(cloudstackAccountVar, cloudstackDomainVar, cloudstackZoneVar, cloudstackCredentialsVar, cloudstackNetworkVar, cloudstackManagementServerVar string, opt func(zone anywherev1.CloudStackAvailabilityZone) CloudStackFiller) CloudStackFiller {
az := anywherev1.CloudStackAvailabilityZone{
Name: strings.ToLower(fmt.Sprintf("az-%s", os.Getenv(cloudstackZoneVar))),
CredentialsRef: os.Getenv(cloudstackCredentialsVar),
Zone: anywherev1.CloudStackZone{
Name: os.Getenv(cloudstackZoneVar),
Network: anywherev1.CloudStackResourceIdentifier{
Name: os.Getenv(cloudstackNetworkVar),
},
},
Domain: os.Getenv(cloudstackDomainVar),
Account: os.Getenv(cloudstackAccountVar),
ManagementApiEndpoint: os.Getenv(cloudstackManagementServerVar),
}
return opt(az)
}
func WithCloudStackMachineConfig(name string, fillers ...CloudStackMachineConfigFiller) CloudStackFiller {
return func(config CloudStackConfig) {
m, ok := config.machineConfigs[name]
if !ok {
m = &anywherev1.CloudStackMachineConfig{
TypeMeta: metav1.TypeMeta{
Kind: anywherev1.CloudStackMachineConfigKind,
APIVersion: anywherev1.SchemeBuilder.GroupVersion.String(),
},
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
}
config.machineConfigs[name] = m
}
FillCloudStackMachineConfig(m, fillers...)
}
}
// WithCloudStackConfigNamespaceForAllMachinesAndDatacenter sets the namespace for all Machines and Datacenter objects.
func WithCloudStackConfigNamespaceForAllMachinesAndDatacenter(ns string) CloudStackFiller {
return func(config CloudStackConfig) {
config.datacenterConfig.Namespace = ns
for _, m := range config.machineConfigs {
m.Namespace = ns
}
}
}