@@ -3,13 +3,15 @@ package main
33import (
44 "bufio"
55 "context"
6+ "flag"
67 "fmt"
78 "os"
89 "path/filepath"
10+ "strconv"
911 "strings"
1012 "text/tabwriter"
1113
12- flag "github.com/spf13/pflag"
14+ "github.com/spf13/pflag"
1315
1416 appsv1 "k8s.io/api/apps/v1"
1517 batchv1 "k8s.io/api/batch/v1"
@@ -18,24 +20,29 @@ import (
1820 utilerrors "k8s.io/apimachinery/pkg/util/errors"
1921 "k8s.io/cli-runtime/pkg/genericclioptions"
2022 "k8s.io/client-go/kubernetes"
23+ "k8s.io/klog/v2"
2124
2225 _ "k8s.io/client-go/plugin/pkg/client/auth"
2326)
2427
2528func main () {
29+
30+ klog .InitFlags (nil )
31+
2632 var sockFound bool
2733 var err error
2834 // flags
29- requestedNamespace := flag .StringP ("namespace" , "n" , "ALL" , "Namespace to search for pods" )
30- requestedPath := flag .StringP ("filename" , "f" , "" , "File or directory to scan" )
31- help := flag .BoolP ("help" , "h" , false , "Print usage" )
32- exitErr := flag .BoolP ("exit-with-error" , "e" , false , "Exit with error code if docker.sock found" )
33- verbose := flag . BoolP ("verbose" , "v" , false , "Enable verbose logging " )
35+ requestedNamespace := pflag .StringP ("namespace" , "n" , "ALL" , "Namespace to search for pods" )
36+ requestedPath := pflag .StringP ("filename" , "f" , "" , "File or directory to scan" )
37+ help := pflag .BoolP ("help" , "h" , false , "Print usage" )
38+ exitErr := pflag .BoolP ("exit-with-error" , "e" , false , "Exit with error code if docker.sock found" )
39+ verbose := pflag . IntP ("verbose" , "v" , 2 , "Logging level " )
3440
35- flag .Parse ()
41+ pflag .Parse ()
3642
43+ flag .Set ("v" , strconv .Itoa (* verbose ))
3744 if * help {
38- flag .PrintDefaults ()
45+ pflag .PrintDefaults ()
3946 os .Exit (0 )
4047 }
4148
@@ -64,7 +71,7 @@ func main() {
6471 }
6572}
6673
67- func runFiles (requestedPath string , w * tabwriter.Writer , verbose bool ) (bool , error ) {
74+ func runFiles (requestedPath string , w * tabwriter.Writer , verbose int ) (bool , error ) {
6875 // run against local files
6976
7077 var files []string
@@ -98,7 +105,7 @@ func runFiles(requestedPath string, w *tabwriter.Writer, verbose bool) (bool, er
98105 return sockFound , err
99106}
100107
101- func runCluster (requestedNamespace string , w * tabwriter.Writer , verbose bool ) (bool , error ) {
108+ func runCluster (requestedNamespace string , w * tabwriter.Writer , verbose int ) (bool , error ) {
102109
103110 var sockFound bool
104111 // append true or false for each namespace to report accurate value if docker.sock is found
@@ -115,7 +122,7 @@ func runCluster(requestedNamespace string, w *tabwriter.Writer, verbose bool) (b
115122 fmt .Fprintf (w , "%s\t %s\t %s\t %s\t \n " , "NAMESPACE" , "TYPE" , "NAME" , "STATUS" )
116123
117124 if requestedNamespace != "ALL" {
118- if verbose {
125+ if verbose > 3 {
119126 fmt .Printf ("user specified namespace: %s\n " , requestedNamespace )
120127 }
121128 namespace , err := clientset .CoreV1 ().Namespaces ().Get (context .Background (), requestedNamespace , metav1.GetOptions {})
@@ -145,7 +152,7 @@ func runCluster(requestedNamespace string, w *tabwriter.Writer, verbose bool) (b
145152 return containsTrue (sockFoundNamespaces ), nil
146153}
147154
148- func printResources (namespace corev1.Namespace , clientset * kubernetes.Clientset , w * tabwriter.Writer , verbose bool ) (bool , error ) {
155+ func printResources (namespace corev1.Namespace , clientset * kubernetes.Clientset , w * tabwriter.Writer , verbose int ) (bool , error ) {
149156
150157 var sockFoundPod , sockFoundDeploy , sockFoundStatefulSet , sockFoundJob , sockFoundCron bool
151158
@@ -281,7 +288,7 @@ func printResources(namespace corev1.Namespace, clientset *kubernetes.Clientset,
281288 }
282289 }
283290 volumeCounter ++
284- if volumeCounter == len (daemonset .Spec .Template .Spec .Volumes ) && verbose {
291+ if volumeCounter == len (daemonset .Spec .Template .Spec .Volumes ) && verbose > 3 {
285292 fmt .Fprintf (w , "%s\t %s\t %s\t %s\t \n " , namespaceName , "daemonset" , daemonset .Name , "not-mounted" )
286293 }
287294 }
@@ -320,7 +327,7 @@ func containsDockerSock(s string) bool {
320327 }
321328}
322329
323- func printVolumes (w * tabwriter.Writer , volumes []corev1.Volume , namespace , resType , resName string , verbose bool ) bool {
330+ func printVolumes (w * tabwriter.Writer , volumes []corev1.Volume , namespace , resType , resName string , verbose int ) bool {
324331 // initialize sockFound to use for exit code
325332 sockFound := false
326333 for _ , v := range volumes {
@@ -330,15 +337,15 @@ func printVolumes(w *tabwriter.Writer, volumes []corev1.Volume, namespace, resTy
330337 mounted = "mounted"
331338 sockFound = true
332339 }
333- if mounted == "mounted" || verbose {
340+ if mounted == "mounted" || verbose > 3 {
334341 fmt .Fprintf (w , "%s\t %s\t %s\t %s\t \n " , namespace , resType , resName , mounted )
335342 }
336343 }
337344 }
338345 return sockFound
339346}
340347
341- func printFiles (w * tabwriter.Writer , filePaths []string , verbose bool ) (bool , error ) {
348+ func printFiles (w * tabwriter.Writer , filePaths []string , verbose int ) (bool , error ) {
342349 // initialize sockFound to use for exit code
343350 sockFound := false
344351 // print output for scanning local manifest files
@@ -352,7 +359,7 @@ func printFiles(w *tabwriter.Writer, filePaths []string, verbose bool) (bool, er
352359 mounted = "mounted"
353360 sockFound = true
354361 }
355- if mounted == "mounted" || verbose {
362+ if mounted == "mounted" || verbose > 3 {
356363 fmt .Fprintf (w , "%s\t %v\t %s\t \n " , file , line , mounted )
357364 }
358365 }
0 commit comments