From dca60a88d89cd148272853f36a07add8b8365ff3 Mon Sep 17 00:00:00 2001 From: mugdha-adhav Date: Thu, 7 Mar 2019 17:17:52 +0530 Subject: [PATCH] Issue #27: Added filter to add warning if pod is created without any labels --- pkg/filterengine/filterengine.go | 1 + pkg/filterengine/filters/pod_label_checker.go | 34 +++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 pkg/filterengine/filters/pod_label_checker.go diff --git a/pkg/filterengine/filterengine.go b/pkg/filterengine/filterengine.go index 7bc3fd582..48050ec23 100644 --- a/pkg/filterengine/filterengine.go +++ b/pkg/filterengine/filterengine.go @@ -15,6 +15,7 @@ var ( Filters = []Filter{ filters.NewImageTagChecker(), filters.NewIngressValidator(), + filters.NewPodLabelChecker(), } ) diff --git a/pkg/filterengine/filters/pod_label_checker.go b/pkg/filterengine/filters/pod_label_checker.go new file mode 100644 index 000000000..5b6f7f652 --- /dev/null +++ b/pkg/filterengine/filters/pod_label_checker.go @@ -0,0 +1,34 @@ +package filters + +import ( + "github.com/infracloudio/botkube/pkg/events" + log "github.com/infracloudio/botkube/pkg/logging" + + apiV1 "k8s.io/api/core/v1" +) + +// PodLabelChecker add recommendations to the event object if pod created without any labels +type PodLabelChecker struct { +} + +// NewPodLabelChecker creates new PodLabelChecker object +func NewPodLabelChecker() *PodLabelChecker { + return &PodLabelChecker{} +} + +// Run filters and modifies event struct +func (f *PodLabelChecker) Run(object interface{}, event *events.Event) { + if event.Kind != "Pod" && event.Type != "create" { + return + } + podObj, ok := object.(*apiV1.Pod) + if !ok { + return + } + + // Check labels in pod + if len(podObj.ObjectMeta.Labels) == 0 { + event.Recommendations = append(event.Recommendations, "pod '"+podObj.ObjectMeta.Name+"' creation without labels should be avoided.\n") + } + log.Logger.Debug("Pod label filter successful!") +}