Skip to content

Commit 4fb8f99

Browse files
committed
fmt and linters
We're not yet ready for golint though.
1 parent b16f10c commit 4fb8f99

File tree

10 files changed

+89
-36
lines changed

10 files changed

+89
-36
lines changed

Makefile

+26-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,35 @@
11

22
all: build
33

4+
tools:
5+
which gometalinter || ( go get -u github.com/alecthomas/gometalinter && gometalinter --install )
6+
which glide || go get -u github.com/Masterminds/glide
7+
8+
lint:
9+
gometalinter -j 1 --vendor --disable-all \
10+
--enable=errcheck \
11+
--enable=vet \
12+
--enable=vetshadow \
13+
--enable=structcheck \
14+
--enable=aligncheck \
15+
--enable=deadcode \
16+
--enable=ineffassign \
17+
--enable=dupl \
18+
--enable=gotype \
19+
--enable=varcheck \
20+
--enable=interfacer \
21+
--enable=goconst \
22+
--enable=megacheck \
23+
--enable=unparam \
24+
--enable=misspell \
25+
--enable=gas \
26+
--enable=goimports \
27+
./...
28+
429
fmt:
530
go fmt ./...
631

732
deps:
8-
which glide || go get -u github.com/Masterminds/glide
933
glide install
1034

1135
build:
@@ -20,4 +44,4 @@ clean:
2044
test:
2145
go test -v ./...
2246

23-
.PHONY: fmt install clean test all
47+
.PHONY: tools lint fmt install clean test all

cmd/execute.go

+38-17
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cmd
22

