Skip to content

Commit f570ea7

Browse files
committed
Issue #4: Fix - validate namespaces input by user via cmdline
* Convert allNamespacesExist() into a member function of the DataCollector object for succinctness
1 parent cbdb872 commit f570ea7

File tree

2 files changed

+16
-19
lines changed

2 files changed

+16
-19
lines changed

cmd/nic-supportpkg.go

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
package cmd
22

33
import (
4-
"context"
54
"fmt"
65
"github.com/nginxinc/nginx-k8s-supportpkg/pkg/data_collector"
76
"github.com/nginxinc/nginx-k8s-supportpkg/pkg/jobs"
87
"github.com/spf13/cobra"
9-
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
108
"os"
119
)
1210

@@ -26,8 +24,7 @@ func Execute() {
2624
os.Exit(1)
2725
}
2826

29-
if allNamespacesExist(collector) == true {
30-
27+
if collector.AllNamespacesExist() == true {
3128
for _, job := range jobs.JobList() {
3229
fmt.Printf("Running job %s...", job.Name)
3330
err = job.Collect(collector)
@@ -63,18 +60,3 @@ func Execute() {
6360
os.Exit(1)
6461
}
6562
}
66-
67-
func allNamespacesExist(dc *data_collector.DataCollector) bool {
68-
69-
var allExist bool = true
70-
for _, namespace := range dc.Namespaces {
71-
_, err := dc.K8sCoreClientSet.CoreV1().Namespaces().Get(context.TODO(), namespace, metav1.GetOptions{})
72-
if err != nil {
73-
dc.Logger.Printf("\t%s: %v\n", namespace, err)
74-
fmt.Printf("\t%s: %v\n", namespace, err)
75-
allExist = false
76-
}
77-
}
78-
79-
return allExist
80-
}

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)