-
Notifications
You must be signed in to change notification settings - Fork 281
/
tinkerbell.go
205 lines (174 loc) Β· 5.92 KB
/
tinkerbell.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package api
import (
"fmt"
"os"
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/providers"
"github.com/aws/eks-anywhere/pkg/templater"
)
type TinkerbellConfig struct {
clusterConfig *anywherev1.Cluster
datacenterConfig *anywherev1.TinkerbellDatacenterConfig
machineConfigs map[string]*anywherev1.TinkerbellMachineConfig
templateConfigs map[string]*anywherev1.TinkerbellTemplateConfig
}
type TinkerbellFiller func(config TinkerbellConfig) error
func AutoFillTinkerbellProvider(filename string, fillers ...TinkerbellFiller) ([]byte, error) {
tinkerbellDatacenterConfig, err := anywherev1.GetTinkerbellDatacenterConfig(filename)
if err != nil {
return nil, fmt.Errorf("unable to get tinkerbell datacenter config from file: %v", err)
}
tinkerbellMachineConfigs, err := anywherev1.GetTinkerbellMachineConfigs(filename)
if err != nil {
return nil, fmt.Errorf("unable to get tinkerbell machine config from file: %v", err)
}
tinkerbellTemplateConfigs, err := anywherev1.GetTinkerbellTemplateConfig(filename)
if err != nil {
return nil, fmt.Errorf("unable to get tinkerbell template configs from file: %v", err)
}
clusterConfig, err := anywherev1.GetClusterConfig(filename)
if err != nil {
return nil, fmt.Errorf("unable to get tinkerbell cluster config from file: %v", err)
}
config := TinkerbellConfig{
clusterConfig: clusterConfig,
datacenterConfig: tinkerbellDatacenterConfig,
machineConfigs: tinkerbellMachineConfigs,
templateConfigs: tinkerbellTemplateConfigs,
}
for _, f := range fillers {
err := f(config)
if err != nil {
return nil, fmt.Errorf("failed to apply tinkerbell config filler: %v", err)
}
}
resources := make([]interface{}, 0, len(config.machineConfigs)+len(config.templateConfigs)+1)
resources = append(resources, config.datacenterConfig)
for _, m := range config.machineConfigs {
resources = append(resources, m)
}
for _, m := range config.templateConfigs {
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 tinkerbell resource: %v", err)
}
yamlResources = append(yamlResources, yamlContent)
}
return templater.AppendYamlResources(yamlResources...), nil
}
func WithTinkerbellServer(value string) TinkerbellFiller {
return func(config TinkerbellConfig) error {
config.datacenterConfig.Spec.TinkerbellIP = value
return nil
}
}
func WithTinkerbellOSImageURL(value string) TinkerbellFiller {
return func(config TinkerbellConfig) error {
config.datacenterConfig.Spec.OSImageURL = value
return nil
}
}
func WithStringFromEnvVarTinkerbell(envVar string, opt func(string) TinkerbellFiller) TinkerbellFiller {
return opt(os.Getenv(envVar))
}
func WithOsFamilyForAllTinkerbellMachines(value anywherev1.OSFamily) TinkerbellFiller {
return func(config TinkerbellConfig) error {
for _, m := range config.machineConfigs {
m.Spec.OSFamily = value
}
return nil
}
}
func WithImageUrlForAllTinkerbellMachines(value string) TinkerbellFiller {
return func(config TinkerbellConfig) error {
for _, t := range config.templateConfigs {
for _, task := range t.Spec.Template.Tasks {
for _, action := range task.Actions {
if action.Name == "stream-image" {
action.Environment["IMG_URL"] = value
}
}
}
}
return nil
}
}
func WithSSHAuthorizedKeyForAllTinkerbellMachines(key string) TinkerbellFiller {
return func(config TinkerbellConfig) error {
for _, m := range config.machineConfigs {
if len(m.Spec.Users) == 0 {
m.Spec.Users = []anywherev1.UserConfiguration{{}}
}
m.Spec.Users[0].Name = "ec2-user"
m.Spec.Users[0].SshAuthorizedKeys = []string{key}
}
return nil
}
}
func WithHardwareSelectorLabels() TinkerbellFiller {
return func(config TinkerbellConfig) error {
clusterName := config.clusterConfig.Name
cpName := providers.GetControlPlaneNodeName(clusterName)
workerName := clusterName
cpMachineConfig := config.machineConfigs[cpName]
cpMachineConfig.Spec.HardwareSelector = map[string]string{HardwareLabelTypeKeyName: ControlPlane}
config.machineConfigs[cpName] = cpMachineConfig
workerMachineConfig := config.machineConfigs[workerName]
workerMachineConfig.Spec.HardwareSelector = map[string]string{HardwareLabelTypeKeyName: Worker}
config.machineConfigs[workerName] = workerMachineConfig
return nil
}
}
func WithTinkerbellEtcdMachineConfig() TinkerbellFiller {
return func(config TinkerbellConfig) error {
clusterName := config.clusterConfig.Name
name := providers.GetEtcdNodeName(clusterName)
_, ok := config.machineConfigs[name]
if !ok {
m := &anywherev1.TinkerbellMachineConfig{
TypeMeta: metav1.TypeMeta{
Kind: anywherev1.TinkerbellMachineConfigKind,
APIVersion: anywherev1.SchemeBuilder.GroupVersion.String(),
},
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
Spec: anywherev1.TinkerbellMachineConfigSpec{
HardwareSelector: map[string]string{HardwareLabelTypeKeyName: ExternalEtcd},
TemplateRef: anywherev1.Ref{
Name: clusterName,
Kind: anywherev1.TinkerbellTemplateConfigKind,
},
},
}
config.machineConfigs[name] = m
}
return nil
}
}
func WithCustomTinkerbellMachineConfig(selector string) TinkerbellFiller {
return func(config TinkerbellConfig) error {
if _, ok := config.machineConfigs[selector]; !ok {
m := &anywherev1.TinkerbellMachineConfig{
TypeMeta: metav1.TypeMeta{
Kind: anywherev1.TinkerbellMachineConfigKind,
APIVersion: anywherev1.SchemeBuilder.GroupVersion.String(),
},
ObjectMeta: metav1.ObjectMeta{
Name: selector,
},
Spec: anywherev1.TinkerbellMachineConfigSpec{
HardwareSelector: map[string]string{HardwareLabelTypeKeyName: selector},
},
}
config.machineConfigs[selector] = m
}
return nil
}
}