Skip to content

Commit

Permalink
propagate nginx external addresses to rpaasinstance
Browse files Browse the repository at this point in the history
  • Loading branch information
wpjunior committed Jan 18, 2024
1 parent 25b9074 commit 539a0f9
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 24 deletions.
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 @@ -6263,6 +6269,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
39 changes: 26 additions & 13 deletions controllers/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,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 +142,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 +167,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 +183,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 +206,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 +219,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 +597,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 +789,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 +813,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 +881,22 @@ 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) {
ingressesStatus := v1alpha1.RpaasInstanceExternalAddressesStatus{}

for _, service := range nginx.Status.Services {
ingressesStatus.IPs = append(ingressesStatus.IPs, service.Hostnames...)
ingressesStatus.Hostnames = append(ingressesStatus.Hostnames, service.Hostnames...)
}

for _, ingress := range nginx.Status.Ingresses {
ingressesStatus.IPs = append(ingressesStatus.IPs, ingress.Hostnames...)
ingressesStatus.Hostnames = append(ingressesStatus.Hostnames, ingress.Hostnames...)
}

return ingressesStatus, nil
}

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
16 changes: 10 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 @@ -182,11 +180,17 @@ func (r *RpaasInstanceReconciler) refreshStatus(ctx context.Context, instance *e
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,
}

if existingNginx != nil {
Expand All @@ -209,7 +213,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

0 comments on commit 539a0f9

Please sign in to comment.