-
Notifications
You must be signed in to change notification settings - Fork 277
/
snow.go
177 lines (146 loc) Β· 5.13 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
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
package framework
import (
"os"
"testing"
"github.com/aws/eks-anywhere/internal/pkg/api"
anywherev1 "github.com/aws/eks-anywhere/pkg/api/v1alpha1"
)
const (
snowAMIIDUbuntu120 = "T_SNOW_AMIID_UBUNTU_1_20"
snowAMIIDUbuntu121 = "T_SNOW_AMIID_UBUNTU_1_21"
snowAMIIDUbuntu122 = "T_SNOW_AMIID_UBUNTU_1_22"
snowAMIIDUbuntu123 = "T_SNOW_AMIID_UBUNTU_1_23"
snowDevices = "T_SNOW_DEVICES"
snowControlPlaneCidr = "T_SNOW_CONTROL_PLANE_CIDR"
snowPodCidr = "T_SNOW_POD_CIDR"
snowCredentialsFile = "EKSA_AWS_CREDENTIALS_FILE"
snowCertificatesFile = "EKSA_AWS_CA_BUNDLES_FILE"
)
var requiredSnowEnvVars = []string{
snowAMIIDUbuntu120,
snowAMIIDUbuntu121,
snowAMIIDUbuntu122,
snowAMIIDUbuntu123,
snowControlPlaneCidr,
snowCredentialsFile,
snowCertificatesFile,
}
type Snow struct {
t *testing.T
fillers []api.SnowFiller
clusterFillers []api.ClusterFiller
cpCidr string
podCidr string
}
type SnowOpt func(*Snow)
func NewSnow(t *testing.T, opts ...SnowOpt) *Snow {
checkRequiredEnvVars(t, requiredSnowEnvVars)
s := &Snow{
t: t,
fillers: []api.SnowFiller{
api.WithSnowStringFromEnvVar(snowAMIIDUbuntu120, api.WithSnowAMIIDForAllMachines),
},
}
s.cpCidr = os.Getenv(snowControlPlaneCidr)
s.podCidr = os.Getenv(snowPodCidr)
for _, opt := range opts {
opt(s)
}
return s
}
func (s *Snow) Name() string {
return "snow"
}
func (s *Snow) Setup() {}
func (s *Snow) CustomizeProviderConfig(file string) []byte {
return s.customizeProviderConfig(file, s.fillers...)
}
func (s *Snow) CleanupVMs(_ string) error {
return nil
}
func (s *Snow) ClusterConfigFillers() []api.ClusterFiller {
ip, err := GenerateUniqueIp(s.cpCidr)
if err != nil {
s.t.Fatalf("failed to generate control plane ip for snow [cidr=%s]: %v", s.cpCidr, err)
}
s.clusterFillers = append(s.clusterFillers, api.WithControlPlaneEndpointIP(ip))
if s.podCidr != "" {
s.clusterFillers = append(s.clusterFillers, api.WithPodCidr(s.podCidr))
}
return s.clusterFillers
}
func (s *Snow) customizeProviderConfig(file string, fillers ...api.SnowFiller) []byte {
providerOutput, err := api.AutoFillSnowProvider(file, fillers...)
if err != nil {
s.t.Fatalf("failed to customize provider config from file: %v", err)
}
return providerOutput
}
func (s *Snow) WithProviderUpgrade(fillers ...api.SnowFiller) ClusterE2ETestOpt {
return func(e *ClusterE2ETest) {
e.ProviderConfigB = s.customizeProviderConfig(e.ClusterConfigLocation, fillers...)
}
}
func WithSnowUbuntu121() SnowOpt {
return func(s *Snow) {
s.fillers = append(s.fillers,
api.WithSnowStringFromEnvVar(snowAMIIDUbuntu121, api.WithSnowAMIIDForAllMachines),
api.WithSnowStringFromEnvVar(snowDevices, api.WithSnowDevicesForAllMachines),
)
}
}
func WithSnowUbuntu122() SnowOpt {
return func(s *Snow) {
s.fillers = append(s.fillers,
api.WithSnowStringFromEnvVar(snowAMIIDUbuntu122, api.WithSnowAMIIDForAllMachines),
api.WithSnowStringFromEnvVar(snowDevices, api.WithSnowDevicesForAllMachines),
)
}
}
func WithSnowUbuntu123() SnowOpt {
return func(s *Snow) {
s.fillers = append(s.fillers,
api.WithSnowStringFromEnvVar(snowAMIIDUbuntu123, api.WithSnowAMIIDForAllMachines),
api.WithSnowStringFromEnvVar(snowDevices, api.WithSnowDevicesForAllMachines),
)
}
}
func WithSnowWorkerNodeGroup(name string, workerNodeGroup *WorkerNodeGroup, fillers ...api.SnowMachineConfigFiller) SnowOpt {
return func(s *Snow) {
s.fillers = append(s.fillers, snowMachineConfig(name, fillers...))
s.clusterFillers = append(s.clusterFillers, buildSnowWorkerNodeGroupClusterFiller(name, workerNodeGroup))
}
}
func (s *Snow) WithNewSnowWorkerNodeGroup(name string, workerNodeGroup *WorkerNodeGroup, fillers ...api.SnowMachineConfigFiller) ClusterE2ETestOpt {
return func(e *ClusterE2ETest) {
e.ProviderConfigB = s.customizeProviderConfig(e.ClusterConfigLocation, snowMachineConfig(name, fillers...))
var err error
e.ClusterConfigB, err = api.AutoFillClusterFromYaml(e.ClusterConfigB, buildSnowWorkerNodeGroupClusterFiller(name, workerNodeGroup))
if err != nil {
e.T.Fatalf("Error filling cluster config: %v", err)
}
}
}
func snowMachineConfig(name string, fillers ...api.SnowMachineConfigFiller) api.SnowFiller {
f := make([]api.SnowMachineConfigFiller, 0, len(fillers)+2)
f = append(f,
api.WithSnowMachineDefaultValues(),
api.WithSnowDevices(os.Getenv(snowDevices)),
)
f = append(f, fillers...)
return api.WithSnowMachineConfig(name, f...)
}
func buildSnowWorkerNodeGroupClusterFiller(machineConfigName string, workerNodeGroup *WorkerNodeGroup) api.ClusterFiller {
workerNodeGroup.MachineConfigKind = anywherev1.SnowMachineConfigKind
workerNodeGroup.MachineConfigName = machineConfigName
return workerNodeGroup.ClusterFiller()
}
func UpdateSnowUbuntuTemplate121Var() api.SnowFiller {
return api.WithSnowStringFromEnvVar(snowAMIIDUbuntu121, api.WithSnowAMIIDForAllMachines)
}
func UpdateSnowUbuntuTemplate122Var() api.SnowFiller {
return api.WithSnowStringFromEnvVar(snowAMIIDUbuntu122, api.WithSnowAMIIDForAllMachines)
}
func UpdateSnowUbuntuTemplate123Var() api.SnowFiller {
return api.WithSnowStringFromEnvVar(snowAMIIDUbuntu123, api.WithSnowAMIIDForAllMachines)
}