-
Notifications
You must be signed in to change notification settings - Fork 284
/
tinkerbellmachineconfig.go
90 lines (78 loc) · 2.43 KB
/
tinkerbellmachineconfig.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
package v1alpha1
import (
"fmt"
"io/ioutil"
"strings"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/yaml"
)
const TinkerbellMachineConfigKind = "TinkerbellMachineConfig"
// +kubebuilder:object:generate=false
type TinkerbellMachineConfigGenerateOpt func(config *TinkerbellMachineConfigGenerate)
// Used for generating yaml for generate clusterconfig command
func NewTinkerbellMachineConfigGenerate(name string, opts ...TinkerbellMachineConfigGenerateOpt) *TinkerbellMachineConfigGenerate {
machineConfig := &TinkerbellMachineConfigGenerate{
TypeMeta: metav1.TypeMeta{
Kind: TinkerbellMachineConfigKind,
APIVersion: SchemeBuilder.GroupVersion.String(),
},
ObjectMeta: ObjectMeta{
Name: name,
},
Spec: TinkerbellMachineConfigSpec{
HardwareSelector: HardwareSelector{},
OSFamily: Bottlerocket,
Users: []UserConfiguration{
{
Name: "ec2-user",
SshAuthorizedKeys: []string{"ssh-rsa AAAA..."},
},
},
},
}
for _, opt := range opts {
opt(machineConfig)
}
return machineConfig
}
func (c *TinkerbellMachineConfigGenerate) APIVersion() string {
return c.TypeMeta.APIVersion
}
func (c *TinkerbellMachineConfigGenerate) Kind() string {
return c.TypeMeta.Kind
}
func (c *TinkerbellMachineConfigGenerate) Name() string {
return c.ObjectMeta.Name
}
func GetTinkerbellMachineConfigs(fileName string) (map[string]*TinkerbellMachineConfig, error) {
configs := make(map[string]*TinkerbellMachineConfig)
content, err := ioutil.ReadFile(fileName)
if err != nil {
return nil, fmt.Errorf("unable to read file due to: %v", err)
}
for _, c := range strings.Split(string(content), YamlSeparator) {
var config TinkerbellMachineConfig
if err = yaml.UnmarshalStrict([]byte(c), &config); err == nil {
if config.Kind == TinkerbellMachineConfigKind {
configs[config.Name] = &config
continue
}
}
_ = yaml.Unmarshal([]byte(c), &config) // this is to check if there is a bad spec in the file
if config.Kind == TinkerbellMachineConfigKind {
return nil, fmt.Errorf("unable to unmarshall content from file due to: %v", err)
}
}
if len(configs) == 0 {
return nil, fmt.Errorf("unable to find kind %v in file", TinkerbellMachineConfigKind)
}
return configs, nil
}
func WithTemplateRef(ref ProviderRefAccessor) TinkerbellMachineConfigGenerateOpt {
return func(c *TinkerbellMachineConfigGenerate) {
c.Spec.TemplateRef = Ref{
Kind: ref.Kind(),
Name: ref.Name(),
}
}
}