Skip to content

Commit f207d82

Browse files
authored
Merge pull request #5 from aws-containers/log-levels
Add logging levels
2 parents 3d28e5a + 261d94c commit f207d82

File tree

4 files changed

+27
-20
lines changed

4 files changed

+27
-20
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,9 @@ test/manifests/docker-volume.pod.kube-system.yaml 14 mounted
8686
test/manifests/docker-volume.statefulset.yaml 26 mounted
8787
```
8888

89-
Use the `--verbose` flag to see all scanned workloads/files
89+
Use the `--verbose` with a log level (1-10) to get more output
9090
```
91-
kubectl dds --verbose
91+
kubectl dds --verbose=4
9292
```
9393
example output
9494
```

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ require (
88
k8s.io/apimachinery v0.23.4
99
k8s.io/cli-runtime v0.23.4
1010
k8s.io/client-go v0.23.4
11+
k8s.io/klog/v2 v2.30.0
1112
)
1213

1314
require (
@@ -66,7 +67,6 @@ require (
6667
gopkg.in/inf.v0 v0.9.1 // indirect
6768
gopkg.in/yaml.v2 v2.4.0 // indirect
6869
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
69-
k8s.io/klog/v2 v2.30.0 // indirect
7070
k8s.io/kube-openapi v0.0.0-20211115234752-e816edb12b65 // indirect
7171
k8s.io/utils v0.0.0-20211116205334-6203023598ed // indirect
7272
sigs.k8s.io/json v0.0.0-20211020170558-c049b76a60c6 // indirect

img/dds-demo.gif

373 KB
Loading

main.go

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ package main
33
import (
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

2528
func 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

Comments
 (0)