This repository has been archived by the owner on Jul 6, 2023. It is now read-only.
/
setup.go
113 lines (95 loc) · 4.11 KB
/
setup.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
package test
import (
"context"
"fmt"
"testing"
"time"
"github.com/kelseyhightower/envconfig"
"github.com/vmware/govmomi/simulator"
"go.uber.org/zap"
appsv1 "k8s.io/api/apps/v1"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"knative.dev/pkg/logging"
)
const (
VSphereSecretName = "preemption-worker-secret"
VCSimDeploymentName = "vcsim"
VCClientJobName = "tagvms"
PollInterval = time.Second * 3
PollTimeout = time.Second * 30
DeployedVMs = 8
TaggedVMs = 2 // vms with preemption tag
)
var (
DefaultLabels = map[string]string{
"app": "vsphere-preemption",
"test": "e2e",
}
)
type Config struct {
KubeConfig string `envconfig:"KUBECONFIG"`
Namespace string `default:"vsphere-preemption-e2e" required:"true"`
WorkerImage string `envconfig:"WORKER_IMAGE" required:"true"` // preemption worker
CLIImage string `envconfig:"CLI_IMAGE" required:"true"` // preemptctl job
TagVmsImage string `envconfig:"TAG_VM_IMAGE" required:"true"` // job configures vcsim
GetVMsImage string `envconfig:"GET_VM_IMAGE" required:"true"` // job reads from vcsim
// Temporal settings (helm minimal install defaults)
Address string `envconfig:"TEMPORAL_URL" default:"temporaltest-frontend.default.svc.cluster.local:7233" required:"true"`
TNamespace string `envconfig:"TEMPORAL_NAMESPACE" default:"vsphere-preemption" required:"true"`
Queue string `envconfig:"TEMPORAL_TASKQUEUE" default:"vsphere-preemption" required:"true"`
// vSphere settings (vcsim defaults)
Insecure string `envconfig:"VCENTER_INSECURE" default:"true"`
VCAddress string `envconfig:"VCENTER_URL" default:"https://vcsim.vsphere-preemption-e2e.svc.cluster.local" required:"true"`
Tag string `envconfig:"VCENTER_TAG" default:"preemptible" required:"true"`
}
type CleanupFunc func(ctx context.Context) error
func SetupEnvironment(t *testing.T, ctx context.Context, client *kubernetes.Clientset, logger *zap.Logger) (CleanupFunc, error) {
t.Helper()
var cfg Config
if err := envconfig.Process("", &cfg); err != nil {
t.Fatal(err)
}
cleanup := func(ctx context.Context) error {
logger.Warn("deleting namespace")
delPolicy := metav1.DeletePropagationForeground
return client.CoreV1().Namespaces().Delete(ctx, cfg.Namespace, metav1.DeleteOptions{
PropagationPolicy: &delPolicy,
})
}
logger.Debug("creating namespace")
ctx = logging.WithLogger(ctx, logger.Sugar())
_, err := createNamespace(ctx, client, cfg.Namespace, DefaultLabels)
if err != nil {
if errors.IsAlreadyExists(err) {
logger.Warn("namespace already exists") // continue on exists
} else {
t.Fatalf("create Kubernetes namespace: %v", err)
}
}
logger.Debug("creating vcenter simulator deployment and service")
if err = createVCSimulator(ctx, client, VCSimDeploymentName, cfg.Namespace, DefaultLabels); err != nil {
t.Fatalf("create vcenter simulator deployment and service: %v", err)
}
if err = WaitDeploymentReady(ctx, client, cfg.Namespace, VCSimDeploymentName, PollInterval, PollTimeout); err != nil {
t.Fatalf("vcenter simulator deployment condition %q did not reach state %q: %v", appsv1.DeploymentAvailable, corev1.ConditionTrue, err)
}
logger.Debug("creating vcenter simulator client job to configure environment")
if _, err = createVSphereTagJob(ctx, client, VCClientJobName, cfg, DefaultLabels); err != nil {
t.Fatalf("create and execute vcenter client job: %v", err)
}
if err = WaitJobComplete(ctx, client, cfg.Namespace, VCClientJobName, PollInterval, PollTimeout); err != nil {
t.Fatalf("vcenter client job condition %q did not reach state %q: %v", batchv1.JobComplete, corev1.ConditionTrue, err)
}
username := simulator.DefaultLogin.Username()
password, _ := simulator.DefaultLogin.Password()
logger.Debug("creating vsphere credentials secret", zap.String("name", VSphereSecretName))
_, err = createVSphereSecret(ctx, client, VSphereSecretName, cfg.Namespace, username, password, DefaultLabels)
if err != nil {
return cleanup, fmt.Errorf("create secret: %v", err)
}
return cleanup, nil
}