Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

propagate nginx external addresses to rpaasinstance resource #141

Merged
merged 2 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ 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
version: v1.55
- run: make test

integration:
Expand All @@ -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: |
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile.api
Original file line number Diff line number Diff line change
@@ -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 \
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile.operator
Original file line number Diff line number Diff line change
@@ -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 && \
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile.purger
Original file line number Diff line number Diff line change
@@ -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 && \
Expand Down
10 changes: 10 additions & 0 deletions api/v1alpha1/rpaasinstance_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,14 @@ type RpaasInstanceStatus struct {
// NginxUpdated is true if the wanted nginx revision hash equals the
// observed nginx revision hash.
NginxUpdated bool `json:"nginxUpdated"`

// External IP addreses of Nginx
ExternalAddresses RpaasInstanceExternalAddressesStatus `json:"externalAddresses,omitempty"`
}

type RpaasInstanceExternalAddressesStatus struct {
IPs []string `json:"ips,omitempty"`
Hostnames []string `json:"hostnames,omitempty"`
}

// +kubebuilder:object:root=true
Expand All @@ -366,6 +374,8 @@ type RpaasInstanceStatus struct {
// +kubebuilder:subresource:scale:specpath=.spec.replicas,statuspath=.status.currentReplicas,selectorpath=.status.podSelector
// +kubebuilder:printcolumn:name="Suspended",type=boolean,JSONPath=`.spec.suspend`
// +kubebuilder:printcolumn:name="Age",type="date",JSONPath=".metadata.creationTimestamp"
// +kubebuilder:printcolumn:name="IPs",type=string,JSONPath=`.status.externalAddresses.ips[*]`
// +kubebuilder:printcolumn:name="Hostnames",type=string,JSONPath=`.status.externalAddresses.hostnames[*]`

// RpaasInstance is the Schema for the rpaasinstances API
type RpaasInstance struct {
Expand Down
18 changes: 18 additions & 0 deletions config/crd/bases/extensions.tsuru.io_rpaasinstances.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ spec:
- jsonPath: .metadata.creationTimestamp
name: Age
type: date
- jsonPath: .status.externalAddresses.ips[*]
name: IPs
type: string
- jsonPath: .status.externalAddresses.hostnames[*]
name: Hostnames
type: string
name: v1alpha1
schema:
openAPIV3Schema:
Expand Down Expand Up @@ -6267,6 +6273,18 @@ spec:
description: CurrentReplicas is the last observed number of pods.
format: int32
type: integer
externalAddresses:
description: External IP addreses of Nginx
properties:
hostnames:
items:
type: string
type: array
ips:
items:
type: string
type: array
type: object
nginxUpdated:
description: NginxUpdated is true if the wanted nginx revision hash
equals the observed nginx revision hash.
Expand Down
43 changes: 30 additions & 13 deletions controllers/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"errors"
"fmt"
"reflect"
"slices"
"sort"
"strconv"
"strings"
Expand All @@ -31,18 +32,15 @@ import (
policyv1 "k8s.io/api/policy/v1"
"k8s.io/apimachinery/pkg/api/equality"
k8sErrors "k8s.io/apimachinery/pkg/api/errors"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
k8slabels "k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/intstr"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/tsuru/rpaas-operator/api/v1alpha1"
extensionsv1alpha1 "github.com/tsuru/rpaas-operator/api/v1alpha1"
controllerUtil "github.com/tsuru/rpaas-operator/internal/controllers/util"
"github.com/tsuru/rpaas-operator/internal/pkg/rpaas/nginx"
"github.com/tsuru/rpaas-operator/pkg/util"
Expand Down Expand Up @@ -145,16 +143,16 @@ main $@
`
)

func (r *RpaasInstanceReconciler) getRpaasInstance(ctx context.Context, objKey types.NamespacedName) (*extensionsv1alpha1.RpaasInstance, error) {
var instance extensionsv1alpha1.RpaasInstance
func (r *RpaasInstanceReconciler) getRpaasInstance(ctx context.Context, objKey types.NamespacedName) (*v1alpha1.RpaasInstance, error) {
var instance v1alpha1.RpaasInstance
if err := r.Client.Get(ctx, objKey, &instance); err != nil {
return nil, err
}

return &instance, nil
}

func (r *RpaasInstanceReconciler) mergeWithFlavors(ctx context.Context, instance *v1alpha1.RpaasInstance) (*extensionsv1alpha1.RpaasInstance, error) {
func (r *RpaasInstanceReconciler) mergeWithFlavors(ctx context.Context, instance *v1alpha1.RpaasInstance) (*v1alpha1.RpaasInstance, error) {
mergedInstance, err := r.mergeInstanceWithFlavors(ctx, instance)
if err != nil {
return nil, err
Expand All @@ -170,7 +168,7 @@ func (r *RpaasInstanceReconciler) mergeWithFlavors(ctx context.Context, instance
return mergedInstance, nil
}

func (r *RpaasInstanceReconciler) mergeInstanceWithFlavors(ctx context.Context, instance *extensionsv1alpha1.RpaasInstance) (*extensionsv1alpha1.RpaasInstance, error) {
func (r *RpaasInstanceReconciler) mergeInstanceWithFlavors(ctx context.Context, instance *v1alpha1.RpaasInstance) (*v1alpha1.RpaasInstance, error) {
defaultFlavors, err := r.listDefaultFlavors(ctx, instance)
if err != nil {
return nil, err
Expand All @@ -186,7 +184,7 @@ func (r *RpaasInstanceReconciler) mergeInstanceWithFlavors(ctx context.Context,
flavorObjectKey.Namespace = instance.Spec.PlanNamespace
}

var flavor extensionsv1alpha1.RpaasFlavor
var flavor v1alpha1.RpaasFlavor
if err := r.Client.Get(ctx, flavorObjectKey, &flavor); err != nil {
return nil, err
}
Expand All @@ -209,7 +207,7 @@ func (r *RpaasInstanceReconciler) mergeInstanceWithFlavors(ctx context.Context,
return instance, nil
}

func mergeInstanceWithFlavor(instance *extensionsv1alpha1.RpaasInstance, flavor extensionsv1alpha1.RpaasFlavor) error {
func mergeInstanceWithFlavor(instance *v1alpha1.RpaasInstance, flavor v1alpha1.RpaasFlavor) error {
if flavor.Spec.InstanceTemplate == nil {
return nil
}
Expand All @@ -222,7 +220,7 @@ func mergeInstanceWithFlavor(instance *extensionsv1alpha1.RpaasInstance, flavor
return nil
}

func (r *RpaasInstanceReconciler) listDefaultFlavors(ctx context.Context, instance *extensionsv1alpha1.RpaasInstance) ([]extensionsv1alpha1.RpaasFlavor, error) {
func (r *RpaasInstanceReconciler) listDefaultFlavors(ctx context.Context, instance *v1alpha1.RpaasInstance) ([]v1alpha1.RpaasFlavor, error) {
flavorList := &v1alpha1.RpaasFlavorList{}
flavorNamespace := instance.Namespace
if instance.Spec.PlanNamespace != "" {
Expand Down Expand Up @@ -600,7 +598,7 @@ func (r *RpaasInstanceReconciler) reconcileHPA(ctx context.Context, instance *v1
func (r *RpaasInstanceReconciler) cleanUpKEDAScaledObject(ctx context.Context, instance *v1alpha1.RpaasInstance) error {
var so kedav1alpha1.ScaledObject
err := r.Client.Get(ctx, types.NamespacedName{Name: instance.Name, Namespace: instance.Namespace}, &so)
if k8serrors.IsNotFound(err) {
if k8sErrors.IsNotFound(err) {
return nil
}

Expand Down Expand Up @@ -792,7 +790,7 @@ func (r *RpaasInstanceReconciler) reconcilePDB(ctx context.Context, instance *v1
var existingPDB policyv1.PodDisruptionBudget
err = r.Get(ctx, client.ObjectKey{Name: pdb.Name, Namespace: pdb.Namespace}, &existingPDB)
if err != nil {
if !k8serrors.IsNotFound(err) {
if !k8sErrors.IsNotFound(err) {
return err
}

Expand All @@ -816,7 +814,7 @@ func (r *RpaasInstanceReconciler) reconcilePDB(ctx context.Context, instance *v1
}

func newPDB(instance *v1alpha1.RpaasInstance, nginx *nginxv1alpha1.Nginx) (*policyv1.PodDisruptionBudget, error) {
set, err := labels.ConvertSelectorToLabelsMap(nginx.Status.PodSelector)
set, err := k8slabels.ConvertSelectorToLabelsMap(nginx.Status.PodSelector)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -884,6 +882,25 @@ func (r *RpaasInstanceReconciler) getNginx(ctx context.Context, instance *v1alph
return found, err
}

func externalAddresssesFromNginx(nginx *nginxv1alpha1.Nginx) v1alpha1.RpaasInstanceExternalAddressesStatus {
ingressesStatus := v1alpha1.RpaasInstanceExternalAddressesStatus{}

for _, service := range nginx.Status.Services {
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.IPs...)
ingressesStatus.Hostnames = append(ingressesStatus.Hostnames, ingress.Hostnames...)
}

slices.Sort(ingressesStatus.IPs)
slices.Sort(ingressesStatus.Hostnames)

return ingressesStatus
}

func (r *RpaasInstanceReconciler) reconcileNginx(ctx context.Context, instance *v1alpha1.RpaasInstance, nginx *nginxv1alpha1.Nginx) error {
found, err := r.getNginx(ctx, instance)
if err != nil {
Expand Down
41 changes: 41 additions & 0 deletions controllers/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
11 changes: 5 additions & 6 deletions controllers/rpaasinstance_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,13 @@ import (
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
k8sErrors "k8s.io/apimachinery/pkg/api/errors"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/record"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

"github.com/tsuru/rpaas-operator/api/v1alpha1"
extensionsv1alpha1 "github.com/tsuru/rpaas-operator/api/v1alpha1"
"github.com/tsuru/rpaas-operator/internal/controllers/certificates"
"github.com/tsuru/rpaas-operator/internal/pkg/rpaas/nginx"
)
Expand Down Expand Up @@ -56,7 +54,7 @@ type RpaasInstanceReconciler struct {

func (r *RpaasInstanceReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
instance, err := r.getRpaasInstance(ctx, req.NamespacedName)
if k8serrors.IsNotFound(err) {
if k8sErrors.IsNotFound(err) {
return reconcile.Result{}, nil
}

Expand All @@ -77,7 +75,7 @@ func (r *RpaasInstanceReconciler) Reconcile(ctx context.Context, req ctrl.Reques
planName.Namespace = instance.Spec.PlanNamespace
}

plan := &extensionsv1alpha1.RpaasPlan{}
plan := &v1alpha1.RpaasPlan{}
err = r.Client.Get(ctx, planName, plan)
if err != nil {
return reconcile.Result{}, err
Expand Down Expand Up @@ -167,7 +165,7 @@ func (r *RpaasInstanceReconciler) Reconcile(ctx context.Context, req ctrl.Reques
return ctrl.Result{}, nil
}

func (r *RpaasInstanceReconciler) refreshStatus(ctx context.Context, instance *extensionsv1alpha1.RpaasInstance, newNginx *nginxv1alpha1.Nginx) error {
func (r *RpaasInstanceReconciler) refreshStatus(ctx context.Context, instance *v1alpha1.RpaasInstance, newNginx *nginxv1alpha1.Nginx) error {
existingNginx, err := r.getNginx(ctx, instance)
if err != nil && !k8sErrors.IsNotFound(err) {
return err
Expand All @@ -187,6 +185,7 @@ func (r *RpaasInstanceReconciler) refreshStatus(ctx context.Context, instance *e
WantedNginxRevisionHash: newHash,
ObservedNginxRevisionHash: existingHash,
NginxUpdated: newHash == existingHash,
ExternalAddresses: externalAddresssesFromNginx(existingNginx),
}

if existingNginx != nil {
Expand All @@ -209,7 +208,7 @@ func (r *RpaasInstanceReconciler) refreshStatus(ctx context.Context, instance *e

func (r *RpaasInstanceReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&extensionsv1alpha1.RpaasInstance{}).
For(&v1alpha1.RpaasInstance{}).
Owns(&corev1.ConfigMap{}).
Owns(&corev1.Secret{}).
Owns(&batchv1.CronJob{}).
Expand Down
8 changes: 5 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module github.com/tsuru/rpaas-operator

go 1.20
go 1.21

toolchain go1.21.0

require (
github.com/Masterminds/sprig/v3 v3.2.2
Expand All @@ -18,6 +20,7 @@ require (
github.com/imdario/mergo v0.3.13
github.com/kedacore/keda/v2 v2.10.1
github.com/labstack/echo/v4 v4.10.2
github.com/lnquy/cron v1.1.1
github.com/mitchellh/mapstructure v1.5.0
github.com/olekukonko/tablewriter v0.0.5
github.com/opentracing-contrib/go-stdlib v1.0.1-0.20201028152118-adbfc141dfc2
Expand All @@ -30,7 +33,7 @@ require (
github.com/stern/stern v1.20.1
github.com/stretchr/testify v1.8.4
github.com/tsuru/go-tsuruclient v0.0.0-20230724221758-523def58dd2d
github.com/tsuru/nginx-operator v0.13.2
github.com/tsuru/nginx-operator v0.14.0
github.com/uber/jaeger-client-go v2.25.0+incompatible
github.com/urfave/cli/v2 v2.3.0
golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e
Expand Down Expand Up @@ -93,7 +96,6 @@ require (
github.com/json-iterator/go v1.1.12 // indirect
github.com/labstack/gommon v0.4.0 // indirect
github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de // indirect
github.com/lnquy/cron v1.1.1 // indirect
github.com/magiconair/properties v1.8.5 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
Expand Down
Loading
Loading