-
Notifications
You must be signed in to change notification settings - Fork 287
/
cloudstack.go
139 lines (121 loc) Β· 3.49 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
package cluster
import (
"context"
anywherev1 "github.com/aws/eks-anywhere/pkg/api/v1alpha1"
)
func cloudstackEntry() *ConfigManagerEntry {
return &ConfigManagerEntry{
APIObjectMapping: map[string]APIObjectGenerator{
anywherev1.CloudStackDatacenterKind: func() APIObject {
return &anywherev1.CloudStackDatacenterConfig{}
},
anywherev1.CloudStackMachineConfigKind: func() APIObject {
return &anywherev1.CloudStackMachineConfig{}
},
},
Processors: []ParsedProcessor{
processCloudStackDatacenter,
machineConfigsProcessor(processCloudStackMachineConfig),
},
Defaulters: []Defaulter{
func(c *Config) error {
if c.CloudStackDatacenter != nil {
c.CloudStackDatacenter.SetDefaults()
}
return nil
},
func(c *Config) error {
for _, mc := range c.CloudStackMachineConfigs {
mc.SetUserDefaults()
}
return nil
},
},
Validations: []Validation{
func(c *Config) error {
if c.CloudStackDatacenter != nil {
return c.CloudStackDatacenter.Validate()
}
return nil
},
func(c *Config) error {
for _, m := range c.CloudStackMachineConfigs {
if err := m.Validate(); err != nil {
return err
}
}
return nil
},
func(c *Config) error {
if c.CloudStackDatacenter != nil {
if err := validateSameNamespace(c, c.CloudStackDatacenter); err != nil {
return err
}
}
return nil
},
func(c *Config) error {
for _, v := range c.CloudStackMachineConfigs {
if err := validateSameNamespace(c, v); err != nil {
return err
}
}
return nil
},
},
}
}
func processCloudStackDatacenter(c *Config, objects ObjectLookup) {
if c.Cluster.Spec.DatacenterRef.Kind == anywherev1.CloudStackDatacenterKind {
datacenter := objects.GetFromRef(c.Cluster.APIVersion, c.Cluster.Spec.DatacenterRef)
if datacenter != nil {
c.CloudStackDatacenter = datacenter.(*anywherev1.CloudStackDatacenterConfig)
}
}
}
func processCloudStackMachineConfig(c *Config, objects ObjectLookup, machineRef *anywherev1.Ref) {
if machineRef == nil {
return
}
if machineRef.Kind != anywherev1.CloudStackMachineConfigKind {
return
}
if c.CloudStackMachineConfigs == nil {
c.CloudStackMachineConfigs = map[string]*anywherev1.CloudStackMachineConfig{}
}
m := objects.GetFromRef(c.Cluster.APIVersion, *machineRef)
if m == nil {
return
}
c.CloudStackMachineConfigs[m.GetName()] = m.(*anywherev1.CloudStackMachineConfig)
}
func getCloudStackDatacenter(ctx context.Context, client Client, c *Config) error {
if c.Cluster.Spec.DatacenterRef.Kind != anywherev1.CloudStackDatacenterKind {
return nil
}
datacenter := &anywherev1.CloudStackDatacenterConfig{}
if err := client.Get(ctx, c.Cluster.Spec.DatacenterRef.Name, c.Cluster.Namespace, datacenter); err != nil {
return err
}
c.CloudStackDatacenter = datacenter
return nil
}
func getCloudStackMachineConfigs(ctx context.Context, client Client, c *Config) error {
if c.Cluster.Spec.DatacenterRef.Kind != anywherev1.CloudStackDatacenterKind {
return nil
}
if c.CloudStackMachineConfigs == nil {
c.CloudStackMachineConfigs = map[string]*anywherev1.CloudStackMachineConfig{}
}
for _, machineRef := range c.Cluster.MachineConfigRefs() {
if machineRef.Kind != anywherev1.CloudStackMachineConfigKind {
continue
}
machine := &anywherev1.CloudStackMachineConfig{}
if err := client.Get(ctx, machineRef.Name, c.Cluster.Namespace, machine); err != nil {
return err
}
c.CloudStackMachineConfigs[machine.Name] = machine
}
return nil
}