Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 21 additions & 10 deletions internal/controller/consumer_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ package controller

import (
"context"
"fmt"

"github.com/go-logr/logr"
corev1 "k8s.io/api/core/v1"
Expand All @@ -32,6 +33,7 @@ import (

"github.com/apache/apisix-ingress-controller/api/v1alpha1"
"github.com/apache/apisix-ingress-controller/internal/controller/indexer"
"github.com/apache/apisix-ingress-controller/internal/controller/status"
"github.com/apache/apisix-ingress-controller/internal/provider"
)

Expand All @@ -42,6 +44,8 @@ type ConsumerReconciler struct { //nolint:revive
Log logr.Logger

Provider provider.Provider

Updater status.Updater
}

// SetupWithManager sets up the controller with the Manager.
Expand Down Expand Up @@ -232,9 +236,7 @@ func (r *ConsumerReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
statusErr = err
}

if err := r.updateStatus(ctx, consumer, statusErr); err != nil {
return ctrl.Result{}, err
}
r.updateStatus(consumer, statusErr)

return ctrl.Result{}, nil
}
Expand Down Expand Up @@ -269,20 +271,29 @@ func (r *ConsumerReconciler) processSpec(ctx context.Context, tctx *provider.Tra
return nil
}

func (r *ConsumerReconciler) updateStatus(ctx context.Context, consumer *v1alpha1.Consumer, err error) error {
func (r *ConsumerReconciler) updateStatus(consumer *v1alpha1.Consumer, err error) {
condition := NewCondition(consumer.Generation, true, "Successfully")
if err != nil {
condition = NewCondition(consumer.Generation, false, err.Error())
}
if !VerifyConditions(&consumer.Status.Conditions, condition) {
return nil
return
}
meta.SetStatusCondition(&consumer.Status.Conditions, condition)
if err := r.Status().Update(ctx, consumer); err != nil {
r.Log.Error(err, "failed to update consumer status", "consumer", consumer)
return err
}
return nil

r.Updater.Update(status.Update{
NamespacedName: NamespacedName(consumer),
Resource: consumer.DeepCopy(),
Mutator: status.MutatorFunc(func(obj client.Object) client.Object {
t, ok := obj.(*v1alpha1.Consumer)
if !ok {
err := fmt.Errorf("unsupported object type %T", obj)
panic(err)
}
t.Status = consumer.Status
return t
}),
})
}

func (r *ConsumerReconciler) getGateway(ctx context.Context, consumer *v1alpha1.Consumer) (*gatewayv1.Gateway, error) {
Expand Down
29 changes: 23 additions & 6 deletions internal/controller/gateway_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (

"github.com/apache/apisix-ingress-controller/api/v1alpha1"
"github.com/apache/apisix-ingress-controller/internal/controller/indexer"
"github.com/apache/apisix-ingress-controller/internal/controller/status"
"github.com/apache/apisix-ingress-controller/internal/provider"
)

Expand All @@ -45,6 +46,8 @@ type GatewayReconciler struct { //nolint:revive
Log logr.Logger

Provider provider.Provider

Updater status.Updater
}

// SetupWithManager sets up the controller with the Manager.
Expand Down Expand Up @@ -117,11 +120,11 @@ func (r *GatewayReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
conditionProgrammedStatus, conditionProgrammedMsg := true, "Programmed"

r.Log.Info("gateway has been accepted", "gateway", gateway.GetName())
type status struct {
type conditionStatus struct {
status bool
msg string
}
acceptStatus := status{
acceptStatus := conditionStatus{
status: true,
msg: acceptedMessage("gateway"),
}
Expand All @@ -131,7 +134,7 @@ func (r *GatewayReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct

r.processListenerConfig(tctx, gateway)
if err := r.processInfrastructure(tctx, gateway); err != nil {
acceptStatus = status{
acceptStatus = conditionStatus{
status: false,
msg: err.Error(),
}
Expand All @@ -147,7 +150,7 @@ func (r *GatewayReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct

gatewayProxy, ok := tctx.GatewayProxies[rk]
if !ok {
acceptStatus = status{
acceptStatus = conditionStatus{
status: false,
msg: "gateway proxy not found",
}
Expand All @@ -167,7 +170,7 @@ func (r *GatewayReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
}

if err := r.Provider.Update(ctx, tctx, gateway); err != nil {
acceptStatus = status{
acceptStatus = conditionStatus{
status: false,
msg: err.Error(),
}
Expand All @@ -189,7 +192,21 @@ func (r *GatewayReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
gateway.Status.Listeners = listenerStatuses
}

return ctrl.Result{}, r.Status().Update(ctx, gateway)
r.Updater.Update(status.Update{
NamespacedName: NamespacedName(gateway),
Resource: gateway.DeepCopy(),
Mutator: status.MutatorFunc(func(obj client.Object) client.Object {
t, ok := obj.(*gatewayv1.Gateway)
if !ok {
err := fmt.Errorf("unsupported object type %T", obj)
panic(err)
}
t.Status = gateway.Status
return t
}),
})

return ctrl.Result{}, nil
}

return ctrl.Result{}, nil
Expand Down
19 changes: 16 additions & 3 deletions internal/controller/gatewayclass_congroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (

"github.com/apache/apisix-ingress-controller/internal/controller/config"
"github.com/apache/apisix-ingress-controller/internal/controller/indexer"
"github.com/apache/apisix-ingress-controller/internal/controller/status"
)

const (
Expand All @@ -46,6 +47,8 @@ type GatewayClassReconciler struct { //nolint:revive

record.EventRecorder
Log logr.Logger

Updater status.Updater
}

// SetupWithManager sets up the controller with the Manager.
Expand Down Expand Up @@ -111,9 +114,19 @@ func (r *GatewayClassReconciler) Reconcile(ctx context.Context, req ctrl.Request
if !IsConditionPresentAndEqual(gc.Status.Conditions, condition) {
r.Log.Info("gatewayclass has been accepted", "gatewayclass", gc.Name)
setGatewayClassCondition(gc, condition)
if err := r.Status().Update(ctx, gc); err != nil {
return ctrl.Result{}, err
}
r.Updater.Update(status.Update{
NamespacedName: NamespacedName(gc),
Resource: gc.DeepCopy(),
Mutator: status.MutatorFunc(func(obj client.Object) client.Object {
t, ok := obj.(*gatewayv1.GatewayClass)
if !ok {
err := fmt.Errorf("unsupported object type %T", obj)
panic(err)
}
t.Status = gc.Status
return t
}),
})
}
return ctrl.Result{}, nil
}
Expand Down
25 changes: 19 additions & 6 deletions internal/controller/httproute_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import (

"github.com/apache/apisix-ingress-controller/api/v1alpha1"
"github.com/apache/apisix-ingress-controller/internal/controller/indexer"
"github.com/apache/apisix-ingress-controller/internal/controller/status"
"github.com/apache/apisix-ingress-controller/internal/provider"
)

Expand All @@ -55,6 +56,8 @@ type HTTPRouteReconciler struct { //nolint:revive
Provider provider.Provider

genericEvent chan event.GenericEvent

Updater status.Updater
}

// SetupWithManager sets up the controller with the Manager.
Expand Down Expand Up @@ -143,13 +146,13 @@ func (r *HTTPRouteReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
return ctrl.Result{}, err
}

type status struct {
type ResourceStatus struct {
status bool
msg string
}

// Only keep acceptStatus since we're using error objects directly now
acceptStatus := status{
acceptStatus := ResourceStatus{
status: true,
msg: "Route is accepted",
}
Expand Down Expand Up @@ -233,10 +236,20 @@ func (r *HTTPRouteReconciler) Reconcile(ctx context.Context, req ctrl.Request) (

hr.Status.Parents = append(hr.Status.Parents, parentStatus)
}
if err := r.Status().Update(ctx, hr); err != nil {
return ctrl.Result{}, err
}
UpdateStatus(r.Client, r.Log, tctx)
r.Updater.Update(status.Update{
NamespacedName: NamespacedName(hr),
Resource: hr.DeepCopy(),
Mutator: status.MutatorFunc(func(obj client.Object) client.Object {
h, ok := obj.(*gatewayv1.HTTPRoute)
if !ok {
err := fmt.Errorf("unsupported object type %T", obj)
panic(err)
}
h.Status = hr.Status
return h
}),
})
UpdateStatus(r.Updater, r.Log, tctx)
return ctrl.Result{}, nil
}

Expand Down
59 changes: 48 additions & 11 deletions internal/controller/httproutepolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ package controller
import (
"cmp"
"context"
"fmt"
"slices"

"github.com/go-logr/logr"
networkingv1 "k8s.io/api/networking/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
Expand All @@ -28,6 +28,7 @@ import (

"github.com/apache/apisix-ingress-controller/api/v1alpha1"
"github.com/apache/apisix-ingress-controller/internal/controller/indexer"
"github.com/apache/apisix-ingress-controller/internal/controller/status"
"github.com/apache/apisix-ingress-controller/internal/provider"
)

Expand Down Expand Up @@ -71,7 +72,20 @@ func (r *HTTPRouteReconciler) processHTTPRoutePolicies(tctx *provider.TranslateC
}

if updated := setAncestorsForHTTPRoutePolicyStatus(httpRoute.Spec.ParentRefs, &policy, condition); updated {
tctx.StatusUpdaters = append(tctx.StatusUpdaters, &policy)
tctx.StatusUpdaters = append(tctx.StatusUpdaters, status.Update{
NamespacedName: NamespacedName(&policy),
Resource: policy.DeepCopy(),
Mutator: status.MutatorFunc(func(obj client.Object) client.Object {
t, ok := obj.(*v1alpha1.HTTPRoutePolicy)
if !ok {
err := fmt.Errorf("unsupported object type %T", obj)
panic(err)
}
tCopy := t.DeepCopy()
tCopy.Status = policy.Status
return tCopy
}),
})
}
}

Expand All @@ -83,7 +97,7 @@ func (r *HTTPRouteReconciler) updateHTTPRoutePolicyStatusOnDeleting(ctx context.
list v1alpha1.HTTPRoutePolicyList
key = indexer.GenIndexKeyWithGK(gatewayv1.GroupName, "HTTPRoute", nn.Namespace, nn.Name)
)
if err := r.List(context.Background(), &list, client.MatchingFields{indexer.PolicyTargetRefs: key}); err != nil {
if err := r.List(ctx, &list, client.MatchingFields{indexer.PolicyTargetRefs: key}); err != nil {
return err
}
var (
Expand All @@ -96,15 +110,15 @@ func (r *HTTPRouteReconciler) updateHTTPRoutePolicyStatusOnDeleting(ctx context.
var namespacedName = types.NamespacedName{Namespace: policy.GetNamespace(), Name: string(ref.Name)}
httpRoute, ok := httpRoutes[namespacedName]
if !ok {
if err := r.Get(context.Background(), namespacedName, &httpRoute); err != nil {
if err := r.Get(ctx, namespacedName, &httpRoute); err != nil {
continue
}
httpRoutes[namespacedName] = httpRoute
}
parentRefs = append(parentRefs, httpRoute.Spec.ParentRefs...)
}
// delete AncestorRef which is not exist in the all parentRefs for each policy
updateDeleteAncestors(ctx, r.Client, r.Log, policy, parentRefs)
updateDeleteAncestors(r.Updater, policy, parentRefs)
}

return nil
Expand Down Expand Up @@ -137,7 +151,20 @@ func (r *IngressReconciler) processHTTPRoutePolicies(tctx *provider.TranslateCon
for i := range list.Items {
policy := list.Items[i]
if updated := setAncestorsForHTTPRoutePolicyStatus(tctx.RouteParentRefs, &policy, condition); updated {
tctx.StatusUpdaters = append(tctx.StatusUpdaters, &policy)
tctx.StatusUpdaters = append(tctx.StatusUpdaters, status.Update{
NamespacedName: NamespacedName(&policy),
Resource: policy.DeepCopy(),
Mutator: status.MutatorFunc(func(obj client.Object) client.Object {
t, ok := obj.(*v1alpha1.HTTPRoutePolicy)
if !ok {
err := fmt.Errorf("unsupported object type %T", obj)
panic(err)
}
tCopy := t.DeepCopy()
tCopy.Status = policy.Status
return tCopy
}),
})
}
}

Expand Down Expand Up @@ -180,7 +207,7 @@ func (r *IngressReconciler) updateHTTPRoutePolicyStatusOnDeleting(ctx context.Co
parentRefs = append(parentRefs, parentRef)
}
// delete AncestorRef which is not exist in the all parentRefs
updateDeleteAncestors(ctx, r.Client, r.Log, policy, parentRefs)
updateDeleteAncestors(r.Updater, policy, parentRefs)
}

return nil
Expand Down Expand Up @@ -229,16 +256,26 @@ func findPoliciesWhichTargetRefTheRule(ruleName *gatewayv1.SectionName, kind str
}

// updateDeleteAncestors removes ancestor references from HTTPRoutePolicy statuses that are no longer present in the provided parentRefs.
func updateDeleteAncestors(ctx context.Context, client client.Client, logger logr.Logger, policy v1alpha1.HTTPRoutePolicy, parentRefs []gatewayv1.ParentReference) {
func updateDeleteAncestors(updater status.Updater, policy v1alpha1.HTTPRoutePolicy, parentRefs []gatewayv1.ParentReference) {
length := len(policy.Status.Ancestors)
policy.Status.Ancestors = slices.DeleteFunc(policy.Status.Ancestors, func(ancestor v1alpha2.PolicyAncestorStatus) bool {
return !slices.ContainsFunc(parentRefs, func(ref gatewayv1.ParentReference) bool {
return parentRefValueEqual(ancestor.AncestorRef, ref)
})
})
if length != len(policy.Status.Ancestors) {
if err := client.Status().Update(ctx, &policy); err != nil {
logger.Error(err, "failed to update HTTPRoutePolicy status")
}
updater.Update(status.Update{
NamespacedName: NamespacedName(&policy),
Resource: policy.DeepCopy(),
Mutator: status.MutatorFunc(func(obj client.Object) client.Object {
t, ok := obj.(*v1alpha1.HTTPRoutePolicy)
if !ok {
err := fmt.Errorf("unsupported object type %T", obj)
panic(err)
}
t.Status = policy.Status
return t
}),
})
}
}
Loading
Loading