Skip to content
This repository has been archived by the owner on Apr 8, 2022. It is now read-only.

add HorizontalPodAutoscaler resource watch support #262

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ resourcesToWatch:
secret: false
configmap: false
ingress: false
hpa: false
slack:
channel: '#YOUR_CHANNEL'
token: 'xoxb-YOUR_TOKEN'
Expand Down Expand Up @@ -127,6 +128,7 @@ resource:
secret: false
configmap: false
ingress: false
hpa: false
```

#### Working with RBAC
Expand Down Expand Up @@ -333,6 +335,7 @@ resource:
secret: false
configmap: false
ingress: false
hpa: false
namespace: ""

```
Expand Down Expand Up @@ -372,6 +375,7 @@ Flags:
--sa watch for service accounts
--secret watch for plain secrets
--svc watch for services
--hpa watch for horizontal pod autoscalers

Use "kubewatch resource [command] --help" for more information about a command.

Expand Down Expand Up @@ -405,6 +409,7 @@ Global Flags:
--sa watch for service accounts
--secret watch for plain secrets
--svc watch for services
--hpa watch for horizontal pod autoscalers

```

Expand Down
5 changes: 5 additions & 0 deletions cmd/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ func configureResource(operation string, cmd *cobra.Command, conf *config.Config
"sa",
&conf.Resource.ServiceAccount,
},
{
"hpa",
&conf.Resource.HorizontalPodAutoscaler,
},
}

for _, flag := range flags {
Expand Down Expand Up @@ -188,4 +192,5 @@ func init() {
resourceConfigCmd.PersistentFlags().Bool("node", false, "watch for Nodes")
resourceConfigCmd.PersistentFlags().Bool("clusterrole", false, "watch for cluster roles")
resourceConfigCmd.PersistentFlags().Bool("sa", false, "watch for service accounts")
resourceConfigCmd.PersistentFlags().Bool("hpa", false, "watch for horizontal pod autoscalers")
}
34 changes: 19 additions & 15 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,22 @@ type Handler struct {

// Resource contains resource configuration
type Resource struct {
Deployment bool `json:"deployment"`
ReplicationController bool `json:"rc"`
ReplicaSet bool `json:"rs"`
DaemonSet bool `json:"ds"`
Services bool `json:"svc"`
Pod bool `json:"po"`
Job bool `json:"job"`
Node bool `json:"node"`
ClusterRole bool `json:"clusterrole"`
ServiceAccount bool `json:"sa"`
PersistentVolume bool `json:"pv"`
Namespace bool `json:"ns"`
Secret bool `json:"secret"`
ConfigMap bool `json:"configmap"`
Ingress bool `json:"ing"`
Deployment bool `json:"deployment"`
ReplicationController bool `json:"rc"`
ReplicaSet bool `json:"rs"`
DaemonSet bool `json:"ds"`
Services bool `json:"svc"`
Pod bool `json:"po"`
Job bool `json:"job"`
Node bool `json:"node"`
ClusterRole bool `json:"clusterrole"`
ServiceAccount bool `json:"sa"`
PersistentVolume bool `json:"pv"`
Namespace bool `json:"ns"`
Secret bool `json:"secret"`
ConfigMap bool `json:"configmap"`
Ingress bool `json:"ing"`
HorizontalPodAutoscaler bool `json:"hpa"`
}

// Config struct contains kubewatch configuration
Expand Down Expand Up @@ -255,6 +256,9 @@ func (c *Config) CheckMissingResourceEnvvars() {
if !c.Resource.ClusterRole && os.Getenv("KW_CLUSTER_ROLE") == "true" {
c.Resource.ClusterRole = true
}
if !c.Resource.HorizontalPodAutoscaler && os.Getenv("KW_HPA") == "true" {
c.Resource.HorizontalPodAutoscaler = true
}
if (c.Handler.Slack.Channel == "") && (os.Getenv("SLACK_CHANNEL") != "") {
c.Handler.Slack.Channel = os.Getenv("SLACK_CHANNEL")
}
Expand Down
1 change: 1 addition & 0 deletions kubewatch-configmap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ data:
pod: true
secret: false
configmap: false
hpa: false
23 changes: 23 additions & 0 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package controller

import (
"fmt"
v1 "k8s.io/api/autoscaling/v1"
"os"
"os/signal"
"strings"
Expand Down Expand Up @@ -502,6 +503,28 @@ func Start(conf *config.Config, eventHandler handlers.Handler) {
go c.Run(stopCh)
}

if conf.Resource.HorizontalPodAutoscaler {
informer := cache.NewSharedIndexInformer(
&cache.ListWatch{
ListFunc: func(options meta_v1.ListOptions) (runtime.Object, error) {
return kubeClient.AutoscalingV1().HorizontalPodAutoscalers(conf.Namespace).List(options)
},
WatchFunc: func(options meta_v1.ListOptions) (watch.Interface, error) {
return kubeClient.AutoscalingV1().HorizontalPodAutoscalers(conf.Namespace).Watch(options)
},
},
&v1.HorizontalPodAutoscaler{},
0, //Skip resync
cache.Indexers{},
)

c := newResourceController(kubeClient, eventHandler, informer, "HorizontalPodAutoscaler")
stopCh := make(chan struct{})
defer close(stopCh)

go c.Run(stopCh)
}

sigterm := make(chan os.Signal, 1)
signal.Notify(sigterm, syscall.SIGTERM)
signal.Notify(sigterm, syscall.SIGINT)
Expand Down
3 changes: 3 additions & 0 deletions pkg/event/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package event

import (
"fmt"
v1 "k8s.io/api/autoscaling/v1"

"github.com/bitnami-labs/kubewatch/pkg/utils"
apps_v1 "k8s.io/api/apps/v1"
Expand Down Expand Up @@ -86,6 +87,8 @@ func New(obj interface{}, action string) Event {
kind = "cluster role"
case *api_v1.ServiceAccount:
kind = "service account"
case *v1.HorizontalPodAutoscaler:
kind = "horizontal pod autoscaler"
case Event:
name = object.Name
kind = object.Kind
Expand Down