-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathapisixpluginconfig_controller.go
More file actions
201 lines (177 loc) · 6.38 KB
/
apisixpluginconfig_controller.go
File metadata and controls
201 lines (177 loc) · 6.38 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package controller
import (
"context"
"fmt"
"github.com/go-logr/logr"
networkingv1 "k8s.io/api/networking/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"github.com/apache/apisix-ingress-controller/api/v1alpha1"
apiv2 "github.com/apache/apisix-ingress-controller/api/v2"
"github.com/apache/apisix-ingress-controller/internal/controller/status"
"github.com/apache/apisix-ingress-controller/internal/utils"
)
// ApisixPluginConfigReconciler reconciles a ApisixPluginConfig object
type ApisixPluginConfigReconciler struct {
client.Client
Scheme *runtime.Scheme
Log logr.Logger
Updater status.Updater
}
// SetupWithManager sets up the controller with the Manager.
func (r *ApisixPluginConfigReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&apiv2.ApisixPluginConfig{}).
WithEventFilter(predicate.GenerationChangedPredicate{}).
Watches(&networkingv1.IngressClass{},
handler.EnqueueRequestsFromMapFunc(r.listApisixPluginConfigForIngressClass),
builder.WithPredicates(
predicate.NewPredicateFuncs(r.matchesIngressController),
),
).
Watches(&v1alpha1.GatewayProxy{},
handler.EnqueueRequestsFromMapFunc(r.listApisixPluginConfigForGatewayProxy),
).
Named("apisixpluginconfig").
Complete(r)
}
func (r *ApisixPluginConfigReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
var pc apiv2.ApisixPluginConfig
if err := r.Get(ctx, req.NamespacedName, &pc); err != nil {
return ctrl.Result{}, client.IgnoreNotFound(err)
}
var (
ic *networkingv1.IngressClass
err error
)
defer func() {
r.updateStatus(&pc, err)
}()
if ic, err = r.getIngressClass(&pc); err != nil {
return ctrl.Result{}, err
}
if err = r.processIngressClassParameters(ctx, &pc, ic); err != nil {
return ctrl.Result{}, err
}
return ctrl.Result{}, nil
}
func (r *ApisixPluginConfigReconciler) listApisixPluginConfigForIngressClass(ctx context.Context, object client.Object) (requests []reconcile.Request) {
ic, ok := object.(*networkingv1.IngressClass)
if !ok {
return nil
}
isDefaultIngressClass := IsDefaultIngressClass(ic)
var pcList apiv2.ApisixPluginConfigList
if err := r.List(ctx, &pcList); err != nil {
return nil
}
for _, pc := range pcList.Items {
if pc.Spec.IngressClassName == ic.Name || (isDefaultIngressClass && pc.Spec.IngressClassName == "") {
requests = append(requests, reconcile.Request{NamespacedName: utils.NamespacedName(&pc)})
}
}
return requests
}
func (r *ApisixPluginConfigReconciler) listApisixPluginConfigForGatewayProxy(ctx context.Context, object client.Object) (requests []reconcile.Request) {
gp, ok := object.(*v1alpha1.GatewayProxy)
if !ok {
return nil
}
var icList networkingv1.IngressClassList
if err := r.List(ctx, &icList); err != nil {
r.Log.Error(err, "failed to list ingress classes for gateway proxy", "gatewayproxy", gp.GetName())
return nil
}
for _, ic := range icList.Items {
requests = append(requests, r.listApisixPluginConfigForIngressClass(ctx, &ic)...)
}
return requests
}
func (r *ApisixPluginConfigReconciler) matchesIngressController(obj client.Object) bool {
ingressClass, ok := obj.(*networkingv1.IngressClass)
if !ok {
return false
}
return matchesController(ingressClass.Spec.Controller)
}
func (r *ApisixPluginConfigReconciler) getIngressClass(pc *apiv2.ApisixPluginConfig) (*networkingv1.IngressClass, error) {
if pc.Spec.IngressClassName == "" {
return r.getDefaultIngressClass()
}
var ic networkingv1.IngressClass
if err := r.Get(context.Background(), client.ObjectKey{Name: pc.Spec.IngressClassName}, &ic); err != nil {
return nil, err
}
return &ic, nil
}
func (r *ApisixPluginConfigReconciler) getDefaultIngressClass() (*networkingv1.IngressClass, error) {
var icList networkingv1.IngressClassList
if err := r.List(context.Background(), &icList); err != nil {
r.Log.Error(err, "failed to list ingress classes")
return nil, err
}
for _, ic := range icList.Items {
if IsDefaultIngressClass(&ic) && matchesController(ic.Spec.Controller) {
return &ic, nil
}
}
return nil, ReasonError{
Reason: string(metav1.StatusReasonNotFound),
Message: "default ingress class not found or does not match the controller",
}
}
// processIngressClassParameters processes the IngressClass parameters that reference GatewayProxy
func (r *ApisixPluginConfigReconciler) processIngressClassParameters(ctx context.Context, pc *apiv2.ApisixPluginConfig, ingressClass *networkingv1.IngressClass) error {
if ingressClass == nil || ingressClass.Spec.Parameters == nil {
return nil
}
var (
parameters = ingressClass.Spec.Parameters
)
if parameters.APIGroup == nil || *parameters.APIGroup != v1alpha1.GroupVersion.Group || parameters.Kind != KindGatewayProxy {
return nil
}
// check if the parameters reference GatewayProxy
var (
gatewayProxy v1alpha1.GatewayProxy
ns = parameters.Namespace
)
if ns == nil {
ns = &pc.Namespace
}
return r.Get(ctx, client.ObjectKey{Namespace: *ns, Name: parameters.Name}, &gatewayProxy)
}
func (r *ApisixPluginConfigReconciler) updateStatus(pc *apiv2.ApisixPluginConfig, err error) {
SetApisixCRDConditionAccepted(&pc.Status, pc.GetGeneration(), err)
r.Updater.Update(status.Update{
NamespacedName: utils.NamespacedName(pc),
Resource: &apiv2.ApisixPluginConfig{},
Mutator: status.MutatorFunc(func(obj client.Object) client.Object {
cp, ok := obj.(*apiv2.ApisixPluginConfig)
if !ok {
err := fmt.Errorf("unsupported object type %T", obj)
panic(err)
}
cpCopy := cp.DeepCopy()
cpCopy.Status = pc.Status
return cpCopy
}),
})
}