|
| 1 | +/* |
| 2 | +Copyright 2023 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package controllers |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + "reflect" |
| 23 | + "strings" |
| 24 | + "time" |
| 25 | + |
| 26 | + "github.com/go-logr/logr" |
| 27 | + "github.com/kubernetes-sigs/blixt/pkg/vars" |
| 28 | + corev1 "k8s.io/api/core/v1" |
| 29 | + "k8s.io/apimachinery/pkg/api/errors" |
| 30 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 31 | + "k8s.io/apimachinery/pkg/runtime" |
| 32 | + "k8s.io/apimachinery/pkg/types" |
| 33 | + ctrl "sigs.k8s.io/controller-runtime" |
| 34 | + "sigs.k8s.io/controller-runtime/pkg/builder" |
| 35 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 36 | + "sigs.k8s.io/controller-runtime/pkg/handler" |
| 37 | + "sigs.k8s.io/controller-runtime/pkg/log" |
| 38 | + "sigs.k8s.io/controller-runtime/pkg/predicate" |
| 39 | + gatewayv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1" |
| 40 | +) |
| 41 | + |
| 42 | +//+kubebuilder:rbac:groups=gateway.networking.k8s.io,resources=gateways,verbs=get;list;watch;create;update;patch;delete |
| 43 | +//+kubebuilder:rbac:groups=gateway.networking.k8s.io,resources=gateways/status,verbs=get;update;patch |
| 44 | +//+kubebuilder:rbac:groups=gateway.networking.k8s.io,resources=gateways/finalizers,verbs=update |
| 45 | + |
| 46 | +//+kubebuilder:rbac:groups=core,resources=services,verbs=get;list;watch;create;update;patch;delete |
| 47 | +//+kubebuilder:rbac:groups=core,resources=services/status,verbs=get |
| 48 | + |
| 49 | +//+kubebuilder:rbac:groups=core,resources=endpoints,verbs=get;list;watch;create;update;patch;delete |
| 50 | +//+kubebuilder:rbac:groups=core,resources=endpoints/status,verbs=get |
| 51 | + |
| 52 | +//+kubebuilder:rbac:groups=core,resources=events,verbs=get;list;watch |
| 53 | + |
| 54 | +const gatewayServiceLabel = "blixt.gateway.networking.k8s.io/owned-by-gateway" |
| 55 | + |
| 56 | +// GatewayReconciler reconciles a Gateway object |
| 57 | +type GatewayReconciler struct { |
| 58 | + client.Client |
| 59 | + Scheme *runtime.Scheme |
| 60 | + Log logr.Logger |
| 61 | +} |
| 62 | + |
| 63 | +// SetupWithManager loads the controller into the provided controller manager. |
| 64 | +func (r *GatewayReconciler) SetupWithManager(mgr ctrl.Manager) error { |
| 65 | + r.Log = log.FromContext(context.Background()) |
| 66 | + |
| 67 | + return ctrl.NewControllerManagedBy(mgr). |
| 68 | + For(&gatewayv1beta1.Gateway{}, |
| 69 | + builder.WithPredicates(predicate.NewPredicateFuncs(r.gatewayHasMatchingGatewayClass)), |
| 70 | + ). |
| 71 | + Watches( |
| 72 | + &corev1.Service{}, |
| 73 | + handler.EnqueueRequestsFromMapFunc(mapServiceToGateway), |
| 74 | + ). |
| 75 | + Watches( |
| 76 | + &gatewayv1beta1.GatewayClass{}, |
| 77 | + handler.EnqueueRequestsFromMapFunc(r.mapGatewayClassToGateway), |
| 78 | + ). |
| 79 | + Complete(r) |
| 80 | +} |
| 81 | + |
| 82 | +func (r *GatewayReconciler) gatewayHasMatchingGatewayClass(obj client.Object) bool { |
| 83 | + gateway, ok := obj.(*gatewayv1beta1.Gateway) |
| 84 | + if !ok { |
| 85 | + r.Log.Error(fmt.Errorf("unexpected object type in gateway watch predicates"), "expected", "*gatewayv1beta1.Gateway", "found", reflect.TypeOf(obj)) |
| 86 | + return false |
| 87 | + } |
| 88 | + |
| 89 | + gatewayClass := &gatewayv1beta1.GatewayClass{} |
| 90 | + if err := r.Client.Get(context.Background(), client.ObjectKey{Name: string(gateway.Spec.GatewayClassName)}, gatewayClass); err != nil { |
| 91 | + if errors.IsNotFound(err) { |
| 92 | + return false |
| 93 | + } |
| 94 | + r.Log.Error(err, "couldn't retrieve gatewayclass for unknown reason, enqueing gateway anyway to avoid miss", "gatewayclass", gateway.Spec.GatewayClassName) |
| 95 | + return true |
| 96 | + } |
| 97 | + |
| 98 | + return gatewayClass.Spec.ControllerName == vars.GatewayClassControllerName |
| 99 | +} |
| 100 | + |
| 101 | +// Reconcile provisions (and de-provisions) resources relevant to this controller. |
| 102 | +// TODO: this whole thing needs a rewrite |
| 103 | +func (r *GatewayReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { |
| 104 | + log := log.FromContext(ctx) |
| 105 | + |
| 106 | + gateway := new(gatewayv1beta1.Gateway) |
| 107 | + if err := r.Client.Get(ctx, req.NamespacedName, gateway); err != nil { |
| 108 | + if errors.IsNotFound(err) { |
| 109 | + log.Info("object enqueued no longer exists, skipping") |
| 110 | + return ctrl.Result{}, nil |
| 111 | + } |
| 112 | + return ctrl.Result{}, err |
| 113 | + } |
| 114 | + |
| 115 | + gatewayClass := new(gatewayv1beta1.GatewayClass) |
| 116 | + if err := r.Client.Get(ctx, types.NamespacedName{Name: string(gateway.Spec.GatewayClassName)}, gatewayClass); err != nil { |
| 117 | + if errors.IsNotFound(err) { |
| 118 | + return ctrl.Result{}, nil |
| 119 | + } |
| 120 | + return ctrl.Result{}, err |
| 121 | + } |
| 122 | + |
| 123 | + if gatewayClass.Spec.ControllerName != vars.GatewayClassControllerName { |
| 124 | + return ctrl.Result{}, nil |
| 125 | + } |
| 126 | + |
| 127 | + log.Info("found a supported Gateway, determining whether the gateway has been accepted") |
| 128 | + oldGateway := gateway.DeepCopy() |
| 129 | + if !isGatewayAccepted(gateway) { |
| 130 | + log.Info("gateway not yet accepted") |
| 131 | + setGatewayListenerStatus(gateway) |
| 132 | + setGatewayStatus(gateway) |
| 133 | + updateConditionGeneration(gateway) |
| 134 | + return ctrl.Result{}, r.Status().Patch(ctx, gateway, client.MergeFrom(oldGateway)) |
| 135 | + } |
| 136 | + |
| 137 | + log.Info("checking for Service for Gateway") |
| 138 | + svc, err := r.getServiceForGateway(ctx, gateway) |
| 139 | + if err != nil { |
| 140 | + return ctrl.Result{}, err |
| 141 | + } |
| 142 | + if svc == nil { |
| 143 | + log.Info("creating Service for Gateway") |
| 144 | + return ctrl.Result{}, r.createServiceForGateway(ctx, gateway) // service creation will requeue gateway |
| 145 | + } |
| 146 | + |
| 147 | + log.Info("checking Service configuration") |
| 148 | + needsUpdate, err := r.ensureServiceConfiguration(ctx, svc, gateway) |
| 149 | + if err != nil { |
| 150 | + return ctrl.Result{}, err |
| 151 | + } |
| 152 | + if needsUpdate { |
| 153 | + return ctrl.Result{}, r.Client.Update(ctx, svc) |
| 154 | + } |
| 155 | + |
| 156 | + log.Info("checking Service status", "namespace", svc.Namespace, "name", svc.Name) |
| 157 | + switch t := svc.Spec.Type; t { |
| 158 | + case corev1.ServiceTypeLoadBalancer: |
| 159 | + if err := r.svcIsHealthy(ctx, svc); err != nil { |
| 160 | + // TODO: only handles metallb right now https://github.com/kubernetes-sigs/blixt/issues/96 |
| 161 | + if strings.Contains(err.Error(), "Failed to allocate IP") { |
| 162 | + r.Log.Info("failed to allocate IP for Gateway", gateway.Namespace, gateway.Name) |
| 163 | + setCond(gateway, metav1.Condition{ |
| 164 | + Type: string(gatewayv1beta1.GatewayConditionProgrammed), |
| 165 | + ObservedGeneration: gateway.Generation, |
| 166 | + Status: metav1.ConditionFalse, |
| 167 | + LastTransitionTime: metav1.Now(), |
| 168 | + Reason: string(gatewayv1beta1.GatewayReasonAddressNotUsable), |
| 169 | + Message: err.Error(), |
| 170 | + }) |
| 171 | + updateConditionGeneration(gateway) |
| 172 | + return ctrl.Result{Requeue: true}, r.Status().Patch(ctx, gateway, client.MergeFrom(oldGateway)) |
| 173 | + } |
| 174 | + return ctrl.Result{}, err |
| 175 | + } |
| 176 | + |
| 177 | + if svc.Spec.ClusterIP == "" || len(svc.Status.LoadBalancer.Ingress) < 1 { |
| 178 | + log.Info("waiting for Service to be ready") |
| 179 | + return ctrl.Result{RequeueAfter: time.Second}, nil |
| 180 | + } |
| 181 | + default: |
| 182 | + return ctrl.Result{}, fmt.Errorf("found unsupported Service type: %s (only LoadBalancer type is currently supported)", t) |
| 183 | + } |
| 184 | + |
| 185 | + // hack for metallb - https://github.com/metallb/metallb/issues/1640 |
| 186 | + // no need to enforce the gateway status here, as this endpoint is not reconciled by the controller |
| 187 | + // and no reconciliation loop is triggered upon its change or deletion. |
| 188 | + created, err := r.hackEnsureEndpoints(ctx, svc) |
| 189 | + if err != nil { |
| 190 | + return ctrl.Result{}, err |
| 191 | + } |
| 192 | + if created { |
| 193 | + return ctrl.Result{Requeue: true}, nil |
| 194 | + } |
| 195 | + |
| 196 | + log.Info("Service is ready, setting Gateway as programmed") |
| 197 | + setGatewayStatusAddresses(gateway, svc) |
| 198 | + setGatewayListenerConditionsAndProgrammed(gateway) |
| 199 | + updateConditionGeneration(gateway) |
| 200 | + return ctrl.Result{}, r.Status().Patch(ctx, gateway, client.MergeFrom(oldGateway)) |
| 201 | +} |
0 commit comments