|
| 1 | +/* |
| 2 | +Copyright 2024 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package secretsstore |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "encoding/json" |
| 22 | + "os" |
| 23 | + "time" |
| 24 | + |
| 25 | + corev1 "k8s.io/api/core/v1" |
| 26 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 27 | + k8stypes "k8s.io/apimachinery/pkg/types" |
| 28 | + "k8s.io/apimachinery/pkg/util/wait" |
| 29 | + "k8s.io/client-go/kubernetes" |
| 30 | + "k8s.io/client-go/rest" |
| 31 | + "k8s.io/klog/v2" |
| 32 | +) |
| 33 | + |
| 34 | +// AgentNotReadyNodeTaintKey is the taint key the driver removes from its local |
| 35 | +// node once it has started. Operators (for example EKS Managed Node Groups) can |
| 36 | +// apply this taint to a node so that workload pods are not scheduled there until |
| 37 | +// the secrets-store CSI driver pod is running, avoiding the startup race where a |
| 38 | +// pod mounts a SecretProviderClass volume before the driver is ready. |
| 39 | +const AgentNotReadyNodeTaintKey = "secrets-store.csi.k8s.io/agent-not-ready" |
| 40 | + |
| 41 | +// nodeNameEnvVar is the environment variable the DaemonSet populates from |
| 42 | +// spec.nodeName (also consumed as --nodeid). It identifies the local node whose |
| 43 | +// taint should be removed. |
| 44 | +const nodeNameEnvVar = "KUBE_NODE_NAME" |
| 45 | + |
| 46 | +// kubernetesClientGetter lazily builds a Kubernetes clientset. It is a function |
| 47 | +// so tests can inject a fake clientset and exercise the failure path. |
| 48 | +type kubernetesClientGetter func() (kubernetes.Interface, error) |
| 49 | + |
| 50 | +// inClusterClientGetter builds a clientset from the in-cluster config. It is the |
| 51 | +// production implementation passed to removeTaintInBackground. |
| 52 | +var inClusterClientGetter kubernetesClientGetter = func() (kubernetes.Interface, error) { |
| 53 | + config, err := rest.InClusterConfig() |
| 54 | + if err != nil { |
| 55 | + return nil, err |
| 56 | + } |
| 57 | + return kubernetes.NewForConfig(config) |
| 58 | +} |
| 59 | + |
| 60 | +// taintRemovalBackoff is the exponential backoff for node taint removal retries. |
| 61 | +// Max delay across the steps is 0.5s * 2^9 = ~4 minutes. |
| 62 | +var taintRemovalBackoff = wait.Backoff{ |
| 63 | + Duration: 500 * time.Millisecond, |
| 64 | + Factor: 2, |
| 65 | + Steps: 10, |
| 66 | +} |
| 67 | + |
| 68 | +// jsonPatch is a single RFC 6902 JSON patch operation. |
| 69 | +type jsonPatch struct { |
| 70 | + OP string `json:"op,omitempty"` |
| 71 | + Path string `json:"path,omitempty"` |
| 72 | + Value interface{} `json:"value"` |
| 73 | +} |
| 74 | + |
| 75 | +// RemoveNotReadyTaintInBackground removes the node startup taint |
| 76 | +// (AgentNotReadyNodeTaintKey) from the local node in a background goroutine, |
| 77 | +// retrying with exponential backoff. It is the production entry point and is safe |
| 78 | +// to call unconditionally: it no-ops on clusters that never apply the taint. |
| 79 | +func RemoveNotReadyTaintInBackground() { |
| 80 | + go removeTaintInBackground(inClusterClientGetter, removeNotReadyTaint) |
| 81 | +} |
| 82 | + |
| 83 | +// removeTaintInBackground retries removalFunc with exponential backoff until it |
| 84 | +// succeeds or the backoff is exhausted. It is intended to be run as a goroutine |
| 85 | +// so driver startup is not blocked on the Kubernetes API being reachable. |
| 86 | +func removeTaintInBackground(clientGetter kubernetesClientGetter, removalFunc func(kubernetesClientGetter) error) { |
| 87 | + backoffErr := wait.ExponentialBackoff(taintRemovalBackoff, func() (bool, error) { |
| 88 | + if err := removalFunc(clientGetter); err != nil { |
| 89 | + klog.ErrorS(err, "Unexpected failure when attempting to remove node taint(s)") |
| 90 | + return false, nil |
| 91 | + } |
| 92 | + return true, nil |
| 93 | + }) |
| 94 | + if backoffErr != nil { |
| 95 | + klog.ErrorS(backoffErr, "Retries exhausted, giving up attempting to remove node taint(s)") |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +// removeNotReadyTaint removes the AgentNotReadyNodeTaintKey taint from the local |
| 100 | +// node. It is a no-op (returns nil) when the node name is unknown or the client |
| 101 | +// cannot be built, so a cluster that never applies the taint is unaffected. It |
| 102 | +// returns an error only on transient API failures so the caller can retry. |
| 103 | +func removeNotReadyTaint(clientGetter kubernetesClientGetter) error { |
| 104 | + nodeName := os.Getenv(nodeNameEnvVar) |
| 105 | + if nodeName == "" { |
| 106 | + klog.V(4).InfoS("node name env var missing, skipping taint removal", "envVar", nodeNameEnvVar) |
| 107 | + return nil |
| 108 | + } |
| 109 | + |
| 110 | + clientset, err := clientGetter() |
| 111 | + if err != nil { |
| 112 | + klog.V(4).InfoS("failed to setup k8s client, skipping taint removal", "error", err) |
| 113 | + return nil |
| 114 | + } |
| 115 | + |
| 116 | + node, err := clientset.CoreV1().Nodes().Get(context.Background(), nodeName, metav1.GetOptions{}) |
| 117 | + if err != nil { |
| 118 | + return err |
| 119 | + } |
| 120 | + |
| 121 | + taintsToKeep := make([]corev1.Taint, 0, len(node.Spec.Taints)) |
| 122 | + for _, taint := range node.Spec.Taints { |
| 123 | + if taint.Key != AgentNotReadyNodeTaintKey { |
| 124 | + taintsToKeep = append(taintsToKeep, taint) |
| 125 | + } else { |
| 126 | + klog.V(4).InfoS("queued taint for removal", "key", taint.Key, "effect", taint.Effect) |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + if len(taintsToKeep) == len(node.Spec.Taints) { |
| 131 | + klog.V(4).InfoS("no taints to remove on node, skipping taint removal", "node", nodeName) |
| 132 | + return nil |
| 133 | + } |
| 134 | + |
| 135 | + // Use a test-and-replace patch so we never clobber a concurrent update to |
| 136 | + // the node's taints: if spec.taints changed since the Get, the test op fails |
| 137 | + // and the patch is rejected, and the backoff retry re-reads the node. |
| 138 | + patchRemoveTaints := []jsonPatch{ |
| 139 | + { |
| 140 | + OP: "test", |
| 141 | + Path: "/spec/taints", |
| 142 | + Value: node.Spec.Taints, |
| 143 | + }, |
| 144 | + { |
| 145 | + OP: "replace", |
| 146 | + Path: "/spec/taints", |
| 147 | + Value: taintsToKeep, |
| 148 | + }, |
| 149 | + } |
| 150 | + |
| 151 | + patch, err := json.Marshal(patchRemoveTaints) |
| 152 | + if err != nil { |
| 153 | + return err |
| 154 | + } |
| 155 | + |
| 156 | + if _, err := clientset.CoreV1().Nodes().Patch(context.Background(), nodeName, k8stypes.JSONPatchType, patch, metav1.PatchOptions{}); err != nil { |
| 157 | + return err |
| 158 | + } |
| 159 | + |
| 160 | + klog.InfoS("removed taint(s) from local node", "node", nodeName, "taintKey", AgentNotReadyNodeTaintKey) |
| 161 | + return nil |
| 162 | +} |
0 commit comments