Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add context to k8s client functions #95

Merged
merged 2 commits into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/controllers/housekeeping/housekeeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func (h *Housekeeper) watchNodes() {

klog.Info("node was modified and ip address has changed, updating metallb config")

nodes, err := kubernetes.GetNodes(h.k8sClient)
nodes, err := kubernetes.GetNodes(context.Background(), h.k8sClient)
if err != nil {
klog.Errorf("error listing nodes: %v", err)
return
Expand Down
2 changes: 1 addition & 1 deletion pkg/controllers/housekeeping/metallb.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func (h *Housekeeper) updateMetalLBConfig() error {
if time.Since(h.lastMetalLBConfigSync) < syncMetalLBMinimalInternval {
return nil
}
nodes, err := kubernetes.GetNodes(h.k8sClient)
nodes, err := kubernetes.GetNodes(context.Background(), h.k8sClient)
if err != nil {
return fmt.Errorf("error listing nodes: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/controllers/housekeeping/sshkeys.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (h *Housekeeper) startSSHKeysSynching() {
func (h *Housekeeper) syncSSHKeys() error {
klog.Info("start syncing ssh public keys to machine")

nodes, err := kubernetes.GetNodes(h.k8sClient)
nodes, err := kubernetes.GetNodes(context.Background(), h.k8sClient)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/controllers/housekeeping/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (h *Housekeeper) startTagSynching() {
func (h *Housekeeper) syncMachineTagsToNodeLabels() error {
klog.Info("start syncing machine tags to node labels")

nodes, err := kubernetes.GetNodes(h.k8sClient)
nodes, err := kubernetes.GetNodes(context.Background(), h.k8sClient)
if err != nil {
return err
}
Expand All @@ -55,7 +55,7 @@ func (h *Housekeeper) syncMachineTagsToNodeLabels() error {
continue
}
labels := h.buildLabelsFromMachineTags(tags)
err := kubernetes.UpdateNodeLabelsWithBackoff(h.k8sClient, n.Name, labels, updateNodeSpecBackoff)
err := kubernetes.UpdateNodeLabelsWithBackoff(context.Background(), h.k8sClient, n.Name, labels, updateNodeSpecBackoff)
if err != nil {
klog.Warningf("tags syncher failed to update tags on node:%s: %v", nodeName, err)
continue
Expand Down
10 changes: 5 additions & 5 deletions pkg/resources/kubernetes/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@ import (
)

// GetNodes returns all nodes of this cluster.
func GetNodes(client clientset.Interface) ([]v1.Node, error) {
nodes, err := client.CoreV1().Nodes().List(context.Background(), metav1.ListOptions{})
func GetNodes(ctx context.Context, client clientset.Interface) ([]v1.Node, error) {
nodes, err := client.CoreV1().Nodes().List(ctx, metav1.ListOptions{})
if err != nil {
return nil, fmt.Errorf("failed to list nodes: %w", err)
}
return nodes.Items, nil
}

// UpdateNodeLabelsWithBackoff updates labels on a given node with a given backoff retry.
func UpdateNodeLabelsWithBackoff(client clientset.Interface, nodeName string, labels map[string]string, backoff wait.Backoff) error {
func UpdateNodeLabelsWithBackoff(ctx context.Context, client clientset.Interface, nodeName string, labels map[string]string, backoff wait.Backoff) error {
return retry.RetryOnConflict(backoff, func() error {

node, err := client.CoreV1().Nodes().Get(context.Background(), nodeName, metav1.GetOptions{})
node, err := client.CoreV1().Nodes().Get(ctx, nodeName, metav1.GetOptions{})
if err != nil {
return err
}
Expand All @@ -36,7 +36,7 @@ func UpdateNodeLabelsWithBackoff(client clientset.Interface, nodeName string, la
node.Labels[key] = value
}

_, err = client.CoreV1().Nodes().Update(context.Background(), node, metav1.UpdateOptions{})
_, err = client.CoreV1().Nodes().Update(ctx, node, metav1.UpdateOptions{})
return err
})
}
Expand Down