-
Notifications
You must be signed in to change notification settings - Fork 259
/
Copy pathprovisioning_and_annotation_test.go
265 lines (234 loc) · 9.2 KB
/
provisioning_and_annotation_test.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
256
257
258
259
260
261
262
263
264
265
//go:build e2e
// +build e2e
package e2e
import (
"context"
"encoding/json"
"fmt"
"os"
"path"
"time"
metal3api "github.com/metal3-io/baremetal-operator/apis/metal3.io/v1alpha1"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"golang.org/x/crypto/ssh"
corev1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/cluster-api/test/framework"
"sigs.k8s.io/cluster-api/util"
"sigs.k8s.io/cluster-api/util/patch"
)
var _ = Describe("Provision, detach, recreate from status and deprovision", Label("required", "provision", "detach", "status", "deprovision"),
func() {
var (
specName = "provisioning-ops"
secretName = "bmc-credentials"
namespace *corev1.Namespace
cancelWatches context.CancelFunc
)
BeforeEach(func() {
namespace, cancelWatches = framework.CreateNamespaceAndWatchEvents(ctx, framework.CreateNamespaceAndWatchEventsInput{
Creator: clusterProxy.GetClient(),
ClientSet: clusterProxy.GetClientSet(),
Name: fmt.Sprintf("%s-%s", specName, util.RandomString(6)),
LogFolder: artifactFolder,
})
})
It("provisions a BMH, applies detached and status annotations, then deprovisions", func() {
By("Creating a secret with BMH credentials")
bmcCredentialsData := map[string]string{
"username": bmc.User,
"password": bmc.Password,
}
CreateSecret(ctx, clusterProxy.GetClient(), namespace.Name, secretName, bmcCredentialsData)
By("Creating a BMH with inspection disabled and hardware details added")
bmh := metal3api.BareMetalHost{
ObjectMeta: metav1.ObjectMeta{
Name: specName,
Namespace: namespace.Name,
Annotations: map[string]string{
metal3api.InspectAnnotationPrefix: "disabled",
metal3api.HardwareDetailsAnnotation: hardwareDetails,
},
},
Spec: metal3api.BareMetalHostSpec{
Online: true,
BMC: metal3api.BMCDetails{
Address: bmc.Address,
CredentialsName: "bmc-credentials",
},
BootMode: metal3api.Legacy,
BootMACAddress: bmc.BootMacAddress,
AutomatedCleaningMode: "disabled",
},
}
err := clusterProxy.GetClient().Create(ctx, &bmh)
Expect(err).NotTo(HaveOccurred())
By("Waiting for the BMH to become available")
WaitForBmhInProvisioningState(ctx, WaitForBmhInProvisioningStateInput{
Client: clusterProxy.GetClient(),
Bmh: bmh,
State: metal3api.StateAvailable,
}, e2eConfig.GetIntervals(specName, "wait-available")...)
By("Patching the BMH to test provisioning")
helper, err := patch.NewHelper(&bmh, clusterProxy.GetClient())
Expect(err).NotTo(HaveOccurred())
bmh.Spec.Image = &metal3api.Image{
URL: e2eConfig.GetVariable("IMAGE_URL"),
Checksum: e2eConfig.GetVariable("IMAGE_CHECKSUM"),
ChecksumType: metal3api.AutoChecksum,
}
bmh.Spec.RootDeviceHints = &metal3api.RootDeviceHints{
DeviceName: "/dev/vda",
}
// The ssh check is not possible in all situations (e.g. fixture) so it can be skipped
if e2eConfig.GetVariable("SSH_CHECK_PROVISIONED") == "true" {
userDataSecretName := "user-data"
sshPubKeyPath := e2eConfig.GetVariable("SSH_PUB_KEY")
createCirrosInstanceAndHostnameUserdata(ctx, clusterProxy.GetClient(), namespace.Name, userDataSecretName, sshPubKeyPath)
bmh.Spec.UserData = &corev1.SecretReference{
Name: userDataSecretName,
Namespace: namespace.Name,
}
}
Expect(helper.Patch(ctx, &bmh)).To(Succeed())
By("Waiting for the BMH to be in provisioning state")
WaitForBmhInProvisioningState(ctx, WaitForBmhInProvisioningStateInput{
Client: clusterProxy.GetClient(),
Bmh: bmh,
State: metal3api.StateProvisioning,
}, e2eConfig.GetIntervals(specName, "wait-provisioning")...)
By("Waiting for the BMH to become provisioned")
WaitForBmhInProvisioningState(ctx, WaitForBmhInProvisioningStateInput{
Client: clusterProxy.GetClient(),
Bmh: bmh,
State: metal3api.StateProvisioned,
}, e2eConfig.GetIntervals(specName, "wait-provisioned")...)
// The ssh check is not possible in all situations (e.g. fixture) so it can be skipped
if e2eConfig.GetVariable("SSH_CHECK_PROVISIONED") == "true" {
By("Verifying the node booting from disk")
keyPath := e2eConfig.GetVariable("SSH_PRIV_KEY")
key, err := os.ReadFile(keyPath)
Expect(err).NotTo(HaveOccurred(), "unable to read private key")
signer, err := ssh.ParsePrivateKey(key)
Expect(err).NotTo(HaveOccurred(), "unable to parse private key")
auth := ssh.PublicKeys(signer)
PerformSSHBootCheck(e2eConfig, "disk", auth, fmt.Sprintf("%s:%s", bmc.IPAddress, bmc.SSHPort))
} else {
Logf("WARNING: Skipping SSH check since SSH_CHECK_PROVISIONED != true")
}
By("Retrieving the latest BMH object")
err = clusterProxy.GetClient().Get(ctx, types.NamespacedName{
Name: bmh.Name,
Namespace: bmh.Namespace,
}, &bmh)
Expect(err).NotTo(HaveOccurred())
By("Adding the detached annotation")
helper, err = patch.NewHelper(&bmh, clusterProxy.GetClient())
Expect(err).NotTo(HaveOccurred())
// Add the detached annotation; "true" is used explicitly to clarify intent.
bmh.ObjectMeta.Annotations["baremetalhost.metal3.io/detached"] = "true"
Expect(helper.Patch(ctx, &bmh)).To(Succeed())
By("Saving the status to a JSON string")
savedStatus := bmh.Status
statusJSON, err := json.Marshal(savedStatus)
Expect(err).NotTo(HaveOccurred())
By("Deleting the BMH")
// Wait for 2 seconds to allow time to confirm annotation is set
// TODO: fix this so we do not need the sleep
time.Sleep(2 * time.Second)
err = clusterProxy.GetClient().Delete(ctx, &bmh)
Expect(err).NotTo(HaveOccurred())
By("Waiting for the BMH to be deleted")
WaitForBmhDeleted(ctx, WaitForBmhDeletedInput{
Client: clusterProxy.GetClient(),
BmhName: bmh.Name,
Namespace: bmh.Namespace,
UndesiredStates: []metal3api.ProvisioningState{
metal3api.StateProvisioning,
metal3api.StateRegistering,
metal3api.StateDeprovisioning,
},
}, e2eConfig.GetIntervals(specName, "wait-deleted")...)
By("Waiting for the secret to be deleted")
Eventually(func() bool {
err := clusterProxy.GetClient().Get(ctx, types.NamespacedName{
Name: "bmc-credentials",
Namespace: namespace.Name,
}, &corev1.Secret{})
return k8serrors.IsNotFound(err)
}, e2eConfig.GetIntervals(specName, "wait-secret-deletion")...).Should(BeTrue())
By("Creating a secret with BMH credentials")
bmcCredentialsData = map[string]string{
"username": bmc.User,
"password": bmc.Password,
}
CreateSecret(ctx, clusterProxy.GetClient(), namespace.Name, secretName, bmcCredentialsData)
By("Recreating the BMH with the previously saved status in the status annotation")
bmh = metal3api.BareMetalHost{
ObjectMeta: metav1.ObjectMeta{
Name: specName,
Namespace: namespace.Name,
Annotations: map[string]string{
metal3api.StatusAnnotation: string(statusJSON),
},
},
Spec: metal3api.BareMetalHostSpec{
Online: true,
BMC: metal3api.BMCDetails{
Address: bmc.Address,
CredentialsName: "bmc-credentials",
},
BootMode: metal3api.Legacy,
BootMACAddress: bmc.BootMacAddress,
AutomatedCleaningMode: "disabled",
Image: &metal3api.Image{
URL: e2eConfig.GetVariable("IMAGE_URL"),
Checksum: e2eConfig.GetVariable("IMAGE_CHECKSUM"),
},
RootDeviceHints: &metal3api.RootDeviceHints{
DeviceName: "/dev/vda",
},
},
}
err = clusterProxy.GetClient().Create(ctx, &bmh)
Expect(err).NotTo(HaveOccurred())
By("Checking that the BMH goes directly to 'provisioned' state")
WaitForBmhInProvisioningState(ctx, WaitForBmhInProvisioningStateInput{
Client: clusterProxy.GetClient(),
Bmh: bmh,
State: metal3api.StateProvisioned,
UndesiredStates: []metal3api.ProvisioningState{
metal3api.StateProvisioning,
metal3api.StateRegistering,
metal3api.StateDeprovisioning,
metal3api.StateDeleting,
},
}, e2eConfig.GetIntervals(specName, "wait-provisioned")...)
By("Triggering the deprovisioning of the BMH")
helper, err = patch.NewHelper(&bmh, clusterProxy.GetClient())
Expect(err).NotTo(HaveOccurred())
bmh.Spec.Image = nil
Expect(helper.Patch(ctx, &bmh)).To(Succeed())
By("Waiting for the BMH to be in deprovisioning state")
WaitForBmhInProvisioningState(ctx, WaitForBmhInProvisioningStateInput{
Client: clusterProxy.GetClient(),
Bmh: bmh,
State: metal3api.StateDeprovisioning,
}, e2eConfig.GetIntervals(specName, "wait-deprovisioning")...)
By("Waiting for the BMH to become available again")
WaitForBmhInProvisioningState(ctx, WaitForBmhInProvisioningStateInput{
Client: clusterProxy.GetClient(),
Bmh: bmh,
State: metal3api.StateAvailable,
}, e2eConfig.GetIntervals(specName, "wait-available")...)
})
AfterEach(func() {
DumpResources(ctx, e2eConfig, clusterProxy, namespace.Name, path.Join(artifactFolder, specName))
if !skipCleanup {
cleanup(ctx, clusterProxy, namespace, cancelWatches, e2eConfig.GetIntervals("default", "wait-namespace-deleted")...)
}
})
})