Skip to content

Commit c3fee23

Browse files
authored
Merge pull request #112 from NVIDIA/use-cordon-uncordon-helper
reuse the cordonUncordon helper method from k8s.io/kubectl/pkg/drain
2 parents 6686f22 + 64e1a86 commit c3fee23

File tree

1 file changed

+22
-22
lines changed

1 file changed

+22
-22
lines changed

internal/kubernetes/client.go

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ const (
3838
nvidiaDomainPrefix = "nvidia.com"
3939
nvidiaResourceNamePrefix = nvidiaDomainPrefix + "/" + "gpu"
4040
nvidiaMigResourcePrefix = nvidiaDomainPrefix + "/" + "mig-"
41+
42+
nodeOperationCordon = "cordon"
43+
nodeOperationUncordon = "uncordon"
4144
)
4245

4346
// Client represents a Kubernetes client wrapper use to perform all the Kubernetes operations required by k8s-driver-manager
@@ -129,42 +132,39 @@ func (c *Client) GetNodeAnnotationValue(nodeName, annotation string) (string, er
129132
// CordonNode cordons a Node given a Node name marking it as Unschedulable
130133
func (c *Client) CordonNode(nodeName string) error {
131134
c.log.Infof("Cordoning node %s", nodeName)
132-
133-
// Get the node
134-
node, err := c.clientset.CoreV1().Nodes().Get(c.ctx, nodeName, metav1.GetOptions{})
135-
if err != nil {
136-
return fmt.Errorf("failed to get node %s: %w", nodeName, err)
137-
}
138-
139-
// Set the unschedulable flag
140-
node.Spec.Unschedulable = true
141-
142-
// Update the node
143-
_, err = c.clientset.CoreV1().Nodes().Update(c.ctx, node, metav1.UpdateOptions{})
144-
if err != nil {
145-
return fmt.Errorf("failed to cordon node %s: %w", nodeName, err)
146-
}
147-
148-
return nil
135+
return c.cordonOrUncordon(nodeName, nodeOperationCordon)
149136
}
150137

151138
// UncordonNode uncordons a Node given a Node name marking it as Schedulable
152139
func (c *Client) UncordonNode(nodeName string) error {
153140
c.log.Infof("Uncordoning node %s", nodeName)
141+
return c.cordonOrUncordon(nodeName, nodeOperationUncordon)
142+
}
154143

144+
func (c *Client) cordonOrUncordon(nodeName string, operation string) error {
155145
// Get the node
156146
node, err := c.clientset.CoreV1().Nodes().Get(c.ctx, nodeName, metav1.GetOptions{})
157147
if err != nil {
158148
return fmt.Errorf("failed to get node %s: %w", nodeName, err)
159149
}
160150

161-
// Clear the unschedulable flag
162-
node.Spec.Unschedulable = false
151+
switch operation {
152+
case nodeOperationCordon:
153+
node.Spec.Unschedulable = true
154+
case nodeOperationUncordon:
155+
node.Spec.Unschedulable = false
156+
default:
157+
// this should never get executed
158+
panic(fmt.Errorf("unknown operation %q", operation))
159+
}
163160

164-
// Update the node
165-
_, err = c.clientset.CoreV1().Nodes().Update(c.ctx, node, metav1.UpdateOptions{})
161+
drainHelper := &drain.Helper{
162+
Ctx: c.ctx,
163+
Client: c.clientset,
164+
}
165+
err = drain.RunCordonOrUncordon(drainHelper, node, true)
166166
if err != nil {
167-
return fmt.Errorf("failed to uncordon node %s: %w", nodeName, err)
167+
return fmt.Errorf("failed to perform %s of node %s: %w", operation, nodeName, err)
168168
}
169169

170170
return nil

0 commit comments

Comments
 (0)