Skip to content

Commit 60263ca

Browse files
authored
feat: [sc-108732] Can't add annotations in pods executed with the runPod collector (#1590)
add new field annotations for run pod collector
1 parent 87cedca commit 60263ca

10 files changed

+166
-28
lines changed

config/crds/troubleshoot.sh_collectors.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8768,6 +8768,10 @@ spec:
87688768
type: object
87698769
runPod:
87708770
properties:
8771+
annotations:
8772+
additionalProperties:
8773+
type: string
8774+
type: object
87718775
collectorName:
87728776
type: string
87738777
exclude:

config/crds/troubleshoot.sh_preflights.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10497,6 +10497,10 @@ spec:
1049710497
type: object
1049810498
runPod:
1049910499
properties:
10500+
annotations:
10501+
additionalProperties:
10502+
type: string
10503+
type: object
1050010504
collectorName:
1050110505
type: string
1050210506
exclude:

config/crds/troubleshoot.sh_supportbundles.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10528,6 +10528,10 @@ spec:
1052810528
type: object
1052910529
runPod:
1053010530
properties:
10531+
annotations:
10532+
additionalProperties:
10533+
type: string
10534+
type: object
1053110535
collectorName:
1053210536
type: string
1053310537
exclude:

pkg/apis/troubleshoot/v1beta2/collector_shared.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ type RunPod struct {
111111
Timeout string `json:"timeout,omitempty" yaml:"timeout,omitempty"`
112112
ImagePullSecret *ImagePullSecrets `json:"imagePullSecret,omitempty" yaml:"imagePullSecret,omitempty"`
113113
PodSpec corev1.PodSpec `json:"podSpec,omitempty" yaml:"podSpec,omitempty"`
114+
Annotations map[string]string `json:"annotations,omitempty" yaml:"annotations,omitempty"`
114115
}
115116