33
import (
4+
"log"
45
"os"
56
"strings"
67

@@ -10,11 +11,11 @@ import (
1011
"github.com/spf13/viper"
1112

1213
"github.com/bpineau/kube-alert/config"
13-
"github.com/bpineau/kube-alert/pkg/log"
14+
klog "github.com/bpineau/kube-alert/pkg/log"
1415
"github.com/bpineau/kube-alert/pkg/run"
1516
)
1617

17-
const AppName = "kube-alert"
18+
const appName = "kube-alert"
1819

1920
var (
2021
cfgFile string
@@ -30,14 +31,14 @@ var (
3031

3132
// rootCmd represents the base command when called without any subcommands
3233
rootCmd = &cobra.Command{
33-
Use: AppName,
34+
Use: appName,
3435
Short: "Monitor pods",
3536
Long: "Monitor pods and alert on failure",
3637

3738
Run: func(cmd *cobra.Command, args []string) {
3839
config := &config.AlertConfig{
3940
DryRun: viper.GetBool("dry-run"),
40-
Logger: log.New(viper.GetString("log.level"), viper.GetString("log.server"), viper.GetString("log.output")),
41+
Logger: klog.New(viper.GetString("log.level"), viper.GetString("log.server"), viper.GetString("log.output")),
4142
DdAppKey: viper.GetString("datadog.app-key"),
4243
DdApiKey: viper.GetString("datadog.api-key"),
4344
HealthPort: viper.GetInt("healthcheck-port"),
@@ -58,44 +59,64 @@ func Execute() {
5859
func init() {
5960
cobra.OnInitialize(initConfig)
6061

61-
defaultCfg := "/etc/" + AppName + "/" + AppName + ".yaml"
62+
defaultCfg := "/etc/" + appName + "/" + appName + ".yaml"
6263
rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", defaultCfg, "configuration file")
6364

6465
rootCmd.PersistentFlags().StringVarP(&apiServer, "api-server", "s", "", "kube api server url")
65-
viper.BindPFlag("api-server", rootCmd.PersistentFlags().Lookup("api-server"))
66+
if err := viper.BindPFlag("api-server", rootCmd.PersistentFlags().Lookup("api-server")); err != nil {
67+
log.Fatal("Failed to bind cli argument:", err)
68+
}
6669

6770
rootCmd.PersistentFlags().StringVarP(&kubeConf, "kube-config", "k", "", "kube config path")
68-
viper.BindPFlag("kube-config", rootCmd.PersistentFlags().Lookup("kube-config"))
69-
viper.BindEnv("kube-config", "KUBECONFIG")
71+
if err := viper.BindPFlag("kube-config", rootCmd.PersistentFlags().Lookup("kube-config")); err != nil {
72+
log.Fatal("Failed to bind cli argument:", err)
73+
}
74+
if err := viper.BindEnv("kube-config", "KUBECONFIG"); err != nil {
75+
log.Fatal("Failed to bind cli argument:", err)
76+
}
7077

7178
rootCmd.PersistentFlags().BoolVarP(&dryRun, "dry-run", "d", false, "dry-run mode")
72-
viper.BindPFlag("dry-run", rootCmd.PersistentFlags().Lookup("dry-run"))
79+
if err := viper.BindPFlag("dry-run", rootCmd.PersistentFlags().Lookup("dry-run")); err != nil {
80+
log.Fatal("Failed to bind cli argument:", err)
81+
}
7382

7483
rootCmd.PersistentFlags().StringVarP(&logLevel, "log-level", "v", "debug", "log level")
75-
viper.BindPFlag("log.level", rootCmd.PersistentFlags().Lookup("log-level"))
84+
if err := viper.BindPFlag("log.level", rootCmd.PersistentFlags().Lookup("log-level")); err != nil {
85+
log.Fatal("Failed to bind cli argument:", err)
86+
}
7687

7788
rootCmd.PersistentFlags().StringVarP(&logOutput, "log-output", "l", "stderr", "log output")
78-
viper.BindPFlag("log.output", rootCmd.PersistentFlags().Lookup("log-output"))
89+
if err := viper.BindPFlag("log.output", rootCmd.PersistentFlags().Lookup("log-output")); err != nil {
90+
log.Fatal("Failed to bind cli argument:", err)
91+
}
7992

8093
rootCmd.PersistentFlags().StringVarP(&logServer, "log-server", "r", "", "log server (if using syslog)")
81-
viper.BindPFlag("log.server", rootCmd.PersistentFlags().Lookup("log-server"))
94+
if err := viper.BindPFlag("log.server", rootCmd.PersistentFlags().Lookup("log-server")); err != nil {
95+
log.Fatal("Failed to bind cli argument:", err)
96+
}
8297

8398
rootCmd.PersistentFlags().StringVarP(&ddAppKey, "datadog-app-key", "a", "", "datadog app key")
84-
viper.BindPFlag("datadog.app-key", rootCmd.PersistentFlags().Lookup("datadog-app-key"))
99+
if err := viper.BindPFlag("datadog.app-key", rootCmd.PersistentFlags().Lookup("datadog-app-key")); err != nil {
100+
log.Fatal("Failed to bind cli argument:", err)
101+
}
85102

86103
rootCmd.PersistentFlags().StringVarP(&ddApiKey, "datadog-api-key", "i", "", "datadog api key")
87-
viper.BindPFlag("datadog.api-key", rootCmd.PersistentFlags().Lookup("datadog-api-key"))
104+
if err := viper.BindPFlag("datadog.api-key", rootCmd.PersistentFlags().Lookup("datadog-api-key")); err != nil {
105+
log.Fatal("Failed to bind cli argument:", err)
106+
}
88107

89108
rootCmd.PersistentFlags().IntVarP(&healthP, "healthcheck-port", "p", 0, "port for answering healthchecks")
90-
viper.BindPFlag("healthcheck-port", rootCmd.PersistentFlags().Lookup("healthcheck-port"))
109+
if err := viper.BindPFlag("healthcheck-port", rootCmd.PersistentFlags().Lookup("healthcheck-port")); err != nil {
110+
log.Fatal("Failed to bind cli argument:", err)
111+
}
91112
}
92113

93114
func initConfig() {
94115
viper.SetConfigType("yaml")
95-
viper.SetConfigName(AppName)
116+
viper.SetConfigName(appName)
96117

97118
// all possible config file paths, by priority
98-
viper.AddConfigPath("/etc/" + AppName + "/")
119+
viper.AddConfigPath("/etc/" + appName + "/")
99120
if home, err := homedir.Dir(); err == nil {
100121
viper.AddConfigPath(home)
101122
}

config/config.go

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package config
22

33
import (
44
"fmt"
5+
56
"github.com/bpineau/kube-alert/pkg/clientset"
67
"github.com/sirupsen/logrus"
78
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

pkg/controllers/controllers.go

+11-9
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import (
44
"fmt"
55
"os"
66
"os/signal"
7-
"syscall"
87
"strings"
8+
"syscall"
99
"time"
1010

1111
"github.com/bpineau/kube-alert/config"
@@ -31,17 +31,19 @@ type Controller interface {
3131
}
3232

3333
type CommonController struct {
34-
Conf *config.AlertConfig
35-
Queue workqueue.RateLimitingInterface
36-
Informer cache.SharedIndexInformer
37-
Handler handlers.Handler
38-
Name string
39-
ListWatch cache.ListerWatcher
40-
ObjType runtime.Object
34+
Conf *config.AlertConfig
35+
Queue workqueue.RateLimitingInterface
36+
Informer cache.SharedIndexInformer
37+
Handler handlers.Handler
38+
Name string
39+
ListWatch cache.ListerWatcher
40+
ObjType runtime.Object
4141
}
4242

4343
func (c *CommonController) Start() {
44-
c.Handler.Init(c.Conf)
44+
if err := c.Handler.Init(c.Conf); err != nil {
45+
c.Conf.Logger.Fatalf("Failed to init %s handler: %s", c.Name, err)
46+
}
4547

4648
c.startInformer()
4749
stopCh := make(chan struct{})

pkg/handlers/cs/cs.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func (h *CsHandler) ObjectCreated(obj interface{}) (bool, string) {
2525
continue
2626
}
2727
if c.Status != "True" {
28-
return false, fmt.Sprintf("%s/%s is unhealthy: %s", cs.Name, c.Message)
28+
return false, fmt.Sprintf("%s is unhealthy: %s", cs.Name, c.Message)
2929
}
3030
}
3131

pkg/handlers/handlers.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package handlers
22

33
import (
44
"github.com/bpineau/kube-alert/config"
5-
"github.com/bpineau/kube-alert/pkg/handlers/pod"
65
"github.com/bpineau/kube-alert/pkg/handlers/cs"
6+
"github.com/bpineau/kube-alert/pkg/handlers/pod"
77
)
88

99
type Handler interface {
@@ -14,5 +14,5 @@ type Handler interface {
1414

1515
var Handlers = map[string]Handler{
1616
"pod": &pod.PodHandler{},
17-
"cs": &cs.CsHandler{},
17+
"cs": &cs.CsHandler{},
1818
}

pkg/health/health.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,17 @@ import (
99
)
1010

1111
func healthCheckReply(w http.ResponseWriter, r *http.Request) {
12-
io.WriteString(w, "ok\n")
12+
if _, err := io.WriteString(w, "ok\n"); err != nil {
13+
fmt.Printf("Failed to reply to http healtcheck: %s\n", err)
14+
}
1315
}
1416

1517
func HealthCheckServe(c *config.AlertConfig) {
1618
if c.HealthPort == 0 {
1719
return
1820
}
1921
http.HandleFunc("/health", healthCheckReply)
20-
http.ListenAndServe(fmt.Sprintf(":%d", c.HealthPort), nil)
22+
if err := http.ListenAndServe(fmt.Sprintf(":%d", c.HealthPort), nil); err != nil {
23+
panic(fmt.Sprintf("Failed to start http healtcheck: %s", err))
24+
}
2125
}

pkg/log/log.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ import (
44
"io"
55
"os"
66

7+
"log/syslog"
8+
79
"github.com/sirupsen/logrus"
810
ls "github.com/sirupsen/logrus/hooks/syslog"
9-
"log/syslog"
1011
)
1112

1213
func New(logLevel string, logServer string, logOutput string) *logrus.Logger {

pkg/notifiers/datadog/datadog.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ var tags = []string{
1515

1616
func (l *DdNotifier) Notify(c *config.AlertConfig, title string, msg string) error {
1717
if c.DdApiKey == "" {
18-
c.Logger.Debug("Omiting datadog notification, api key missing")
18+
c.Logger.Debug("Omitting datadog notification, api key missing")
1919
return nil
2020
}
2121

pkg/run/run.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import (
77

88
"github.com/bpineau/kube-alert/config"
99
"github.com/bpineau/kube-alert/pkg/controllers"
10-
"github.com/bpineau/kube-alert/pkg/controllers/pod"
1110
"github.com/bpineau/kube-alert/pkg/controllers/cs"
11+
"github.com/bpineau/kube-alert/pkg/controllers/pod"
1212
"github.com/bpineau/kube-alert/pkg/handlers"
1313
"github.com/bpineau/kube-alert/pkg/health"
1414
)

0 commit comments

Comments
 (0)