/
snow.go
150 lines (130 loc) Β· 3.79 KB
/
snow.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
package api
import (
"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 SnowConfig struct {
datacenterConfig *anywherev1.SnowDatacenterConfig
machineConfigs map[string]*anywherev1.SnowMachineConfig
ipPools map[string]*anywherev1.SnowIPPool
}
type SnowFiller func(config SnowConfig)
// SnowToConfigFiller transforms a set of SnowFiller's in a single ClusterConfigFiller.
func SnowToConfigFiller(fillers ...SnowFiller) ClusterConfigFiller {
return func(c *cluster.Config) {
updateSnow(c, fillers...)
}
}
func updateSnow(config *cluster.Config, fillers ...SnowFiller) {
if config.SnowIPPools == nil {
config.SnowIPPools = map[string]*anywherev1.SnowIPPool{}
}
sc := SnowConfig{
datacenterConfig: config.SnowDatacenter,
machineConfigs: config.SnowMachineConfigs,
ipPools: config.SnowIPPools,
}
for _, f := range fillers {
f(sc)
}
}
func WithSnowStringFromEnvVar(envVar string, opt func(string) SnowFiller) SnowFiller {
return opt(os.Getenv(envVar))
}
func WithSnowAMIIDForAllMachines(id string) SnowFiller {
return func(config SnowConfig) {
for _, m := range config.machineConfigs {
m.Spec.AMIID = id
}
}
}
// WithSnowInstanceTypeForAllMachines specifies an instance type for all the snow machine configs.
func WithSnowInstanceTypeForAllMachines(instanceType string) SnowFiller {
return func(config SnowConfig) {
for _, m := range config.machineConfigs {
m.Spec.InstanceType = instanceType
}
}
}
func WithSnowPhysicalNetworkConnectorForAllMachines(connectorType anywherev1.PhysicalNetworkConnectorType) SnowFiller {
return func(config SnowConfig) {
for _, m := range config.machineConfigs {
m.Spec.PhysicalNetworkConnector = connectorType
}
}
}
func WithSnowSshKeyNameForAllMachines(keyName string) SnowFiller {
return func(config SnowConfig) {
for _, m := range config.machineConfigs {
m.Spec.SshKeyName = keyName
}
}
}
func WithSnowDevicesForAllMachines(devices string) SnowFiller {
return func(config SnowConfig) {
for _, m := range config.machineConfigs {
m.Spec.Devices = strings.Split(devices, ",")
}
}
}
func WithSnowMachineConfig(name string, fillers ...SnowMachineConfigFiller) SnowFiller {
return func(config SnowConfig) {
m, ok := config.machineConfigs[name]
if !ok {
m = &anywherev1.SnowMachineConfig{
TypeMeta: metav1.TypeMeta{
Kind: anywherev1.SnowMachineConfigKind,
APIVersion: anywherev1.SchemeBuilder.GroupVersion.String(),
},
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
}
config.machineConfigs[name] = m
}
FillSnowMachineConfig(m, fillers...)
}
}
// WithOsFamilyForAllSnowMachines sets the OSFamily in the SnowMachineConfig.
func WithOsFamilyForAllSnowMachines(value anywherev1.OSFamily) SnowFiller {
return func(config SnowConfig) {
for _, m := range config.machineConfigs {
m.Spec.OSFamily = value
}
}
}
// WithChangeForAllSnowMachines applies the same change to all SnowMachineConfigs.
func WithChangeForAllSnowMachines(change SnowMachineConfigFiller) SnowFiller {
return func(config SnowConfig) {
for _, m := range config.machineConfigs {
change(m)
}
}
}
// WithSnowIPPool sets a SnowIPPool.
func WithSnowIPPool(name, ipStart, ipEnd, gateway, subnet string) SnowFiller {
return func(config SnowConfig) {
config.ipPools[name] = &anywherev1.SnowIPPool{
TypeMeta: metav1.TypeMeta{
Kind: anywherev1.SnowIPPoolKind,
APIVersion: anywherev1.SchemeBuilder.GroupVersion.String(),
},
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
Spec: anywherev1.SnowIPPoolSpec{
Pools: []anywherev1.IPPool{
{
IPStart: ipStart,
IPEnd: ipEnd,
Subnet: subnet,
Gateway: gateway,
},
},
},
}
}
}