Commit 1a6e814a authored by Adam Harrison's avatar Adam Harrison
Browse files

Basic slack notification support

parent 55ba7797
FROM ubuntu FROM ubuntu
RUN apt-get update && apt-get install -y ca-certificates && rm -rf /var/cache/apt
ADD https://storage.googleapis.com/kubernetes-release/release/v1.4.8/bin/linux/amd64/kubectl /usr/bin/kubectl ADD https://storage.googleapis.com/kubernetes-release/release/v1.4.8/bin/linux/amd64/kubectl /usr/bin/kubectl
RUN chmod 0755 /usr/bin/kubectl RUN chmod 0755 /usr/bin/kubectl
COPY ./kured /usr/bin/kured COPY ./kured /usr/bin/kured
......
...@@ -16,6 +16,7 @@ import ( ...@@ -16,6 +16,7 @@ import (
"github.com/weaveworks/kured/pkg/alerts" "github.com/weaveworks/kured/pkg/alerts"
"github.com/weaveworks/kured/pkg/daemonsetlock" "github.com/weaveworks/kured/pkg/daemonsetlock"
"github.com/weaveworks/kured/pkg/delaytick" "github.com/weaveworks/kured/pkg/delaytick"
"github.com/weaveworks/kured/pkg/notifications/slack"
) )
var ( var (
...@@ -27,6 +28,8 @@ var ( ...@@ -27,6 +28,8 @@ var (
prometheusURL string prometheusURL string
alertFilter *regexp.Regexp alertFilter *regexp.Regexp
rebootSentinel string rebootSentinel string
slackHookURL string
slackUsername string
) )
func main() { func main() {
...@@ -50,6 +53,11 @@ func main() { ...@@ -50,6 +53,11 @@ func main() {
rootCmd.PersistentFlags().StringVar(&rebootSentinel, "reboot-sentinel", "/var/run/reboot-required", rootCmd.PersistentFlags().StringVar(&rebootSentinel, "reboot-sentinel", "/var/run/reboot-required",
"path to file whose existence signals need to reboot") "path to file whose existence signals need to reboot")
rootCmd.PersistentFlags().StringVar(&slackHookURL, "slack-hook-url", "",
"slack hook URL for reboot notfications")
rootCmd.PersistentFlags().StringVar(&slackUsername, "slack-username", "kured",
"slack username for reboot notfications")
if err := rootCmd.Execute(); err != nil { if err := rootCmd.Execute(); err != nil {
log.Fatal(err) log.Fatal(err)
} }
...@@ -169,8 +177,15 @@ func waitForDrain(client *kubernetes.Clientset, nodeID string) { ...@@ -169,8 +177,15 @@ func waitForDrain(client *kubernetes.Clientset, nodeID string) {
} }
} }
func reboot() { func reboot(nodeID string) {
log.Infof("Commanding reboot") log.Infof("Commanding reboot")
if slackHookURL != "" {
if err := slack.NotifyReboot(slackHookURL, slackUsername, nodeID); err != nil {
log.Warnf("Error notifying slack: %v", err)
}
}
// Relies on /var/run/dbus/system_bus_socket bind mount to talk to systemd // Relies on /var/run/dbus/system_bus_socket bind mount to talk to systemd
rebootCmd := exec.Command("/bin/systemctl", "reboot") rebootCmd := exec.Command("/bin/systemctl", "reboot")
if err := rebootCmd.Run(); err != nil { if err := rebootCmd.Run(); err != nil {
...@@ -237,7 +252,7 @@ func root(cmd *cobra.Command, args []string) { ...@@ -237,7 +252,7 @@ func root(cmd *cobra.Command, args []string) {
drain(nodeID) drain(nodeID)
waitForDrain(client, nodeID) waitForDrain(client, nodeID)
} }
reboot() reboot(nodeID)
break break
} }
} }
......
...@@ -23,6 +23,7 @@ spec: ...@@ -23,6 +23,7 @@ spec:
# - --period=60 # - --period=60
# - --prometheus-url=http://prometheus.monitoring.svc.cluster.local # - --prometheus-url=http://prometheus.monitoring.svc.cluster.local
# - --reboot-sentinel=/var/run/reboot-required # - --reboot-sentinel=/var/run/reboot-required
# - --slack-hook-url=https://hooks.slack.com/...
env: env:
# Pass in the name of the node on which this pod is scheduled # Pass in the name of the node on which this pod is scheduled
# for use with drain/uncordon operations and lock acquisition # for use with drain/uncordon operations and lock acquisition
......
package slack
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"time"
)
var (
httpClient = &http.Client{Timeout: 5 * time.Second}
)
type body struct {
Text string `json:"text,omitempty"`
Username string `json:"username,omitempty"`
}
func NotifyReboot(hookURL, username, nodeID string) error {
msg := body{
Text: fmt.Sprintf("Rebooting node %s", nodeID),
Username: username,
}
var buf bytes.Buffer
if err := json.NewEncoder(&buf).Encode(&msg); err != nil {
return err
}
resp, err := httpClient.Post(hookURL, "application/json", &buf)
defer resp.Body.Close()
if err != nil {
return err
}
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
return fmt.Errorf(resp.Status)
}
return nil
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment