-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
154 lines (134 loc) · 3.95 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package main
import (
"os"
"os/signal"
"syscall"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
klog "k8s.io/klog/v2"
)
// Set on build
var version string
func main() {
log.SetOutput(os.Stdout)
log.SetFormatter(&log.JSONFormatter{})
// Ensure klog also outputs to logrus
klog.SetOutput(log.StandardLogger().WriterLevel(log.WarnLevel))
app := cli.NewApp()
app.Name = "Shawarma"
app.Usage = "Sidecar for monitoring a Kubernetes service and notifying the main application when it is live"
app.Copyright = "(c) 2019-2022 CenterEdge Software"
app.Version = version
app.Flags = []cli.Flag{
&cli.StringFlag{
Name: "log-level",
Aliases: []string{"l"},
Usage: "Set the log level (panic, fatal, error, warn, info, debug, trace)",
Value: "warn",
EnvVars: []string{"LOG_LEVEL"},
},
&cli.StringFlag{
Name: "kubeconfig",
Usage: "Path to a kubeconfig file, if not running in-cluster",
},
}
app.Before = func(c *cli.Context) error {
// In case of empty environment variable, pull default here too
levelString := c.String("log-level")
if levelString == "" {
levelString = "warn"
}
level, err := log.ParseLevel(levelString)
if err != nil {
return err
}
log.SetLevel(level)
return nil
}
app.Commands = []*cli.Command{
{
Name: "monitor",
Aliases: []string{"m"},
Usage: "Monitor a Kubernetes service",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "service",
Aliases: []string{"svc"},
Usage: "Kubernetes service to monitor for this pod",
EnvVars: []string{"SHAWARMA_SERVICE"},
},
&cli.StringFlag{
Name: "service-labels",
Usage: "Kubernetes service labels to monitor for this pod, comma-delimited ex. \"label1=value1,label2=value2\"",
EnvVars: []string{"SHAWARMA_SERVICE_LABELS"},
},
&cli.StringFlag{
Name: "pod",
Aliases: []string{"p"},
Usage: "Kubernetes pod to monitor",
EnvVars: []string{"MY_POD_NAME"},
},
&cli.StringFlag{
Name: "namespace",
Aliases: []string{"n"},
Value: "default",
Usage: "Kubernetes namespace to monitor",
EnvVars: []string{"MY_POD_NAMESPACE"},
},
&cli.StringFlag{
Name: "url",
Aliases: []string{"u"},
Value: "http://localhost/applicationstate",
Usage: "URL which receives a POST on state change",
EnvVars: []string{"SHAWARMA_URL"},
},
&cli.BoolFlag{
Name: "disable-notifier",
Aliases: []string{"d"},
Usage: "Enable/Disable state change notification",
EnvVars: []string{"SHAWARMA_DISABLE_STATE_NOTIFIER"},
},
&cli.IntFlag{
Name: "listen-port",
Aliases: []string{"l"},
Value: 8099,
Usage: "Default port to be used to start the http server",
EnvVars: []string{"SHAWARMA_LISTEN_PORT"},
},
},
Action: func(c *cli.Context) error {
config := MonitorConfig{
Namespace: c.String("namespace"),
PodName: c.String("pod"),
ServiceName: c.String("service"),
ServiceLabelSelector: c.String("service-labels"),
URL: c.String("url"),
DisableStateNotifier: c.Bool("disable-notifier"),
PathToConfig: c.String("kubeconfig"),
}
if config.ServiceName == "" && config.ServiceLabelSelector == "" {
return cli.Exit("The service name or labels must be supplied", 1)
}
// In case of empty environment variable, pull default here too
if config.URL == "" {
config.URL = "http://localhost/applicationstate"
}
// Start server in a Go routine thread
go httpServer(c.String("listen-port"))
monitor := NewMonitor(config)
term := make(chan os.Signal, 1)
signal.Notify(term, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-term // wait for SIGINT or SIGTERM
log.Debug("Shutdown signal received")
monitor.Stop()
}()
return monitor.Start()
},
},
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}