-
Notifications
You must be signed in to change notification settings - Fork 51
/
defaults.go
255 lines (231 loc) · 7.1 KB
/
defaults.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*
Copyright 2022 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package helpers
import (
"fmt"
"sync/atomic"
infrav1 "github.com/syself/cluster-api-provider-hetzner/api/v1beta1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
)
const (
bareMetalHostID = 1
sshFingerprint = "my-fingerprint"
defaultOSSSHKeyName = "os-sshkey"
defaultRescueSSHKeyName = "rescue-sshkey"
// DefaultWWN specifies the default WWN.
DefaultWWN = "eui.002538b411b2cee8"
// DefaultWWN2 specifies the default WWN.
DefaultWWN2 = "eui.0025388801b4dff2"
)
var defaultPlacementGroupName = "caph-placement-group"
var globalServerIdCounter int32
// BareMetalHost returns a bare metal host given options.
func BareMetalHost(name, namespace string, opts ...HostOpts) *infrav1.HetznerBareMetalHost {
serverID := atomic.AddInt32(&globalServerIdCounter, 1)
host := &infrav1.HetznerBareMetalHost{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
},
Spec: infrav1.HetznerBareMetalHostSpec{
ServerID: int(serverID),
},
}
for _, o := range opts {
o(host)
}
return host
}
// HostOpts define options to customize the host spec.
type HostOpts func(*infrav1.HetznerBareMetalHost)
// WithError gives the option to define a host with error in spec.status.
func WithError(errorType infrav1.ErrorType, errorMessage string, errorCount int, lastUpdated metav1.Time) HostOpts {
return func(host *infrav1.HetznerBareMetalHost) {
host.Spec.Status.ErrorType = errorType
host.Spec.Status.ErrorMessage = errorMessage
host.Spec.Status.ErrorCount = errorCount
host.Spec.Status.LastUpdated = &lastUpdated
}
}
// WithRebootTypes gives the option to define a host with custom reboot types.
func WithRebootTypes(rebootTypes []infrav1.RebootType) HostOpts {
return func(host *infrav1.HetznerBareMetalHost) {
host.Spec.Status.RebootTypes = rebootTypes
}
}
// WithRootDeviceHintWWN gives the option to define a host with root device hints.
func WithRootDeviceHintWWN() HostOpts {
return func(host *infrav1.HetznerBareMetalHost) {
host.Spec.RootDeviceHints = &infrav1.RootDeviceHints{
WWN: DefaultWWN,
}
}
}
// WithRootDeviceHintRaid gives the option to define a host with root device hints.
func WithRootDeviceHintRaid() HostOpts {
return func(host *infrav1.HetznerBareMetalHost) {
host.Spec.RootDeviceHints = &infrav1.RootDeviceHints{
Raid: infrav1.Raid{WWN: []string{DefaultWWN, DefaultWWN2}},
}
}
}
// WithHetznerClusterRef gives the option to define a host with cluster ref.
func WithHetznerClusterRef(hetznerClusterRef string) HostOpts {
return func(host *infrav1.HetznerBareMetalHost) {
host.Spec.Status.HetznerClusterRef = hetznerClusterRef
}
}
// WithSSHSpec gives the option to define a host with ssh spec.
func WithSSHSpec() HostOpts {
return func(host *infrav1.HetznerBareMetalHost) {
host.Spec.Status.SSHSpec = &infrav1.SSHSpec{
SecretRef: infrav1.SSHSecretRef{
Name: defaultOSSSHKeyName,
Key: infrav1.SSHSecretKeyRef{
Name: "sshkey-name",
PublicKey: "public-key",
PrivateKey: "private-key",
},
},
}
}
}
// WithSSHSpecInclPorts gives the option to define a host with ssh spec incl. ports.
func WithSSHSpecInclPorts(portAfterInstallImage, portAfterCloudInit int) HostOpts {
return func(host *infrav1.HetznerBareMetalHost) {
host.Spec.Status.SSHSpec = &infrav1.SSHSpec{
SecretRef: infrav1.SSHSecretRef{
Name: defaultOSSSHKeyName,
Key: infrav1.SSHSecretKeyRef{
Name: "sshkey-name",
PublicKey: "public-key",
PrivateKey: "private-key",
},
},
PortAfterInstallImage: portAfterInstallImage,
PortAfterCloudInit: portAfterCloudInit,
}
}
}
// WithSSHStatus gives the option to define a host with ssh status.
func WithSSHStatus() HostOpts {
return func(host *infrav1.HetznerBareMetalHost) {
host.Spec.Status.SSHStatus = infrav1.SSHStatus{
OSKey: &infrav1.SSHKey{
Name: defaultOSSSHKeyName,
Fingerprint: sshFingerprint,
},
RescueKey: &infrav1.SSHKey{
Name: defaultRescueSSHKeyName,
Fingerprint: sshFingerprint,
},
}
}
}
// WithIPv4 gives the option to define a host with IP.
func WithIPv4() HostOpts {
return func(host *infrav1.HetznerBareMetalHost) {
host.Spec.Status.IPv4 = "1.2.3.4"
}
}
// WithConsumerRef gives the option to define a host with consumer ref.
func WithConsumerRef() HostOpts {
return func(host *infrav1.HetznerBareMetalHost) {
host.Spec.ConsumerRef = &corev1.ObjectReference{
Name: "bm-machine",
Namespace: "default",
Kind: "HetznerBareMetalMachine",
}
}
}
// GetDefaultHetznerClusterSpec returns the default Hetzner cluster spec.
func GetDefaultHetznerClusterSpec() infrav1.HetznerClusterSpec {
return infrav1.HetznerClusterSpec{
ControlPlaneLoadBalancer: infrav1.LoadBalancerSpec{
Enabled: true,
Algorithm: "round_robin",
ExtraServices: []infrav1.LoadBalancerServiceSpec{
{
DestinationPort: 8132,
ListenPort: 8132,
Protocol: "tcp",
},
{
DestinationPort: 8133,
ListenPort: 8133,
Protocol: "tcp",
},
},
Port: 6443,
Region: "fsn1",
Type: "lb11",
},
ControlPlaneEndpoint: &clusterv1.APIEndpoint{},
ControlPlaneRegions: []infrav1.Region{"fsn1"},
HCloudNetwork: infrav1.HCloudNetworkSpec{
CIDRBlock: "10.0.0.0/16",
Enabled: true,
NetworkZone: "eu-central",
SubnetCIDRBlock: "10.0.0.0/24",
},
HCloudPlacementGroups: []infrav1.HCloudPlacementGroupSpec{
{
Name: defaultPlacementGroupName,
Type: "spread",
},
{
Name: "md-0",
Type: "spread",
},
},
HetznerSecret: infrav1.HetznerSecretRef{
Key: infrav1.HetznerSecretKeyRef{
HCloudToken: "hcloud",
HetznerRobotUser: "robot-user",
HetznerRobotPassword: "robot-password",
},
Name: "hetzner-secret",
},
SSHKeys: infrav1.HetznerSSHKeys{
HCloud: []infrav1.SSHKey{
{
Name: "testsshkey",
},
},
RobotRescueSecretRef: infrav1.SSHSecretRef{
Name: "rescue-ssh-secret",
Key: infrav1.SSHSecretKeyRef{
Name: "sshkey-name",
PublicKey: "public-key",
PrivateKey: "private-key",
},
},
},
}
}
// GetDefaultSSHSecret returns the default ssh secret given name and namespace.
func GetDefaultSSHSecret(name, namespace string) *corev1.Secret {
return &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
},
Data: map[string][]byte{
"private-key": []byte(fmt.Sprintf("%s-private-key", name)),
"sshkey-name": []byte("my-name"),
"public-key": []byte("my-public-key"),
},
}
}