From 996505450c6fbedbfab57ec6683d778227956c51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wilson=20J=C3=BAnior?= Date: Thu, 18 Jan 2024 16:31:23 -0300 Subject: [PATCH] Add tests --- .github/workflows/ci.yaml | 6 ++-- Dockerfile.api | 2 +- Dockerfile.operator | 2 +- Dockerfile.purger | 2 +- controllers/controller.go | 12 +++++--- controllers/controller_test.go | 41 +++++++++++++++++++++++++ controllers/rpaasinstance_controller.go | 7 +---- 7 files changed, 56 insertions(+), 16 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index b119bacfb..9f4005622 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -9,7 +9,7 @@ jobs: - uses: actions/checkout@v2 - uses: actions/setup-go@v3 with: - go-version: "1.20" + go-version: "1.21" - uses: golangci/golangci-lint-action@v3 with: version: v1.52.2 @@ -34,7 +34,7 @@ jobs: version: v3.7.0 - uses: actions/setup-go@v3 with: - go-version: "1.20" + go-version: "1.21" - uses: actions/cache@v2 with: path: | @@ -194,7 +194,7 @@ jobs: EOF - uses: actions/setup-go@v3 with: - go-version: "1.20" + go-version: "1.21" - uses: goreleaser/goreleaser-action@v4 with: version: latest diff --git a/Dockerfile.api b/Dockerfile.api index 488eb80c0..faed0df13 100644 --- a/Dockerfile.api +++ b/Dockerfile.api @@ -1,4 +1,4 @@ -FROM golang:1.20-alpine3.18 AS builder +FROM golang:1.21-alpine3.18 AS builder COPY . /go/src/github.com/tsuru/rpaas-operator WORKDIR /go/src/github.com/tsuru/rpaas-operator RUN set -x \ diff --git a/Dockerfile.operator b/Dockerfile.operator index 5526c5160..64f991117 100644 --- a/Dockerfile.operator +++ b/Dockerfile.operator @@ -1,4 +1,4 @@ -FROM golang:1.20-alpine3.18 AS builder +FROM golang:1.21-alpine3.18 AS builder COPY . /go/src/github.com/tsuru/rpaas-operator WORKDIR /go/src/github.com/tsuru/rpaas-operator RUN apk add --update gcc git make musl-dev && \ diff --git a/Dockerfile.purger b/Dockerfile.purger index c983c638c..078e379de 100644 --- a/Dockerfile.purger +++ b/Dockerfile.purger @@ -1,4 +1,4 @@ -FROM golang:1.20-alpine3.18 AS builder +FROM golang:1.21-alpine3.18 AS builder COPY . /go/src/github.com/tsuru/rpaas-operator WORKDIR /go/src/github.com/tsuru/rpaas-operator RUN apk add --update gcc git make musl-dev && \ diff --git a/controllers/controller.go b/controllers/controller.go index 198867ac7..cc9f50cf8 100644 --- a/controllers/controller.go +++ b/controllers/controller.go @@ -14,6 +14,7 @@ import ( "errors" "fmt" "reflect" + "slices" "sort" "strconv" "strings" @@ -881,20 +882,23 @@ func (r *RpaasInstanceReconciler) getNginx(ctx context.Context, instance *v1alph return found, err } -func (r *RpaasInstanceReconciler) getNginxExternalAddressses(ctx context.Context, nginx *nginxv1alpha1.Nginx) (v1alpha1.RpaasInstanceExternalAddressesStatus, error) { +func externalAddresssesFromNginx(nginx *nginxv1alpha1.Nginx) v1alpha1.RpaasInstanceExternalAddressesStatus { ingressesStatus := v1alpha1.RpaasInstanceExternalAddressesStatus{} for _, service := range nginx.Status.Services { - ingressesStatus.IPs = append(ingressesStatus.IPs, service.Hostnames...) + ingressesStatus.IPs = append(ingressesStatus.IPs, service.IPs...) ingressesStatus.Hostnames = append(ingressesStatus.Hostnames, service.Hostnames...) } for _, ingress := range nginx.Status.Ingresses { - ingressesStatus.IPs = append(ingressesStatus.IPs, ingress.Hostnames...) + ingressesStatus.IPs = append(ingressesStatus.IPs, ingress.IPs...) ingressesStatus.Hostnames = append(ingressesStatus.Hostnames, ingress.Hostnames...) } - return ingressesStatus, nil + slices.Sort(ingressesStatus.IPs) + slices.Sort(ingressesStatus.Hostnames) + + return ingressesStatus } func (r *RpaasInstanceReconciler) reconcileNginx(ctx context.Context, instance *v1alpha1.RpaasInstance, nginx *nginxv1alpha1.Nginx) error { diff --git a/controllers/controller_test.go b/controllers/controller_test.go index c2f045d83..dc53d5607 100644 --- a/controllers/controller_test.go +++ b/controllers/controller_test.go @@ -2122,6 +2122,47 @@ func TestRpaasInstanceController_Reconcile_Suspended(t *testing.T) { assert.Equal(t, "Warning RpaasInstanceSuspended no modifications will be done by RPaaS controller", <-fr.Events) } +func TestExternalAddresssesFromNginx(t *testing.T) { + externalAddresses := externalAddresssesFromNginx(&nginxv1alpha1.Nginx{ + Status: nginxv1alpha1.NginxStatus{ + Ingresses: []nginxv1alpha1.IngressStatus{ + { + Name: "ing1", + IPs: []string{"1.1.1.3", "1.1.1.1"}, + }, + { + Name: "ing2", + IPs: []string{"1.1.1.2", "1.1.1.4"}, + }, + { + Name: "ing3", + Hostnames: []string{"host2", "host1"}, + }, + }, + Services: []nginxv1alpha1.ServiceStatus{ + { + Name: "svc", + IPs: []string{"8.1.1.3", "8.1.1.1"}, + }, + { + Name: "svc2", + IPs: []string{"8.1.1.2", "8.1.1.4"}, + }, + { + Name: "svc3", + Hostnames: []string{"host9", "host8"}, + }, + }, + }, + }) + + assert.Equal(t, v1alpha1.RpaasInstanceExternalAddressesStatus{ + IPs: []string{"1.1.1.1", "1.1.1.2", "1.1.1.3", "1.1.1.4", "8.1.1.1", "8.1.1.2", "8.1.1.3", "8.1.1.4"}, + Hostnames: []string{"host1", "host2", "host8", "host9"}, + }, externalAddresses) + +} + func newRpaasInstanceReconciler(objs ...runtime.Object) *RpaasInstanceReconciler { return &RpaasInstanceReconciler{ Client: fake.NewClientBuilder().WithScheme(extensionsruntime.NewScheme()).WithRuntimeObjects(objs...).Build(), diff --git a/controllers/rpaasinstance_controller.go b/controllers/rpaasinstance_controller.go index 6507c9626..ee0eb785a 100644 --- a/controllers/rpaasinstance_controller.go +++ b/controllers/rpaasinstance_controller.go @@ -180,17 +180,12 @@ func (r *RpaasInstanceReconciler) refreshStatus(ctx context.Context, instance *v return err } - externalAddresses, err := r.getNginxExternalAddressses(ctx, existingNginx) - if err != nil { - return err - } - newStatus := v1alpha1.RpaasInstanceStatus{ ObservedGeneration: instance.Generation, WantedNginxRevisionHash: newHash, ObservedNginxRevisionHash: existingHash, NginxUpdated: newHash == existingHash, - ExternalAddresses: externalAddresses, + ExternalAddresses: externalAddresssesFromNginx(existingNginx), } if existingNginx != nil {