-
Notifications
You must be signed in to change notification settings - Fork 10
/
scanner.go
111 lines (88 loc) · 3.15 KB
/
scanner.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package main
import (
"strings"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)
var scannerCmd = &cobra.Command {
Use: "scanner",
Short:"Scans cluster (from current context) for objects violating rules",
Long: "Scans cluster (from current context) for objects violating rules specified by flags",
Run: scanCluster,
}
var scannerViper = viper.New()
func init() {
rootCmd.AddCommand(scannerCmd)
scannerCmd.Flags().String("namespace", "",
"Whether specific namespace should be scanned. If omitted, all namespaces are scanned.")
initCommonFlags(scannerCmd)
if err := scannerViper.BindPFlags(scannerCmd.Flags()); err != nil {
errorWithUsage(err)
}
scannerViper.AutomaticEnv()
scannerViper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
}
func scanCluster(cmd *cobra.Command, args []string) {
config := &config{}
if err := scannerViper.Unmarshal(config); err != nil {
errorWithUsage(err)
}
log.Debugf("Configuration is: %+v", config)
kubeClientSet, err := KubeClientSet(false)
if err != nil {
log.Fatal(err.Error())
}
log.Debugf("Init finished!")
validatePods(kubeClientSet, config)
validateIngresses(kubeClientSet, config)
log.Debugf("Check completed!")
}
func validatePods(clientset *kubernetes.Clientset, config *config) {
log.Debugf("Check Pods...")
namespaceToScan := config.Namespace
pods, err := clientset.CoreV1().Pods(namespaceToScan).List(metav1.ListOptions{})
if err != nil {
log.Fatal(err.Error())
}
if namespaceToScan == "" {
log.Debugf("There are %d pods in all namespaces", len(pods.Items))
} else {
log.Debugf("There are %d pods in the namespace '%s'", len(pods.Items), namespaceToScan)
}
for _, pod := range pods.Items {
validation := &objectValidation{"Pod", nil, &validationViolationSet{}}
validatePodSpec(validation, &pod.ObjectMeta, &pod.Spec, config)
if len(validation.Violations.Violations) > 0 {
log.Debugf("Pod from namespace '%s' with name '%s' has following violations:", pod.ObjectMeta.Namespace, pod.ObjectMeta.Name)
for _, v := range validation.Violations.Violations {
log.Debugf(" %s", v.Message)
}
}
}
}
func validateIngresses(clientset *kubernetes.Clientset, config *config) {
log.Debugf("Check Ingresses...")
namespaceToScan := config.Namespace
ingresses, err := clientset.ExtensionsV1beta1().Ingresses(namespaceToScan).List(metav1.ListOptions{})
if err != nil {
panic(err.Error())
}
if namespaceToScan == "" {
log.Debugf("There are %d ingresses in all namespaces", len(ingresses.Items))
} else {
log.Debugf("There are %d ingresses in the namespace '%s'", len(ingresses.Items), namespaceToScan)
}
for _, ingress := range ingresses.Items {
validation := &objectValidation{"Ingress", nil, &validationViolationSet{}}
ValidateIngress(validation, &ingress, config, clientset)
if len(validation.Violations.Violations) > 0 {
log.Debugf("Ingress from namespace '%s' with name '%s' has following violations:", ingress.ObjectMeta.Namespace, ingress.ObjectMeta.Name)
for _, v := range validation.Violations.Violations {
log.Debugf(" %s", v.Message)
}
}
}
}