Skip to content

Commit

Permalink
simplify k8s client get operation into list, and abstract update stat…
Browse files Browse the repository at this point in the history
…us of all gateways under gatewayclass into a method

Signed-off-by: shawnh2 <shawnhxh@outlook.com>
  • Loading branch information
shawnh2 committed Feb 22, 2024
1 parent 3b0e63f commit ae6cae6
Showing 1 changed file with 27 additions and 32 deletions.
59 changes: 27 additions & 32 deletions internal/provider/kubernetes/predicates.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,22 +224,11 @@ func (r *gatewayAPIReconciler) validateServiceForReconcile(obj client.Object) bo
}

// Merged gateways will have only this label, update status of all Gateways under found GatewayClass.
gclass, ok := labels[gatewayapi.OwningGatewayClassLabel]
if ok && r.mergeGateways[gclass] {
res, _ := r.resources.GatewayAPIResources.Load(string(r.classController))
if res != nil {
if (*res)[gclass] != nil && len((*res)[gclass].Gateways) > 0 {
for _, gtw := range (*res)[gclass].Gateways {
curGtw := new(gwapiv1.Gateway)
key := types.NamespacedName{Namespace: gtw.Namespace, Name: gtw.Name}
if err := r.client.Get(ctx, key, curGtw); err != nil {
r.log.Info("gateway not found", "gatewayclass", gclass, "name", key.String())
return false
}

r.updateStatusForGateway(ctx, curGtw)
}
}
gcName, ok := labels[gatewayapi.OwningGatewayClassLabel]
if ok && r.mergeGateways[gcName] {
if err := r.updateStatusForGatewaysUnderGatewayClass(ctx, gcName); err != nil {
r.log.Info("no Gateways found under GatewayClass", "name", gcName)
return false
}
return false
}
Expand Down Expand Up @@ -387,22 +376,11 @@ func (r *gatewayAPIReconciler) validateDeploymentForReconcile(obj client.Object)
}

// Merged gateways will have only this label, update status of all Gateways under found GatewayClass.
gclass, ok := labels[gatewayapi.OwningGatewayClassLabel]
if ok && r.mergeGateways[gclass] {
res, _ := r.resources.GatewayAPIResources.Load(string(r.classController))
if res != nil {
if (*res)[gclass] != nil && len((*res)[gclass].Gateways) > 0 {
for _, gtw := range (*res)[gclass].Gateways {
curGtw := new(gwapiv1.Gateway)
key := types.NamespacedName{Namespace: gtw.Namespace, Name: gtw.Name}
if err := r.client.Get(ctx, key, curGtw); err != nil {
r.log.Info("gateway not found", "gatewayclass", gclass, "name", key.String())
return false
}

r.updateStatusForGateway(ctx, curGtw)
}
}
gcName, ok := labels[gatewayapi.OwningGatewayClassLabel]
if ok && r.mergeGateways[gcName] {
if err := r.updateStatusForGatewaysUnderGatewayClass(ctx, gcName); err != nil {
r.log.Info("no Gateways found under GatewayClass", "name", gcName)
return false
}
return false
}
Expand Down Expand Up @@ -465,6 +443,23 @@ func (r *gatewayAPIReconciler) findOwningGateway(ctx context.Context, labels map
return gtw
}

// updateStatusForGatewaysUnderGatewayClass updates status of all Gateways under the GatewayClass.
func (r *gatewayAPIReconciler) updateStatusForGatewaysUnderGatewayClass(ctx context.Context, gatewayClassName string) error {
gateways := new(gwapiv1.GatewayList)
if err := r.client.List(ctx, gateways, &client.ListOptions{
FieldSelector: fields.OneTermEqualSelector(classGatewayIndex, gatewayClassName),
}); err != nil {
return err
}

for _, gateway := range gateways.Items {
gateway := gateway
r.updateStatusForGateway(ctx, &gateway)
}

return nil
}

func (r *gatewayAPIReconciler) handleNode(obj client.Object) bool {
ctx := context.Background()
node, ok := obj.(*corev1.Node)
Expand Down

0 comments on commit ae6cae6

Please sign in to comment.