Skip to content

Commit f004a4f

Browse files
committed
propagate nginx external addresses to rpaasinstance
1 parent bf66cc8 commit f004a4f

File tree

6 files changed

+83
-24
lines changed

6 files changed

+83
-24
lines changed

Diff for: api/v1alpha1/rpaasinstance_types.go

+10
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,14 @@ type RpaasInstanceStatus struct {
358358
// NginxUpdated is true if the wanted nginx revision hash equals the
359359
// observed nginx revision hash.
360360
NginxUpdated bool `json:"nginxUpdated"`
361+
362+
// External IP addreses of Nginx
363+
ExternalAddresses RpaasInstanceExternalAddressesStatus `json:"externalAddresses,omitempty"`
364+
}
365+
366+
type RpaasInstanceExternalAddressesStatus struct {
367+
IPs []string `json:"ips,omitempty"`
368+
Hostnames []string `json:"hostnames,omitempty"`
361369
}
362370

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

370380
// RpaasInstance is the Schema for the rpaasinstances API
371381
type RpaasInstance struct {

Diff for: config/crd/bases/extensions.tsuru.io_rpaasinstances.yaml

+18
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ spec:
2525
- jsonPath: .metadata.creationTimestamp
2626
name: Age
2727
type: date
28+
- jsonPath: .status.externalAddresses.ips[*]
29+
name: IPs
30+
type: string
31+
- jsonPath: .status.externalAddresses.hostnames[*]
32+
name: Hostnames
33+
type: string
2834
name: v1alpha1
2935
schema:
3036
openAPIV3Schema:
@@ -6267,6 +6273,18 @@ spec:
62676273
description: CurrentReplicas is the last observed number of pods.
62686274
format: int32
62696275
type: integer
6276+
externalAddresses:
6277+
description: External IP addreses of Nginx
6278+
properties:
6279+
hostnames:
6280+
items:
6281+
type: string
6282+
type: array
6283+
ips:
6284+
items:
6285+
type: string
6286+
type: array
6287+
type: object
62706288
nginxUpdated:
62716289
description: NginxUpdated is true if the wanted nginx revision hash
62726290
equals the observed nginx revision hash.

Diff for: controllers/controller.go

+26-13
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,15 @@ import (
3131
policyv1 "k8s.io/api/policy/v1"
3232
"k8s.io/apimachinery/pkg/api/equality"
3333
k8sErrors "k8s.io/apimachinery/pkg/api/errors"
34-
k8serrors "k8s.io/apimachinery/pkg/api/errors"
3534
"k8s.io/apimachinery/pkg/api/resource"
3635
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
37-
"k8s.io/apimachinery/pkg/labels"
3836
k8slabels "k8s.io/apimachinery/pkg/labels"
3937
"k8s.io/apimachinery/pkg/runtime/schema"
4038
"k8s.io/apimachinery/pkg/types"
4139
"k8s.io/apimachinery/pkg/util/intstr"
4240
"sigs.k8s.io/controller-runtime/pkg/client"
4341

4442
"github.com/tsuru/rpaas-operator/api/v1alpha1"
45-
extensionsv1alpha1 "github.com/tsuru/rpaas-operator/api/v1alpha1"
4643
controllerUtil "github.com/tsuru/rpaas-operator/internal/controllers/util"
4744
"github.com/tsuru/rpaas-operator/internal/pkg/rpaas/nginx"
4845
"github.com/tsuru/rpaas-operator/pkg/util"
@@ -145,16 +142,16 @@ main $@
145142
`
146143
)
147144

148-
func (r *RpaasInstanceReconciler) getRpaasInstance(ctx context.Context, objKey types.NamespacedName) (*extensionsv1alpha1.RpaasInstance, error) {
149-
var instance extensionsv1alpha1.RpaasInstance
145+
func (r *RpaasInstanceReconciler) getRpaasInstance(ctx context.Context, objKey types.NamespacedName) (*v1alpha1.RpaasInstance, error) {
146+
var instance v1alpha1.RpaasInstance
150147
if err := r.Client.Get(ctx, objKey, &instance); err != nil {
151148
return nil, err
152149
}
153150

154151
return &instance, nil
155152
}
156153

157-
func (r *RpaasInstanceReconciler) mergeWithFlavors(ctx context.Context, instance *v1alpha1.RpaasInstance) (*extensionsv1alpha1.RpaasInstance, error) {
154+
func (r *RpaasInstanceReconciler) mergeWithFlavors(ctx context.Context, instance *v1alpha1.RpaasInstance) (*v1alpha1.RpaasInstance, error) {
158155
mergedInstance, err := r.mergeInstanceWithFlavors(ctx, instance)
159156
if err != nil {
160157
return nil, err
@@ -170,7 +167,7 @@ func (r *RpaasInstanceReconciler) mergeWithFlavors(ctx context.Context, instance
170167
return mergedInstance, nil
171168
}
172169

173-
func (r *RpaasInstanceReconciler) mergeInstanceWithFlavors(ctx context.Context, instance *extensionsv1alpha1.RpaasInstance) (*extensionsv1alpha1.RpaasInstance, error) {
170+
func (r *RpaasInstanceReconciler) mergeInstanceWithFlavors(ctx context.Context, instance *v1alpha1.RpaasInstance) (*v1alpha1.RpaasInstance, error) {
174171
defaultFlavors, err := r.listDefaultFlavors(ctx, instance)
175172
if err != nil {
176173
return nil, err
@@ -186,7 +183,7 @@ func (r *RpaasInstanceReconciler) mergeInstanceWithFlavors(ctx context.Context,
186183
flavorObjectKey.Namespace = instance.Spec.PlanNamespace
187184
}
188185

189-
var flavor extensionsv1alpha1.RpaasFlavor
186+
var flavor v1alpha1.RpaasFlavor
190187
if err := r.Client.Get(ctx, flavorObjectKey, &flavor); err != nil {
191188
return nil, err
192189
}
@@ -209,7 +206,7 @@ func (r *RpaasInstanceReconciler) mergeInstanceWithFlavors(ctx context.Context,
209206
return instance, nil
210207
}
211208

212-
func mergeInstanceWithFlavor(instance *extensionsv1alpha1.RpaasInstance, flavor extensionsv1alpha1.RpaasFlavor) error {
209+
func mergeInstanceWithFlavor(instance *v1alpha1.RpaasInstance, flavor v1alpha1.RpaasFlavor) error {
213210
if flavor.Spec.InstanceTemplate == nil {
214211
return nil
215212
}
@@ -222,7 +219,7 @@ func mergeInstanceWithFlavor(instance *extensionsv1alpha1.RpaasInstance, flavor
222219
return nil
223220
}
224221

225-
func (r *RpaasInstanceReconciler) listDefaultFlavors(ctx context.Context, instance *extensionsv1alpha1.RpaasInstance) ([]extensionsv1alpha1.RpaasFlavor, error) {
222+
func (r *RpaasInstanceReconciler) listDefaultFlavors(ctx context.Context, instance *v1alpha1.RpaasInstance) ([]v1alpha1.RpaasFlavor, error) {
226223
flavorList := &v1alpha1.RpaasFlavorList{}
227224
flavorNamespace := instance.Namespace
228225
if instance.Spec.PlanNamespace != "" {
@@ -600,7 +597,7 @@ func (r *RpaasInstanceReconciler) reconcileHPA(ctx context.Context, instance *v1
600597
func (r *RpaasInstanceReconciler) cleanUpKEDAScaledObject(ctx context.Context, instance *v1alpha1.RpaasInstance) error {
601598
var so kedav1alpha1.ScaledObject
602599
err := r.Client.Get(ctx, types.NamespacedName{Name: instance.Name, Namespace: instance.Namespace}, &so)
603-
if k8serrors.IsNotFound(err) {
600+
if k8sErrors.IsNotFound(err) {
604601
return nil
605602
}
606603

@@ -792,7 +789,7 @@ func (r *RpaasInstanceReconciler) reconcilePDB(ctx context.Context, instance *v1
792789
var existingPDB policyv1.PodDisruptionBudget
793790
err = r.Get(ctx, client.ObjectKey{Name: pdb.Name, Namespace: pdb.Namespace}, &existingPDB)
794791
if err != nil {
795-
if !k8serrors.IsNotFound(err) {
792+
if !k8sErrors.IsNotFound(err) {
796793
return err
797794
}
798795

@@ -816,7 +813,7 @@ func (r *RpaasInstanceReconciler) reconcilePDB(ctx context.Context, instance *v1
816813
}
817814

818815
func newPDB(instance *v1alpha1.RpaasInstance, nginx *nginxv1alpha1.Nginx) (*policyv1.PodDisruptionBudget, error) {
819-
set, err := labels.ConvertSelectorToLabelsMap(nginx.Status.PodSelector)
816+
set, err := k8slabels.ConvertSelectorToLabelsMap(nginx.Status.PodSelector)
820817
if err != nil {
821818
return nil, err
822819
}
@@ -884,6 +881,22 @@ func (r *RpaasInstanceReconciler) getNginx(ctx context.Context, instance *v1alph
884881
return found, err
885882
}
886883

884+
func (r *RpaasInstanceReconciler) getNginxExternalAddressses(ctx context.Context, nginx *nginxv1alpha1.Nginx) (v1alpha1.RpaasInstanceExternalAddressesStatus, error) {
885+
ingressesStatus := v1alpha1.RpaasInstanceExternalAddressesStatus{}
886+
887+
for _, service := range nginx.Status.Services {
888+
ingressesStatus.IPs = append(ingressesStatus.IPs, service.Hostnames...)
889+
ingressesStatus.Hostnames = append(ingressesStatus.Hostnames, service.Hostnames...)
890+
}
891+
892+
for _, ingress := range nginx.Status.Ingresses {
893+
ingressesStatus.IPs = append(ingressesStatus.IPs, ingress.Hostnames...)
894+
ingressesStatus.Hostnames = append(ingressesStatus.Hostnames, ingress.Hostnames...)
895+
}
896+
897+
return ingressesStatus, nil
898+
}
899+
887900
func (r *RpaasInstanceReconciler) reconcileNginx(ctx context.Context, instance *v1alpha1.RpaasInstance, nginx *nginxv1alpha1.Nginx) error {
888901
found, err := r.getNginx(ctx, instance)
889902
if err != nil {

Diff for: controllers/rpaasinstance_controller.go

+10-6
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,13 @@ import (
1515
batchv1 "k8s.io/api/batch/v1"
1616
corev1 "k8s.io/api/core/v1"
1717
k8sErrors "k8s.io/apimachinery/pkg/api/errors"
18-
k8serrors "k8s.io/apimachinery/pkg/api/errors"
1918
"k8s.io/apimachinery/pkg/types"
2019
"k8s.io/client-go/tools/record"
2120
ctrl "sigs.k8s.io/controller-runtime"
2221
"sigs.k8s.io/controller-runtime/pkg/client"
2322
"sigs.k8s.io/controller-runtime/pkg/reconcile"
2423

2524
"github.com/tsuru/rpaas-operator/api/v1alpha1"
26-
extensionsv1alpha1 "github.com/tsuru/rpaas-operator/api/v1alpha1"
2725
"github.com/tsuru/rpaas-operator/internal/controllers/certificates"
2826
"github.com/tsuru/rpaas-operator/internal/pkg/rpaas/nginx"
2927
)
@@ -56,7 +54,7 @@ type RpaasInstanceReconciler struct {
5654

5755
func (r *RpaasInstanceReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
5856
instance, err := r.getRpaasInstance(ctx, req.NamespacedName)
59-
if k8serrors.IsNotFound(err) {
57+
if k8sErrors.IsNotFound(err) {
6058
return reconcile.Result{}, nil
6159
}
6260

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

80-
plan := &extensionsv1alpha1.RpaasPlan{}
78+
plan := &v1alpha1.RpaasPlan{}
8179
err = r.Client.Get(ctx, planName, plan)
8280
if err != nil {
8381
return reconcile.Result{}, err
@@ -167,7 +165,7 @@ func (r *RpaasInstanceReconciler) Reconcile(ctx context.Context, req ctrl.Reques
167165
return ctrl.Result{}, nil
168166
}
169167

170-
func (r *RpaasInstanceReconciler) refreshStatus(ctx context.Context, instance *extensionsv1alpha1.RpaasInstance, newNginx *nginxv1alpha1.Nginx) error {
168+
func (r *RpaasInstanceReconciler) refreshStatus(ctx context.Context, instance *v1alpha1.RpaasInstance, newNginx *nginxv1alpha1.Nginx) error {
171169
existingNginx, err := r.getNginx(ctx, instance)
172170
if err != nil && !k8sErrors.IsNotFound(err) {
173171
return err
@@ -182,11 +180,17 @@ func (r *RpaasInstanceReconciler) refreshStatus(ctx context.Context, instance *e
182180
return err
183181
}
184182

183+
externalAddresses, err := r.getNginxExternalAddressses(ctx, existingNginx)
184+
if err != nil {
185+
return err
186+
}
187+
185188
newStatus := v1alpha1.RpaasInstanceStatus{
186189
ObservedGeneration: instance.Generation,
187190
WantedNginxRevisionHash: newHash,
188191
ObservedNginxRevisionHash: existingHash,
189192
NginxUpdated: newHash == existingHash,
193+
ExternalAddresses: externalAddresses,
190194
}
191195

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

210214
func (r *RpaasInstanceReconciler) SetupWithManager(mgr ctrl.Manager) error {
211215
return ctrl.NewControllerManagedBy(mgr).
212-
For(&extensionsv1alpha1.RpaasInstance{}).
216+
For(&v1alpha1.RpaasInstance{}).
213217
Owns(&corev1.ConfigMap{}).
214218
Owns(&corev1.Secret{}).
215219
Owns(&batchv1.CronJob{}).

Diff for: go.mod

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
module github.com/tsuru/rpaas-operator
22

3-
go 1.20
3+
go 1.21
4+
5+
toolchain go1.21.0
46

57
require (
68
github.com/Masterminds/sprig/v3 v3.2.2
@@ -18,6 +20,7 @@ require (
1820
github.com/imdario/mergo v0.3.13
1921
github.com/kedacore/keda/v2 v2.10.1
2022
github.com/labstack/echo/v4 v4.10.2
23+
github.com/lnquy/cron v1.1.1
2124
github.com/mitchellh/mapstructure v1.5.0
2225
github.com/olekukonko/tablewriter v0.0.5
2326
github.com/opentracing-contrib/go-stdlib v1.0.1-0.20201028152118-adbfc141dfc2
@@ -30,7 +33,7 @@ require (
3033
github.com/stern/stern v1.20.1
3134
github.com/stretchr/testify v1.8.4
3235
github.com/tsuru/go-tsuruclient v0.0.0-20230724221758-523def58dd2d
33-
github.com/tsuru/nginx-operator v0.13.2
36+
github.com/tsuru/nginx-operator v0.14.0
3437
github.com/uber/jaeger-client-go v2.25.0+incompatible
3538
github.com/urfave/cli/v2 v2.3.0
3639
golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e
@@ -93,7 +96,6 @@ require (
9396
github.com/json-iterator/go v1.1.12 // indirect
9497
github.com/labstack/gommon v0.4.0 // indirect
9598
github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de // indirect
96-
github.com/lnquy/cron v1.1.1 // indirect
9799
github.com/magiconair/properties v1.8.5 // indirect
98100
github.com/mailru/easyjson v0.7.7 // indirect
99101
github.com/mattn/go-colorable v0.1.13 // indirect

0 commit comments

Comments
 (0)