-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.go
95 lines (76 loc) · 2.46 KB
/
main.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
package main
import (
"context"
log "github.com/sirupsen/logrus"
"gopkg.in/alecthomas/kingpin.v2"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"github.com/uswitch/klint/alerts"
"github.com/uswitch/klint/engine"
"github.com/uswitch/klint/rules"
)
type options struct {
kubeconfig string
namespace string
debug bool
slackToken string
awsRegion string
ageLimit int
jsonFormat bool
}
func createClientConfig(opts *options) (*rest.Config, error) {
if opts.kubeconfig == "" {
return rest.InClusterConfig()
}
return clientcmd.BuildConfigFromFlags("", opts.kubeconfig)
}
func createClientSet(config *rest.Config) (*kubernetes.Clientset, error) {
c, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, err
}
return c, nil
}
func main() {
opts := &options{}
kingpin.Flag("kubeconfig", "Path to kubeconfig.").StringVar(&opts.kubeconfig)
kingpin.Flag("namespace", "Namespace to monitor").Default("").StringVar(&opts.namespace)
kingpin.Flag("age-limit", "Will discard updates for resources old than n minutes. 0 disables").Default("5").IntVar(&opts.ageLimit)
kingpin.Flag("debug", "Debug mode").BoolVar(&opts.debug)
kingpin.Flag("slack-token", "").Envar("SLACK_TOKEN").StringVar(&opts.slackToken)
kingpin.Flag("aws-region", "").Envar("AWS_REGION").Default("eu-west-1").StringVar(&opts.awsRegion)
kingpin.Flag("json", "Output log data in JSON format").Default("false").BoolVar(&opts.jsonFormat)
kingpin.Parse()
if opts.debug {
log.SetLevel(log.DebugLevel)
log.Debugln("Debug logging enabled")
} else {
log.SetLevel(log.InfoLevel)
}
if opts.jsonFormat {
log.SetFormatter(&log.JSONFormatter{})
}
config, err := createClientConfig(opts)
if err != nil {
log.Fatalf("error creating client config: %s", err)
}
clientSet, err := createClientSet(config)
if err != nil {
log.Fatalf("error creating client: %s", err)
}
executionContext, stop := context.WithCancel(context.Background())
defer stop()
engine := engine.NewEngine(clientSet)
engine.AddRule(rules.UnsuccessfulExitRule)
engine.AddRule(rules.ResourceAnnotationRule)
engine.AddRule(rules.ScrapeNeedsPortsRule)
engine.AddRule(rules.RequireCronJobHistoryLimits)
engine.AddRule(rules.IngressNeedsAnnotation)
if len(opts.slackToken) > 0 {
engine.AddOutput(alerts.NewSlackOutput(opts.slackToken))
}
engine.AddOutput(alerts.NewSNSOutput(opts.awsRegion))
go engine.Run(executionContext, opts.namespace, opts.ageLimit)
select {}
}