-
Notifications
You must be signed in to change notification settings - Fork 207
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract operations into internal pkg
This reduces the global variables, and regroups all the operations in a single place. This will allow further refactor to represent all the k8s operations kured needs on a single node. Signed-off-by: Jean-Philippe Evrard <[email protected]>
- Loading branch information
Showing
5 changed files
with
241 additions
and
208 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package k8soperations | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/types" | ||
"k8s.io/client-go/kubernetes" | ||
"log/slog" | ||
"strings" | ||
) | ||
|
||
const ( | ||
KuredNodeWasUnschedulableBeforeDrainAnnotation string = "kured.dev/node-unschedulable-before-drain" | ||
) | ||
|
||
func AddNodeAnnotations(client *kubernetes.Clientset, nodeID string, annotations map[string]string) error { | ||
node, err := client.CoreV1().Nodes().Get(context.TODO(), nodeID, metav1.GetOptions{}) | ||
if err != nil { | ||
return fmt.Errorf("error retrieving node object via k8s API: %v", err) | ||
} | ||
for k, v := range annotations { | ||
node.Annotations[k] = v | ||
slog.Debug(fmt.Sprintf("adding node annotation: %s=%s", k, v), "node", node.GetName()) | ||
} | ||
|
||
bytes, err := json.Marshal(node) | ||
if err != nil { | ||
return fmt.Errorf("error marshalling node object into JSON: %v", err) | ||
} | ||
|
||
_, err = client.CoreV1().Nodes().Patch(context.TODO(), node.GetName(), types.StrategicMergePatchType, bytes, metav1.PatchOptions{}) | ||
if err != nil { | ||
var annotationsErr string | ||
for k, v := range annotations { | ||
annotationsErr += fmt.Sprintf("%s=%s ", k, v) | ||
} | ||
return fmt.Errorf("error adding node annotations %s via k8s API: %v", annotationsErr, err) | ||
} | ||
return nil | ||
} | ||
|
||
func DeleteNodeAnnotation(client *kubernetes.Clientset, nodeID, key string) error { | ||
// JSON Patch takes as path input a JSON Pointer, defined in RFC6901 | ||
// So we replace all instances of "/" with "~1" as per: | ||
// https://tools.ietf.org/html/rfc6901#section-3 | ||
patch := []byte(fmt.Sprintf("[{\"op\":\"remove\",\"path\":\"/metadata/annotations/%s\"}]", strings.ReplaceAll(key, "/", "~1"))) | ||
_, err := client.CoreV1().Nodes().Patch(context.TODO(), nodeID, types.JSONPatchType, patch, metav1.PatchOptions{}) | ||
if err != nil { | ||
return fmt.Errorf("error deleting node annotation %s via k8s API: %v", key, err) | ||
} | ||
return nil | ||
} | ||
|
||
func updateNodeLabels(client *kubernetes.Clientset, node *v1.Node, labels []string) error { | ||
labelsMap := make(map[string]string) | ||
for _, label := range labels { | ||
k := strings.Split(label, "=")[0] | ||
v := strings.Split(label, "=")[1] | ||
labelsMap[k] = v | ||
slog.Debug(fmt.Sprintf("Updating node %s label: %s=%s", node.GetName(), k, v), "node", node.GetName()) | ||
} | ||
|
||
bytes, err := json.Marshal(map[string]interface{}{ | ||
"metadata": map[string]interface{}{ | ||
"labels": labelsMap, | ||
}, | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("error marshalling node object into JSON: %v", err) | ||
} | ||
|
||
_, err = client.CoreV1().Nodes().Patch(context.TODO(), node.GetName(), types.StrategicMergePatchType, bytes, metav1.PatchOptions{}) | ||
if err != nil { | ||
var labelsErr string | ||
for _, label := range labels { | ||
k := strings.Split(label, "=")[0] | ||
v := strings.Split(label, "=")[1] | ||
labelsErr += fmt.Sprintf("%s=%s ", k, v) | ||
} | ||
return fmt.Errorf("error updating node labels %s via k8s API: %v", labelsErr, err) | ||
} | ||
return nil | ||
} |
Oops, something went wrong.