From 71ad73cc86d0d76531761b992893ced374dc56eb Mon Sep 17 00:00:00 2001 From: Jay Gabriels Date: Sun, 15 Apr 2018 11:04:17 -0700 Subject: [PATCH] add slack and improve hpa --- alerts/slack.go | 21 +++++++++++++++++++++ checks/hpa.go | 16 ++++++++++++---- main.go | 12 ++++++------ 3 files changed, 39 insertions(+), 10 deletions(-) create mode 100644 alerts/slack.go diff --git a/alerts/slack.go b/alerts/slack.go new file mode 100644 index 0000000..4aad3e2 --- /dev/null +++ b/alerts/slack.go @@ -0,0 +1,21 @@ +package alerts + +import ( + "os" + + "github.com/bluele/slack" +) + +var ( + token = os.Getenv("SLACK_API_TOKEN") + channelName = os.Getenv("SLACK_CHANNEL") +) + +func main() { + + api := slack.New(token) + err := api.ChatPostMessage(channelName, "Hello, world!", nil) + if err != nil { + panic(err) + } +} diff --git a/checks/hpa.go b/checks/hpa.go index 58633f1..73b09eb 100644 --- a/checks/hpa.go +++ b/checks/hpa.go @@ -5,6 +5,7 @@ import ( "k8s.io/api/autoscaling/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/watch" "k8s.io/client-go/kubernetes" "k8s.io/client-go/rest" ) @@ -29,17 +30,24 @@ func HPA() { hpaWatch, _ := clientset.AutoscalingV1().HorizontalPodAutoscalers("").Watch(metav1.ListOptions{}) - data := <-hpaWatch.ResultChan() + for { + select { + case event := <-hpaWatch.ResultChan(): + handleEventz(event) + } + } +} - hpa := data.Object.(*v1.HorizontalPodAutoscaler) +func handleEventz(event watch.Event) { + hpa := event.Object.(*v1.HorizontalPodAutoscaler) if hpa.Status.CurrentReplicas > hpa.Status.DesiredReplicas { fmt.Println(hpa.ObjectMeta.Namespace + " is scaling DOWN") fmt.Println("Current VS Desired Replicas: ", hpa.Status.CurrentReplicas, hpa.Status.DesiredReplicas) - fmt.Println("Current CPU utilization: ", hpa.Status.CurrentCPUUtilizationPercentage) + fmt.Println("Current CPU utilization: ", *hpa.Status.CurrentCPUUtilizationPercentage) } else if hpa.Status.CurrentReplicas < hpa.Status.DesiredReplicas { fmt.Println(hpa.ObjectMeta.Namespace + " is scaling up") fmt.Println("Current VS Desired Replicas: ", hpa.Status.CurrentReplicas, hpa.Status.DesiredReplicas) - fmt.Println("Current CPU utilization: ", hpa.Status.CurrentCPUUtilizationPercentage) + fmt.Println("Current CPU utilization: ", *hpa.Status.CurrentCPUUtilizationPercentage) } } diff --git a/main.go b/main.go index 8631eed..9d888ac 100644 --- a/main.go +++ b/main.go @@ -12,11 +12,11 @@ func main() { fmt.Println("starting kubieous...") // How often should the checks be performed - podCheckTimer := time.NewTicker(30 * time.Second) - nodeCheckTimer := time.NewTicker(60 * time.Second) - hpaCheckTimer := time.NewTicker(5 * time.Second) + podCheckTimer := time.NewTicker(99999 * time.Second) + nodeCheckTimer := time.NewTicker(99999 * time.Second) - checks.PodEventStream() + // checks.PodEventStream() + go checks.HPA() // Montoring Loop for { @@ -25,8 +25,8 @@ func main() { go checks.Pods() case _ = <-nodeCheckTimer.C: go checks.Nodes() - case _ = <-hpaCheckTimer.C: - go checks.HPA() } } + + fmt.Println("stopping kubieous") }