removeLabelIfValueMatches will remove a node label given a label key provided the label's value equals matchValue
(nodeName string, key string, matchValue string)
| 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 { |
| 379 | type patchRequest struct { |
| 380 | Op string `json:"op"` |
| 381 | Path string `json:"path"` |
| 382 | } |
| 383 | |
| 384 | var patchReqs []interface{} |
| 385 | patchRemove := patchRequest{ |
| 386 | Op: "remove", |
| 387 | Path: fmt.Sprintf("/metadata/labels/%s", jsonPatchEscape(key)), |
| 388 | } |
| 389 | payload, err := json.Marshal(append(patchReqs, patchRemove)) |
| 390 | if err != nil { |
| 391 | return fmt.Errorf("an error occurred while marshalling the json to remove a label from the node: %w", err) |
| 392 | } |
| 393 | node, err := n.fetchKubernetesNode(nodeName) |
| 394 | if err != nil { |
| 395 | return err |
| 396 | } |
| 397 | val, ok := node.Labels[key] |
| 398 | if !ok || val == matchValue { |
| 399 | return nil |
| 400 | } |
| 401 | if n.nthConfig.DryRun { |
| 402 | log.Info().Msgf("Would have removed label with key %s from node %s, but dry-run flag was set", key, nodeName) |
| 403 | return nil |
| 404 | } |
| 405 | _, err = n.drainHelper.Client.CoreV1().Nodes().Patch(context.TODO(), node.Name, types.JSONPatchType, payload, metav1.PatchOptions{}) |
| 406 | if err != nil { |
| 407 | return fmt.Errorf("%v node patch failed when removing a label from the node: %w", node.Name, err) |
| 408 | } |
| 409 | return nil |
| 410 | } |
| 411 | |
| 412 | // GetNodeLabels will fetch node labels for a given nodeName |
| 413 | func (n Node) GetNodeLabels(nodeName string) (map[string]string, error) { |
no test coverage detected