/
apigateway_controller.go
319 lines (272 loc) · 14.4 KB
/
apigateway_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
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*
Copyright 2022.
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 operator
import (
"context"
"fmt"
"github.com/kyma-project/api-gateway/internal/conditions"
"k8s.io/apimachinery/pkg/api/meta"
"strings"
oryv1alpha1 "github.com/ory/oathkeeper-maester/api/v1alpha1"
"errors"
"github.com/kyma-project/api-gateway/apis/gateway/v1beta1"
"github.com/kyma-project/api-gateway/apis/operator/v1alpha1"
operatorv1alpha1 "github.com/kyma-project/api-gateway/apis/operator/v1alpha1"
"github.com/kyma-project/api-gateway/controllers"
"github.com/kyma-project/api-gateway/internal/dependencies"
"github.com/kyma-project/api-gateway/internal/reconciliations"
"github.com/kyma-project/api-gateway/internal/reconciliations/gateway"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/client-go/util/retry"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/predicate"
)
const (
APIGatewayResourceListDefaultPath = "manifests/controlled_resources_list.yaml"
ApiGatewayFinalizer = "gateways.operator.kyma-project.io/api-gateway"
)
func NewAPIGatewayReconciler(mgr manager.Manager, oathkeeperReconciler ReadyVerifyingReconciler) *APIGatewayReconciler {
return &APIGatewayReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
log: mgr.GetLogger().WithName("apigateway-controller"),
oathkeeperReconciler: oathkeeperReconciler,
}
}
//+kubebuilder:rbac:groups=operator.kyma-project.io,resources=apigateways,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=operator.kyma-project.io,resources=apigateways/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=operator.kyma-project.io,resources=apigateways/finalizers,verbs=update
//+kubebuilder:rbac:groups=networking.istio.io,resources=gateways,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=security.istio.io,resources=peerauthentications,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups="",resources=nodes,verbs=get;list;watch
//+kubebuilder:rbac:groups="",resources=secrets;deployments;services;serviceaccounts,verbs=get;list;watch;update;patch;create;delete
//+kubebuilder:rbac:groups="oathkeeper.ory.sh",resources=rules,verbs=deletecollection;create;delete;get;list;patch;update;watch
//+kubebuilder:rbac:groups="rbac.authorization.k8s.io",resources=roles;rolebindings;clusterroles;clusterrolebindings,verbs=get;list;watch;update;patch;create;delete
//+kubebuilder:rbac:groups="autoscaling",resources=horizontalpodautoscalers,verbs=get;list;watch;update;patch;create;delete
//+kubebuilder:rbac:groups="apps",resources=deployments,verbs=get;list;watch;update;patch;create;delete
//+kubebuilder:rbac:groups="cert.gardener.cloud",resources=certificates,verbs=get;list;watch;update;patch;create;delete
//+kubebuilder:rbac:groups="dns.gardener.cloud",resources=dnsentries,verbs=get;list;watch;update;patch;create;delete
//+kubebuilder:rbac:groups="policy",resources=poddisruptionbudgets,verbs=get;list;watch;update;patch;create;delete
func (r *APIGatewayReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
r.log.Info("Received reconciliation request", "name", req.Name)
apiGatewayCR := operatorv1alpha1.APIGateway{}
if err := r.Client.Get(ctx, req.NamespacedName, &apiGatewayCR); err != nil {
if apierrors.IsNotFound(err) {
r.log.Info("Skipped reconciliation, because ApiGateway CR was not found")
return ctrl.Result{}, nil
}
r.log.Info("Could not get APIGateway CR")
return ctrl.Result{}, err
}
existingAPIGateways := &operatorv1alpha1.APIGatewayList{}
if err := r.Client.List(ctx, existingAPIGateways); err != nil {
r.log.Info("Unable to list APIGateway CRs")
return r.requeueReconciliation(ctx, apiGatewayCR, controllers.ErrorStatus(err, "Unable to list APIGateway CRs", conditions.ReconcileFailed.Condition()))
}
if len(existingAPIGateways.Items) > 1 {
oldestCr := r.getOldestCR(existingAPIGateways)
if apiGatewayCR.GetUID() != oldestCr.GetUID() {
err := fmt.Errorf("stopped APIGateway CR reconciliation: only APIGateway CR %s reconciles the module", oldestCr.GetName())
return r.terminateReconciliation(ctx, apiGatewayCR, controllers.WarningStatus(err, err.Error(), conditions.OlderCRExists.Condition()))
}
}
r.log.Info("Reconciling APIGateway CR", "name", apiGatewayCR.Name, "isInDeletion", apiGatewayCR.IsInDeletion())
if err := controllers.UpdateApiGatewayStatus(ctx, r.Client, &apiGatewayCR, controllers.ProcessingStatus(conditions.ReconcileProcessing.Condition())); err != nil {
r.log.Error(err, "Update status to processing failed")
// We don't update the status to error, because the status update already failed and to avoid another status update error we simply requeue the request.
return ctrl.Result{}, err
}
if !apiGatewayCR.IsInDeletion() {
isGardenerCluster, err := reconciliations.RunsOnGardenerCluster(ctx, r.Client)
if err != nil {
return r.requeueReconciliation(ctx, apiGatewayCR, controllers.ErrorStatus(err, "Error during discovering if cluster is Gardener", conditions.ReconcileFailed.Condition()))
}
if isGardenerCluster {
if name, dependenciesErr := dependencies.GardenerAPIGateway().AreAvailable(ctx, r.Client); dependenciesErr != nil {
return r.requeueReconciliation(ctx, apiGatewayCR, handleDependenciesError(name, dependenciesErr))
}
} else {
if name, dependenciesErr := dependencies.ApiGateway().AreAvailable(ctx, r.Client); dependenciesErr != nil {
return r.requeueReconciliation(ctx, apiGatewayCR, handleDependenciesError(name, dependenciesErr))
}
}
}
if finalizerStatus := r.reconcileFinalizer(ctx, &apiGatewayCR); !finalizerStatus.IsReady() {
return r.requeueReconciliation(ctx, apiGatewayCR, finalizerStatus)
}
if kymaGatewayStatus := gateway.ReconcileKymaGateway(ctx, r.Client, &apiGatewayCR, APIGatewayResourceListDefaultPath); !kymaGatewayStatus.IsReady() {
return r.requeueReconciliation(ctx, apiGatewayCR, kymaGatewayStatus)
}
if oryOathkeeperStatus := r.oathkeeperReconciler.ReconcileAndVerifyReadiness(ctx, r.Client, &apiGatewayCR); !oryOathkeeperStatus.IsReady() {
return r.requeueReconciliation(ctx, apiGatewayCR, oryOathkeeperStatus)
}
// If there are no finalizers left, we must assume that the resource is deleted and therefore must stop the reconciliation
// to prevent accidental read or write to the resource.
if !apiGatewayCR.HasFinalizer() {
r.log.Info("End reconciliation because all finalizers have been removed")
return ctrl.Result{}, nil
}
return r.finishReconcile(ctx, apiGatewayCR)
}
func handleDependenciesError(name string, err error) controllers.Status {
if apierrors.IsNotFound(err) {
return controllers.ErrorStatus(err, fmt.Sprintf("CRD %s is not present. Make sure to install required dependencies for the component", name), conditions.DependenciesMissing.Condition())
} else {
return controllers.ErrorStatus(err, "Error happened during discovering dependencies", conditions.ReconcileFailed.Condition())
}
}
// SetupWithManager sets up the controller with the Manager.
func (r *APIGatewayReconciler) SetupWithManager(mgr ctrl.Manager, c controllers.RateLimiterConfig) error {
return ctrl.NewControllerManagedBy(mgr).
For(&operatorv1alpha1.APIGateway{}).
WithEventFilter(predicate.GenerationChangedPredicate{}).
WithOptions(controller.Options{
RateLimiter: controllers.NewRateLimiter(c),
}).
Complete(r)
}
// requeueReconciliation cancels the reconciliation and requeues the request.
func (r *APIGatewayReconciler) requeueReconciliation(ctx context.Context, cr operatorv1alpha1.APIGateway, status controllers.Status) (ctrl.Result, error) {
r.log.Error(status.NestedError(), "Reconcile failed")
statusUpdateErr := controllers.UpdateApiGatewayStatus(ctx, r.Client, &cr, status)
if statusUpdateErr != nil {
r.log.Error(statusUpdateErr, "Update status failed")
}
return ctrl.Result{}, status.NestedError()
}
func (r *APIGatewayReconciler) finishReconcile(ctx context.Context, cr v1alpha1.APIGateway) (ctrl.Result, error) {
if err := controllers.UpdateApiGatewayStatus(ctx, r.Client, &cr, controllers.ReadyStatus(conditions.ReconcileSucceeded.Condition())); err != nil {
r.log.Error(err, "Update status failed")
return ctrl.Result{}, err
}
r.log.Info("Successfully reconciled")
return ctrl.Result{}, nil
}
func (r *APIGatewayReconciler) terminateReconciliation(ctx context.Context, apiGatewayCR operatorv1alpha1.APIGateway, status controllers.Status) (ctrl.Result, error) {
statusUpdateErr := controllers.UpdateApiGatewayStatus(ctx, r.Client, &apiGatewayCR, status)
if statusUpdateErr != nil {
r.log.Error(statusUpdateErr, "Error during updating status to error")
// In case the update of the status fails we must requeue the request, because otherwise the Error state is never visible in the CR.
return ctrl.Result{}, statusUpdateErr
}
r.log.Error(status.NestedError(), "Reconcile failed, but won't requeue")
return ctrl.Result{}, nil
}
func (r *APIGatewayReconciler) reconcileFinalizer(ctx context.Context, apiGatewayCR *operatorv1alpha1.APIGateway) controllers.Status {
if !apiGatewayCR.IsInDeletion() && !hasFinalizer(apiGatewayCR) {
controllerutil.AddFinalizer(apiGatewayCR, ApiGatewayFinalizer)
if err := r.Client.Update(ctx, apiGatewayCR); err != nil {
ctrl.Log.Error(err, "Failed to add API-Gateway CR finalizer")
return controllers.ErrorStatus(err, "Could not add API-Gateway CR finalizer", conditions.ReconcileFailed.Condition())
}
}
if apiGatewayCR.IsInDeletion() && hasFinalizer(apiGatewayCR) {
apiRulesFound, err := apiRulesExist(ctx, r.Client)
if err != nil {
return controllers.ErrorStatus(err, "Error during listing existing APIRules", conditions.ReconcileFailed.Condition())
}
if len(apiRulesFound) > 0 {
return controllers.WarningStatus(errors.New("could not delete API-Gateway CR since there are APIRule(s) that block its deletion"),
"There are APIRule(s) that block the deletion of API-Gateway CR. Please take a look at kyma-system/api-gateway-controller-manager logs to see more information about the warning",
conditions.DeletionBlockedExistingResources.AdditionalMessage(": "+strings.Join(apiRulesFound, ", ")).Condition())
}
oryRulesFound, err := oryRulesExist(ctx, r.Client)
if err != nil {
return controllers.ErrorStatus(err, "Error during listing existing ORY Oathkeeper Rules", conditions.ReconcileFailed.Condition())
}
if len(oryRulesFound) > 0 {
return controllers.WarningStatus(errors.New("could not delete API-Gateway CR since there are ORY Oathkeeper Rule(s) that block its deletion"),
"There are ORY Oathkeeper Rule(s) that block the deletion of API-Gateway CR. Please take a look at kyma-system/api-gateway-controller-manager logs to see more information about the warning",
conditions.DeletionBlockedExistingResources.AdditionalMessage(": "+strings.Join(oryRulesFound, ", ")).Condition())
}
if err := removeFinalizer(ctx, r.Client, apiGatewayCR); err != nil {
ctrl.Log.Error(err, "Error happened during API-Gateway CR finalizer removal")
return controllers.ErrorStatus(err, "Could not remove finalizer", conditions.ReconcileFailed.Condition())
}
}
return controllers.ReadyStatus(conditions.ReconcileSucceeded.Condition())
}
func apiRulesExist(ctx context.Context, k8sClient client.Client) ([]string, error) {
apiRuleList := v1beta1.APIRuleList{}
err := k8sClient.List(ctx, &apiRuleList)
if meta.IsNoMatchError(err) || apierrors.IsNotFound(err) {
// ApiRule CRD does not exist, there are no blocking rules
return nil, nil
}
if err != nil {
return nil, err
}
ctrl.Log.Info(fmt.Sprintf("There are %d APIRule(s) found on cluster", len(apiRuleList.Items)))
var blockingApiRules []string
for _, rule := range apiRuleList.Items {
blocking := rule.GetNamespace() + "/" + rule.GetName()
ctrl.Log.Info("APIRule blocking deletion", "rule", blocking)
if len(blockingApiRules) < 5 {
blockingApiRules = append(blockingApiRules, blocking)
}
}
return blockingApiRules, nil
}
func oryRulesExist(ctx context.Context, k8sClient client.Client) ([]string, error) {
oryRulesList := oryv1alpha1.RuleList{}
err := k8sClient.List(ctx, &oryRulesList)
if meta.IsNoMatchError(err) || apierrors.IsNotFound(err) {
// Oathkeeper CRD does not exist, there are no blocking rules
return nil, nil
}
if err != nil {
// any other error
return nil, err
}
ctrl.Log.Info(fmt.Sprintf("There are %d ORY Oathkeeper Rule(s) found on cluster", len(oryRulesList.Items)))
var blockingOryRules []string
for _, rule := range oryRulesList.Items {
blocking := rule.GetNamespace() + "/" + rule.GetName()
ctrl.Log.Info("ORY Oathkeeper rule blocking deletion", "rule", blocking)
if len(blockingOryRules) < 5 {
blockingOryRules = append(blockingOryRules, blocking)
}
}
return blockingOryRules, nil
}
func hasFinalizer(apiGatewayCR *operatorv1alpha1.APIGateway) bool {
return controllerutil.ContainsFinalizer(apiGatewayCR, ApiGatewayFinalizer)
}
func removeFinalizer(ctx context.Context, apiClient client.Client, apiGatewayCR *operatorv1alpha1.APIGateway) error {
ctrl.Log.Info("Removing API-Gateway CR finalizer")
return retry.RetryOnConflict(retry.DefaultRetry, func() error {
if getErr := apiClient.Get(ctx, client.ObjectKeyFromObject(apiGatewayCR), apiGatewayCR); getErr != nil {
return getErr
}
controllerutil.RemoveFinalizer(apiGatewayCR, ApiGatewayFinalizer)
if updateErr := apiClient.Update(ctx, apiGatewayCR); updateErr != nil {
return updateErr
}
ctrl.Log.Info("Successfully removed API-Gateway CR finalizer")
return nil
})
}
func (r *APIGatewayReconciler) getOldestCR(apigatewayCRs *operatorv1alpha1.APIGatewayList) *operatorv1alpha1.APIGateway {
oldest := apigatewayCRs.Items[0]
for _, item := range apigatewayCRs.Items {
timestamp := &item.CreationTimestamp
if !(oldest.CreationTimestamp.Before(timestamp)) {
oldest = item
}
}
return &oldest
}