removeLabel will remove a node label given a label key
(nodeName string, key string)
| 345 | |
| 346 | // removeLabel will remove a node label given a label key |
| 347 | func (n Node) removeLabel(nodeName string, key string) error { |
| 348 | type patchRequest struct { |
| 349 | Op string `json:"op"` |
| 350 | Path string `json:"path"` |
| 351 | } |
| 352 | |
| 353 | var patchReqs []interface{} |
| 354 | patchRemove := patchRequest{ |
| 355 | Op: "remove", |
| 356 | Path: fmt.Sprintf("/metadata/labels/%s", jsonPatchEscape(key)), |
| 357 | } |
| 358 | payload, err := json.Marshal(append(patchReqs, patchRemove)) |
| 359 | if err != nil { |
| 360 | return fmt.Errorf("an error occurred while marshalling the json to remove a label from the node: %w", err) |
| 361 | } |
| 362 | node, err := n.fetchKubernetesNode(nodeName) |
| 363 | if err != nil { |
| 364 | return err |
| 365 | } |
| 366 | if n.nthConfig.DryRun { |
| 367 | log.Info().Msgf("Would have removed label with key %s from node %s, but dry-run flag was set", key, nodeName) |
| 368 | return nil |
| 369 | } |
| 370 | _, err = n.drainHelper.Client.CoreV1().Nodes().Patch(context.TODO(), node.Name, types.JSONPatchType, payload, metav1.PatchOptions{}) |
| 371 | if err != nil { |
| 372 | return fmt.Errorf("%v node patch failed when removing a label from the node: %w", node.Name, err) |
| 373 | } |
| 374 | return nil |
| 375 | } |
| 376 | |
| 377 | // removeLabelIfValueMatches will remove a node label given a label key provided the label's value equals matchValue |
| 378 | func (n Node) removeLabelIfValueMatches(nodeName string, key string, matchValue string) error { |
no test coverage detected