-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
148 lines (125 loc) · 3.23 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package main
import (
"errors"
"fmt"
"net/http"
_ "net/http/pprof"
"time"
"github.com/sirupsen/logrus"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/manager/signals"
"github.com/castai/spot-handler/castai"
"github.com/castai/spot-handler/config"
"github.com/castai/spot-handler/handler"
"github.com/castai/spot-handler/version"
)
var (
GitCommit = "undefined"
GitRef = "no-ref"
Version = "local"
)
func main() {
cfg := config.Get()
logger := logrus.New()
log := logrus.WithFields(logrus.Fields{})
kubeconfig, err := retrieveKubeConfig(log)
if err != nil {
log.Fatalf("err retrieving kubeconfig: %v", err)
}
clientset, err := kubernetes.NewForConfig(kubeconfig)
if err != nil {
log.Fatalf("err creating clientset: %v", err)
}
k8sVersion, err := version.Get(log, clientset)
if err != nil {
log.Warnf("failed getting kubernetes version: %v", err)
}
handlerVersion := &version.HandlerVersion{
GitCommit: GitCommit,
GitRef: GitRef,
Version: Version,
}
log = logger.WithFields(logrus.Fields{
"version": handlerVersion,
})
k8sVersionField := "unknown"
if k8sVersion != nil {
k8sVersionField = k8sVersion.Full()
}
log = log.WithFields(logrus.Fields{
"k8s_version": k8sVersionField,
})
interruptChecker, err := buildInterruptChecker(cfg.Provider)
if err != nil {
log.Fatalf("interrupt checker: %v", err)
}
// Set 5 seconds until we timeout calling mothership and retry.
castHttpClient, err := castai.NewRestyClient(
cfg.APIUrl,
cfg.APIKey,
cfg.TLSCACert,
logrus.Level(cfg.LogLevel),
5*time.Second,
Version,
)
if err != nil {
log.Fatalf("failed to create http client: %v", err)
}
castClient := castai.NewClient(logger, castHttpClient, cfg.ClusterID)
spotHandler := handler.NewSpotHandler(
log,
castClient,
clientset,
interruptChecker,
time.Duration(cfg.PollIntervalSeconds)*time.Second,
cfg.NodeName,
)
if cfg.PprofPort != 0 {
go func() {
addr := fmt.Sprintf(":%d", cfg.PprofPort)
log.Infof("starting pprof server on %s", addr)
if err := http.ListenAndServe(addr, http.DefaultServeMux); err != nil {
log.Errorf("failed to start pprof http server: %v", err)
}
}()
}
log.Infof("running spot handler, provider=%s", cfg.Provider)
if err := spotHandler.Run(signals.SetupSignalHandler()); err != nil {
logErr := &logContextErr{}
if errors.As(err, &logErr) {
log = logger.WithFields(logErr.fields)
}
log.Fatalf("spot handler failed: %v", err)
}
}
func buildInterruptChecker(provider string) (handler.MetadataChecker, error) {
switch provider {
case "azure":
return handler.NewAzureInterruptChecker(), nil
case "gcp":
return handler.NewGCPChecker(), nil
case "aws":
return handler.NewAWSInterruptChecker(), nil
default:
return nil, fmt.Errorf("unknown provider: %s", provider)
}
}
func retrieveKubeConfig(log logrus.FieldLogger) (*rest.Config, error) {
inClusterConfig, err := rest.InClusterConfig()
if err != nil {
return nil, err
}
log.Debug("using in cluster kubeconfig")
return inClusterConfig, nil
}
type logContextErr struct {
err error
fields logrus.Fields
}
func (e *logContextErr) Error() string {
return e.err.Error()
}
func (e *logContextErr) Unwrap() error {
return e.err
}