|
| 1 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 2 | +// you may not use this file except in compliance with the License. |
| 3 | +// You may obtain a copy of the License at |
| 4 | +// |
| 5 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +// |
| 7 | +// Unless required by applicable law or agreed to in writing, software |
| 8 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | +// See the License for the specific language governing permissions and |
| 11 | +// limitations under the License. |
| 12 | + |
| 13 | +package controller |
| 14 | + |
| 15 | +import ( |
| 16 | + "cmp" |
| 17 | + "context" |
| 18 | + |
| 19 | + "github.com/go-logr/logr" |
| 20 | + networkingv1 "k8s.io/api/networking/v1" |
| 21 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 22 | + "k8s.io/apimachinery/pkg/runtime" |
| 23 | + ctrl "sigs.k8s.io/controller-runtime" |
| 24 | + "sigs.k8s.io/controller-runtime/pkg/builder" |
| 25 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 26 | + "sigs.k8s.io/controller-runtime/pkg/handler" |
| 27 | + "sigs.k8s.io/controller-runtime/pkg/predicate" |
| 28 | + "sigs.k8s.io/controller-runtime/pkg/reconcile" |
| 29 | + |
| 30 | + "github.com/apache/apisix-ingress-controller/api/v1alpha1" |
| 31 | + apiv2 "github.com/apache/apisix-ingress-controller/api/v2" |
| 32 | + "github.com/apache/apisix-ingress-controller/internal/controller/status" |
| 33 | + "github.com/apache/apisix-ingress-controller/internal/utils" |
| 34 | +) |
| 35 | + |
| 36 | +// ApisixUpstreamReconciler reconciles a ApisixUpstream object |
| 37 | +type ApisixUpstreamReconciler struct { |
| 38 | + client.Client |
| 39 | + Scheme *runtime.Scheme |
| 40 | + Log logr.Logger |
| 41 | + Updater status.Updater |
| 42 | +} |
| 43 | + |
| 44 | +// SetupWithManager sets up the controller with the Manager. |
| 45 | +func (r *ApisixUpstreamReconciler) SetupWithManager(mgr ctrl.Manager) error { |
| 46 | + return ctrl.NewControllerManagedBy(mgr). |
| 47 | + For(&apiv2.ApisixUpstream{}). |
| 48 | + WithEventFilter(predicate.GenerationChangedPredicate{}). |
| 49 | + Watches(&networkingv1.IngressClass{}, |
| 50 | + handler.EnqueueRequestsFromMapFunc(r.listApisixUpstreamForIngressClass), |
| 51 | + builder.WithPredicates( |
| 52 | + predicate.NewPredicateFuncs(r.matchesIngressController), |
| 53 | + ), |
| 54 | + ). |
| 55 | + Watches(&v1alpha1.GatewayProxy{}, |
| 56 | + handler.EnqueueRequestsFromMapFunc(r.listApisixUpstreamForGatewayProxy), |
| 57 | + ). |
| 58 | + Named("apisixupstream"). |
| 59 | + Complete(r) |
| 60 | +} |
| 61 | + |
| 62 | +func (r *ApisixUpstreamReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { |
| 63 | + var au apiv2.ApisixUpstream |
| 64 | + if err := r.Get(ctx, req.NamespacedName, &au); err != nil { |
| 65 | + return ctrl.Result{}, client.IgnoreNotFound(err) |
| 66 | + } |
| 67 | + |
| 68 | + var ( |
| 69 | + ic *networkingv1.IngressClass |
| 70 | + err error |
| 71 | + ) |
| 72 | + defer func() { |
| 73 | + r.updateStatus(&au, err) |
| 74 | + }() |
| 75 | + |
| 76 | + if ic, err = r.getIngressClass(&au); err != nil { |
| 77 | + return ctrl.Result{}, err |
| 78 | + } |
| 79 | + if err = r.processIngressClassParameters(ctx, &au, ic); err != nil { |
| 80 | + return ctrl.Result{}, err |
| 81 | + } |
| 82 | + return ctrl.Result{}, nil |
| 83 | +} |
| 84 | + |
| 85 | +func (r *ApisixUpstreamReconciler) listApisixUpstreamForIngressClass(ctx context.Context, object client.Object) (requests []reconcile.Request) { |
| 86 | + ic, ok := object.(*networkingv1.IngressClass) |
| 87 | + if !ok { |
| 88 | + return nil |
| 89 | + } |
| 90 | + |
| 91 | + isDefaultIngressClass := IsDefaultIngressClass(ic) |
| 92 | + var auList apiv2.ApisixUpstreamList |
| 93 | + if err := r.List(ctx, &auList); err != nil { |
| 94 | + return nil |
| 95 | + } |
| 96 | + for _, pc := range auList.Items { |
| 97 | + if pc.Spec.IngressClassName == ic.Name || (isDefaultIngressClass && pc.Spec.IngressClassName == "") { |
| 98 | + requests = append(requests, reconcile.Request{NamespacedName: utils.NamespacedName(&pc)}) |
| 99 | + } |
| 100 | + } |
| 101 | + return requests |
| 102 | +} |
| 103 | + |
| 104 | +func (r *ApisixUpstreamReconciler) listApisixUpstreamForGatewayProxy(ctx context.Context, object client.Object) (requests []reconcile.Request) { |
| 105 | + gp, ok := object.(*v1alpha1.GatewayProxy) |
| 106 | + if !ok { |
| 107 | + return nil |
| 108 | + } |
| 109 | + |
| 110 | + var icList networkingv1.IngressClassList |
| 111 | + if err := r.List(ctx, &icList); err != nil { |
| 112 | + r.Log.Error(err, "failed to list ingress classes for gateway proxy", "gatewayproxy", gp.GetName()) |
| 113 | + return nil |
| 114 | + } |
| 115 | + |
| 116 | + for _, ic := range icList.Items { |
| 117 | + requests = append(requests, r.listApisixUpstreamForIngressClass(ctx, &ic)...) |
| 118 | + } |
| 119 | + |
| 120 | + return requests |
| 121 | +} |
| 122 | + |
| 123 | +func (r *ApisixUpstreamReconciler) matchesIngressController(obj client.Object) bool { |
| 124 | + ingressClass, ok := obj.(*networkingv1.IngressClass) |
| 125 | + if !ok { |
| 126 | + return false |
| 127 | + } |
| 128 | + return matchesController(ingressClass.Spec.Controller) |
| 129 | +} |
| 130 | + |
| 131 | +func (r *ApisixUpstreamReconciler) getIngressClass(au *apiv2.ApisixUpstream) (*networkingv1.IngressClass, error) { |
| 132 | + if au.Spec.IngressClassName == "" { |
| 133 | + return r.getDefaultIngressClass() |
| 134 | + } |
| 135 | + |
| 136 | + var ic networkingv1.IngressClass |
| 137 | + if err := r.Get(context.Background(), client.ObjectKey{Name: au.Spec.IngressClassName}, &ic); err != nil { |
| 138 | + return nil, err |
| 139 | + } |
| 140 | + return &ic, nil |
| 141 | +} |
| 142 | + |
| 143 | +func (r *ApisixUpstreamReconciler) processIngressClassParameters(ctx context.Context, au *apiv2.ApisixUpstream, ic *networkingv1.IngressClass) error { |
| 144 | + if ic == nil || ic.Spec.Parameters == nil { |
| 145 | + return nil |
| 146 | + } |
| 147 | + |
| 148 | + var ( |
| 149 | + parameters = ic.Spec.Parameters |
| 150 | + ) |
| 151 | + if parameters.APIGroup == nil || *parameters.APIGroup != v1alpha1.GroupVersion.Group || parameters.Kind != KindGatewayProxy { |
| 152 | + return nil |
| 153 | + } |
| 154 | + |
| 155 | + // check if the parameters reference GatewayProxy |
| 156 | + var ( |
| 157 | + gp v1alpha1.GatewayProxy |
| 158 | + ns = cmp.Or(parameters.Namespace, &au.Namespace) |
| 159 | + ) |
| 160 | + |
| 161 | + return r.Get(ctx, client.ObjectKey{Namespace: *ns, Name: parameters.Name}, &gp) |
| 162 | +} |
| 163 | + |
| 164 | +func (r *ApisixUpstreamReconciler) getDefaultIngressClass() (*networkingv1.IngressClass, error) { |
| 165 | + var icList networkingv1.IngressClassList |
| 166 | + if err := r.List(context.Background(), &icList); err != nil { |
| 167 | + r.Log.Error(err, "failed to list ingress classes") |
| 168 | + return nil, err |
| 169 | + } |
| 170 | + for _, ic := range icList.Items { |
| 171 | + if IsDefaultIngressClass(&ic) && matchesController(ic.Spec.Controller) { |
| 172 | + return &ic, nil |
| 173 | + } |
| 174 | + } |
| 175 | + return nil, ReasonError{ |
| 176 | + Reason: string(metav1.StatusReasonNotFound), |
| 177 | + Message: "default ingress class not found or does not match the controller", |
| 178 | + } |
| 179 | +} |
| 180 | + |
| 181 | +func (r *ApisixUpstreamReconciler) updateStatus(au *apiv2.ApisixUpstream, err error) { |
| 182 | + SetApisixCRDConditionAccepted(&au.Status, au.GetGeneration(), err) |
| 183 | + r.Updater.Update(status.Update{ |
| 184 | + NamespacedName: utils.NamespacedName(au), |
| 185 | + Resource: &apiv2.ApisixUpstream{}, |
| 186 | + Mutator: status.MutatorFunc(func(obj client.Object) client.Object { |
| 187 | + cp := obj.(*apiv2.ApisixUpstream).DeepCopy() |
| 188 | + cp.Status = au.Status |
| 189 | + return cp |
| 190 | + }), |
| 191 | + }) |
| 192 | +} |
0 commit comments