/
injectproxy_controller.go
68 lines (58 loc) · 2.26 KB
/
injectproxy_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
/*
Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
Licensed under the Universal Permissive License v 1.0 as shown at http://oss.oracle.com/licenses/upl.
*/
package controllers
import (
"context"
"github.com/oracle/oci-service-operator/pkg/loggerutil"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"github.com/oracle/oci-service-operator/pkg/servicemanager/servicemesh/injectproxy"
"github.com/oracle/oci-service-operator/pkg/servicemanager/servicemesh/utils/commons"
merrors "github.com/oracle/oci-service-operator/pkg/servicemanager/servicemesh/utils/errors"
)
type InjectProxyReconciler struct {
client client.Client
log loggerutil.OSOKLogger
handler injectproxy.ResourceHandler
}
func NewInjectProxyReconciler(
client client.Client,
log loggerutil.OSOKLogger,
handler injectproxy.ResourceHandler,
) *InjectProxyReconciler {
return &InjectProxyReconciler{client, log, handler}
}
// Reconcile will keep a watch on namespaces with proxy injection label present and inject proxies initially by evicting the pods
func (r *InjectProxyReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
return merrors.HandleErrorAndRequeue(ctx, r.reconcileProxy(ctx, req), r.log)
}
func (r *InjectProxyReconciler) reconcileProxy(ctx context.Context, req ctrl.Request) error {
namespace := &corev1.Namespace{}
err := r.client.Get(ctx, req.NamespacedName, namespace)
if err != nil {
if errors.IsNotFound(err) {
r.log.InfoLogWithFixedMessage(ctx, "Namespace deleted")
return nil
}
r.log.ErrorLogWithFixedMessage(ctx, err, "Error in reading Namespace object")
return merrors.NewRequeueOnError(err)
}
if !namespace.DeletionTimestamp.IsZero() {
r.log.InfoLogWithFixedMessage(ctx, "Namespace DeletionTimestamp is set, about to be deleted")
return nil
}
return r.handler.Reconcile(ctx, namespace)
}
func (r *InjectProxyReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
WithOptions(controller.Options{
RateLimiter: commons.DefaultControllerRateLimiter(commons.MaxDelay),
}).
For(&corev1.Namespace{}).
Complete(r)
}