-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathapisixtls_controller.go
More file actions
287 lines (259 loc) · 9.85 KB
/
apisixtls_controller.go
File metadata and controls
287 lines (259 loc) · 9.85 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you 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"
corev1 "k8s.io/api/core/v1"
networkingv1 "k8s.io/api/networking/v1"
networkingv1beta1 "k8s.io/api/networking/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
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/indexer"
"github.com/apache/apisix-ingress-controller/internal/controller/status"
"github.com/apache/apisix-ingress-controller/internal/manager/readiness"
"github.com/apache/apisix-ingress-controller/internal/provider"
"github.com/apache/apisix-ingress-controller/internal/utils"
pkgutils "github.com/apache/apisix-ingress-controller/pkg/utils"
)
// ApisixTlsReconciler reconciles a ApisixTls object
type ApisixTlsReconciler struct {
client.Client
Scheme *runtime.Scheme
Log logr.Logger
Provider provider.Provider
Updater status.Updater
Readier readiness.ReadinessManager
ICGV schema.GroupVersion
SupportsEndpointSlice bool // cache capability
}
// SetupWithManager sets up the controller with the Manager.
func (r *ApisixTlsReconciler) SetupWithManager(mgr ctrl.Manager) error {
var icWatch client.Object
switch r.ICGV.String() {
case networkingv1beta1.SchemeGroupVersion.String():
icWatch = &networkingv1beta1.IngressClass{}
default:
icWatch = &networkingv1.IngressClass{}
}
return ctrl.NewControllerManagedBy(mgr).
For(&apiv2.ApisixTls{},
builder.WithPredicates(
MatchesIngressClassPredicate(r.Client, r.Log, r.ICGV.String()),
),
).
WithEventFilter(
predicate.Or(
predicate.GenerationChangedPredicate{},
predicate.AnnotationChangedPredicate{},
predicate.NewPredicateFuncs(TypePredicate[*corev1.Secret]()),
),
).
Watches(
icWatch,
handler.EnqueueRequestsFromMapFunc(r.listApisixTlsForIngressClass),
builder.WithPredicates(
predicate.NewPredicateFuncs(matchesIngressController),
),
).
Watches(&v1alpha1.GatewayProxy{},
handler.EnqueueRequestsFromMapFunc(r.listApisixTlsForGatewayProxy),
).
Watches(&corev1.Secret{},
handler.EnqueueRequestsFromMapFunc(r.listApisixTlsForSecret),
).
Complete(r)
}
// Reconcile implements the reconciliation logic for ApisixTls
func (r *ApisixTlsReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
defer r.Readier.Done(&apiv2.ApisixTls{}, req.NamespacedName)
var tls apiv2.ApisixTls
if err := r.Get(ctx, req.NamespacedName, &tls); err != nil {
if client.IgnoreNotFound(err) == nil {
// Create a minimal object for deletion
tls.Namespace = req.Namespace
tls.Name = req.Name
tls.TypeMeta = metav1.TypeMeta{
Kind: KindApisixTls,
APIVersion: apiv2.GroupVersion.String(),
}
// Delete from provider
if err := r.Provider.Delete(ctx, &tls); err != nil {
r.Log.Error(err, "failed to delete TLS from provider")
return ctrl.Result{}, err
}
r.Log.Info("deleted apisix tls", "tls", tls.Name)
return ctrl.Result{}, nil
}
return ctrl.Result{}, err
}
r.Log.Info("reconciling apisix tls", "tls", tls.Name)
// create a translate context
tctx := provider.NewDefaultTranslateContext(ctx)
// get the ingress class
ingressClass, err := FindMatchingIngressClassByObject(tctx, r.Client, r.Log, &tls, r.ICGV.String())
if err != nil {
r.Log.V(1).Info("no matching IngressClass available, skip processing",
"ingressClassName", tls.Spec.IngressClassName,
"error", err.Error())
return ctrl.Result{}, nil
}
// process IngressClass parameters if they reference GatewayProxy
if err := ProcessIngressClassParameters(tctx, r.Client, r.Log, &tls, ingressClass, r.SupportsEndpointSlice); err != nil {
r.Log.Error(err, "failed to process IngressClass parameters", "ingressClass", ingressClass.Name)
r.updateStatus(&tls, metav1.Condition{
Type: string(apiv2.ConditionTypeAccepted),
Status: metav1.ConditionFalse,
ObservedGeneration: tls.Generation,
LastTransitionTime: metav1.Now(),
Reason: string(apiv2.ConditionReasonInvalidSpec),
Message: err.Error(),
})
return ctrl.Result{}, client.IgnoreNotFound(err)
}
// process ApisixTls validation
if err := r.processApisixTls(ctx, tctx, &tls); err != nil {
r.Log.Error(err, "failed to process ApisixTls")
r.updateStatus(&tls, metav1.Condition{
Type: string(apiv2.ConditionTypeAccepted),
Status: metav1.ConditionFalse,
ObservedGeneration: tls.Generation,
LastTransitionTime: metav1.Now(),
Reason: string(apiv2.ConditionReasonInvalidSpec),
Message: err.Error(),
})
return ctrl.Result{}, client.IgnoreNotFound(err)
}
if err := r.Provider.Update(ctx, tctx, &tls); err != nil {
r.Log.Error(err, "failed to sync apisix tls to provider")
// Update status with failure condition
r.updateStatus(&tls, metav1.Condition{
Type: string(apiv2.ConditionTypeAccepted),
Status: metav1.ConditionFalse,
ObservedGeneration: tls.Generation,
LastTransitionTime: metav1.Now(),
Reason: string(apiv2.ConditionReasonSyncFailed),
Message: err.Error(),
})
return ctrl.Result{}, err
}
// Update status with success condition
r.updateStatus(&tls, metav1.Condition{
Type: string(apiv2.ConditionTypeAccepted),
Status: metav1.ConditionTrue,
ObservedGeneration: tls.Generation,
LastTransitionTime: metav1.Now(),
Reason: string(apiv2.ConditionReasonAccepted),
Message: "The apisix tls has been accepted and synced to APISIX",
})
return ctrl.Result{}, nil
}
func (r *ApisixTlsReconciler) processApisixTls(ctx context.Context, tc *provider.TranslateContext, tls *apiv2.ApisixTls) error {
// Validate the main TLS secret
if err := r.validateSecret(ctx, tc, tls.Spec.Secret); err != nil {
return fmt.Errorf("invalid apisix tls secret: %w", err)
}
// Validate the client CA secret if mutual TLS is configured
if tls.Spec.Client != nil {
if err := r.validateSecret(ctx, tc, tls.Spec.Client.CASecret); err != nil {
return fmt.Errorf("invalid client CA secret: %w", err)
}
}
return nil
}
func (r *ApisixTlsReconciler) validateSecret(ctx context.Context, tc *provider.TranslateContext, secretRef apiv2.ApisixSecret) error {
secretKey := types.NamespacedName{
Namespace: secretRef.Namespace,
Name: secretRef.Name,
}
var secret corev1.Secret
if err := r.Get(ctx, secretKey, &secret); err != nil {
return fmt.Errorf("failed to get secret %s: %w", secretKey.String(), err)
}
tc.Secrets[secretKey] = &secret
return nil
}
// updateStatus updates the ApisixTls status with the given condition
func (r *ApisixTlsReconciler) updateStatus(tls *apiv2.ApisixTls, condition metav1.Condition) {
r.Updater.Update(status.Update{
NamespacedName: utils.NamespacedName(tls),
Resource: &apiv2.ApisixTls{},
Mutator: status.MutatorFunc(func(obj client.Object) client.Object {
tlsCopy, ok := obj.(*apiv2.ApisixTls)
if !ok {
err := fmt.Errorf("unsupported object type %T", obj)
panic(err)
}
tlsResult := tlsCopy.DeepCopy()
tlsResult.Status.Conditions = []metav1.Condition{condition}
return tlsResult
}),
})
}
func (r *ApisixTlsReconciler) listApisixTlsForSecret(ctx context.Context, obj client.Object) []reconcile.Request {
secret, ok := obj.(*corev1.Secret)
if !ok {
return nil
}
return ListRequests(
ctx,
r.Client,
r.Log,
&apiv2.ApisixConsumerList{},
client.MatchingFields{
indexer.SecretIndexRef: indexer.GenIndexKey(secret.GetNamespace(), secret.GetName()),
},
)
}
// listApisixTlsForIngressClass list all TLS that use a specific ingress class
func (r *ApisixTlsReconciler) listApisixTlsForIngressClass(ctx context.Context, obj client.Object) []reconcile.Request {
ingressClass := pkgutils.ConvertToIngressClassV1(obj)
return ListMatchingRequests(
ctx,
r.Client,
r.Log,
&apiv2.ApisixTlsList{},
func(obj client.Object) bool {
atls, ok := obj.(*apiv2.ApisixTls)
if !ok {
r.Log.Error(fmt.Errorf("expected ApisixTls, got %T", obj), "failed to match object type")
return false
}
return (IsDefaultIngressClass(ingressClass) && atls.Spec.IngressClassName == "") || atls.Spec.IngressClassName == ingressClass.Name
},
)
}
// listApisixTlsForGatewayProxy list all TLS that use a specific gateway proxy
func (r *ApisixTlsReconciler) listApisixTlsForGatewayProxy(ctx context.Context, obj client.Object) []reconcile.Request {
switch r.ICGV.String() {
case networkingv1beta1.SchemeGroupVersion.String():
return listIngressClassV1beta1RequestsForGatewayProxy(ctx, r.Client, obj, r.Log, r.listApisixTlsForIngressClass)
default:
return listIngressClassRequestsForGatewayProxy(ctx, r.Client, obj, r.Log, r.listApisixTlsForIngressClass)
}
}