Skip to content

Commit

Permalink
review feedback
Browse files Browse the repository at this point in the history
Signed-off-by: Mattia Lavacca <[email protected]>
  • Loading branch information
mlavacca committed Dec 7, 2022
1 parent 1918983 commit 2964fa1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 17 deletions.
24 changes: 13 additions & 11 deletions controllers/gateway_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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))
}
6 changes: 0 additions & 6 deletions controllers/gateway_controller_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 2964fa1

Please sign in to comment.