|
| 1 | +/* |
| 2 | +Copyright (c) Microsoft Corporation. |
| 3 | +Licensed under the MIT license. |
| 4 | +*/ |
| 5 | + |
| 6 | +// Package controllers feature a number of controllers that are in use |
| 7 | +// by the AKS property provider. |
| 8 | +package controllers |
| 9 | + |
| 10 | +import ( |
| 11 | + "context" |
| 12 | + "time" |
| 13 | + |
| 14 | + corev1 "k8s.io/api/core/v1" |
| 15 | + "k8s.io/apimachinery/pkg/api/errors" |
| 16 | + "k8s.io/klog/v2" |
| 17 | + ctrl "sigs.k8s.io/controller-runtime" |
| 18 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 19 | + |
| 20 | + "go.goms.io/fleet/pkg/propertyprovider/aks/trackers" |
| 21 | +) |
| 22 | + |
| 23 | +// TO-DO (chenyu1): this is a relatively expensive watcher, due to how frequent pods can change |
| 24 | +// in a Kubernetes cluster; unfortunately at this moment there does not seem to be a better way |
| 25 | +// to observe the changes of requested resources in a cluster. The alternative, which is to use |
| 26 | +// Lists, adds too much overhead to the API server. |
| 27 | + |
| 28 | +// PodReconciler reconciles Pod objects. |
| 29 | +type PodReconciler struct { |
| 30 | + PT *trackers.PodTracker |
| 31 | + Client client.Client |
| 32 | +} |
| 33 | + |
| 34 | +// Reconcile reconciles a pod object. |
| 35 | +func (p *PodReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { |
| 36 | + podRef := klog.KRef(req.Namespace, req.Name) |
| 37 | + startTime := time.Now() |
| 38 | + klog.V(2).InfoS("Reconciliation starts for pod objects in the AKS property provider", "pod", podRef) |
| 39 | + defer func() { |
| 40 | + latency := time.Since(startTime).Milliseconds() |
| 41 | + klog.V(2).InfoS("Reconciliation ends for pod objects in the AKS property provider", "pod", podRef, "latency", latency) |
| 42 | + }() |
| 43 | + |
| 44 | + // Retrieve the pod object. |
| 45 | + pod := &corev1.Pod{} |
| 46 | + if err := p.Client.Get(ctx, req.NamespacedName, pod); err != nil { |
| 47 | + // Failed to get the pod object. |
| 48 | + if errors.IsNotFound(err) { |
| 49 | + // This branch essentially processes the pod deletion event (the actual deletion). |
| 50 | + // At this point the pod may have not been tracked by the tracker at all; if that's |
| 51 | + // the case, the removal (untracking) operation is a no-op. |
| 52 | + // |
| 53 | + // Note that this controller will not add any finalizer to pod objects, so as to |
| 54 | + // avoid blocking normal Kuberneters operations under unexpected circumstances. |
| 55 | + p.PT.Remove(req.NamespacedName.String()) |
| 56 | + return ctrl.Result{}, nil |
| 57 | + } |
| 58 | + |
| 59 | + // For other errors, retry the reconciliation. |
| 60 | + klog.ErrorS(err, "Failed to get the pod object", "pod", podRef) |
| 61 | + return ctrl.Result{}, err |
| 62 | + } |
| 63 | + |
| 64 | + // Note that this controller will not untrack a pod when it is first marked for deletion; |
| 65 | + // instead, it performs the untracking when the pod object is actually gone from the |
| 66 | + // etcd store. This is intentional, as when a pod is marked for deletion, workloads might |
| 67 | + // not have been successfully terminated yet, and untracking the pod too early might lead to a |
| 68 | + // case of temporary inconsistency. |
| 69 | + |
| 70 | + // Track the pod if: |
| 71 | + // |
| 72 | + // * it is **NOT** of the Succeeded or Failed state; and |
| 73 | + // * it has been assigned to a node. |
| 74 | + // |
| 75 | + // This behavior is consistent with how the Kubernetes CLI tool reports requested capacity |
| 76 | + // on a specific node (`kubectl describe node` command). |
| 77 | + // |
| 78 | + // Note that the tracker will attempt to track the pod even if it has been marked for deletion. |
| 79 | + if len(pod.Spec.NodeName) > 0 && pod.Status.Phase != corev1.PodSucceeded && pod.Status.Phase != corev1.PodFailed { |
| 80 | + klog.V(2).InfoS("Attempt to track the pod", "pod", podRef) |
| 81 | + p.PT.AddOrUpdate(pod) |
| 82 | + } else { |
| 83 | + // Untrack the pod. |
| 84 | + // |
| 85 | + // It may have been descheduled, or transited into a terminal state. |
| 86 | + klog.V(2).InfoS("Untrack the pod", "pod", podRef) |
| 87 | + p.PT.Remove(req.NamespacedName.String()) |
| 88 | + } |
| 89 | + |
| 90 | + return ctrl.Result{}, nil |
| 91 | +} |
| 92 | + |
| 93 | +func (p *PodReconciler) SetupWithManager(mgr ctrl.Manager) error { |
| 94 | + // Reconcile any pod changes (create, update, delete). |
| 95 | + return ctrl.NewControllerManagedBy(mgr). |
| 96 | + For(&corev1.Pod{}). |
| 97 | + Complete(p) |
| 98 | +} |
0 commit comments