This repository has been archived by the owner on Mar 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 54
/
main.go
142 lines (132 loc) · 4.96 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
// Copyright 2018 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"flag"
"fmt"
"os"
"path/filepath"
"strings"
"time"
"github.com/GoogleCloudPlatform/k8s-node-termination-handler/termination"
"github.com/golang/glog"
"k8s.io/api/core/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
v1core "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/tools/record"
)
const eventSource = "NodeTerminationHandler"
var (
inClusterVar = flag.Bool("in-cluster", true, "Set to false if run outside of a k8s cluster.")
regularVMTimeoutVar = flag.Duration("regular-vm-timeout", time.Hour, "Termination timeout for regular VMs. Defaults to an hour which is the timeout duration of GPU VMs.")
excludePodsVar = flag.String("exclude-pods", "", "List of pods to exclude from graceful eviction. Expected format is comma separated 'podName:podNamespace'.")
kubeconfig *string
// TODO: Update this to use NoExecute taints once that graduates out of alpha.
taintVar = flag.String("taint", "", "Taint to place on the node while handling terminations. Example: cloud.google.com/impending-node-termination::NoSchedule")
annotationVar = flag.String("annotation", "", "Annotation to set on Node objects while handling terminations")
systemPodGracePeriodVar = flag.Duration("system-pod-grace-period", 30*time.Second, "Time required for system pods to exit gracefully.")
)
func main() {
if home := os.Getenv("HOME"); home != "" {
kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file")
} else {
kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
}
flag.Parse()
client, err := getKubeClient()
if err != nil {
glog.Fatalf("Failed to get kubernetes API Server Client. Error: %v", err)
}
excludePods, err := processExcludePods()
if err != nil {
glog.Fatal(err)
}
if *taintVar == "" && *annotationVar == "" {
glog.Fatalf("Must specify one of taint or annotation")
}
taint, err := processTaint()
if err != nil {
glog.Fatal(err)
}
glog.Infof("Excluding pods %v", excludePods)
eventBroadcaster := record.NewBroadcaster()
eventBroadcaster.StartLogging(glog.Infof)
eventBroadcaster.StartRecordingToSink(&v1core.EventSinkImpl{Interface: client.CoreV1().Events("")})
recorder := eventBroadcaster.NewRecorder(scheme.Scheme, v1.EventSource{Component: eventSource})
gceTerminationSource, err := termination.NewGCETerminationSource(*regularVMTimeoutVar)
if err != nil {
glog.Fatal(err)
}
nodeName := gceTerminationSource.GetState().NodeName
taintHandler := termination.NewNodeTaintHandler(taint, *annotationVar, nodeName, client, recorder)
evictionHandler := termination.NewPodEvictionHandler(nodeName, client, recorder, *systemPodGracePeriodVar)
terminationHandler := termination.NewNodeTerminationHandler(gceTerminationSource, taintHandler, evictionHandler, excludePods)
err = terminationHandler.Start()
if err != nil {
glog.Fatal(err)
}
glog.Fatalf("Unexpected execution flow")
}
func getKubeClient() (*kubernetes.Clientset, error) {
var (
config *rest.Config
err error
)
if *inClusterVar {
// creates the in-cluster config
config, err = rest.InClusterConfig()
if err != nil {
return nil, err
}
} else {
config, err = clientcmd.BuildConfigFromFlags("", *kubeconfig)
if err != nil {
return nil, err
}
}
glog.V(10).Infof("Using kube config: %+v", config)
// creates the clientset
return kubernetes.NewForConfig(config)
}
func processExcludePods() (map[string]string, error) {
ret := map[string]string{}
for _, p := range strings.Split(*excludePodsVar, ",") {
parts := strings.Split(p, ":")
if len(parts) != 2 {
return nil, fmt.Errorf("Invalid value specified for --exclude-pods flag - %v", *excludePodsVar)
}
ret[parts[0]] = parts[1]
}
return ret, nil
}
func processTaint() (*v1.Taint, error) {
if len(*annotationVar) != 0 && len(*taintVar) != 0 {
return nil, fmt.Errorf("Annotation must not be specified when taints are specified")
}
if len(*taintVar) == 0 {
return nil, nil
}
parts := strings.Split(*taintVar, ":")
if len(parts) != 3 {
return nil, fmt.Errorf("Invalid value specified for --taint flag. Expected format 'name:value:effect'. Input is %q", *taintVar)
}
return &v1.Taint{
Key: parts[0],
Value: parts[1],
Effect: v1.TaintEffect(parts[2]),
}, nil
}