Skip to content

Commit 41a9a76

Browse files
authored
Merge pull request #216 from nginx/213-run-targeted-exec-commands
fix: Run exec commands only on container that is nginx-ingress
2 parents 8eedd67 + 8f39d12 commit 41a9a76

File tree

1 file changed

+40
-32
lines changed

1 file changed

+40
-32
lines changed

pkg/jobs/nic_job_list.go

Lines changed: 40 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,13 @@ import (
2323
"context"
2424
"encoding/json"
2525
"fmt"
26-
"github.com/nginxinc/nginx-k8s-supportpkg/pkg/crds"
27-
"github.com/nginxinc/nginx-k8s-supportpkg/pkg/data_collector"
28-
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2926
"path/filepath"
3027
"strings"
3128
"time"
29+
30+
"github.com/nginxinc/nginx-k8s-supportpkg/pkg/crds"
31+
"github.com/nginxinc/nginx-k8s-supportpkg/pkg/data_collector"
32+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3233
)
3334

3435
func NICJobList() []Job {
@@ -47,14 +48,15 @@ func NICJobList() []Job {
4748
for _, pod := range pods.Items {
4849
if strings.Contains(pod.Name, "ingress") {
4950
for _, container := range pod.Spec.Containers {
50-
51-
res, err := dc.PodExecutor(namespace, pod.Name, container.Name, command, ctx)
52-
if err != nil {
53-
jobResult.Error = err
54-
dc.Logger.Printf("\tCommand execution %s failed for pod %s in namespace %s: %v\n", command, pod.Name, namespace, err)
55-
} else {
56-
fileName := fmt.Sprintf("%s__%s__nginx-ingress-version.txt", pod.Name, container.Name)
57-
jobResult.Files[filepath.Join(dc.BaseDir, "exec", namespace, fileName)] = res
51+
if container.Name == "nginx-ingress" {
52+
res, err := dc.PodExecutor(namespace, pod.Name, container.Name, command, ctx)
53+
if err != nil {
54+
jobResult.Error = err
55+
dc.Logger.Printf("\tCommand execution %s failed for pod %s in namespace %s: %v\n", command, pod.Name, namespace, err)
56+
} else {
57+
fileName := fmt.Sprintf("%s__%s__nginx-ingress-version.txt", pod.Name, container.Name)
58+
jobResult.Files[filepath.Join(dc.BaseDir, "exec", namespace, fileName)] = res
59+
}
5860
}
5961
}
6062
}
@@ -78,13 +80,15 @@ func NICJobList() []Job {
7880
for _, pod := range pods.Items {
7981
if strings.Contains(pod.Name, "ingress") {
8082
for _, container := range pod.Spec.Containers {
81-
res, err := dc.PodExecutor(namespace, pod.Name, container.Name, command, ctx)
82-
if err != nil {
83-
jobResult.Error = err
84-
dc.Logger.Printf("\tCommand execution %s failed for pod %s in namespace %s: %v\n", command, pod.Name, namespace, err)
85-
} else {
86-
fileName := fmt.Sprintf("%s__%s__nginx-t.txt", pod.Name, container.Name)
87-
jobResult.Files[filepath.Join(dc.BaseDir, "exec", namespace, fileName)] = res
83+
if container.Name == "nginx-ingress" {
84+
res, err := dc.PodExecutor(namespace, pod.Name, container.Name, command, ctx)
85+
if err != nil {
86+
jobResult.Error = err
87+
dc.Logger.Printf("\tCommand execution %s failed for pod %s in namespace %s: %v\n", command, pod.Name, namespace, err)
88+
} else {
89+
fileName := fmt.Sprintf("%s__%s__nginx-t.txt", pod.Name, container.Name)
90+
jobResult.Files[filepath.Join(dc.BaseDir, "exec", namespace, fileName)] = res
91+
}
8892
}
8993
}
9094
}
@@ -108,13 +112,15 @@ func NICJobList() []Job {
108112
for _, pod := range pods.Items {
109113
if strings.Contains(pod.Name, "ingress") {
110114
for _, container := range pod.Spec.Containers {
111-
res, err := dc.PodExecutor(namespace, pod.Name, container.Name, command, ctx)
112-
if err != nil {
113-
jobResult.Error = err
114-
dc.Logger.Printf("\tCommand execution %s failed for pod %s in namespace %s: %v\n", command, pod.Name, namespace, err)
115-
} else {
116-
fileName := fmt.Sprintf("%s__%s__nginx-agent.conf", pod.Name, container.Name)
117-
jobResult.Files[filepath.Join(dc.BaseDir, "exec", namespace, fileName)] = res
115+
if container.Name == "nginx-ingress" {
116+
res, err := dc.PodExecutor(namespace, pod.Name, container.Name, command, ctx)
117+
if err != nil {
118+
jobResult.Error = err
119+
dc.Logger.Printf("\tCommand execution %s failed for pod %s in namespace %s: %v\n", command, pod.Name, namespace, err)
120+
} else {
121+
fileName := fmt.Sprintf("%s__%s__nginx-agent.conf", pod.Name, container.Name)
122+
jobResult.Files[filepath.Join(dc.BaseDir, "exec", namespace, fileName)] = res
123+
}
118124
}
119125
}
120126
}
@@ -138,13 +144,15 @@ func NICJobList() []Job {
138144
for _, pod := range pods.Items {
139145
if strings.Contains(pod.Name, "ingress") {
140146
for _, container := range pod.Spec.Containers {
141-
res, err := dc.PodExecutor(namespace, pod.Name, container.Name, command, ctx)
142-
if err != nil {
143-
jobResult.Error = err
144-
dc.Logger.Printf("\tCommand execution %s failed for pod %s in namespace %s: %v\n", command, pod.Name, namespace, err)
145-
} else {
146-
fileName := fmt.Sprintf("%s__%s__nginx-agent-version.txt", pod.Name, container.Name)
147-
jobResult.Files[filepath.Join(dc.BaseDir, "exec", namespace, fileName)] = res
147+
if container.Name == "nginx-ingress" {
148+
res, err := dc.PodExecutor(namespace, pod.Name, container.Name, command, ctx)
149+
if err != nil {
150+
jobResult.Error = err
151+
dc.Logger.Printf("\tCommand execution %s failed for pod %s in namespace %s: %v\n", command, pod.Name, namespace, err)
152+
} else {
153+
fileName := fmt.Sprintf("%s__%s__nginx-agent-version.txt", pod.Name, container.Name)
154+
jobResult.Files[filepath.Join(dc.BaseDir, "exec", namespace, fileName)] = res
155+
}
148156
}
149157
}
150158
}

0 commit comments

Comments
 (0)