diff --git a/config/config.go b/config/config.go index a826982a..8f81fbd8 100644 --- a/config/config.go +++ b/config/config.go @@ -55,7 +55,8 @@ type Config struct { AllowedReasons []string ForbiddenReasons []string - // Patterns are compiled from IgnorePodNames after loading + // Patterns are compiled from IgnorePodNames after populating + // IgnorePodNames configuration IgnorePodNamePatterns []*regexp.Regexp } diff --git a/filter/containerNameFilter.go b/filter/containerNameFilter.go index 0046da54..0e4511d1 100644 --- a/filter/containerNameFilter.go +++ b/filter/containerNameFilter.go @@ -12,7 +12,7 @@ func (f ContainerNameFilter) Execute(ctx *Context) bool { if len(ctx.Config.IgnoreContainerNames) > 0 && slices.Contains(ctx.Config.IgnoreContainerNames, container.Name) { logrus.Infof( - "skip pod %s as in container ignore list", + "skipping container %s as it is in the container ignore list", container.Name) return true } diff --git a/filter/containerReasonsFilter.go b/filter/containerReasonsFilter.go index b6b377a6..08ed81e6 100644 --- a/filter/containerReasonsFilter.go +++ b/filter/containerReasonsFilter.go @@ -20,10 +20,23 @@ func (f ContainerReasonsFilter) Execute(ctx *Context) bool { ctx.Container.LastTerminatedOn = container.State.Terminated.StartedAt.Time } + if (ctx.Container.Reason == "CrashLoopBackOff" || + ctx.Container.HasRestarts) && + container.LastTerminationState.Terminated != nil { + ctx.Container.Reason = + container.LastTerminationState.Terminated.Reason + ctx.Container.Msg = + container.LastTerminationState.Terminated.Message + ctx.Container.ExitCode = + container.LastTerminationState.Terminated.ExitCode + ctx.Container.LastTerminatedOn = + container.LastTerminationState.Terminated.StartedAt.Time + } + if len(ctx.Config.AllowedReasons) > 0 && !slices.Contains(ctx.Config.AllowedReasons, ctx.Container.Reason) { logrus.Infof( - "skip reason %s as not in reason allow list", + "skipping reason %s as it is not in the reason allow list", ctx.Container.Reason) return true } @@ -31,22 +44,10 @@ func (f ContainerReasonsFilter) Execute(ctx *Context) bool { if len(ctx.Config.ForbiddenReasons) > 0 && slices.Contains(ctx.Config.ForbiddenReasons, ctx.Container.Reason) { logrus.Infof( - "skip reason %s as in reason forbid list", + "skipping reason %s as it is in the reason forbid list", ctx.Container.Reason) return true } - if (ctx.Container.Reason == "CrashLoopBackOff" || - ctx.Container.HasRestarts) && - container.LastTerminationState.Terminated != nil { - ctx.Container.Reason = - container.LastTerminationState.Terminated.Reason - ctx.Container.Msg = - container.LastTerminationState.Terminated.Message - ctx.Container.ExitCode = - container.LastTerminationState.Terminated.ExitCode - ctx.Container.LastTerminatedOn = - container.LastTerminationState.Terminated.StartedAt.Time - } lastState := ctx.Memory.GetPodContainer(ctx.Pod.Namespace, ctx.Pod.Name, diff --git a/filter/nsFilter.go b/filter/namespaceFilter.go similarity index 69% rename from filter/nsFilter.go rename to filter/namespaceFilter.go index dcbc609f..a64098a0 100644 --- a/filter/nsFilter.go +++ b/filter/namespaceFilter.go @@ -5,14 +5,14 @@ import ( "golang.org/x/exp/slices" ) -type NsFilter struct{} +type NamespaceFilter struct{} -func (f NsFilter) Execute(ctx *Context) bool { +func (f NamespaceFilter) Execute(ctx *Context) bool { // filter by namespaces in config if specified if len(ctx.Config.AllowedNamespaces) > 0 && !slices.Contains(ctx.Config.AllowedNamespaces, ctx.Pod.Namespace) { logrus.Infof( - "skip namespace %s as not in namespace allow list", + "skipping namespace %s as it is not in the namespace allow list", ctx.Pod.Namespace) return true } @@ -20,7 +20,7 @@ func (f NsFilter) Execute(ctx *Context) bool { if len(ctx.Config.ForbiddenNamespaces) > 0 && slices.Contains(ctx.Config.ForbiddenNamespaces, ctx.Pod.Namespace) { logrus.Infof( - "skip namespace %s as in namespace forbid list", + "skipping namespace %s as it is in the namespace forbid list", ctx.Pod.Namespace) return true } diff --git a/filter/podEventsFilter.go b/filter/podEventsFilter.go index d7cf7f75..3ac97390 100644 --- a/filter/podEventsFilter.go +++ b/filter/podEventsFilter.go @@ -27,15 +27,6 @@ func (f PodEventsFilter) Execute(ctx *Context) bool { ctx.ContainersHasIssues = false return true } - - /* - if ev.Reason == "FailedScheduling" || - ev.Reason == "NetworkNotReady" || - ev.Reason == "FailedMount" { - ctx.PodHasIssues = true - ctx.ContainersHasIssues = false - return false - }*/ } } return false diff --git a/filter/podNameFilter.go b/filter/podNameFilter.go new file mode 100644 index 00000000..c8903b38 --- /dev/null +++ b/filter/podNameFilter.go @@ -0,0 +1,20 @@ +package filter + +import ( + "github.com/sirupsen/logrus" +) + +type PodNameFilter struct{} + +func (f PodNameFilter) Execute(ctx *Context) bool { + for _, pattern := range ctx.Config.IgnorePodNamePatterns { + if pattern.MatchString(ctx.Pod.Name) { + logrus.Infof( + "skipping pod %s as it is in the ignore pod name list", + ctx.Pod.Name) + return true + } + } + + return false +} diff --git a/go.mod b/go.mod index a9c17527..434afb66 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/abahmed/kwatch -go 1.22.3 +go 1.22.4 require ( github.com/bwmarrin/discordgo v0.28.1 @@ -17,7 +17,7 @@ require ( require ( github.com/davecgh/go-spew v1.1.1 // indirect - github.com/emicklei/go-restful/v3 v3.12.0 // indirect + github.com/emicklei/go-restful/v3 v3.12.1 // indirect github.com/evanphx/json-patch v5.9.0+incompatible // indirect github.com/go-logr/logr v1.4.2 // indirect github.com/go-openapi/jsonpointer v0.21.0 // indirect @@ -30,7 +30,7 @@ require ( github.com/google/go-querystring v1.1.0 // indirect github.com/google/gofuzz v1.2.0 // indirect github.com/google/uuid v1.6.0 // indirect - github.com/gorilla/websocket v1.5.1 // indirect + github.com/gorilla/websocket v1.5.2 // indirect github.com/imdario/mergo v0.3.13 // indirect github.com/josharian/intern v1.0.0 // indirect github.com/json-iterator/go v1.1.12 // indirect @@ -41,13 +41,13 @@ require ( github.com/pkg/errors v0.9.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/spf13/pflag v1.0.5 // indirect - golang.org/x/crypto v0.23.0 // indirect - golang.org/x/exp v0.0.0-20240525044651-4c93da0ed11d - golang.org/x/net v0.25.0 // indirect - golang.org/x/oauth2 v0.20.0 // indirect - golang.org/x/sys v0.20.0 // indirect - golang.org/x/term v0.20.0 // indirect - golang.org/x/text v0.15.0 // indirect + golang.org/x/crypto v0.24.0 // indirect + golang.org/x/exp v0.0.0-20240604190554-fc45aab8b7f8 + golang.org/x/net v0.26.0 // indirect + golang.org/x/oauth2 v0.21.0 // indirect + golang.org/x/sys v0.21.0 // indirect + golang.org/x/term v0.21.0 // indirect + golang.org/x/text v0.16.0 // indirect golang.org/x/time v0.5.0 // indirect google.golang.org/protobuf v1.34.1 // indirect gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect diff --git a/go.sum b/go.sum index 1121ea78..e18ee924 100644 --- a/go.sum +++ b/go.sum @@ -3,8 +3,8 @@ github.com/bwmarrin/discordgo v0.28.1/go.mod h1:NJZpH+1AfhIcyQsPeuBKsUtYrRnjkyu0 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/emicklei/go-restful/v3 v3.12.0 h1:y2DdzBAURM29NFF94q6RaY4vjIH1rtwDapwQtU84iWk= -github.com/emicklei/go-restful/v3 v3.12.0/go.mod h1:6n3XBCmQQb25CM2LCACGz8ukIrRry+4bhvbpWn3mrbc= +github.com/emicklei/go-restful/v3 v3.12.1 h1:PJMDIM/ak7btuL8Ex0iYET9hxM3CI2sjZtzpL63nKAU= +github.com/emicklei/go-restful/v3 v3.12.1/go.mod h1:6n3XBCmQQb25CM2LCACGz8ukIrRry+4bhvbpWn3mrbc= github.com/evanphx/json-patch v5.9.0+incompatible h1:fBXyNpNMuTTDdquAq/uisOr2lShz4oaXpDTX2bLe7ls= github.com/evanphx/json-patch v5.9.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk= github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY= @@ -43,8 +43,8 @@ github.com/google/pprof v0.0.0-20240424215950-a892ee059fd6/go.mod h1:kf6iHlnVGwg github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= -github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/QY= -github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY= +github.com/gorilla/websocket v1.5.2 h1:qoW6V1GT3aZxybsbC6oLnailWnB+qTMVwMreOso9XUw= +github.com/gorilla/websocket v1.5.2/go.mod h1:0n9H61RBAcf5/38py2MCYbxzPIY9rOkpvvMT24Rqs30= github.com/imdario/mergo v0.3.13 h1:lFzP57bqS/wsqKssCGmtLAb8A0wKjLGrve2q3PPVcBk= github.com/imdario/mergo v0.3.13/go.mod h1:4lJ1jqUDcsbIECGy0RUJAXNIhg+6ocWgb1ALK2O4oXg= github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY= @@ -94,12 +94,10 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4= -golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI= -golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8= -golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842 h1:vr/HnozRka3pE4EsMEg1lgkXJkTFJCVUX+S/ZT6wYzM= -golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842/go.mod h1:XtvwrStGgqGPLc4cjQfWqZHG1YFdYs6swckp8vpsjnc= -golang.org/x/exp v0.0.0-20240525044651-4c93da0ed11d h1:N0hmiNbwsSNwHBAvR3QB5w25pUwH4tK0Y/RltD1j1h4= -golang.org/x/exp v0.0.0-20240525044651-4c93da0ed11d/go.mod h1:XtvwrStGgqGPLc4cjQfWqZHG1YFdYs6swckp8vpsjnc= +golang.org/x/crypto v0.24.0 h1:mnl8DM0o513X8fdIkmyFE/5hTYxbwYOjDS/+rK6qpRI= +golang.org/x/crypto v0.24.0/go.mod h1:Z1PMYSOR5nyMcyAVAIQSKCDwalqy85Aqn1x3Ws4L5DM= +golang.org/x/exp v0.0.0-20240604190554-fc45aab8b7f8 h1:LoYXNGAShUG3m/ehNk4iFctuhGX/+R1ZpfJ4/ia80JM= +golang.org/x/exp v0.0.0-20240604190554-fc45aab8b7f8/go.mod h1:jj3sYF3dwk5D+ghuXyeI3r5MFf+NT2An6/9dOA95KSI= golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= @@ -107,10 +105,10 @@ golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLL golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= -golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac= -golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM= -golang.org/x/oauth2 v0.20.0 h1:4mQdhULixXKP1rwYBW0vAijoXnkTG0BLCDRzfe1idMo= -golang.org/x/oauth2 v0.20.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI= +golang.org/x/net v0.26.0 h1:soB7SVo0PWrY4vPW/+ay0jKDNScG2X9wFeYlXIvJsOQ= +golang.org/x/net v0.26.0/go.mod h1:5YKkiSynbBIh3p6iOc/vibscux0x38BZDkn8sCUPxHE= +golang.org/x/oauth2 v0.21.0 h1:tsimM75w1tF/uws5rbeHzIWxEqElMehnc+iW793zsZs= +golang.org/x/oauth2 v0.21.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= @@ -119,23 +117,23 @@ golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y= -golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws= +golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= -golang.org/x/term v0.20.0 h1:VnkxpohqXaOBYJtBmEppKUG6mXpi+4O6purfc2+sMhw= -golang.org/x/term v0.20.0/go.mod h1:8UkIAJTvZgivsXaD6/pH6U9ecQzZ45awqEOzuCvwpFY= +golang.org/x/term v0.21.0 h1:WVXCp+/EBEHOj53Rvu+7KiT/iElMrO8ACK16SMZ3jaA= +golang.org/x/term v0.21.0/go.mod h1:ooXLefLobQVslOqselCNF4SxFAaoS6KujMbsGzSDmX0= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk= -golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4= +golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI= golang.org/x/time v0.5.0 h1:o7cqy6amK/52YcAKIPlM3a+Fpj35zvRj2TP+e1xFSfk= golang.org/x/time v0.5.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= -golang.org/x/tools v0.21.0 h1:qc0xYgIbsSDt9EyWz05J5wfa7LOVW0YTLOXrqdLAWIw= -golang.org/x/tools v0.21.0/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk= +golang.org/x/tools v0.22.0 h1:gqSGLZqv+AI9lIQzniJ0nZDRG5GBPsSi+DRNHWNz6yA= +golang.org/x/tools v0.22.0/go.mod h1:aCwcsjqvq7Yqt6TNyX7QMU2enbQ/Gt0bo6krSeEri+c= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/handler/handler.go b/handler/handler.go index c02b6ae9..d8aa6d15 100644 --- a/handler/handler.go +++ b/handler/handler.go @@ -29,13 +29,15 @@ func NewHandler( alertManager *alertmanager.AlertManager) Handler { // Order is important podFilters := []filter.Filter{ - filter.NsFilter{}, + filter.NamespaceFilter{}, + filter.PodNameFilter{}, filter.PodStatusFilter{}, filter.PodEventsFilter{}, - //filter.PodOwnersFilter{}, + filter.PodOwnersFilter{}, } containersFilters := []filter.Filter{ + filter.NamespaceFilter{}, filter.ContainerNameFilter{}, filter.ContainerRestartsFilter{}, filter.ContainerStateFilter{}, diff --git a/main.go b/main.go index c6bbffcb..630d4a55 100644 --- a/main.go +++ b/main.go @@ -14,7 +14,6 @@ import ( "github.com/abahmed/kwatch/version" "github.com/abahmed/kwatch/watcher" "github.com/sirupsen/logrus" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) func main() { @@ -54,13 +53,8 @@ func main() { &alertManager, ) - namespace := metav1.NamespaceAll - if len(config.AllowedNamespaces) == 1 { - namespace = config.AllowedNamespaces[0] - } - // start watcher - watcher.Start(client, namespace, h.ProcessPod) + watcher.Start(client, config, h.ProcessPod) } func setLogFormatter(formatter string) { diff --git a/watcher/start.go b/watcher/start.go index 147cb77d..46e57b6c 100644 --- a/watcher/start.go +++ b/watcher/start.go @@ -3,6 +3,7 @@ package watcher import ( "context" + "github.com/abahmed/kwatch/config" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -16,8 +17,13 @@ import ( // Start creates an instance of watcher after initialization and runs it func Start( client kubernetes.Interface, - namespace string, + config *config.Config, handleFunc func(string, *corev1.Pod)) { + namespace := metav1.NamespaceAll + if len(config.AllowedNamespaces) == 1 { + namespace = config.AllowedNamespaces[0] + } + watchFunc := func(options metav1.ListOptions) (watch.Interface, error) { return client.CoreV1().Pods(namespace).Watch( diff --git a/watcher/watcher.go b/watcher/watcher.go index 6e72c997..21fa86c1 100644 --- a/watcher/watcher.go +++ b/watcher/watcher.go @@ -41,7 +41,12 @@ func (w *Watcher) processEvents() { } for event := range w.watcher.ResultChan() { - pod := event.Object.(*corev1.Pod) + pod, ok := event.Object.(*corev1.Pod) + if !ok { + logrus.Warnf("failed to cast event to pod object: %v", event.Object) + continue + } + w.queue.Add(watcherEvent{ eventType: string(event.Type), pod: pod.DeepCopy(), @@ -57,7 +62,6 @@ func (w *Watcher) runWorker() { func (w *Watcher) processNextItem() bool { newEvent, quit := w.queue.Get() - if quit { return false }