Skip to content

Commit 639bfec

Browse files
authored
Merge pull request #5 from nginxinc/mrajagopal-branch
Issue #4: Fix - validate namespaces input by user via cmdline
2 parents 0718a1b + f570ea7 commit 639bfec

File tree

2 files changed

+35
-13
lines changed

2 files changed

+35
-13
lines changed

cmd/nic-supportpkg.go

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,28 +24,35 @@ func Execute() {
2424
os.Exit(1)
2525
}
2626

27-
for _, job := range jobs.JobList() {
28-
fmt.Printf("Running job %s...", job.Name)
29-
err = job.Collect(collector)
27+
if collector.AllNamespacesExist() == true {
28+
for _, job := range jobs.JobList() {
29+
fmt.Printf("Running job %s...", job.Name)
30+
err = job.Collect(collector)
31+
if err != nil {
32+
fmt.Printf(" Error: %s\n", err)
33+
} else {
34+
fmt.Print(" OK\n")
35+
}
36+
}
37+
38+
tarFile, err := collector.WrapUp()
3039
if err != nil {
31-
fmt.Printf(" Error: %s\n", err)
40+
fmt.Println(fmt.Errorf("error when wrapping up: %s", err))
41+
os.Exit(1)
3242
} else {
33-
fmt.Print(" OK\n")
43+
fmt.Printf("Supportpkg successfully generated: %s\n", tarFile)
3444
}
35-
}
36-
37-
tarFile, err := collector.WrapUp()
38-
if err != nil {
39-
fmt.Println(fmt.Errorf("error when wrapping up: %s", err))
40-
os.Exit(1)
4145
} else {
42-
fmt.Printf("Supportpkg successfully generated: %s\n", tarFile)
46+
fmt.Println(" Error: Some namespaces do not exist")
4347
}
4448
},
4549
}
4650

4751
rootCmd.Flags().StringSliceVarP(&namespaces, "namespace", "n", []string{}, "list of namespaces to collect information from")
48-
rootCmd.MarkFlagRequired("namespace")
52+
if err := rootCmd.MarkFlagRequired("namespace"); err != nil {
53+
fmt.Println(err)
54+
os.Exit(1)
55+
}
4956
rootCmd.SetUsageTemplate("Usage: \n nic supportpkg [-n|--namespace] ns1 [-n|--namespace] ns2 ...\n nic supportpkg [-n|--namespace] ns1,ns2 ...\n")
5057

5158
if err := rootCmd.Execute(); err != nil {

pkg/data_collector/data_collector.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"io"
1111
corev1 "k8s.io/api/core/v1"
1212
crdClient "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
13+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1314
"k8s.io/client-go/kubernetes"
1415
"k8s.io/client-go/kubernetes/scheme"
1516
"k8s.io/client-go/rest"
@@ -175,3 +176,17 @@ func (c *DataCollector) PodExecutor(namespace string, pod string, command []stri
175176
return nil, err
176177
}
177178
}
179+
180+
func (c *DataCollector) AllNamespacesExist() bool {
181+
var allExist bool = true
182+
for _, namespace := range c.Namespaces {
183+
_, err := c.K8sCoreClientSet.CoreV1().Namespaces().Get(context.TODO(), namespace, metav1.GetOptions{})
184+
if err != nil {
185+
c.Logger.Printf("\t%s: %v\n", namespace, err)
186+
fmt.Printf("\t%s: %v\n", namespace, err)
187+
allExist = false
188+
}
189+
}
190+
191+
return allExist
192+
}

0 commit comments

Comments
 (0)