Issue #27: Added filter to add warning if pod is created without any labels

This commit is contained in:
mugdha-adhav 2019-03-07 17:17:52 +05:30 committed by Prasad Ghangal
parent 22883ef37b
commit e518cb538c
2 changed files with 35 additions and 0 deletions

View File

@ -15,6 +15,7 @@ var (
Filters = []Filter{
filters.NewImageTagChecker(),
filters.NewIngressValidator(),
filters.NewPodLabelChecker(),
}
)

View File

@ -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!")
}