diff --git a/config/crds/troubleshoot.sh_collectors.yaml b/config/crds/troubleshoot.sh_collectors.yaml index 30bd49fc5..8a7619172 100644 --- a/config/crds/troubleshoot.sh_collectors.yaml +++ b/config/crds/troubleshoot.sh_collectors.yaml @@ -8768,6 +8768,10 @@ spec: type: object runPod: properties: + annotations: + additionalProperties: + type: string + type: object collectorName: type: string exclude: diff --git a/config/crds/troubleshoot.sh_preflights.yaml b/config/crds/troubleshoot.sh_preflights.yaml index d63045e97..8d483325b 100644 --- a/config/crds/troubleshoot.sh_preflights.yaml +++ b/config/crds/troubleshoot.sh_preflights.yaml @@ -10497,6 +10497,10 @@ spec: type: object runPod: properties: + annotations: + additionalProperties: + type: string + type: object collectorName: type: string exclude: diff --git a/config/crds/troubleshoot.sh_supportbundles.yaml b/config/crds/troubleshoot.sh_supportbundles.yaml index 84828d7a8..f9f650947 100644 --- a/config/crds/troubleshoot.sh_supportbundles.yaml +++ b/config/crds/troubleshoot.sh_supportbundles.yaml @@ -10528,6 +10528,10 @@ spec: type: object runPod: properties: + annotations: + additionalProperties: + type: string + type: object collectorName: type: string exclude: diff --git a/pkg/apis/troubleshoot/v1beta2/collector_shared.go b/pkg/apis/troubleshoot/v1beta2/collector_shared.go index b23a74379..b05c79280 100644 --- a/pkg/apis/troubleshoot/v1beta2/collector_shared.go +++ b/pkg/apis/troubleshoot/v1beta2/collector_shared.go @@ -111,6 +111,7 @@ type RunPod struct { Timeout string `json:"timeout,omitempty" yaml:"timeout,omitempty"` ImagePullSecret *ImagePullSecrets `json:"imagePullSecret,omitempty" yaml:"imagePullSecret,omitempty"` PodSpec corev1.PodSpec `json:"podSpec,omitempty" yaml:"podSpec,omitempty"` + Annotations map[string]string `json:"annotations,omitempty" yaml:"annotations,omitempty"` } type RunDaemonSet struct { diff --git a/pkg/apis/troubleshoot/v1beta2/zz_generated.deepcopy.go b/pkg/apis/troubleshoot/v1beta2/zz_generated.deepcopy.go index 4ea1bd9a1..cce96046b 100644 --- a/pkg/apis/troubleshoot/v1beta2/zz_generated.deepcopy.go +++ b/pkg/apis/troubleshoot/v1beta2/zz_generated.deepcopy.go @@ -4386,6 +4386,13 @@ func (in *RunPod) DeepCopyInto(out *RunPod) { (*in).DeepCopyInto(*out) } in.PodSpec.DeepCopyInto(&out.PodSpec) + if in.Annotations != nil { + in, out := &in.Annotations, &out.Annotations + *out = make(map[string]string, len(*in)) + for key, val := range *in { + (*out)[key] = val + } + } } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new RunPod. diff --git a/pkg/collect/run_pod.go b/pkg/collect/run_pod.go index 7df4d2908..77da6d5f2 100644 --- a/pkg/collect/run_pod.go +++ b/pkg/collect/run_pod.go @@ -118,33 +118,7 @@ func (c *CollectRunPod) Collect(progressChan chan<- interface{}) (result Collect } func runPodWithSpec(ctx context.Context, client *kubernetes.Clientset, runPodCollector *troubleshootv1beta2.RunPod) (*corev1.Pod, error) { - podLabels := make(map[string]string) - podLabels["troubleshoot-role"] = "run-collector" - - namespace := "default" - if runPodCollector.Namespace != "" { - namespace = runPodCollector.Namespace - } - - podName := "run-pod" - if runPodCollector.CollectorName != "" { - podName = runPodCollector.CollectorName - } else if runPodCollector.Name != "" { - podName = runPodCollector.Name - } - - pod := corev1.Pod{ - ObjectMeta: metav1.ObjectMeta{ - Name: podName, - Namespace: namespace, - Labels: podLabels, - }, - TypeMeta: metav1.TypeMeta{ - APIVersion: "v1", - Kind: "Pod", - }, - Spec: runPodCollector.PodSpec, - } + pod := createPodStruct(runPodCollector) if runPodCollector.ImagePullSecret != nil && runPodCollector.ImagePullSecret.Data != nil { secretName, err := createSecret(ctx, client, pod.Namespace, runPodCollector.ImagePullSecret) @@ -154,7 +128,7 @@ func runPodWithSpec(ctx context.Context, client *kubernetes.Clientset, runPodCol pod.Spec.ImagePullSecrets = append(pod.Spec.ImagePullSecrets, corev1.LocalObjectReference{Name: secretName}) } - created, err := client.CoreV1().Pods(namespace).Create(ctx, &pod, metav1.CreateOptions{}) + created, err := client.CoreV1().Pods(pod.Namespace).Create(ctx, &pod, metav1.CreateOptions{}) klog.V(2).Infof("Pod %s has been created", pod.Name) if err != nil { @@ -484,3 +458,36 @@ func deletePod(ctx context.Context, client *kubernetes.Clientset, pod *corev1.Po klog.V(2).Infof("Pod %s in %s namespace has been deleted", pod.Name, pod.Namespace) } } + +func createPodStruct(runPodCollector *troubleshootv1beta2.RunPod) corev1.Pod { + podLabels := make(map[string]string) + podLabels["troubleshoot-role"] = "run-collector" + + namespace := "default" + if runPodCollector.Namespace != "" { + namespace = runPodCollector.Namespace + } + + podName := "run-pod" + if runPodCollector.CollectorName != "" { + podName = runPodCollector.CollectorName + } else if runPodCollector.Name != "" { + podName = runPodCollector.Name + } + + pod := corev1.Pod{ + ObjectMeta: metav1.ObjectMeta{ + Name: podName, + Namespace: namespace, + Labels: podLabels, + Annotations: runPodCollector.Annotations, + }, + TypeMeta: metav1.TypeMeta{ + APIVersion: "v1", + Kind: "Pod", + }, + Spec: runPodCollector.PodSpec, + } + + return pod +} diff --git a/pkg/collect/run_pod_test.go b/pkg/collect/run_pod_test.go new file mode 100644 index 000000000..0e6d5d8bf --- /dev/null +++ b/pkg/collect/run_pod_test.go @@ -0,0 +1,93 @@ +package collect + +import ( + "testing" + + troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2" + corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +func TestCreatePodStruct(t *testing.T) { + runPodCollector := &troubleshootv1beta2.RunPod{ + Namespace: "test-namespace", + Name: "test-pod", + Annotations: map[string]string{ + "annotation1": "value1", + "annotation2": "value2", + }, + PodSpec: corev1.PodSpec{ + Containers: []corev1.Container{ + { + Name: "test-container", + Image: "test-image", + }, + }, + }, + } + + expectedPod := corev1.Pod{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test-pod", + Namespace: "test-namespace", + Labels: map[string]string{"troubleshoot-role": "run-collector"}, + Annotations: map[string]string{"annotation1": "value1", "annotation2": "value2"}, + }, + TypeMeta: metav1.TypeMeta{ + APIVersion: "v1", + Kind: "Pod", + }, + Spec: corev1.PodSpec{ + Containers: []corev1.Container{ + { + Name: "test-container", + Image: "test-image", + }, + }, + }, + } + + pod := createPodStruct(runPodCollector) + + if pod.Name != expectedPod.Name { + t.Errorf("Expected pod name %s, but got %s", expectedPod.Name, pod.Name) + } + + if pod.Namespace != expectedPod.Namespace { + t.Errorf("Expected pod namespace %s, but got %s", expectedPod.Namespace, pod.Namespace) + } + + if len(pod.Labels) != len(expectedPod.Labels) { + t.Errorf("Expected %d labels, but got %d", len(expectedPod.Labels), len(pod.Labels)) + } + + for key, value := range expectedPod.Labels { + if pod.Labels[key] != value { + t.Errorf("Expected label %s=%s, but got %s=%s", key, value, key, pod.Labels[key]) + } + } + + if len(pod.Annotations) != len(expectedPod.Annotations) { + t.Errorf("Expected %d annotations, but got %d", len(expectedPod.Annotations), len(pod.Annotations)) + } + + for key, value := range expectedPod.Annotations { + if pod.Annotations[key] != value { + t.Errorf("Expected annotation %s=%s, but got %s=%s", key, value, key, pod.Annotations[key]) + } + } + + if len(pod.Spec.Containers) != len(expectedPod.Spec.Containers) { + t.Errorf("Expected %d containers, but got %d", len(expectedPod.Spec.Containers), len(pod.Spec.Containers)) + } + + for i, container := range expectedPod.Spec.Containers { + if pod.Spec.Containers[i].Name != container.Name { + t.Errorf("Expected container name %s, but got %s", container.Name, pod.Spec.Containers[i].Name) + } + + if pod.Spec.Containers[i].Image != container.Image { + t.Errorf("Expected container image %s, but got %s", container.Image, pod.Spec.Containers[i].Image) + } + } +} diff --git a/schemas/collector-troubleshoot-v1beta2.json b/schemas/collector-troubleshoot-v1beta2.json index 6e865a88b..95ab40f31 100644 --- a/schemas/collector-troubleshoot-v1beta2.json +++ b/schemas/collector-troubleshoot-v1beta2.json @@ -7595,6 +7595,12 @@ "namespace" ], "properties": { + "annotations": { + "type": "object", + "additionalProperties": { + "type": "string" + } + }, "collectorName": { "type": "string" }, diff --git a/schemas/preflight-troubleshoot-v1beta2.json b/schemas/preflight-troubleshoot-v1beta2.json index bc5424cf8..ce3c9b40a 100644 --- a/schemas/preflight-troubleshoot-v1beta2.json +++ b/schemas/preflight-troubleshoot-v1beta2.json @@ -10259,6 +10259,12 @@ "namespace" ], "properties": { + "annotations": { + "type": "object", + "additionalProperties": { + "type": "string" + } + }, "collectorName": { "type": "string" }, diff --git a/schemas/supportbundle-troubleshoot-v1beta2.json b/schemas/supportbundle-troubleshoot-v1beta2.json index cd22fae94..35273b7d3 100644 --- a/schemas/supportbundle-troubleshoot-v1beta2.json +++ b/schemas/supportbundle-troubleshoot-v1beta2.json @@ -10305,6 +10305,12 @@ "namespace" ], "properties": { + "annotations": { + "type": "object", + "additionalProperties": { + "type": "string" + } + }, "collectorName": { "type": "string" },