|
| 1 | +/* |
| 2 | +Copyright 2025. |
| 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 | +package main |
| 17 | + |
| 18 | +import ( |
| 19 | + "context" |
| 20 | + "fmt" |
| 21 | + "time" |
| 22 | + |
| 23 | + "github.com/go-logr/logr" |
| 24 | + "github.com/spf13/cobra" |
| 25 | + corev1 "k8s.io/api/core/v1" |
| 26 | + "k8s.io/apimachinery/pkg/fields" |
| 27 | + "k8s.io/apimachinery/pkg/types" |
| 28 | + "k8s.io/apimachinery/pkg/util/wait" |
| 29 | + "k8s.io/client-go/rest" |
| 30 | + ctrl "sigs.k8s.io/controller-runtime" |
| 31 | + "sigs.k8s.io/controller-runtime/pkg/cache" |
| 32 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 33 | + "sigs.k8s.io/controller-runtime/pkg/log" |
| 34 | + metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server" |
| 35 | + |
| 36 | + "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/consts" |
| 37 | + snolog "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/log" |
| 38 | + "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/utils" |
| 39 | +) |
| 40 | + |
| 41 | +var ( |
| 42 | + waitForConfigCmd = &cobra.Command{ |
| 43 | + Use: "wait-for-config", |
| 44 | + Short: "Wait for SR-IOV configuration to be applied", |
| 45 | + Long: "Init container command that sets annotation on pod and waits for " + |
| 46 | + "sriov-config-daemon to apply configuration and remove the annotation", |
| 47 | + RunE: runWaitForConfigCmd, |
| 48 | + } |
| 49 | + |
| 50 | + waitForConfigOpts struct { |
| 51 | + podName string |
| 52 | + podNamespace string |
| 53 | + } |
| 54 | +) |
| 55 | + |
| 56 | +func init() { |
| 57 | + rootCmd.AddCommand(waitForConfigCmd) |
| 58 | + waitForConfigCmd.PersistentFlags().StringVar(&waitForConfigOpts.podName, "pod-name", "", |
| 59 | + "kubernetes pod name of the device plugin") |
| 60 | + waitForConfigCmd.PersistentFlags().StringVar(&waitForConfigOpts.podNamespace, "pod-namespace", "", |
| 61 | + "kubernetes namespace where the device plugin pod is running") |
| 62 | +} |
| 63 | + |
| 64 | +type WaitForConfigReconciler struct { |
| 65 | + client.Client |
| 66 | + Pod types.NamespacedName |
| 67 | + Cancel context.CancelFunc |
| 68 | +} |
| 69 | + |
| 70 | +func (r *WaitForConfigReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { |
| 71 | + logger := log.FromContext(ctx) |
| 72 | + |
| 73 | + // This check is currently redundant since the cache is configured to watch only the target pod object. |
| 74 | + // However, it is intentionally included to document this assumption and to provide a safeguard in case |
| 75 | + // the cache configuration is modified in the future to watch additional pods. |
| 76 | + if r.Pod != req.NamespacedName { |
| 77 | + return ctrl.Result{}, nil |
| 78 | + } |
| 79 | + |
| 80 | + pod := &corev1.Pod{} |
| 81 | + if err := r.Get(ctx, req.NamespacedName, pod); err != nil { |
| 82 | + logger.Error(err, "Failed to get pod") |
| 83 | + return ctrl.Result{}, client.IgnoreNotFound(err) |
| 84 | + } |
| 85 | + |
| 86 | + if !utils.ObjectHasAnnotationKey(pod, consts.DevicePluginWaitConfigAnnotation) { |
| 87 | + logger.Info("Annotation removed, device plugin can proceed") |
| 88 | + r.Cancel() |
| 89 | + return ctrl.Result{}, nil |
| 90 | + } |
| 91 | + |
| 92 | + logger.Info("Annotation still present, waiting...") |
| 93 | + return ctrl.Result{RequeueAfter: consts.DaemonRequeueTime}, nil |
| 94 | +} |
| 95 | + |
| 96 | +func (r *WaitForConfigReconciler) SetupWithManager(mgr ctrl.Manager) error { |
| 97 | + return ctrl.NewControllerManagedBy(mgr). |
| 98 | + For(&corev1.Pod{}). |
| 99 | + Complete(r) |
| 100 | +} |
| 101 | + |
| 102 | +func validateWaitForConfigOpts() error { |
| 103 | + if waitForConfigOpts.podName == "" { |
| 104 | + return fmt.Errorf("--pod-name is required") |
| 105 | + } |
| 106 | + if waitForConfigOpts.podNamespace == "" { |
| 107 | + return fmt.Errorf("--pod-namespace is required") |
| 108 | + } |
| 109 | + return nil |
| 110 | +} |
| 111 | + |
| 112 | +func runWaitForConfigCmd(cmd *cobra.Command, args []string) error { |
| 113 | + snolog.InitLog() |
| 114 | + setupLog := log.Log.WithName("wait-for-config") |
| 115 | + |
| 116 | + if err := validateWaitForConfigOpts(); err != nil { |
| 117 | + setupLog.Error(err, "invalid command line arguments") |
| 118 | + return err |
| 119 | + } |
| 120 | + |
| 121 | + config, err := rest.InClusterConfig() |
| 122 | + if err != nil { |
| 123 | + setupLog.Error(err, "failed to get in-cluster config") |
| 124 | + return err |
| 125 | + } |
| 126 | + |
| 127 | + return startWaitForConfigManager(setupLog, config, types.NamespacedName{Name: waitForConfigOpts.podName, Namespace: waitForConfigOpts.podNamespace}) |
| 128 | +} |
| 129 | + |
| 130 | +func startWaitForConfigManager(setupLog logr.Logger, config *rest.Config, podName types.NamespacedName) error { |
| 131 | + ctx, cancel := context.WithCancel(ctrl.SetupSignalHandler()) |
| 132 | + defer cancel() |
| 133 | + |
| 134 | + setupLog.Info("Starting wait-for-config", "pod", podName) |
| 135 | + |
| 136 | + // Create a temporary client to set the annotation immediately |
| 137 | + tempClient, err := client.New(config, client.Options{}) |
| 138 | + if err != nil { |
| 139 | + setupLog.Error(err, "failed to create kubernetes client") |
| 140 | + return err |
| 141 | + } |
| 142 | + |
| 143 | + // Set annotation on pod to signal that we are waiting for config |
| 144 | + setupLog.Info("Setting annotation on pod", "annotation", consts.DevicePluginWaitConfigAnnotation) |
| 145 | + err = setAnnotationOnPod(ctx, setupLog, tempClient, podName) |
| 146 | + if err != nil { |
| 147 | + setupLog.Error(err, "failed to set annotation on pod") |
| 148 | + return err |
| 149 | + } |
| 150 | + setupLog.Info("Annotation set successfully, waiting for removal") |
| 151 | + |
| 152 | + // Configure Manager |
| 153 | + // Watch only specific pod object |
| 154 | + selector := fields.SelectorFromSet(fields.Set{"metadata.name": podName.Name}) |
| 155 | + mgr, err := ctrl.NewManager(config, ctrl.Options{ |
| 156 | + Metrics: metricsserver.Options{BindAddress: "0"}, |
| 157 | + Cache: cache.Options{ |
| 158 | + DefaultNamespaces: map[string]cache.Config{podName.Namespace: {}}, |
| 159 | + ByObject: map[client.Object]cache.ByObject{ |
| 160 | + &corev1.Pod{}: {Field: selector}, |
| 161 | + }, |
| 162 | + }, |
| 163 | + }) |
| 164 | + if err != nil { |
| 165 | + setupLog.Error(err, "unable to start manager") |
| 166 | + return err |
| 167 | + } |
| 168 | + |
| 169 | + if err = (&WaitForConfigReconciler{ |
| 170 | + Client: mgr.GetClient(), |
| 171 | + Pod: podName, |
| 172 | + Cancel: cancel, |
| 173 | + }).SetupWithManager(mgr); err != nil { |
| 174 | + setupLog.Error(err, "unable to create controller") |
| 175 | + return err |
| 176 | + } |
| 177 | + |
| 178 | + setupLog.Info("Starting manager") |
| 179 | + if err := mgr.Start(ctx); err != nil { |
| 180 | + setupLog.Error(err, "problem running manager") |
| 181 | + return err |
| 182 | + } |
| 183 | + return nil |
| 184 | +} |
| 185 | + |
| 186 | +// setAnnotationOnPod sets the wait-for-config annotation on the pod |
| 187 | +func setAnnotationOnPod(ctx context.Context, logger logr.Logger, c client.Client, podName types.NamespacedName) error { |
| 188 | + return wait.ExponentialBackoff(wait.Backoff{ |
| 189 | + Steps: 10, |
| 190 | + Duration: 1 * time.Second, |
| 191 | + Factor: 2.0, |
| 192 | + Jitter: 0.1, |
| 193 | + Cap: 30 * time.Second, |
| 194 | + }, func() (bool, error) { |
| 195 | + pod := &corev1.Pod{} |
| 196 | + if err := c.Get(ctx, podName, pod); err != nil { |
| 197 | + logger.Error(err, "failed to get pod, retrying") |
| 198 | + return false, nil |
| 199 | + } |
| 200 | + if err := utils.AnnotateObject(ctx, pod, consts.DevicePluginWaitConfigAnnotation, "true", c); err != nil { |
| 201 | + logger.Error(err, "failed to annotate pod, retrying") |
| 202 | + return false, nil |
| 203 | + } |
| 204 | + return true, nil |
| 205 | + }) |
| 206 | +} |
0 commit comments