Skip to content

Commit

Permalink
feat: [sc-108732] Can't add annotations in pods executed with the run…
Browse files Browse the repository at this point in the history
…Pod collector (#1590)

add new field annotations for run pod collector
  • Loading branch information
nvanthao authored Aug 8, 2024
1 parent 87cedca commit 60263ca
Show file tree
Hide file tree
Showing 10 changed files with 166 additions and 28 deletions.
4 changes: 4 additions & 0 deletions config/crds/troubleshoot.sh_collectors.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8768,6 +8768,10 @@ spec:
type: object
runPod:
properties:
annotations:
additionalProperties:
type: string
type: object
collectorName:
type: string
exclude:
Expand Down
4 changes: 4 additions & 0 deletions config/crds/troubleshoot.sh_preflights.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10497,6 +10497,10 @@ spec:
type: object
runPod:
properties:
annotations:
additionalProperties:
type: string
type: object
collectorName:
type: string
exclude:
Expand Down
4 changes: 4 additions & 0 deletions config/crds/troubleshoot.sh_supportbundles.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10528,6 +10528,10 @@ spec:
type: object
runPod:
properties:
annotations:
additionalProperties:
type: string
type: object
collectorName:
type: string
exclude:
Expand Down
1 change: 1 addition & 0 deletions pkg/apis/troubleshoot/v1beta2/collector_shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 7 additions & 0 deletions pkg/apis/troubleshoot/v1beta2/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

63 changes: 35 additions & 28 deletions pkg/collect/run_pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 {
Expand Down Expand Up @@ -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
}
93 changes: 93 additions & 0 deletions pkg/collect/run_pod_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
6 changes: 6 additions & 0 deletions schemas/collector-troubleshoot-v1beta2.json
Original file line number Diff line number Diff line change
Expand Up @@ -7595,6 +7595,12 @@
"namespace"
],
"properties": {
"annotations": {
"type": "object",
"additionalProperties": {
"type": "string"
}
},
"collectorName": {
"type": "string"
},
Expand Down
6 changes: 6 additions & 0 deletions schemas/preflight-troubleshoot-v1beta2.json
Original file line number Diff line number Diff line change
Expand Up @@ -10259,6 +10259,12 @@
"namespace"
],
"properties": {
"annotations": {
"type": "object",
"additionalProperties": {
"type": "string"
}
},
"collectorName": {
"type": "string"
},
Expand Down
6 changes: 6 additions & 0 deletions schemas/supportbundle-troubleshoot-v1beta2.json
Original file line number Diff line number Diff line change
Expand Up @@ -10305,6 +10305,12 @@
"namespace"
],
"properties": {
"annotations": {
"type": "object",
"additionalProperties": {
"type": "string"
}
},
"collectorName": {
"type": "string"
},
Expand Down

0 comments on commit 60263ca

Please sign in to comment.