From 776c35c1e1ea08d5202fce6620ff440a1bdb1453 Mon Sep 17 00:00:00 2001 From: Christian Kotzbauer Date: Wed, 2 Aug 2023 19:41:29 +0200 Subject: [PATCH 1/2] cleanup: use Background context Signed-off-by: Christian Kotzbauer --- cmd/kured/main.go | 14 +++++++------- pkg/daemonsetlock/daemonsetlock.go | 4 ++-- pkg/taints/taints.go | 4 ++-- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/cmd/kured/main.go b/cmd/kured/main.go index 69d55a019..521a3e23f 100644 --- a/cmd/kured/main.go +++ b/cmd/kured/main.go @@ -395,7 +395,7 @@ func (pb PrometheusBlockingChecker) isBlocked() bool { func (kb KubernetesBlockingChecker) isBlocked() bool { fieldSelector := fmt.Sprintf("spec.nodeName=%s,status.phase!=Succeeded,status.phase!=Failed,status.phase!=Unknown", kb.nodename) for _, labelSelector := range kb.filter { - podList, err := kb.client.CoreV1().Pods("").List(context.TODO(), metav1.ListOptions{ + podList, err := kb.client.CoreV1().Pods("").List(context.Background(), metav1.ListOptions{ LabelSelector: labelSelector, FieldSelector: fieldSelector, Limit: 10}) @@ -580,7 +580,7 @@ type nodeMeta struct { } func addNodeAnnotations(client *kubernetes.Clientset, nodeID string, annotations map[string]string) error { - node, err := client.CoreV1().Nodes().Get(context.TODO(), nodeID, metav1.GetOptions{}) + node, err := client.CoreV1().Nodes().Get(context.Background(), nodeID, metav1.GetOptions{}) if err != nil { log.Errorf("Error retrieving node object via k8s API: %s", err) return err @@ -596,7 +596,7 @@ func addNodeAnnotations(client *kubernetes.Clientset, nodeID string, annotations return err } - _, err = client.CoreV1().Nodes().Patch(context.TODO(), node.GetName(), types.StrategicMergePatchType, bytes, metav1.PatchOptions{}) + _, err = client.CoreV1().Nodes().Patch(context.Background(), node.GetName(), types.StrategicMergePatchType, bytes, metav1.PatchOptions{}) if err != nil { var annotationsErr string for k, v := range annotations { @@ -615,7 +615,7 @@ func deleteNodeAnnotation(client *kubernetes.Clientset, nodeID, key string) erro // So we replace all instances of "/" with "~1" as per: // https://tools.ietf.org/html/rfc6901#section-3 patch := []byte(fmt.Sprintf("[{\"op\":\"remove\",\"path\":\"/metadata/annotations/%s\"}]", strings.ReplaceAll(key, "/", "~1"))) - _, err := client.CoreV1().Nodes().Patch(context.TODO(), nodeID, types.JSONPatchType, patch, metav1.PatchOptions{}) + _, err := client.CoreV1().Nodes().Patch(context.Background(), nodeID, types.JSONPatchType, patch, metav1.PatchOptions{}) if err != nil { log.Errorf("Error deleting node annotation %s via k8s API: %v", key, err) return err @@ -641,7 +641,7 @@ func updateNodeLabels(client *kubernetes.Clientset, node *v1.Node, labels []stri log.Fatalf("Error marshalling node object into JSON: %v", err) } - _, err = client.CoreV1().Nodes().Patch(context.TODO(), node.GetName(), types.StrategicMergePatchType, bytes, metav1.PatchOptions{}) + _, err = client.CoreV1().Nodes().Patch(context.Background(), node.GetName(), types.StrategicMergePatchType, bytes, metav1.PatchOptions{}) if err != nil { var labelsErr string for _, label := range labels { @@ -671,7 +671,7 @@ func rebootAsRequired(nodeID string, rebootCommand []string, sentinelCommand []s tick := delaytick.New(source, 1*time.Minute) for range tick { if holding(lock, &nodeMeta, concurrency > 1) { - node, err := client.CoreV1().Nodes().Get(context.TODO(), nodeID, metav1.GetOptions{}) + node, err := client.CoreV1().Nodes().Get(context.Background(), nodeID, metav1.GetOptions{}) if err != nil { log.Errorf("Error retrieving node object via k8s API: %v", err) continue @@ -739,7 +739,7 @@ func rebootAsRequired(nodeID string, rebootCommand []string, sentinelCommand []s } log.Infof("Reboot required") - node, err := client.CoreV1().Nodes().Get(context.TODO(), nodeID, metav1.GetOptions{}) + node, err := client.CoreV1().Nodes().Get(context.Background(), nodeID, metav1.GetOptions{}) if err != nil { log.Fatalf("Error retrieving node object via k8s API: %v", err) } diff --git a/pkg/daemonsetlock/daemonsetlock.go b/pkg/daemonsetlock/daemonsetlock.go index 2d8949b5a..fa189adfa 100644 --- a/pkg/daemonsetlock/daemonsetlock.go +++ b/pkg/daemonsetlock/daemonsetlock.go @@ -75,7 +75,7 @@ func (dsl *DaemonSetLock) Acquire(metadata interface{}, TTL time.Duration) (bool } ds.ObjectMeta.Annotations[dsl.annotation] = string(valueBytes) - _, err = dsl.client.AppsV1().DaemonSets(dsl.namespace).Update(context.TODO(), ds, metav1.UpdateOptions{}) + _, err = dsl.client.AppsV1().DaemonSets(dsl.namespace).Update(context.Background(), ds, metav1.UpdateOptions{}) if err != nil { if se, ok := err.(*errors.StatusError); ok && se.ErrStatus.Reason == metav1.StatusReasonConflict { // Something else updated the resource between us reading and writing - try again soon @@ -245,7 +245,7 @@ func (dsl *DaemonSetLock) Release() error { delete(ds.ObjectMeta.Annotations, dsl.annotation) - _, err = dsl.client.AppsV1().DaemonSets(dsl.namespace).Update(context.TODO(), ds, metav1.UpdateOptions{}) + _, err = dsl.client.AppsV1().DaemonSets(dsl.namespace).Update(context.Background(), ds, metav1.UpdateOptions{}) if err != nil { if se, ok := err.(*errors.StatusError); ok && se.ErrStatus.Reason == metav1.StatusReasonConflict { // Something else updated the resource between us reading and writing - try again soon diff --git a/pkg/taints/taints.go b/pkg/taints/taints.go index a9592e90c..ac6c20369 100644 --- a/pkg/taints/taints.go +++ b/pkg/taints/taints.go @@ -65,7 +65,7 @@ func (t *Taint) Disable() { } func taintExists(client *kubernetes.Clientset, nodeID, taintName string) (bool, int, *v1.Node) { - updatedNode, err := client.CoreV1().Nodes().Get(context.TODO(), nodeID, metav1.GetOptions{}) + updatedNode, err := client.CoreV1().Nodes().Get(context.Background(), nodeID, metav1.GetOptions{}) if err != nil || updatedNode == nil { log.Fatalf("Error reading node %s: %v", nodeID, err) } @@ -153,7 +153,7 @@ func preferNoSchedule(client *kubernetes.Clientset, nodeID, taintName string, ef log.Fatalf("Error encoding taint patch for node %s: %v", nodeID, err) } - _, err = client.CoreV1().Nodes().Patch(context.TODO(), nodeID, types.JSONPatchType, patchBytes, metav1.PatchOptions{}) + _, err = client.CoreV1().Nodes().Patch(context.Background(), nodeID, types.JSONPatchType, patchBytes, metav1.PatchOptions{}) if err != nil { log.Fatalf("Error patching taint for node %s: %v", nodeID, err) } From 59cbea5e25e62131bd3a6650956ceb87038db3b2 Mon Sep 17 00:00:00 2001 From: Christian Kotzbauer Date: Mon, 14 Aug 2023 19:08:44 +0200 Subject: [PATCH 2/2] feat: add another background Signed-off-by: Christian Kotzbauer --- pkg/daemonsetlock/daemonsetlock.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/daemonsetlock/daemonsetlock.go b/pkg/daemonsetlock/daemonsetlock.go index fa189adfa..b9bbf525e 100644 --- a/pkg/daemonsetlock/daemonsetlock.go +++ b/pkg/daemonsetlock/daemonsetlock.go @@ -294,7 +294,7 @@ func (dsl *DaemonSetLock) ReleaseMultiple() error { } ds.ObjectMeta.Annotations[dsl.annotation] = string(newAnnotationBytes) - _, err = dsl.client.AppsV1().DaemonSets(dsl.namespace).Update(context.TODO(), ds, metav1.UpdateOptions{}) + _, err = dsl.client.AppsV1().DaemonSets(dsl.namespace).Update(context.Background(), ds, metav1.UpdateOptions{}) if err != nil { if se, ok := err.(*errors.StatusError); ok && se.ErrStatus.Reason == metav1.StatusReasonConflict { // Something else updated the resource between us reading and writing - try again soon