This repository has been archived by the owner on Mar 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathgateway_class_controller.go
100 lines (85 loc) · 3.35 KB
/
gateway_class_controller.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package controllers
import (
"context"
"time"
ctrl "sigs.k8s.io/controller-runtime"
gwv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1"
"github.com/hashicorp/consul-api-gateway/internal/k8s/gatewayclient"
"github.com/hashicorp/consul-api-gateway/internal/k8s/reconciler"
"github.com/hashicorp/go-hclog"
)
const (
gatewayClassFinalizer = "gateway-exists-finalizer.networking.x-k8s.io"
)
// GatewayClassReconciler reconciles a GatewayClass object
type GatewayClassReconciler struct {
Client gatewayclient.Client
Log hclog.Logger
ControllerName string
Manager reconciler.ReconcileManager
}
//+kubebuilder:rbac:groups=gateway.networking.k8s.io,resources=gatewayclasses,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=gateway.networking.k8s.io,resources=gatewayclasses/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=gateway.networking.k8s.io,resources=gatewayclasses/finalizers,verbs=update
// Reconcile is part of the main kubernetes reconciliation loop which aims to
// move the current state of the cluster closer to the desired state.
// For more details, check Reconcile and its Result here:
// - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.8.3/pkg/reconcile
func (r *GatewayClassReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
logger := r.Log.With("gatewayClass", req.NamespacedName)
gc, err := r.Client.GetGatewayClass(ctx, req.NamespacedName)
if err != nil {
logger.Error("failed to get gateway class", "error", err)
return ctrl.Result{}, err
}
if gc == nil {
// we've been deleted clean up cached resources
err = r.Manager.DeleteGatewayClass(ctx, req.NamespacedName.Name)
return ctrl.Result{}, err
}
if string(gc.Spec.ControllerName) != r.ControllerName {
// no-op if we don't manage this gateway class
return ctrl.Result{}, nil
}
if !gc.ObjectMeta.DeletionTimestamp.IsZero() {
// we have a deletion, ensure we're not in use
used, err := r.Client.GatewayClassInUse(ctx, gc)
if err != nil {
logger.Error("failed to check if the gateway class is still in use", "error", err)
return ctrl.Result{}, err
}
if used {
// requeue as to not block the reconciliation loop
return ctrl.Result{RequeueAfter: 10 * time.Second}, nil
}
// remove finalizer
if _, err := r.Client.RemoveFinalizer(ctx, gc, gatewayClassFinalizer); err != nil {
logger.Error("error removing gateway class finalizer", "error", err)
return ctrl.Result{}, err
}
return ctrl.Result{}, nil
}
// we're creating or updating
updated, err := r.Client.EnsureFinalizer(ctx, gc, gatewayClassFinalizer)
if err != nil {
logger.Error("error adding gateway class finalizer", "error", err)
return ctrl.Result{}, err
}
if updated {
// since we've updated the finalizers, returning here will enqueue another event
return ctrl.Result{}, nil
}
if err := r.Manager.UpsertGatewayClass(ctx, gc); err != nil {
logger.Error("error upserting gateway class", "error", err)
return ctrl.Result{}, err
}
return ctrl.Result{}, nil
}
// SetupWithManager sets up the controller with the Manager.
func (r *GatewayClassReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&gwv1beta1.GatewayClass{}).
Complete(gatewayclient.NewRequeueingMiddleware(r.Log, r))
}