/
templates.go
109 lines (100 loc) · 2.67 KB
/
templates.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
package templates
import (
"bytes"
"encoding/base64"
"github.com/google/safetext/yamltemplate"
)
var templateBody = `---
apiVersion: v1
kind: Secret
metadata:
name: {{ .Name }}-bmc-secret
type: Opaque
data:
username: {{ .EncodedUsername }}
password: {{ .EncodedPassword }}
---
apiVersion: metal3.io/v1alpha1
kind: BareMetalHost
metadata:
name: {{ .Name }}
spec:
{{- if .AutomatedCleaningMode }}
automatedCleaningMode: {{ .AutomatedCleaningMode }}
{{- end }}
online: true
{{- if .HardwareProfile }}
hardwareProfile: {{ .HardwareProfile }}
{{- end }}
{{- if .BootMacAddress }}
bootMACAddress: {{ .BootMacAddress }}
{{- end }}
{{- if .BootMode }}
bootMode: {{ .BootMode }}
{{- end }}
bmc:
address: {{ .BMCAddress }}
credentialsName: {{ .Name }}-bmc-secret
{{- if .DisableCertificateVerification }}
disableCertificateVerification: true
{{- end}}
{{- if .Consumer }}
consumerRef:
name: {{ .Consumer }}
{{- if .ConsumerNamespace }}
namespace: {{ .ConsumerNamespace }}
{{- end }}
{{- end }}
{{- if .ImageURL }}
image:
{{- if .ImageChecksum }}
checksum: {{ .ImageChecksum}}
{{- end}}
{{- if .ImageChecksumType }}
checksumType: {{ .ImageChecksumType}}
{{- end}}
{{- if .ImageFormat }}
format: {{ .ImageFormat}}
{{- end}}
url: {{ .ImageURL}}
{{- end}}
`
// Template holds the arguments to pass to the template.
type Template struct {
Name string
BMCAddress string
DisableCertificateVerification bool
Username string
Password string
HardwareProfile string
BootMacAddress string
BootMode string
Consumer string
ConsumerNamespace string
AutomatedCleaningMode string
ImageURL string
ImageChecksum string
ImageChecksumType string
ImageFormat string
}
// EncodedUsername returns the username in the format needed to store
// it in a Secret.
func (t Template) EncodedUsername() string {
return encodeToSecret(t.Username)
}
// EncodedPassword returns the password in the format needed to store
// it in a Secret.
func (t Template) EncodedPassword() string {
return encodeToSecret(t.Password)
}
func encodeToSecret(input string) string {
return base64.StdEncoding.EncodeToString([]byte(input))
}
// Render returns the string from the template or an error if there
// was a problem rendering it.
func (t Template) Render() (string, error) {
buf := new(bytes.Buffer)
tmpl := yamltemplate.Must(yamltemplate.New("yaml_out").Parse(templateBody))
err := tmpl.Execute(buf, t)
return buf.String(), err
}