4
4
"context"
5
5
"encoding/json"
6
6
"fmt"
7
+ "log"
7
8
"time"
8
9
9
10
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -12,6 +13,10 @@ import (
12
13
"k8s.io/client-go/tools/clientcmd"
13
14
)
14
15
16
+ const (
17
+ timeoutToLabelAllPodsMinutes = 120
18
+ )
19
+
15
20
type patchStringValue struct {
16
21
Op string `json:"op"`
17
22
Path string `json:"path"`
@@ -50,32 +55,21 @@ func (a *AddSharedLabelsToAllPods) Run() error {
50
55
return fmt .Errorf ("error creating Kubernetes client: %w" , err )
51
56
}
52
57
53
- ctx , cancel := context . WithTimeout ( context . Background (), defaultTimeoutSeconds * time . Second )
58
+ ctx , cancel := contextToLabelAllPods ( )
54
59
defer cancel ()
55
60
56
61
resources , err := clientset .CoreV1 ().Pods (a .Namespace ).List (ctx , metav1.ListOptions {})
57
62
58
- patch := []patchStringValue {}
59
-
60
- for i := 0 ; i < a .NumSharedLabelsPerPod ; i ++ {
61
- patch = append (patch , patchStringValue {
62
- Op : "add" ,
63
- Path : "/metadata/labels/shared-lab-" + fmt .Sprintf ("%05d" , i ),
64
- Value : "val" ,
65
- })
66
- }
67
-
68
- patchBytes , err := json .Marshal (patch )
63
+ patchBytes , err := getSharedLabelsPatch (a .NumSharedLabelsPerPod )
69
64
if err != nil {
70
- return fmt .Errorf ("error marshalling patch: %w" , err )
65
+ return fmt .Errorf ("error getting label patch: %w" , err )
71
66
}
72
67
73
68
for _ , resource := range resources .Items {
74
- clientset .CoreV1 ().Pods (a .Namespace ).Patch (ctx , resource .Name ,
75
- types .JSONPatchType ,
76
- patchBytes ,
77
- metav1.PatchOptions {},
78
- )
69
+ err = patchLabel (ctx , clientset , a .Namespace , resource .Name , patchBytes )
70
+ if err != nil {
71
+ log .Printf ("Error adding shared labels to pod %s: %s\n " , resource .Name , err )
72
+ }
79
73
}
80
74
81
75
return nil
@@ -85,3 +79,38 @@ func (a *AddSharedLabelsToAllPods) Run() error {
85
79
func (a * AddSharedLabelsToAllPods ) Stop () error {
86
80
return nil
87
81
}
82
+
83
+ func patchLabel (ctx context.Context , clientset * kubernetes.Clientset , namespace , podName string , patchBytes []byte ) error {
84
+ log .Println ("Labeling Pod" , podName )
85
+ _ , err := clientset .CoreV1 ().Pods (namespace ).Patch (ctx , podName ,
86
+ types .JSONPatchType ,
87
+ patchBytes ,
88
+ metav1.PatchOptions {},
89
+ )
90
+ if err != nil {
91
+ return fmt .Errorf ("error patching pod: %w" , err )
92
+ }
93
+
94
+ return nil
95
+ }
96
+
97
+ func getSharedLabelsPatch (numLabels int ) ([]byte , error ) {
98
+ patch := []patchStringValue {}
99
+ for i := 0 ; i < numLabels ; i ++ {
100
+ patch = append (patch , patchStringValue {
101
+ Op : "add" ,
102
+ Path : "/metadata/labels/shared-lab-" + fmt .Sprintf ("%05d" , i ),
103
+ Value : "val" ,
104
+ })
105
+ }
106
+ b , err := json .Marshal (patch )
107
+ if err != nil {
108
+ return nil , fmt .Errorf ("error marshalling patch: %w" , err )
109
+ }
110
+
111
+ return b , nil
112
+ }
113
+
114
+ func contextToLabelAllPods () (context.Context , context.CancelFunc ) {
115
+ return context .WithTimeout (context .Background (), timeoutToLabelAllPodsMinutes * time .Minute )
116
+ }
0 commit comments