-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathlabels.go
More file actions
71 lines (56 loc) · 1.73 KB
/
Copy pathlabels.go
File metadata and controls
71 lines (56 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
Copyright 2022-2023 Lawrence Livermore National Security, LLC
(c.f. AUTHORS, NOTICE.LLNS, COPYING)
This is part of the Flux resource manager framework.
For details, see https://github.com/flux-framework.
SPDX-License-Identifier: Apache-2.0
*/
package controllers
import (
"context"
"encoding/json"
"fmt"
"strings"
api "github.com/flux-framework/flux-operator/api/v1alpha2"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes"
ctrl "sigs.k8s.io/controller-runtime"
)
type patchPayload struct {
Operation string `json:"op"`
Path string `json:"path"`
Value string `json:"value"`
}
func (r *MiniClusterReconciler) addBrokerLabel(
ctx context.Context,
cluster *api.MiniCluster,
) (ctrl.Result, error) {
// creates the clientset
clientset, err := kubernetes.NewForConfig(r.RESTConfig)
if err != nil {
return ctrl.Result{}, err
}
// List pods, and only get ones with undefined labels
pods, err := clientset.CoreV1().Pods(cluster.Namespace).List(ctx, metav1.ListOptions{LabelSelector: "!job-index"})
if err != nil {
return ctrl.Result{}, err
}
for _, pod := range pods.Items {
// Add labels to all pods to indicate job-index
prefix := fmt.Sprintf("%s-", cluster.Name)
podName := strings.Replace(pod.GetName(), prefix, "", 1)
podIndex := strings.SplitN(podName, "-", 2)[0]
payload := []patchPayload{{
Operation: "add",
Path: "/metadata/labels/job-index",
Value: podIndex,
}}
payloadBytes, _ := json.Marshal(payload)
_, err = clientset.CoreV1().Pods(pod.GetNamespace()).Patch(ctx, pod.GetName(), types.JSONPatchType, payloadBytes, metav1.PatchOptions{})
if err != nil {
return ctrl.Result{}, err
}
}
return ctrl.Result{}, nil
}