diff --git a/controllers/gateway_controller.go b/controllers/gateway_controller.go index 6d0e5ec0..045f52c7 100644 --- a/controllers/gateway_controller.go +++ b/controllers/gateway_controller.go @@ -98,8 +98,10 @@ func (r *GatewayReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct gatewayReadyStatus, gatewayReadyStatusIsSet := isGatewayReady(gw) oldGateway := gw.DeepCopy() - initGatewayStatus(gw) - factorizeStatus(gw, oldGateway) + if !gatewayReadyStatusIsSet { + initGatewayStatus(gw) + r.Status().Patch(ctx, gw, client.MergeFrom(oldGateway)) + } log.Info("checking for Service for Gateway") svc, err := r.getServiceForGateway(ctx, gw) @@ -108,8 +110,8 @@ func (r *GatewayReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct } if svc == nil { // if the ready status is not set, or the gateway is marked as ready, mark it as not ready - if !gatewayReadyStatusIsSet || gatewayReadyStatus { - return ctrl.Result{}, r.patchGatewayStatus(ctx, gw, oldGateway) // status patch will requeue gateway + if gatewayReadyStatus { + return ctrl.Result{}, r.Status().Patch(ctx, gw, client.MergeFrom(oldGateway)) // status patch will requeue gateway } log.Info("creating Service for Gateway") return ctrl.Result{}, r.createServiceForGateway(ctx, gw) // service creation will requeue gateway @@ -125,8 +127,8 @@ func (r *GatewayReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct } if needsUpdate { // if the ready status is not set, or the gateway is marked as ready, mark it as not ready - if !gatewayReadyStatusIsSet || gatewayReadyStatus { - return ctrl.Result{}, r.patchGatewayStatus(ctx, gw, oldGateway) // status patch will requeue gateway + if gatewayReadyStatus { + return ctrl.Result{}, r.Status().Patch(ctx, gw, client.MergeFrom(oldGateway)) // status patch will requeue gateway } return ctrl.Result{}, r.Client.Update(ctx, svc) } @@ -136,16 +138,16 @@ func (r *GatewayReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct case corev1.ServiceTypeLoadBalancer: if svc.Spec.ClusterIP == "" || len(svc.Status.LoadBalancer.Ingress) < 1 { // if the ready status is not set, or the gateway is marked as ready, mark it as not ready - if !gatewayReadyStatusIsSet || gatewayReadyStatus { - return ctrl.Result{}, r.patchGatewayStatus(ctx, gw, oldGateway) // status patch will requeue gateway + if gatewayReadyStatus { + return ctrl.Result{}, r.Status().Patch(ctx, gw, client.MergeFrom(oldGateway)) // status patch will requeue gateway } log.Info("waiting for Service to be ready") return ctrl.Result{Requeue: true}, nil } default: // if the ready status is not set, or the gateway is marked as ready, mark it as not ready - if !gatewayReadyStatusIsSet || gatewayReadyStatus { - return ctrl.Result{}, r.patchGatewayStatus(ctx, gw, oldGateway) // status patch will requeue gateway + if gatewayReadyStatus { + return ctrl.Result{}, r.Status().Patch(ctx, gw, client.MergeFrom(oldGateway)) // status patch will requeue gateway } return ctrl.Result{}, fmt.Errorf("found unsupported Service type: %s (only LoadBalancer type is currently supported)", t) } @@ -164,5 +166,5 @@ func (r *GatewayReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct log.Info("Service is ready, updating Gateway") updateGatewayStatus(ctx, gw, svc) factorizeStatus(gw, oldGateway) - return ctrl.Result{}, r.patchGatewayStatus(ctx, gw, oldGateway) + return ctrl.Result{}, r.Status().Patch(ctx, gw, client.MergeFrom(oldGateway)) } diff --git a/controllers/gateway_controller_status.go b/controllers/gateway_controller_status.go index f0bd7a0a..8a462e66 100644 --- a/controllers/gateway_controller_status.go +++ b/controllers/gateway_controller_status.go @@ -5,15 +5,9 @@ import ( corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "sigs.k8s.io/controller-runtime/pkg/client" gatewayv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1" ) -// patchGatewayStatus applies the diff between the old gateway status and the new one as a patch. -func (r *GatewayReconciler) patchGatewayStatus(ctx context.Context, gateway, oldGateway *gatewayv1beta1.Gateway) error { - return r.Status().Patch(ctx, gateway, client.MergeFrom(oldGateway)) -} - // updateGatewayStatus computes the new Gateway status, setting its ready condition and all the // ready listeners's ready conditions to true, unless a resolvedRefs error is discovered. In // that case, the proper listener ready condition and the gateway one are set to false.