diff --git a/api/v1alpha1/nginx_types.go b/api/v1alpha1/nginx_types.go index b5270a19..55329699 100644 --- a/api/v1alpha1/nginx_types.go +++ b/api/v1alpha1/nginx_types.go @@ -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 { @@ -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() { diff --git a/config/crd/bases/nginx.tsuru.io_nginxes.yaml b/config/crd/bases/nginx.tsuru.io_nginxes.yaml index b5da1443..f801636d 100644 --- a/config/crd/bases/nginx.tsuru.io_nginxes.yaml +++ b/config/crd/bases/nginx.tsuru.io_nginxes.yaml @@ -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: @@ -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 @@ -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 diff --git a/controllers/nginx_controller.go b/controllers/nginx_controller.go index 5fac9c9c..be5e41b6 100644 --- a/controllers/nginx_controller.go +++ b/controllers/nginx_controller.go @@ -8,6 +8,7 @@ import ( "context" "fmt" "reflect" + "slices" "sort" "strings" "time" @@ -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) } @@ -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 @@ -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) } @@ -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 { @@ -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 {