Skip to content

Commit

Permalink
🐛 ignoring reasons and namespace (#312)
Browse files Browse the repository at this point in the history
* 🐛 ignoring reasons and namespace

* 🐛 ignoring reasons and namespace
  • Loading branch information
abahmed authored Jun 9, 2024
1 parent abe81a9 commit 88c55ac
Show file tree
Hide file tree
Showing 12 changed files with 90 additions and 73 deletions.
3 changes: 2 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion filter/containerNameFilter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
29 changes: 15 additions & 14 deletions filter/containerReasonsFilter.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,33 +20,34 @@ 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
}

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,
Expand Down
8 changes: 4 additions & 4 deletions filter/nsFilter.go → filter/namespaceFilter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@ 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
}

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
}
Expand Down
9 changes: 0 additions & 9 deletions filter/podEventsFilter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions filter/podNameFilter.go
Original file line number Diff line number Diff line change
@@ -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
}
20 changes: 10 additions & 10 deletions go.mod
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
42 changes: 20 additions & 22 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down Expand Up @@ -94,23 +94,21 @@ 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=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
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=
Expand All @@ -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=
Expand Down
6 changes: 4 additions & 2 deletions handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{},
Expand Down
8 changes: 1 addition & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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) {
Expand Down
8 changes: 7 additions & 1 deletion watcher/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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(
Expand Down
8 changes: 6 additions & 2 deletions watcher/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -57,7 +62,6 @@ func (w *Watcher) runWorker() {

func (w *Watcher) processNextItem() bool {
newEvent, quit := w.queue.Get()

if quit {
return false
}
Expand Down

0 comments on commit 88c55ac

Please sign in to comment.