-
Notifications
You must be signed in to change notification settings - Fork 190
/
controller.go
93 lines (78 loc) · 3.04 KB
/
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
package pod
import (
"context"
"github.com/microsoft/retina/pkg/common/apiretry"
"github.com/microsoft/retina/pkg/log"
"go.uber.org/zap"
corev1 "k8s.io/api/core/v1"
errors "k8s.io/apimachinery/pkg/api/errors"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
retinaCommon "github.com/microsoft/retina/pkg/common"
"github.com/microsoft/retina/pkg/controllers/cache"
)
// PodReconciler reconciles a Pod object
type PodReconciler struct {
client.Client
cache *cache.Cache
l *log.ZapLogger
}
func New(client client.Client, cache *cache.Cache) *PodReconciler {
return &PodReconciler{
Client: client,
cache: cache,
l: log.Logger().Named(string("PodReconciler")),
}
}
// +kubebuilder:rbac:groups="",resources=pods,verbs=get;list
func (r *PodReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
r.l.Info("Reconciling Pod", zap.String("Pod", req.NamespacedName.String()))
pod := &corev1.Pod{}
if err := apiretry.Do(
func() error {
return r.Client.Get(ctx, req.NamespacedName, pod)
},
); err != nil {
if errors.IsNotFound(err) {
// RetinaEndpoint deleted since reconcile request received.
r.l.Debug("Pod is not found", zap.String("Pod", req.NamespacedName.String()))
// It's safe to use req.NamespacedName as the Pod and RetinaEndpoint share the same namespace and name.
if err := r.cache.DeleteRetinaEndpoint(req.NamespacedName.String()); err != nil {
r.l.Warn("Failed to delete RetinaEndpoint in Cache from Pod", zap.Error(err), zap.String("RetinaEndpoint", req.NamespacedName.String()))
}
return ctrl.Result{}, nil
}
r.l.Error("Failed to fetch Pod", zap.Error(err), zap.String("Pod", req.NamespacedName.String()))
return ctrl.Result{}, client.IgnoreNotFound(err)
}
if pod.Spec.HostNetwork {
r.l.Debug("Ignoring host network pod", zap.String("Pod", req.NamespacedName.String()))
return ctrl.Result{}, nil
}
if !pod.ObjectMeta.DeletionTimestamp.IsZero() {
r.l.Info("Pod is being deleted", zap.String("Pod", req.Name))
if err := r.cache.DeleteRetinaEndpoint(req.NamespacedName.String()); err != nil {
r.l.Warn("Failed to delete RetinaEndpoint in Cache from Pod", zap.Error(err), zap.String("Pod", req.NamespacedName.String()))
}
return ctrl.Result{}, nil
}
if pod.Status.PodIP == "" {
r.l.Debug("Pod has no IP address", zap.String("Pod", req.NamespacedName.String()))
return ctrl.Result{}, nil
}
retinaEndpointCommon := retinaCommon.RetinaEndpointCommonFromPod(pod)
if err := r.cache.UpdateRetinaEndpoint(retinaEndpointCommon); err != nil {
r.l.Error("Failed to update RetinaEndpoint in Cache", zap.Error(err), zap.String("Pod", req.NamespacedName.String()))
return ctrl.Result{}, err
}
return ctrl.Result{}, nil
}
// SetupWithManager sets up the controller with the Manager.
func (r *PodReconciler) SetupWithManager(mgr ctrl.Manager) error {
r.l.Info("Setting up Pod controller")
return ctrl.NewControllerManagedBy(mgr).
For(&corev1.Pod{}).
Complete(r)
}