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

Fixes node label error handling #1892

Merged
merged 15 commits into from
Mar 8, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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 charts/aws-vpc-cni/templates/clusterrole.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ rules:
- apiGroups: [""]
resources:
- nodes
verbs: ["list", "watch", "get", "update"]
verbs: ["list", "watch", "get", "update", "patch"]
- apiGroups: ["extensions"]
resources:
- '*'
Expand Down
2 changes: 1 addition & 1 deletion config/master/aws-k8s-cni-cn.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ rules:
- apiGroups: [""]
resources:
- nodes
verbs: ["list", "watch", "get", "update"]
verbs: ["list", "watch", "get", "update", "patch"]
- apiGroups: ["extensions"]
resources:
- '*'
Expand Down
2 changes: 1 addition & 1 deletion config/master/aws-k8s-cni-us-gov-east-1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ rules:
- apiGroups: [""]
resources:
- nodes
verbs: ["list", "watch", "get", "update"]
verbs: ["list", "watch", "get", "update", "patch"]
- apiGroups: ["extensions"]
resources:
- '*'
Expand Down
2 changes: 1 addition & 1 deletion config/master/aws-k8s-cni-us-gov-west-1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ rules:
- apiGroups: [""]
resources:
- nodes
verbs: ["list", "watch", "get", "update"]
verbs: ["list", "watch", "get", "update", "patch"]
- apiGroups: ["extensions"]
resources:
- '*'
Expand Down
2 changes: 1 addition & 1 deletion config/master/aws-k8s-cni.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ rules:
- apiGroups: [""]
resources:
- nodes
verbs: ["list", "watch", "get", "update"]
verbs: ["list", "watch", "get", "update", "patch"]
- apiGroups: ["extensions"]
resources:
- '*'
Expand Down
65 changes: 35 additions & 30 deletions pkg/ipamd/ipamd.go
Original file line number Diff line number Diff line change
Expand Up @@ -1850,41 +1850,46 @@ func (c *IPAMContext) getTrunkLinkIndex() (int, error) {

// SetNodeLabel sets or deletes a node label
func (c *IPAMContext) SetNodeLabel(ctx context.Context, key, value string) error {
var node corev1.Node
// Find my node
err := c.cachedK8SClient.Get(ctx, types.NamespacedName{Name: c.myNodeName}, &node)
log.Debugf("Node found %q - labels - %q", node.Name, len(node.Labels))

if err != nil {
log.Errorf("Failed to get node: %v", err)
return err
request := types.NamespacedName{
Name: c.myNodeName,
}

if labelValue, ok := node.Labels[key]; ok && labelValue == value {
log.Debugf("Node label %q is already %q", key, labelValue)
return nil
}
err := retry.RetryOnConflict(retry.DefaultBackoff, func() error {
node := &corev1.Node{}
// Find my node
err := c.cachedK8SClient.Get(ctx, request, node)
log.Debugf("Node found %q - labels - %q", node.Name, len(node.Labels))

// Make deep copy for modification
updateNode := node.DeepCopy()
if err != nil {
log.Errorf("Failed to get node: %v", err)
return err
}

// Set node label
if value != "" {
updateNode.Labels[key] = value
} else {
// Empty value, delete the label
log.Debugf("Deleting label %q", key)
delete(updateNode.Labels, key)
}
if labelValue, ok := node.Labels[key]; ok && labelValue == value {
log.Debugf("Node label %q is already %q", key, labelValue)
return nil
}

// Update node status to advertise the resource.
err = c.cachedK8SClient.Update(ctx, updateNode)
if err != nil {
log.Errorf("Failed to update node %s with label %q: %q, error: %v", c.myNodeName, key, value, err)
}
log.Debugf("Updated node %s with label %q: %q", c.myNodeName, key, value)
// Make deep copy for modification
updateNode := node.DeepCopy()

return nil
// Set node label
if value != "" {
updateNode.Labels[key] = value
} else {
// Empty value, delete the label
log.Debugf("Deleting label %q", key)
delete(updateNode.Labels, key)
}

if err = c.rawK8SClient.Patch(ctx, updateNode, client.MergeFrom(node)); err != nil {
log.Errorf("Failed to patch node %s with label %q: %q, error: %v", c.myNodeName, key, value, err)
return err
}
log.Debugf("Updated node %s with label %q: %q", c.myNodeName, key, value)
return nil
})
return err
}

// GetPod returns the pod matching the name and namespace
Expand All @@ -1906,10 +1911,10 @@ func (c *IPAMContext) GetPod(podName, namespace string) (*corev1.Pod, error) {
// AnnotatePod annotates the pod with the provided key and value
func (c *IPAMContext) AnnotatePod(podNamespace, podName, key, val string) error {
ctx := context.TODO()
var pod *corev1.Pod
var err error

err = retry.RetryOnConflict(retry.DefaultBackoff, func() error {
pod := &corev1.Pod{}
if pod, err = c.GetPod(podNamespace, podName); err != nil {
return err
}
Expand Down