116117
type RunDaemonSet struct {

pkg/apis/troubleshoot/v1beta2/zz_generated.deepcopy.go

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/collect/run_pod.go

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -118,33 +118,7 @@ func (c *CollectRunPod) Collect(progressChan chan<- interface{}) (result Collect
118118
}
119119

120120
func runPodWithSpec(ctx context.Context, client *kubernetes.Clientset, runPodCollector *troubleshootv1beta2.RunPod) (*corev1.Pod, error) {
121-
podLabels := make(map[string]string)
122-
podLabels["troubleshoot-role"] = "run-collector"
123-
124-
namespace := "default"
125-
if runPodCollector.Namespace != "" {
126-
namespace = runPodCollector.Namespace
127-
}
128-
129-
podName := "run-pod"
130-
if runPodCollector.CollectorName != "" {
131-
podName = runPodCollector.CollectorName
132-
} else if runPodCollector.Name != "" {
133-
podName = runPodCollector.Name
134-
}
135-
136-
pod := corev1.Pod{
137-
ObjectMeta: metav1.ObjectMeta{
138-
Name: podName,
139-
Namespace: namespace,
140-
Labels: podLabels,
141-
},
142-
TypeMeta: metav1.TypeMeta{
143-
APIVersion: "v1",
144-
Kind: "Pod",
145-
},
146-
Spec: runPodCollector.PodSpec,
147-
}
121+
pod := createPodStruct(runPodCollector)
148122

149123
if runPodCollector.ImagePullSecret != nil && runPodCollector.ImagePullSecret.Data != nil {
150124
secretName, err := createSecret(ctx, client, pod.Namespace, runPodCollector.ImagePullSecret)
@@ -154,7 +128,7 @@ func runPodWithSpec(ctx context.Context, client *kubernetes.Clientset, runPodCol
154128
pod.Spec.ImagePullSecrets = append(pod.Spec.ImagePullSecrets, corev1.LocalObjectReference{Name: secretName})
155129
}
156130

157-
created, err := client.CoreV1().Pods(namespace).Create(ctx, &pod, metav1.CreateOptions{})
131+
created, err := client.CoreV1().Pods(pod.Namespace).Create(ctx, &pod, metav1.CreateOptions{})
158132
klog.V(2).Infof("Pod %s has been created", pod.Name)
159133

160134
if err != nil {
@@ -484,3 +458,36 @@ func deletePod(ctx context.Context, client *kubernetes.Clientset, pod *corev1.Po
484458
klog.V(2).Infof("Pod %s in %s namespace has been deleted", pod.Name, pod.Namespace)
485459
}
486460
}
461+
462+
func createPodStruct(runPodCollector *troubleshootv1beta2.RunPod) corev1.Pod {
463+
podLabels := make(map[string]string)
464+
podLabels["troubleshoot-role"] = "run-collector"
465+
466+
namespace := "default"
467+
if runPodCollector.Namespace != "" {
468+
namespace = runPodCollector.Namespace
469+
}
470+
471+
podName := "run-pod"
472+
if runPodCollector.CollectorName != "" {
473+
podName = runPodCollector.CollectorName
474+
} else if runPodCollector.Name != "" {
475+
podName = runPodCollector.Name
476+
}
477+
478+
pod := corev1.Pod{
479+
ObjectMeta: metav1.ObjectMeta{
480+
Name: podName,
481+
Namespace: namespace,
482+
Labels: podLabels,
483+
Annotations: runPodCollector.Annotations,
484+
},
485+
TypeMeta: metav1.TypeMeta{
486+
APIVersion: "v1",
487+
Kind: "Pod",
488+
},
489+
Spec: runPodCollector.PodSpec,
490+
}
491+
492+
return pod
493+
}

pkg/collect/run_pod_test.go

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package collect
2+
3+
import (
4+
"testing"
5+
6+
troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
7+
corev1 "k8s.io/api/core/v1"
8+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
9+
)
10+
11+
func TestCreatePodStruct(t *testing.T) {
12+
runPodCollector := &troubleshootv1beta2.RunPod{
13+
Namespace: "test-namespace",
14+
Name: "test-pod",
15+
Annotations: map[string]string{
16+
"annotation1": "value1",
17+
"annotation2": "value2",
18+
},
19+
PodSpec: corev1.PodSpec{
20+
Containers: []corev1.Container{
21+
{
22+
Name: "test-container",
23+
Image: "test-image",
24+
},
25+
},
26+
},
27+
}
28+
29+
expectedPod := corev1.Pod{
30+
ObjectMeta: metav1.ObjectMeta{
31+
Name: "test-pod",
32+
Namespace: "test-namespace",
33+
Labels: map[string]string{"troubleshoot-role": "run-collector"},
34+
Annotations: map[string]string{"annotation1": "value1", "annotation2": "value2"},
35+
},
36+
TypeMeta: metav1.TypeMeta{
37+
APIVersion: "v1",
38+
Kind: "Pod",
39+
},
40+
Spec: corev1.PodSpec{
41+
Containers: []corev1.Container{
42+
{
43+
Name: "test-container",
44+
Image: "test-image",
45+
},
46+
},
47+
},
48+
}
49+
50+
pod := createPodStruct(runPodCollector)
51+
52+
if pod.Name != expectedPod.Name {
53+
t.Errorf("Expected pod name %s, but got %s", expectedPod.Name, pod.Name)
54+
}
55+
56+
if pod.Namespace != expectedPod.Namespace {
57+
t.Errorf("Expected pod namespace %s, but got %s", expectedPod.Namespace, pod.Namespace)
58+
}
59+
60+
if len(pod.Labels) != len(expectedPod.Labels) {
61+
t.Errorf("Expected %d labels, but got %d", len(expectedPod.Labels), len(pod.Labels))
62+
}
63+
64+
for key, value := range expectedPod.Labels {
65+
if pod.Labels[key] != value {
66+
t.Errorf("Expected label %s=%s, but got %s=%s", key, value, key, pod.Labels[key])
67+
}
68+
}
69+
70+
if len(pod.Annotations) != len(expectedPod.Annotations) {
71+
t.Errorf("Expected %d annotations, but got %d", len(expectedPod.Annotations), len(pod.Annotations))
72+
}
73+
74+
for key, value := range expectedPod.Annotations {
75+
if pod.Annotations[key] != value {
76+
t.Errorf("Expected annotation %s=%s, but got %s=%s", key, value, key, pod.Annotations[key])
77+
}
78+
}
79+
80+
if len(pod.Spec.Containers) != len(expectedPod.Spec.Containers) {
81+
t.Errorf("Expected %d containers, but got %d", len(expectedPod.Spec.Containers), len(pod.Spec.Containers))
82+
}
83+
84+
for i, container := range expectedPod.Spec.Containers {
85+
if pod.Spec.Containers[i].Name != container.Name {
86+
t.Errorf("Expected container name %s, but got %s", container.Name, pod.Spec.Containers[i].Name)
87+
}
88+
89+
if pod.Spec.Containers[i].Image != container.Image {
90+
t.Errorf("Expected container image %s, but got %s", container.Image, pod.Spec.Containers[i].Image)
91+
}
92+
}
93+
}

schemas/collector-troubleshoot-v1beta2.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7595,6 +7595,12 @@
75957595
"namespace"
75967596
],
75977597
"properties": {
7598+
"annotations": {
7599+
"type": "object",
7600+
"additionalProperties": {
7601+
"type": "string"
7602+
}
7603+
},
75987604
"collectorName": {
75997605
"type": "string"
76007606
},

schemas/preflight-troubleshoot-v1beta2.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10259,6 +10259,12 @@
1025910259
"namespace"
1026010260
],
1026110261
"properties": {
10262+
"annotations": {
10263+
"type": "object",
10264+
"additionalProperties": {
10265+
"type": "string"
10266+
}
10267+
},
1026210268
"collectorName": {
1026310269
"type": "string"
1026410270
},

schemas/supportbundle-troubleshoot-v1beta2.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10305,6 +10305,12 @@
1030510305
"namespace"
1030610306
],
1030710307
"properties": {
10308+
"annotations": {
10309+
"type": "object",
10310+
"additionalProperties": {
10311+
"type": "string"
10312+
}
10313+
},
1030810314
"collectorName": {
1030910315
"type": "string"
1031010316
},

0 commit comments

Comments
 (0)