Skip to content

Commit

Permalink
Add addresses fields to status
Browse files Browse the repository at this point in the history
Co-authored-by: Claudio Gisch <claudio.gisch@hotmail.com>
  • Loading branch information
wpjunior and crgisch committed Jan 17, 2024
1 parent ff8580f commit 29bf889
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 7 deletions.
10 changes: 8 additions & 2 deletions api/v1alpha1/nginx_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (
// +kubebuilder:printcolumn:name="Current",type=integer,JSONPath=`.status.currentReplicas`
// +kubebuilder:printcolumn:name="Desired",type=integer,JSONPath=`.spec.replicas`
// +kubebuilder:printcolumn:name="Age",type=date,JSONPath=`.metadata.creationTimestamp`
// +kubebuilder:printcolumn:name="Ingress IPs",type=string,JSONPath=`.status.ingresses[*].ips[*]`
// +kubebuilder:printcolumn:name="Service IPs",type=string,JSONPath=`.status.services[*].ips[*]`

// Nginx is the Schema for the nginxes API
type Nginx struct {
Expand Down Expand Up @@ -262,12 +264,16 @@ type DeploymentStatus struct {

type ServiceStatus struct {
// Name is the name of the Service created by nginx
Name string `json:"name"`
Name string `json:"name"`
IPs []string `json:"ips,omitempty"`
Hostnames []string `json:"hostnames,omitempty"`
}

type IngressStatus struct {
// Name is the name of the Ingress created by nginx
Name string `json:"name"`
Name string `json:"name"`
IPs []string `json:"ips,omitempty"`
Hostnames []string `json:"hostnames,omitempty"`
}

func init() {
Expand Down
22 changes: 22 additions & 0 deletions config/crd/bases/nginx.tsuru.io_nginxes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ spec:
- jsonPath: .metadata.creationTimestamp
name: Age
type: date
- jsonPath: .status.ingresses[*].ips[*]
name: Ingress IPs
type: string
- jsonPath: .status.services[*].ips[*]
name: Service IPs
type: string
name: v1alpha1
schema:
openAPIV3Schema:
Expand Down Expand Up @@ -5680,6 +5686,14 @@ spec:
ingresses:
items:
properties:
hostnames:
items:
type: string
type: array
ips:
items:
type: string
type: array
name:
description: Name is the name of the Ingress created by nginx
type: string
Expand All @@ -5693,6 +5707,14 @@ spec:
services:
items:
properties:
hostnames:
items:
type: string
type: array
ips:
items:
type: string
type: array
name:
description: Name is the name of the Service created by nginx
type: string
Expand Down
54 changes: 49 additions & 5 deletions controllers/nginx_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"context"
"fmt"
"reflect"
"slices"

Check failure on line 11 in controllers/nginx_controller.go

View workflow job for this annotation

GitHub Actions / test

package slices is not in GOROOT (/opt/hostedtoolcache/go/1.20.12/x64/src/slices)
"sort"
"strings"
"time"
Expand Down Expand Up @@ -51,6 +52,8 @@ func (r *NginxReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&nginxv1alpha1.Nginx{}).
Owns(&appsv1.Deployment{}).
Owns(&networkingv1.Ingress{}).
Owns(&corev1.Service{}).
Complete(r)
}

Expand Down Expand Up @@ -239,6 +242,12 @@ func (r *NginxReconciler) reconcileIngress(ctx context.Context, nginx *nginxv1al
return nil
}

for key, value := range currentIngress.Annotations {
if newIngress.Annotations[key] == "" {
newIngress.Annotations[key] = value
}
}

newIngress.ResourceVersion = currentIngress.ResourceVersion
newIngress.Finalizers = currentIngress.Finalizers

Expand All @@ -250,8 +259,13 @@ func shouldUpdateIngress(currentIngress, newIngress *networkingv1.Ingress) bool
return false
}

return !reflect.DeepEqual(currentIngress.Annotations, newIngress.Annotations) ||
!reflect.DeepEqual(currentIngress.Labels, newIngress.Labels) ||
for key, value := range newIngress.Annotations {
if currentIngress.Annotations[key] != value {
return true
}
}

return !reflect.DeepEqual(currentIngress.Labels, newIngress.Labels) ||
!reflect.DeepEqual(currentIngress.Spec, newIngress.Spec)
}

Expand Down Expand Up @@ -361,9 +375,24 @@ func listServices(ctx context.Context, c client.Client, nginx *nginxv1alpha1.Ngi

var services []nginxv1alpha1.ServiceStatus
for _, s := range serviceList.Items {
services = append(services, nginxv1alpha1.ServiceStatus{
svc := nginxv1alpha1.ServiceStatus{
Name: s.Name,
})
}

for _, ingStatus := range s.Status.LoadBalancer.Ingress {
if ingStatus.IP != "" {
svc.IPs = append(svc.IPs, ingStatus.IP)
}

if ingStatus.Hostname != "" {
svc.Hostnames = append(svc.Hostnames, ingStatus.Hostname)
}
}

slices.Sort(svc.IPs)
slices.Sort(svc.Hostnames)

services = append(services, svc)
}

sort.Slice(services, func(i, j int) bool {
Expand All @@ -386,7 +415,22 @@ func listIngresses(ctx context.Context, c client.Client, nginx *nginxv1alpha1.Ng

var ingresses []nginxv1alpha1.IngressStatus
for _, i := range ingressList.Items {
ingresses = append(ingresses, nginxv1alpha1.IngressStatus{Name: i.Name})
ing := nginxv1alpha1.IngressStatus{Name: i.Name}

for _, ingStatus := range i.Status.LoadBalancer.Ingress {
if ingStatus.IP != "" {
ing.IPs = append(ing.IPs, ingStatus.IP)
}

if ingStatus.Hostname != "" {
ing.Hostnames = append(ing.Hostnames, ingStatus.Hostname)
}
}

slices.Sort(ing.IPs)
slices.Sort(ing.Hostnames)

ingresses = append(ingresses, ing)
}

sort.Slice(ingresses, func(i, j int) bool {
Expand Down

0 comments on commit 29bf889

Please sign in to comment.