-
Notifications
You must be signed in to change notification settings - Fork 10
/
common_apps.go
833 lines (722 loc) · 26 KB
/
common_apps.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
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
package framework
import (
"context"
"fmt"
"strings"
"sync"
"time"
"github.com/google/uuid"
. "github.com/onsi/ginkgo"
goversion "github.com/hashicorp/go-version"
"github.com/prometheus/client_golang/api"
v1 "github.com/prometheus/client_golang/api/prometheus/v1"
"helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/chart/loader"
helmcli "helm.sh/helm/v3/pkg/cli"
"helm.sh/helm/v3/pkg/kube"
admissionregv1 "k8s.io/api/admissionregistration/v1"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/intstr"
"github.com/flomesh-io/fsm/pkg/constants"
"github.com/flomesh-io/fsm/pkg/k8s"
)
const (
// DefaultFsmGrafanaPort is the default Grafana port
DefaultFsmGrafanaPort = 3000
// DefaultFsmPrometheusPort default FSM prometheus port
DefaultFsmPrometheusPort = 7070
// FsmGrafanaAppLabel is the FSM Grafana deployment app label
FsmGrafanaAppLabel = "fsm-grafana"
// FsmPrometheusAppLabel is the FSM Prometheus deployment app label
FsmPrometheusAppLabel = "fsm-prometheus"
// FSM Grafana Dashboard specifics
// MeshDetails is dashboard uuid and name as we have them load in Grafana
MeshDetails string = "PLyKJcHGz/mesh-and-sidecar-details"
// MemRSSPanel is the ID of the MemRSS panel on FSM's MeshDetails dashboard
MemRSSPanel int = 13
// CPUPanel is the ID of the CPU panel on FSM's MeshDetails dashboard
CPUPanel int = 14
// maxPodCreationRetries determines the max number of retries for creating
// a Pod (including via a Deployment) upon failure
maxPodCreationRetries = 2
// delayIntervalForPodCreationRetries
delayIntervalForPodCreationRetries = 5 * time.Second
)
var (
// FsmCtlLabels is the list of app labels for FSM CTL
FsmCtlLabels = []string{constants.FSMControllerName, FsmGrafanaAppLabel, FsmPrometheusAppLabel, constants.FSMInjectorName, constants.FSMBootstrapName}
// NginxIngressSvc is the namespaced name of the nginx ingress service
NginxIngressSvc = types.NamespacedName{Namespace: "ingress-ns", Name: "ingress-nginx-controller"}
)
// CreateServiceAccount is a wrapper to create a service account
// If creating on OpenShift, add privileged SCC
func (td *FsmTestData) CreateServiceAccount(ns string, svcAccount *corev1.ServiceAccount) (*corev1.ServiceAccount, error) {
svcAc, err := td.Client.CoreV1().ServiceAccounts(ns).Create(context.Background(), svcAccount, metav1.CreateOptions{})
if err != nil {
err := fmt.Errorf("Could not create Service Account: %w", err)
return nil, err
}
if Td.DeployOnOpenShift {
err = Td.AddOpenShiftSCC("privileged", svcAc.Name, svcAc.Namespace)
return svcAc, err
}
return svcAc, nil
}
// createRole is a wrapper to create a role
func (td *FsmTestData) createRole(ns string, role *rbacv1.Role) (*rbacv1.Role, error) {
r, err := td.Client.RbacV1().Roles(ns).Create(context.Background(), role, metav1.CreateOptions{})
if err != nil {
err := fmt.Errorf("Could not create Role: %w", err)
return nil, err
}
return r, nil
}
// createRoleBinding is a wrapper to create a role binding
func (td *FsmTestData) createRoleBinding(ns string, roleBinding *rbacv1.RoleBinding) (*rbacv1.RoleBinding, error) {
rb, err := td.Client.RbacV1().RoleBindings(ns).Create(context.Background(), roleBinding, metav1.CreateOptions{})
if err != nil {
err := fmt.Errorf("Could not create RoleBinding: %w", err)
return nil, err
}
return rb, nil
}
func (td *FsmTestData) getMaxPodCreationRetries() int {
if td.RetryAppPodCreation {
return maxPodCreationRetries
}
return 1
}
// CreatePod is a wrapper to create a pod
func (td *FsmTestData) CreatePod(ns string, pod corev1.Pod) (*corev1.Pod, error) {
maxRetries := td.getMaxPodCreationRetries()
for i := 1; i <= maxRetries; i++ {
if i > 1 {
// Sleep before next retry
time.Sleep(delayIntervalForPodCreationRetries)
}
podRet, err := td.Client.CoreV1().Pods(ns).Create(context.Background(), &pod, metav1.CreateOptions{})
if err != nil {
td.T.Logf("Could not create Pod in attempt %d due to error: %w", i, err)
continue
}
return podRet, nil
}
return nil, fmt.Errorf("Error creating pod in namespace %s after %d attempts", ns, maxRetries)
}
// CreateDeployment is a wrapper to create a deployment
func (td *FsmTestData) CreateDeployment(ns string, deployment appsv1.Deployment) (*appsv1.Deployment, error) {
maxRetries := td.getMaxPodCreationRetries()
for i := 1; i <= maxRetries; i++ {
if i > 1 {
// Sleep before next retry
time.Sleep(delayIntervalForPodCreationRetries)
}
deploymentRet, err := td.Client.AppsV1().Deployments(ns).Create(context.Background(), &deployment, metav1.CreateOptions{})
if err != nil {
td.T.Logf("Could not create Deployment in attempt %d due to error: %v", i, err)
continue
}
return deploymentRet, nil
}
return nil, fmt.Errorf("Error creating Deployment in namespace %s after %d attempts", ns, maxRetries)
}
// CreateService is a wrapper to create a service
func (td *FsmTestData) CreateService(ns string, svc corev1.Service) (*corev1.Service, error) {
sv, err := td.Client.CoreV1().Services(ns).Create(context.Background(), &svc, metav1.CreateOptions{})
if err != nil {
err := fmt.Errorf("Could not create Service: %w", err)
return nil, err
}
return sv, nil
}
// CreateMutatingWebhook is a wrapper to create a mutating webhook configuration
func (td *FsmTestData) CreateMutatingWebhook(mwhc *admissionregv1.MutatingWebhookConfiguration) (*admissionregv1.MutatingWebhookConfiguration, error) {
mw, err := td.Client.AdmissionregistrationV1().MutatingWebhookConfigurations().Create(context.Background(), mwhc, metav1.CreateOptions{})
if err != nil {
err := fmt.Errorf("Could not create MutatingWebhook: %w", err)
return nil, err
}
return mw, nil
}
// GetMutatingWebhook is a wrapper to get a mutating webhook configuration
func (td *FsmTestData) GetMutatingWebhook(mwhcName string) (*admissionregv1.MutatingWebhookConfiguration, error) {
mwhc, err := td.Client.AdmissionregistrationV1().MutatingWebhookConfigurations().Get(context.Background(), mwhcName, metav1.GetOptions{})
if err != nil {
err := fmt.Errorf("Could not get MutatingWebhook: %w", err)
return nil, err
}
return mwhc, nil
}
// GetPodsForLabel returns the Pods matching a specific `appLabel`
func (td *FsmTestData) GetPodsForLabel(ns string, labelSel metav1.LabelSelector) ([]corev1.Pod, error) {
// Apparently there has to be a conversion between metav1 and labels
labelMap, _ := metav1.LabelSelectorAsMap(&labelSel)
pods, err := Td.Client.CoreV1().Pods(ns).List(context.Background(), metav1.ListOptions{
LabelSelector: labels.SelectorFromSet(labelMap).String(),
})
if err != nil {
return nil, err
}
return pods.Items, nil
}
/* Application templates
* The following functions contain high level helpers to create and get test application definitions.
*
* These abstractions aim to simplify and avoid tests having to individually type the the same k8s definitions for
* some common or recurrent deployment forms.
*/
// SimplePodAppDef defines some parametrization to create a pod-based application from template
type SimplePodAppDef struct {
Namespace string
PodName string
ServiceName string
ServiceAccountName string
ContainerName string
Image string
Command []string
Args []string
Ports []int
AppProtocol string
OS string
Labels map[string]string
}
// SimplePodApp returns a set of k8s typed definitions for a pod-based k8s definition.
// Includes Pod, Service and ServiceAccount types
func (td *FsmTestData) SimplePodApp(def SimplePodAppDef) (corev1.ServiceAccount, corev1.Pod, corev1.Service, error) {
if len(def.OS) == 0 {
return corev1.ServiceAccount{}, corev1.Pod{}, corev1.Service{}, fmt.Errorf("ClusterOS must be explicitly specified")
}
if len(def.PodName) == 0 {
return corev1.ServiceAccount{}, corev1.Pod{}, corev1.Service{}, fmt.Errorf("PodName must be explicitly specified")
}
if def.Labels == nil {
def.Labels = map[string]string{constants.AppLabel: def.PodName}
}
serviceAccountName := def.ServiceAccountName
if serviceAccountName == "" {
serviceAccountName = RandomNameWithPrefix("serviceaccount")
}
serviceName := def.ServiceName
if serviceName == "" {
serviceName = RandomNameWithPrefix("service")
}
containerName := def.ContainerName
if containerName == "" {
containerName = def.PodName
}
serviceAccountDefinition := Td.SimpleServiceAccount(serviceAccountName, def.Namespace)
podDefinition := corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: def.PodName,
Namespace: def.Namespace,
Labels: def.Labels,
},
Spec: corev1.PodSpec{
TerminationGracePeriodSeconds: new(int64), // 0
ServiceAccountName: serviceAccountName,
NodeSelector: map[string]string{
"kubernetes.io/os": def.OS,
},
Containers: []corev1.Container{
{
Name: containerName,
Image: def.Image,
ImagePullPolicy: corev1.PullIfNotPresent,
Resources: corev1.ResourceRequirements{
Limits: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("100m"),
corev1.ResourceMemory: resource.MustParse("128Mi"),
},
},
Env: []corev1.EnvVar{
{
Name: "POD_IP",
ValueFrom: &corev1.EnvVarSource{
FieldRef: &corev1.ObjectFieldSelector{
APIVersion: "v1",
FieldPath: "status.podIP",
},
},
},
},
},
},
},
}
if td.AreRegistryCredsPresent() {
podDefinition.Spec.ImagePullSecrets = []corev1.LocalObjectReference{
{
Name: RegistrySecretName,
},
}
}
if def.Command != nil && len(def.Command) > 0 {
podDefinition.Spec.Containers[0].Command = def.Command
}
if def.Args != nil && len(def.Args) > 0 {
podDefinition.Spec.Containers[0].Args = def.Args
}
serviceDefinition := corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: serviceName,
Labels: def.Labels,
},
Spec: corev1.ServiceSpec{
Selector: def.Labels,
},
}
if def.Ports != nil && len(def.Ports) > 0 {
podDefinition.Spec.Containers[0].Ports = []corev1.ContainerPort{}
serviceDefinition.Spec.Ports = []corev1.ServicePort{}
for _, p := range def.Ports {
podDefinition.Spec.Containers[0].Ports = append(podDefinition.Spec.Containers[0].Ports,
corev1.ContainerPort{
ContainerPort: int32(p),
},
)
svcPort := corev1.ServicePort{
Port: int32(p),
TargetPort: intstr.FromInt(p),
}
if def.AppProtocol != "" {
if ver, err := td.getKubernetesServerVersionNumber(); err != nil {
svcPort.Name = fmt.Sprintf("%s-%d", def.AppProtocol, p) // use named port with AppProtocol
} else {
// use appProtocol field in servicePort if k8s server version >= 1.19
if ver[0] >= 1 && ver[1] >= 19 {
svcPort.AppProtocol = &def.AppProtocol // set the appProtocol field
} else {
svcPort.Name = fmt.Sprintf("%s-%d", def.AppProtocol, p) // use named port with AppProtocol
}
}
}
serviceDefinition.Spec.Ports = append(serviceDefinition.Spec.Ports, svcPort)
}
}
return serviceAccountDefinition, podDefinition, serviceDefinition, nil
}
// SimpleServiceAccount returns a k8s typed definition for a service account.
func (td *FsmTestData) SimpleServiceAccount(name string, namespace string) corev1.ServiceAccount {
serviceAccountDefinition := corev1.ServiceAccount{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
},
}
return serviceAccountDefinition
}
// simpleRole returns a k8s typed definition for a role.
func (td *FsmTestData) simpleRole(name string, namespace string) rbacv1.Role {
roleDefinition := rbacv1.Role{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
},
}
return roleDefinition
}
// simpleRoleBinding returns a k8s typed definition for a role binding.
func (td *FsmTestData) simpleRoleBinding(name string, namespace string) rbacv1.RoleBinding {
roleBindingDefinition := rbacv1.RoleBinding{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
},
}
return roleBindingDefinition
}
// getKubernetesServerVersionNumber returns the version number in chunks, ex. v1.19.3 => [1, 19, 3]
func (td *FsmTestData) getKubernetesServerVersionNumber() ([]int, error) {
version, err := td.Client.Discovery().ServerVersion()
if err != nil {
return nil, fmt.Errorf("Error getting K8s server version: %w", err)
}
ver, err := goversion.NewVersion(version.String())
if err != nil {
return nil, fmt.Errorf("Error parsing k8s server version %s: %w", version, err)
}
return ver.Segments(), nil
}
// SimpleDeploymentAppDef defines some parametrization to create a deployment-based application from template
type SimpleDeploymentAppDef struct {
Namespace string
DeploymentName string
ServiceName string
ContainerName string
ServiceAccountName string
Image string
ReplicaCount int32
Command PodCommand
Args []string
Ports []int
AppProtocol string
OS string
Labels map[string]string
}
// PodCommand describes a command for a pod
type PodCommand []string
// PodCommandDefault is the default pod command (nothing)
var PodCommandDefault = []string{}
// SimpleDeploymentApp creates returns a set of k8s typed definitions for a deployment-based k8s definition.
// Includes Deployment, Service and ServiceAccount types
func (td *FsmTestData) SimpleDeploymentApp(def SimpleDeploymentAppDef) (corev1.ServiceAccount, appsv1.Deployment, corev1.Service, error) {
if len(def.OS) == 0 {
return corev1.ServiceAccount{}, appsv1.Deployment{}, corev1.Service{}, fmt.Errorf("ClusterOS must be explicitly specified")
}
if def.Labels == nil {
def.Labels = map[string]string{constants.AppLabel: def.DeploymentName}
}
serviceAccountName := def.ServiceAccountName
if serviceAccountName == "" {
serviceAccountName = RandomNameWithPrefix("serviceaccount")
}
serviceName := def.ServiceName
if serviceName == "" {
serviceName = RandomNameWithPrefix("service")
}
serviceAccountDefinition := corev1.ServiceAccount{
ObjectMeta: metav1.ObjectMeta{
Name: serviceAccountName,
Namespace: def.Namespace,
},
}
containerName := def.ContainerName
if containerName == "" {
containerName = def.DeploymentName
}
// Required, as replica count is a pointer to distinguish between 0 and not specified
replicaCountExplicitDeclaration := def.ReplicaCount
deploymentDefinition := appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: def.DeploymentName,
Namespace: def.Namespace,
},
Spec: appsv1.DeploymentSpec{
Replicas: &replicaCountExplicitDeclaration,
Selector: &metav1.LabelSelector{
MatchLabels: def.Labels,
},
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: def.Labels,
},
Spec: corev1.PodSpec{
TerminationGracePeriodSeconds: new(int64), // 0
ServiceAccountName: serviceAccountName,
NodeSelector: map[string]string{
"kubernetes.io/os": def.OS,
},
Containers: []corev1.Container{
{
Name: containerName,
Image: def.Image,
ImagePullPolicy: corev1.PullIfNotPresent,
},
},
},
},
},
}
if td.AreRegistryCredsPresent() {
deploymentDefinition.Spec.Template.Spec.ImagePullSecrets = []corev1.LocalObjectReference{
{
Name: RegistrySecretName,
},
}
}
if def.Command != nil && len(def.Command) > 0 {
deploymentDefinition.Spec.Template.Spec.Containers[0].Command = def.Command
}
if def.Args != nil && len(def.Args) > 0 {
deploymentDefinition.Spec.Template.Spec.Containers[0].Args = def.Args
}
serviceDefinition := corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: serviceName,
Namespace: def.Namespace,
Labels: def.Labels,
},
Spec: corev1.ServiceSpec{
Selector: def.Labels,
},
}
if def.Ports != nil && len(def.Ports) > 0 {
deploymentDefinition.Spec.Template.Spec.Containers[0].Ports = []corev1.ContainerPort{}
serviceDefinition.Spec.Ports = []corev1.ServicePort{}
for _, p := range def.Ports {
deploymentDefinition.Spec.Template.Spec.Containers[0].Ports = append(deploymentDefinition.Spec.Template.Spec.Containers[0].Ports,
corev1.ContainerPort{
ContainerPort: int32(p),
},
)
svcPort := corev1.ServicePort{
Port: int32(p),
TargetPort: intstr.FromInt(p),
}
if def.AppProtocol != "" {
if ver, err := td.getKubernetesServerVersionNumber(); err != nil {
svcPort.Name = fmt.Sprintf("%s-%d", def.AppProtocol, p) // use named port with AppProtocol
} else {
// use appProtocol field in servicePort if k8s server version >= 1.19
if ver[0] >= 1 && ver[1] >= 19 {
svcPort.AppProtocol = &def.AppProtocol // set the appProtocol field
} else {
svcPort.Name = fmt.Sprintf("%s-%d", def.AppProtocol, p) // use named port with AppProtocol
}
}
}
serviceDefinition.Spec.Ports = append(serviceDefinition.Spec.Ports, svcPort)
}
}
return serviceAccountDefinition, deploymentDefinition, serviceDefinition, nil
}
// GetOSSpecificHTTPBinPod returns a OS pod that runs httpbin.
func (td *FsmTestData) GetOSSpecificHTTPBinPod(podName string, namespace string, customCommand ...string) (corev1.ServiceAccount, corev1.Pod, corev1.Service, error) {
appDef := SimplePodAppDef{
PodName: podName,
Namespace: namespace,
Image: "flomesh/httpbin:ken",
Ports: []int{80},
OS: Td.ClusterOS,
}
if len(customCommand) > 0 {
appDef.Command = customCommand
}
return Td.SimplePodApp(appDef)
}
// GetOSSpecificSleepPod returns a simple OS specific busy loop pod.
func (td *FsmTestData) GetOSSpecificSleepPod(sourceNs string) (corev1.ServiceAccount, corev1.Pod, corev1.Service, error) {
return Td.SimplePodApp(SimplePodAppDef{
PodName: RandomNameWithPrefix("pod"),
Namespace: sourceNs,
Command: []string{"/bin/bash", "-c", "--"},
Args: []string{"while true; do sleep 30; done;"},
Image: "flomesh/alpine-debug",
Ports: []int{80},
OS: td.ClusterOS,
})
}
// GetOSSpecificTCPEchoPod returns a simple OS specific tcp-echo pod.
func (td *FsmTestData) GetOSSpecificTCPEchoPod(podName string, namespace string, destinationPort int) (corev1.ServiceAccount, corev1.Pod, corev1.Service, error) {
var image string
var command string
installOpts := Td.GetFSMInstallOpts()
image = fmt.Sprintf("%s/fsm-demo-tcp-echo-server:%s", installOpts.ContainerRegistryLoc, installOpts.FsmImagetag)
command = "/tcp-echo-server"
return Td.SimplePodApp(
SimplePodAppDef{
PodName: podName,
Namespace: namespace,
Image: image,
Command: []string{command},
Args: []string{"--port", fmt.Sprintf("%d", destinationPort)},
Ports: []int{destinationPort},
AppProtocol: constants.ProtocolTCP,
OS: Td.ClusterOS,
})
}
// GetGrafanaPodHandle generic func to forward a grafana pod and returns a handler pointing to the locally forwarded resource
func (td *FsmTestData) GetGrafanaPodHandle(ns string, grafanaPodName string, port uint16) (*Grafana, error) {
dialer, err := k8s.DialerToPod(td.RestConfig, td.Client, grafanaPodName, ns)
if err != nil {
return nil, err
}
portForwarder, err := k8s.NewPortForwarder(dialer, fmt.Sprintf("%d:%d", port, port))
if err != nil {
return nil, fmt.Errorf("Error setting up port forwarding: %w", err)
}
err = portForwarder.Start(func(pf *k8s.PortForwarder) error {
return nil
})
if err != nil {
return nil, fmt.Errorf("Could not start forwarding: %w", err)
}
return &Grafana{
Schema: "http",
Hostname: "localhost",
Port: port,
User: "admin", // default value of grafana deployment
Password: "admin", // default value of grafana deployment
pfwd: portForwarder,
}, nil
}
// GetPrometheusPodHandle generic func to forward a prometheus pod and returns a handler pointing to the locally forwarded resource
func (td *FsmTestData) GetPrometheusPodHandle(ns string, prometheusPodName string, port uint16) (*Prometheus, error) {
dialer, err := k8s.DialerToPod(td.RestConfig, td.Client, prometheusPodName, ns)
if err != nil {
return nil, err
}
portForwarder, err := k8s.NewPortForwarder(dialer, fmt.Sprintf("%d:%d", port, port))
if err != nil {
return nil, fmt.Errorf("Error setting up port forwarding: %w", err)
}
err = portForwarder.Start(func(pf *k8s.PortForwarder) error {
return nil
})
if err != nil {
return nil, fmt.Errorf("Could not start forwarding: %w", err)
}
client, err := api.NewClient(api.Config{
Address: fmt.Sprintf("http://localhost:%d", port),
})
if err != nil {
return nil, err
}
v1api := v1.NewAPI(client)
return &Prometheus{
Client: client,
API: v1api,
pfwd: portForwarder,
}, nil
}
func (td *FsmTestData) waitForFSMControlPlane() error {
var errController, errInjector, errBootstrap error
waitGroup := sync.WaitGroup{}
waitGroup.Add(3)
go func() {
defer GinkgoRecover()
errController = td.WaitForPodsRunningReady(td.FsmNamespace, 1, &metav1.LabelSelector{
MatchLabels: map[string]string{
constants.AppLabel: constants.FSMControllerName,
},
})
waitGroup.Done()
}()
go func() {
defer GinkgoRecover()
errInjector = td.WaitForPodsRunningReady(td.FsmNamespace, 1, &metav1.LabelSelector{
MatchLabels: map[string]string{
constants.AppLabel: constants.FSMInjectorName,
},
})
waitGroup.Done()
}()
go func() {
defer GinkgoRecover()
errBootstrap = td.WaitForPodsRunningReady(td.FsmNamespace, 1, &metav1.LabelSelector{
MatchLabels: map[string]string{
constants.AppLabel: constants.FSMBootstrapName,
},
})
waitGroup.Done()
}()
waitGroup.Wait()
if errController != nil || errInjector != nil {
return fmt.Errorf("FSM Control plane was not ready in time (%v, %v, %v)", errController, errInjector, errBootstrap)
}
return nil
}
// GetFSMPrometheusHandle convenience wrapper, will get the Prometheus instance regularly deployed
// by FSM installation in test <FsmNamespace>
func (td *FsmTestData) GetFSMPrometheusHandle() (*Prometheus, error) {
prometheusPod, err := Td.GetPodsForLabel(Td.FsmNamespace, metav1.LabelSelector{
MatchLabels: map[string]string{
constants.AppLabel: FsmPrometheusAppLabel,
},
})
if err != nil || len(prometheusPod) == 0 {
return nil, fmt.Errorf("Error getting Prometheus pods: %w (prom pods len: %d)", err, len(prometheusPod))
}
pHandle, err := Td.GetPrometheusPodHandle(prometheusPod[0].Namespace, prometheusPod[0].Name, DefaultFsmPrometheusPort)
if err != nil {
return nil, err
}
return pHandle, nil
}
// GetFSMGrafanaHandle convenience wrapper, will get the Grafana instance regularly deployed
// by FSM installation in test <FsmNamespace>
func (td *FsmTestData) GetFSMGrafanaHandle() (*Grafana, error) {
grafanaPod, err := Td.GetPodsForLabel(Td.FsmNamespace, metav1.LabelSelector{
MatchLabels: map[string]string{
constants.AppLabel: FsmGrafanaAppLabel,
},
})
if err != nil || len(grafanaPod) == 0 {
return nil, fmt.Errorf("Error getting Grafana pods: %w (graf pods len: %d)", err, len(grafanaPod))
}
gHandle, err := Td.GetGrafanaPodHandle(grafanaPod[0].Namespace, grafanaPod[0].Name, DefaultFsmGrafanaPort)
if err != nil {
return nil, err
}
return gHandle, nil
}
// InstallNginxIngress installs the k8s Nginx Ingress controller and returns the IP address
// that clients can send traffic to for ingress
func (td *FsmTestData) InstallNginxIngress() (string, error) {
// Check the node's provider so this works for preprovisioned kind clusters
nodes, err := td.Client.CoreV1().Nodes().List(context.Background(), metav1.ListOptions{})
if err != nil {
return "", fmt.Errorf("Error listing nodes to install nginx ingress: %w", err)
}
providerID := nodes.Items[0].Spec.ProviderID
isKind := strings.HasPrefix(providerID, "kind://")
var vals map[string]interface{}
if isKind {
vals = map[string]interface{}{
"controller": map[string]interface{}{
"hostPort": map[string]interface{}{
"enabled": true,
},
"nodeSelector": map[string]interface{}{
"ingress-ready": "true",
},
"service": map[string]interface{}{
"type": "NodePort",
},
},
}
}
if err := td.CreateNs(NginxIngressSvc.Namespace, nil); err != nil {
return "", fmt.Errorf("Error creating namespace for nginx ingress: %w", err)
}
helmConfig := &action.Configuration{}
if err := helmConfig.Init(Td.Env.RESTClientGetter(), NginxIngressSvc.Namespace, "secret", Td.T.Logf); err != nil {
return "", fmt.Errorf("Error initializing Helm config for nginx ingress: %w", err)
}
helmConfig.KubeClient.(*kube.Client).Namespace = NginxIngressSvc.Namespace
install := action.NewInstall(helmConfig)
install.RepoURL = "https://kubernetes.github.io/ingress-nginx"
install.Namespace = NginxIngressSvc.Namespace
install.ReleaseName = "ingress-nginx"
install.Version = "4.0.18"
install.Wait = true
install.Timeout = 5 * time.Minute
chartPath, err := install.LocateChart("ingress-nginx", helmcli.New())
if err != nil {
return "", fmt.Errorf("Error locating ingress-nginx Helm chart: %w", err)
}
chart, err := loader.Load(chartPath)
if err != nil {
return "", fmt.Errorf("Error loading ingress-nginx chart %s: %w", chartPath, err)
}
if _, err = install.Run(chart, vals); err != nil {
return "", fmt.Errorf("Error installing ingress-nginx: %w", err)
}
ingressAddr := "localhost"
if !isKind {
svc, err := Td.Client.CoreV1().Services(NginxIngressSvc.Namespace).Get(context.Background(), NginxIngressSvc.Name, metav1.GetOptions{})
if err != nil {
return "", fmt.Errorf("Error getting service: %s/%s: %w", NginxIngressSvc.Namespace, NginxIngressSvc.Name, err)
}
ingressAddr = svc.Status.LoadBalancer.Ingress[0].IP
if len(ingressAddr) == 0 {
ingressAddr = svc.Status.LoadBalancer.Ingress[0].Hostname
}
}
return ingressAddr, nil
}
// RandomNameWithPrefix generates a random string with the given prefix.
//
// If the prefix is empty, the default prefix "test" will be used
func RandomNameWithPrefix(prefix string) string {
if prefix == "" || len(prefix) > 100 {
prefix = "test"
}
return fmt.Sprintf("%s-%s", prefix, uuid.New().String())
}