-
Notifications
You must be signed in to change notification settings - Fork 286
/
snow.go
120 lines (99 loc) Β· 2.93 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
package api
import (
"fmt"
"os"
"strings"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/yaml"
anywherev1 "github.com/aws/eks-anywhere/pkg/api/v1alpha1"
"github.com/aws/eks-anywhere/pkg/cluster"
"github.com/aws/eks-anywhere/pkg/templater"
)
type SnowConfig struct {
datacenterConfig *anywherev1.SnowDatacenterConfig
machineConfigs map[string]*anywherev1.SnowMachineConfig
}
type SnowFiller func(config SnowConfig)
func AutoFillSnowProvider(filename string, fillers ...SnowFiller) ([]byte, error) {
config, err := cluster.ParseConfigFromFile(filename)
if err != nil {
return nil, err
}
snowConfig := SnowConfig{
datacenterConfig: config.SnowDatacenter,
machineConfigs: config.SnowMachineConfigs,
}
for _, f := range fillers {
f(snowConfig)
}
resources := make([]interface{}, 0, len(snowConfig.machineConfigs)+1)
resources = append(resources, snowConfig.datacenterConfig)
for _, m := range snowConfig.machineConfigs {
resources = append(resources, m)
}
yamlResources := make([][]byte, 0, len(resources))
for _, r := range resources {
yamlContent, err := yaml.Marshal(r)
if err != nil {
return nil, fmt.Errorf("marshalling snow resource: %v", err)
}
yamlResources = append(yamlResources, yamlContent)
}
return templater.AppendYamlResources(yamlResources...), nil
}
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
}
}
}
func WithSnowInstanceTypeForAllMachines(instanceType anywherev1.SnowInstanceType) 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...)
}
}