From abe6c5e11dec9e574c3b4dc7b99102b71f85fcf5 Mon Sep 17 00:00:00 2001 From: Andre Baptista Aguas Date: Sat, 8 Jun 2024 21:48:13 +0200 Subject: [PATCH] terratest roundrobin --- terratest/examples/roundrobin2_ref_gslb.yaml | 12 +++++++ .../examples/roundrobin2_ref_ingress.yaml | 19 +++++++++++ .../k8gb_abstract_full_roundrobin_test.go | 15 +++++---- terratest/test/k8gb_full_roundrobin_test.go | 6 +++- terratest/utils/extensions.go | 33 +++++++++++++------ 5 files changed, 67 insertions(+), 18 deletions(-) create mode 100644 terratest/examples/roundrobin2_ref_gslb.yaml create mode 100644 terratest/examples/roundrobin2_ref_ingress.yaml diff --git a/terratest/examples/roundrobin2_ref_gslb.yaml b/terratest/examples/roundrobin2_ref_gslb.yaml new file mode 100644 index 0000000000..84e0794c81 --- /dev/null +++ b/terratest/examples/roundrobin2_ref_gslb.yaml @@ -0,0 +1,12 @@ +apiVersion: k8gb.absa.oss/v1beta1 +kind: Gslb +metadata: + name: roundrobin-test-gslb +spec: + resourceRef: + ingress: + matchLabels: + app: roundrobin-test-gslb + strategy: + type: roundRobin # Use a round robin load balancing strategy, when deciding which downstream clusters to route clients too + dnsTtlSeconds: 5 diff --git a/terratest/examples/roundrobin2_ref_ingress.yaml b/terratest/examples/roundrobin2_ref_ingress.yaml new file mode 100644 index 0000000000..209ef8e28d --- /dev/null +++ b/terratest/examples/roundrobin2_ref_ingress.yaml @@ -0,0 +1,19 @@ +apiVersion: networking.k8s.io/v1 +kind: Ingress +metadata: + name: roundrobin-test-gslb + labels: + app: roundrobin-test-gslb +spec: + ingressClassName: nginx + rules: + - host: roundrobin-test.cloud.example.com + http: + paths: + - path: / + pathType: Prefix + backend: + service: + name: frontend-podinfo # Gslb should reflect Healthy status and create associated DNS records + port: + name: http diff --git a/terratest/test/k8gb_abstract_full_roundrobin_test.go b/terratest/test/k8gb_abstract_full_roundrobin_test.go index 81d3cfd92d..b6249abb14 100644 --- a/terratest/test/k8gb_abstract_full_roundrobin_test.go +++ b/terratest/test/k8gb_abstract_full_roundrobin_test.go @@ -26,7 +26,7 @@ import ( "github.com/stretchr/testify/require" ) -func abstractTestFullRoundRobin(t *testing.T, n int) { +func abstractTestFullRoundRobin(t *testing.T, n int, host string, gslbPath string, ingressPath string) { if n < 2 || n > 8 { t.Logf("Use value of n that represents the number of clusters from interval [2,8]") t.FailNow() @@ -35,15 +35,16 @@ func abstractTestFullRoundRobin(t *testing.T, n int) { tags := []string{"eu", "us", "cz", "af", "ru", "ap", "uk", "ca"} var instances []*utils.Instance - const host = "roundrobin-test.cloud.example.com" - const gslbPath = "../examples/roundrobin2.yaml" - // start all the test apps on all the clusters for i := 0; i < n; i += 1 { - instance, er := utils.NewWorkflow(t, fmt.Sprintf("k3d-test-gslb%d", i+1), 5053+i). + workflow := utils.NewWorkflow(t, fmt.Sprintf("k3d-test-gslb%d", i+1), 5053+i). WithGslb(gslbPath, host). - WithTestApp(tags[i]). - Start() + WithTestApp(tags[i]) + if ingressPath != "" { + workflow = workflow.WithIngress(ingressPath) + } + + instance, er := workflow.Start() require.NoError(t, er) instances = append(instances, instance) defer instance.Kill() diff --git a/terratest/test/k8gb_full_roundrobin_test.go b/terratest/test/k8gb_full_roundrobin_test.go index 317d2a634c..c92f9b97eb 100644 --- a/terratest/test/k8gb_full_roundrobin_test.go +++ b/terratest/test/k8gb_full_roundrobin_test.go @@ -26,5 +26,9 @@ import ( ) func TestFullRoundRobin(t *testing.T) { - abstractTestFullRoundRobin(t, settings.ClustersNumber) + host = "roundrobin-test.cloud.example.com" + abstractTestFullRoundRobin(t, settings.ClustersNumber, host, + "../examples/roundrobin2.yaml", "") + abstractTestFullRoundRobin(t, settings.ClustersNumber, host, + "../examples/roundrobin2_ref_gslb.yaml", "../examples/roundrobin2_ref_ingress.yaml") } diff --git a/terratest/utils/extensions.go b/terratest/utils/extensions.go index 629d7a5166..18e3ea8400 100644 --- a/terratest/utils/extensions.go +++ b/terratest/utils/extensions.go @@ -125,7 +125,15 @@ func (w *Workflow) WithIngress(path string) *Workflow { if path == "" { w.error = fmt.Errorf("empty ingress resource path") } - w.settings.ingressResourcePath = path + var err error + w.settings.ingressResourcePath, err = filepath.Abs(path) + if err != nil { + w.error = fmt.Errorf("reading %s; %s", path, err) + } + w.settings.ingressName, err = w.getManifestName(w.settings.ingressResourcePath) + if err != nil { + w.error = err + } return w } @@ -148,9 +156,6 @@ func (w *Workflow) WithGslb(path, host string) *Workflow { w.error = err } w.state.gslb.host = host - if err != nil { - w.error = err - } return w } @@ -211,12 +216,20 @@ func (w *Workflow) Start() (*Instance, error) { // gslb if w.settings.gslbResourcePath != "" { - w.t.Logf("Create ingress %s from %s", w.state.gslb.name, w.settings.gslbResourcePath) - k8s.KubectlApply(w.t, w.k8sOptions, w.settings.gslbResourcePath) - k8s.WaitUntilIngressAvailable(w.t, w.k8sOptions, w.state.gslb.name, 100, 1*time.Second) - ingress := k8s.GetIngress(w.t, w.k8sOptions, w.state.gslb.name) - require.Equal(w.t, ingress.Name, w.state.gslb.name) - w.settings.ingressName = w.state.gslb.name + if w.settings.ingressResourcePath == "" { + w.t.Logf("Create ingress %s from %s", w.state.gslb.name, w.settings.gslbResourcePath) + k8s.KubectlApply(w.t, w.k8sOptions, w.settings.gslbResourcePath) + k8s.WaitUntilIngressAvailable(w.t, w.k8sOptions, w.state.gslb.name, 100, 1*time.Second) + ingress := k8s.GetIngress(w.t, w.k8sOptions, w.state.gslb.name) + require.Equal(w.t, ingress.Name, w.state.gslb.name) + w.settings.ingressName = w.state.gslb.name + } else { + w.t.Logf("Create gslb %s from %s", w.state.gslb.name, w.settings.gslbResourcePath) + k8s.KubectlApply(w.t, w.k8sOptions, w.settings.gslbResourcePath) + w.t.Logf("Create ingress %s from %s", w.settings.ingressName, w.settings.ingressResourcePath) + k8s.KubectlApply(w.t, w.k8sOptions, w.settings.ingressResourcePath) + k8s.WaitUntilIngressAvailable(w.t, w.k8sOptions, w.settings.ingressName, 100, 1*time.Second) + } } return &Instance{