-
Notifications
You must be signed in to change notification settings - Fork 135
Expand file tree
/
Copy pathhelper_test.go
More file actions
432 lines (370 loc) · 12.2 KB
/
helper_test.go
File metadata and controls
432 lines (370 loc) · 12.2 KB
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
//go:build e2e
package e2e
import (
"context"
"crypto/tls"
"errors"
"fmt"
"math/rand"
"net/http"
"os"
"strconv"
"strings"
"testing"
"time"
hrobot "github.com/syself/hrobot-go"
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/util/intstr"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
"github.com/hetznercloud/hcloud-cloud-controller-manager/internal/annotation"
"github.com/hetznercloud/hcloud-cloud-controller-manager/internal/hcops"
"github.com/hetznercloud/hcloud-cloud-controller-manager/internal/testsupport"
"github.com/hetznercloud/hcloud-cloud-controller-manager/internal/utils"
"github.com/hetznercloud/hcloud-go/v2/hcloud"
)
// Load Balancer creation timeouts. Services that involve certificate
// provisioning take longer to become ready.
const (
lbCreateTimeoutDefault = 8 * time.Minute
lbCreateTimeoutCert = 16 * time.Minute
)
// lbCreateTimeoutFor returns the timeout to use when waiting for the given
// Load Balancer service to become ready.
func lbCreateTimeoutFor(svc *corev1.Service) time.Duration {
certAnnotations := []annotation.Name{
annotation.LBSvcHTTPCertificates,
annotation.LBSvcHTTPCertificateType,
annotation.LBSvcHTTPManagedCertificateName,
annotation.LBSvcHTTPManagedCertificateDomains,
}
for _, a := range certAnnotations {
if _, ok := svc.Annotations[string(a)]; ok {
return lbCreateTimeoutCert
}
}
return lbCreateTimeoutDefault
}
var rng *rand.Rand
func init() {
rng = rand.New(rand.NewSource(time.Now().UnixNano()))
}
// pollBackoff is the standard exponential backoff used while polling for
// k8s/hcloud state in this suite: 1s base, doubling, capped at 30s.
var pollBackoff = hcloud.ExponentialBackoffWithOpts(hcloud.ExponentialBackoffOpts{
Base: time.Second,
Multiplier: 2,
Cap: 30 * time.Second,
})
type TestCluster struct {
hcloud *hcloud.Client
hrobot hrobot.RobotClient
k8sClient *kubernetes.Clientset
// envName is used as the name prefix in all resources
envName string
certDomain string
certificates *utils.SyncSet[int64]
loadBalancers *utils.SyncSet[string]
}
func (tc *TestCluster) Start() error {
// Hetzner Cloud Client
token := os.Getenv("HCLOUD_TOKEN")
if token == "" {
return fmt.Errorf("no valid HCLOUD_TOKEN found")
}
opts := []hcloud.ClientOption{
hcloud.WithToken(token),
hcloud.WithApplication("hcloud-ccm-testsuite", "1.0"),
hcloud.WithPollOpts(hcloud.PollOpts{
BackoffFunc: hcloud.ExponentialBackoffWithOpts(hcloud.ExponentialBackoffOpts{
Base: time.Second,
Multiplier: 2,
Cap: 10 * time.Second,
}),
}),
}
tc.hcloud = hcloud.NewClient(opts...)
// Hetzner Robot Client
if enabled := os.Getenv("ROBOT_ENABLED"); enabled == "true" {
robotUser := os.Getenv("ROBOT_USER")
robotPassword := os.Getenv("ROBOT_PASSWORD")
tc.hrobot = hrobot.NewBasicAuthClient(robotUser, robotPassword)
}
tc.envName = os.Getenv("ENV_NAME")
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
configOverrides := &clientcmd.ConfigOverrides{}
kubeConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, configOverrides)
clientConfig, err := kubeConfig.ClientConfig()
if err != nil {
return fmt.Errorf("kubeConfig.ClientConfig: %s", err)
}
tc.k8sClient, err = kubernetes.NewForConfig(clientConfig)
if err != nil {
return fmt.Errorf("kubernetes.NewForConfig: %s", err)
}
// Tests using this value should skip if empty
// The domain specified here must be available in Hetzner DNS of the account running the tests.
tc.certDomain = os.Getenv("CERT_DOMAIN")
tc.certificates = utils.NewSyncSet[int64]()
tc.loadBalancers = utils.NewSyncSet[string]()
return nil
}
func (tc *TestCluster) Stop() error {
errs := make([]error, 0, tc.loadBalancers.Size()+tc.certificates.Size())
ctx := context.Background()
uids := tc.loadBalancers.All()
selector := fmt.Sprintf("%s in (%s)", hcops.LabelServiceUID, strings.Join(uids, ","))
lbs, err := tc.hcloud.LoadBalancer.AllWithOpts(ctx, hcloud.LoadBalancerListOpts{
ListOpts: hcloud.ListOpts{
LabelSelector: selector,
},
})
if err != nil {
return fmt.Errorf("fetching load balancers via selector %s: %w", selector, err)
}
for _, lb := range lbs {
// Stop is called after `m.Run()`, so we can't use t.Log anymore.
fmt.Printf("force-deleting leaked load balancer %d (%s)\n", lb.ID, lb.Name)
if _, err := tc.hcloud.LoadBalancer.Delete(ctx, lb); err != nil {
errs = append(errs, fmt.Errorf("delete leaked load balancer %d failed: %w", lb.ID, err))
}
}
for _, item := range tc.certificates.All() {
// Stop is called after `m.Run()`, so we can't use t.Log anymore.
fmt.Printf("deleting certificate %d\n", item)
if _, err := tc.hcloud.Certificate.Delete(ctx, &hcloud.Certificate{ID: item}); err != nil {
errs = append(errs, fmt.Errorf("delete certificate %d failed: %w", item, err))
}
}
return errors.Join(errs...)
}
// CreateTLSCertificate creates a TLS certificate used for testing and posts it
// to the Hetzner Cloud backend.
//
// The baseName of the certificate gets a random number suffix attached.
// baseName and suffix are separated by a single "-" character.
func (tc *TestCluster) CreateTLSCertificate(t *testing.T, baseName string) (*hcloud.Certificate, error) {
rndInt := rng.Int()
name := fmt.Sprintf("%s-%d", baseName, rndInt)
p := testsupport.NewTLSPair(t, fmt.Sprintf("www.example%d.com", rndInt))
opts := hcloud.CertificateCreateOpts{
Name: name,
Certificate: p.Cert,
PrivateKey: p.Key,
}
cert, _, err := tc.hcloud.Certificate.Create(t.Context(), opts)
if err != nil {
return nil, fmt.Errorf("%s: %w", name, err)
}
if cert == nil {
return nil, errors.New("no certificate created")
}
tc.certificates.Add(cert.ID)
return cert, nil
}
// NetworkName returns the network name.
func (tc *TestCluster) NetworkName() string {
return tc.envName
}
// ControlNodeName returns the control node name.
func (tc *TestCluster) ControlNodeName() string {
return fmt.Sprintf("%s-control", tc.envName)
}
// WorkerNodeName returns the worker node name, zero indexed.
func (tc *TestCluster) WorkerNodeName(index int) string {
return fmt.Sprintf("%s-worker-%d", tc.envName, index)
}
type lbTestHelper struct {
t *testing.T
namespace string
podName string
port int
}
// DeployTestPod deploys a basic nginx pod within the k8s cluster
// and waits until it is "ready".
func (l *lbTestHelper) DeployTestPod() (*corev1.Pod, error) {
l.t.Helper()
ctx := l.t.Context()
if l.namespace == "" {
l.namespace = "hccm-test-" + strconv.Itoa(rand.Int())
}
_, err := testCluster.k8sClient.CoreV1().Namespaces().Create(ctx, &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: l.namespace,
},
}, metav1.CreateOptions{})
if err != nil && !k8serrors.IsAlreadyExists(err) {
return nil, fmt.Errorf("error deploying test pod: %w", err)
}
podName := fmt.Sprintf("pod-%s", l.podName)
testPod := corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: podName,
Labels: map[string]string{
"app": podName,
},
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: "nginx-hello-world",
Image: "nginxdemos/hello:plain-text",
Ports: []corev1.ContainerPort{
{
ContainerPort: 80,
Name: "http",
},
},
},
},
},
}
pod, err := testCluster.k8sClient.CoreV1().Pods(l.namespace).Create(ctx, &testPod, metav1.CreateOptions{})
if err != nil {
return nil, fmt.Errorf("could not create test pod: %w", err)
}
err = wait.PollUntilContextTimeout(ctx, 1*time.Second, 2*time.Minute, false, func(ctx context.Context) (done bool, err error) {
p, err := testCluster.k8sClient.CoreV1().Pods(l.namespace).Get(ctx, podName, metav1.GetOptions{})
if err != nil {
return false, err
}
for _, condition := range p.Status.Conditions {
if condition.Type == corev1.PodReady && condition.Status == corev1.ConditionTrue {
return true, nil
}
}
pod = p
return false, nil
})
if err != nil {
return nil, fmt.Errorf("pod %s did not come up after 2 minutes: %w", podName, err)
}
return pod, nil
}
// ServiceDefinition returns a service definition for a Hetzner Cloud Load Balancer (k8s service).
func (l *lbTestHelper) ServiceDefinition(pod *corev1.Pod, annotations map[string]string) *corev1.Service {
l.t.Helper()
port := l.port
if port == 0 {
port = 80
}
return &corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("svc-%s", l.podName),
Annotations: annotations,
},
Spec: corev1.ServiceSpec{
Selector: map[string]string{
"app": pod.Name,
},
Type: corev1.ServiceTypeLoadBalancer,
Ports: []corev1.ServicePort{
{
Port: int32(port),
TargetPort: intstr.FromInt(80),
Name: "http",
},
},
},
}
}
// CreateService creates a k8s service based on the given service definition
// and waits until it is "ready". The wait timeout is derived from the service
// annotations: services that provision certificates get a longer timeout.
func (l *lbTestHelper) CreateService(lbSvc *corev1.Service) (*corev1.Service, error) {
l.t.Helper()
timeout := lbCreateTimeoutFor(lbSvc)
lbSvc, err := testCluster.k8sClient.CoreV1().Services(l.namespace).Create(l.t.Context(), lbSvc, metav1.CreateOptions{})
if err != nil {
return nil, fmt.Errorf("could not create service: %w", err)
}
testCluster.loadBalancers.Add(string(lbSvc.UID))
ctx, cancel := context.WithTimeout(l.t.Context(), timeout)
defer cancel()
retries := 0
for {
svc, err := testCluster.k8sClient.CoreV1().Services(l.namespace).Get(ctx, lbSvc.Name, metav1.GetOptions{})
if err != nil {
return nil, fmt.Errorf("error fetching load balancer service: %w", err)
}
if len(svc.Status.LoadBalancer.Ingress) > 0 {
return svc, nil
}
select {
case <-ctx.Done():
return nil, fmt.Errorf("timed out waiting for load balancer service to receive ingress IPs")
case <-time.After(pollBackoff(retries)):
retries++
}
}
}
// TearDown deletes the created pod and service.
func (l *lbTestHelper) TearDown() {
l.t.Helper()
// No namespace was created yet (e.g. DeployTestPod never ran because a
// prior step failed); nothing to clean up.
if l.namespace == "" {
return
}
// Use context.Background() rather than t.Context(): cleanup must run to
// completion even when the test has already been cancelled or failed.
ctx := context.Background()
err := wait.PollUntilContextTimeout(ctx, 1*time.Second, 3*time.Minute, true, func(ctx context.Context) (bool, error) {
err := testCluster.k8sClient.CoreV1().Namespaces().Delete(ctx, l.namespace, metav1.DeleteOptions{})
if err != nil && !k8serrors.IsNotFound(err) {
return false, err
}
return k8serrors.IsNotFound(err), nil
})
if err != nil {
// The cluster is deleted afterward, so we can info log this error
l.t.Logf("error tearing down test namespace: %v", err)
}
}
// WaitForHTTPAvailable tries to connect to the given IP via HTTP or HTTPS
// (controlled by useHTTPS). It uses exponential backoff starting at 1s and
// capping at 30s, waiting up to 8 minutes for a successful HTTP 200 response.
// Each individual request has a 5s timeout.
func (l *lbTestHelper) WaitForHTTPAvailable(ingressIP string, useHTTPS bool) error {
l.t.Helper()
client := &http.Client{
Timeout: 5 * time.Second,
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true, // nolint
},
},
}
proto := "http"
if useHTTPS {
proto = "https"
}
ctx, cancel := context.WithTimeout(l.t.Context(), 8*time.Minute)
defer cancel()
retries := 0
for {
resp, err := client.Get(fmt.Sprintf("%s://%s", proto, ingressIP))
if err != nil {
l.t.Logf("request to %s failed, keep waiting: %v", ingressIP, err)
} else {
resp.Body.Close()
switch resp.StatusCode {
case http.StatusOK:
return nil
case http.StatusServiceUnavailable:
l.t.Log("service still unavailable, keep waiting")
default:
return fmt.Errorf("got unexpected HTTP status %d", resp.StatusCode)
}
}
select {
case <-ctx.Done():
return fmt.Errorf("timed out after 8m waiting for %s to be available", ingressIP)
case <-time.After(pollBackoff(retries)):
retries++
}
}
}