/
main.go
170 lines (146 loc) · 4.75 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package main
import (
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"context"
"fmt"
"os"
"os/signal"
"regexp"
"syscall"
"time"
"github.com/j2gg0s/kubenotify/pkg/client"
"github.com/j2gg0s/kubenotify/pkg/notify"
"github.com/j2gg0s/kubenotify/pkg/sentry"
"github.com/spf13/cobra"
"k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes"
)
var (
debug = false
outofCluster = false
kubeClient kubernetes.Interface
excludes = []string{
`metadata\.[acdfgmors].*`,
`status\..*`,
`spec\.template\.spec\.containers\.[123456789]`,
// jaeger injected
`metadata\.labels\.sidecar\.jaegertracing\.io\/injected`,
}
includes = []string{}
webhooks = []string{}
ignoreBefore = "1m"
includeResources = []string{}
includeNamespaces = []string{}
resync = "1m"
disableRevision = true
)
func main() {
root := cobra.Command{
Use: "kubenotify",
Long: "subscribe kubernetes workload change event, support Deployment, StatefulSet and DaemonSet",
}
root.PersistentFlags().BoolVar(&debug, "debug", debug, "enable debug log")
root.PersistentFlags().BoolVar(&disableRevision, "disable-revision", disableRevision, "disable revision")
root.PersistentFlags().BoolVar(&outofCluster, "outof-cluster", outofCluster, "use outof cluster config directly")
root.PersistentFlags().StringVar(&ignoreBefore, "ignore-before", ignoreBefore, "ignore create before when start")
root.PersistentFlags().StringSliceVar(&excludes, "excludes", excludes, "excludes resource field when diff")
root.PersistentFlags().StringSliceVar(&includes, "includes", includes, "only include resource field when diff")
root.PersistentFlags().StringSliceVar(&includeResources, "resources", includeResources, "watch only these resource, default all, support Deployment, StatefulSet, DaemonSet")
root.PersistentFlags().StringSliceVar(&includeNamespaces, "namespaces", includeNamespaces, "watch resource under these namepsace, default all")
root.PersistentFlags().StringVar(&resync, "resync", resync, "duration to resync resource")
root.PersistentFlags().StringSliceVar(&webhooks, "webhooks", webhooks, "webhook to notify")
root.PersistentPreRunE = func(*cobra.Command, []string) error {
if debug {
zerolog.SetGlobalLevel(zerolog.DebugLevel)
} else {
zerolog.SetGlobalLevel(zerolog.InfoLevel)
}
var err error
kubeClient, err = client.NewKubeClient(outofCluster)
if err != nil {
return err
}
return nil
}
root.RunE = func(cmd *cobra.Command, args []string) error {
var err error
opts := []sentry.Option{}
if debug {
opts = append(opts, sentry.EnableDebug())
}
if disableRevision {
opts = append(opts, sentry.DisableRevision())
}
if len(excludes) > 0 {
rExcludes := make([]*regexp.Regexp, 0, len(excludes))
for _, exclude := range excludes {
reg, err := regexp.Compile(exclude)
if err != nil {
return fmt.Errorf("compile regex %s: %w", exclude, err)
}
rExcludes = append(rExcludes, reg)
}
opts = append(opts, sentry.WithExcludes(rExcludes))
}
if len(includes) > 0 {
rIncludes := make([]*regexp.Regexp, 0, len(includes))
for _, include := range includes {
reg, err := regexp.Compile(include)
if err != nil {
return fmt.Errorf("compile regex %s: %w", include, err)
}
rIncludes = append(rIncludes, reg)
}
opts = append(opts, sentry.WithIncludes(rIncludes))
}
if len(includeResources) > 0 {
opts = append(opts, sentry.IncludeResources(includeResources...))
}
if len(includeNamespaces) > 0 {
opts = append(opts, sentry.IncludeNamespaces(includeNamespaces...))
}
d, err := time.ParseDuration(ignoreBefore)
if err != nil {
return fmt.Errorf("parse duration %s: %w", ignoreBefore, err)
}
opts = append(opts, sentry.WithIgnoreCreatedBefore(d))
ctx, cancel := context.WithCancel(cmd.Context())
defer cancel()
notifyFunc := notify.StdoutNotify()
if len(webhooks) > 0 {
notifyFunc = notify.WebhooksNotify(webhooks)
}
var informer informers.SharedInformerFactory
{
d, err := time.ParseDuration(resync)
if err != nil {
return fmt.Errorf("prase duration %s: %w", resync, err)
}
informer = informers.NewSharedInformerFactory(kubeClient, d)
}
ctl, err := sentry.New(
informer.Core().V1().Pods(),
informer.Apps().V1().ReplicaSets(),
informer.Apps().V1().Deployments(),
informer.Apps().V1().StatefulSets(),
informer.Apps().V1().DaemonSets(),
informer.Apps().V1().ControllerRevisions(),
notifyFunc,
opts...,
)
if err != nil {
return err
}
go ctl.Run(1, ctx.Done())
go informer.Start(ctx.Done())
sigterm := make(chan os.Signal, 1)
signal.Notify(sigterm, syscall.SIGTERM)
signal.Notify(sigterm, syscall.SIGINT)
<-sigterm
return nil
}
if err := root.Execute(); err != nil {
log.Err(err).Send()
}
}