This repository has been archived by the owner on Apr 18, 2022. It is now read-only.
forked from nrmitchi/k8s-controller-sidecars
-
Notifications
You must be signed in to change notification settings - Fork 2
/
handler.go
143 lines (118 loc) · 4.09 KB
/
handler.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
package main
import (
"strings"
"time"
"github.com/avast/retry-go"
log "github.com/Sirupsen/logrus"
core_v1 "k8s.io/api/core/v1"
"k8s.io/client-go/tools/clientcmd"
set "github.com/deckarep/golang-set"
)
// Handler interface contains the methods that are required
type Handler interface {
Init() error
ObjectCreated(obj interface{})
ObjectDeleted(obj interface{})
ObjectUpdated(objOld, objNew interface{})
}
// SidecarShutdownHandler is a sample implementation of Handler
type SidecarShutdownHandler struct{}
// Init handles any handler initialization
func (t *SidecarShutdownHandler) Init() error {
log.Info("SidecarShutdownHandler.Init")
return nil
}
// Send a shutdown signal to all containers in the Pod
func sendShutdownSignal(pod *core_v1.Pod, containers set.Set) {
log.Infof("It's going down, I'm yelling TIMBERRRRR for pod: %s", pod.Name)
// Multiple arguments must be provided as separate "command" parameters
// The first one is added automatically.
// Todo: Update requestFromConfig to handle this better
command := "sh&command=-c&command=kill+-s+TERM+1" // "kill -s TERM 1"
// creates the connection
config, err := clientcmd.BuildConfigFromFlags("", "")
if err != nil {
log.Fatal(err)
}
// Create a round tripper with all necessary kubernetes security details
wrappedRoundTripper, err := roundTripperFromConfig(config)
if err != nil {
log.Fatalln(err)
}
for _, c := range containers.ToSlice() {
// Create a request out of config and the query parameters
req, err := requestFromConfig(config, pod.Name, c.(string), pod.Namespace, command)
if err != nil {
log.Infoln(err)
}
err = retry.Do(
func() error {
// Send the request and let the callback do its work
_, err = wrappedRoundTripper.RoundTrip(req)
if err != nil {
return err
}
return nil
},
retry.Delay(3*time.Second),
retry.Attempts(5),
)
if err != nil {
log.Errorln(err)
}
}
}
// ObjectCreated is called when an object is created
func (t *SidecarShutdownHandler) ObjectCreated(obj interface{}) {
log.Debug("SidecarShutdownHandler.ObjectCreated")
// assert the type to a Pod object to pull out relevant data
pod := obj.(*core_v1.Pod)
sidecarsString, exists := pod.Annotations["lemonade.com/sidecars"]
if exists {
log.Debugf(" ResourceTrackable: true")
log.Infof(" Sidecars: %s, node: %s, phase: %s", sidecarsString, pod.Spec.NodeName, pod.Status.Phase)
} else {
return
}
sidecars := set.NewSet()
for _, s := range strings.Split(sidecarsString, ",") {
sidecars.Add(s)
}
allContainers := set.NewSet()
runningContainers := set.NewSet()
completedContainers := set.NewSet()
for _, containerStatus := range pod.Status.ContainerStatuses {
allContainers.Add(containerStatus.Name)
if containerStatus.Ready {
runningContainers.Add(containerStatus.Name)
} else {
terminated := containerStatus.State.Terminated
if terminated != nil && (terminated.Reason == "Completed" || terminated.Reason == "Error") {
completedContainers.Add(containerStatus.Name)
}
}
}
log.Debugf(" all : %s", allContainers)
log.Debugf(" running : %s", runningContainers)
log.Debugf(" completed : %s", completedContainers)
log.Debugf(" sidecars : %s", sidecars)
// If we have accounted for all of the containers, and the sidecar containers are the only
// ones still running, issue them each a shutdown command
if runningContainers.Union(completedContainers).Equal(allContainers) {
log.Debugf(" We have all the containers")
if runningContainers.Equal(sidecars) {
log.Infof(" Sending shutdown signal to containers: %s, %s", pod.Name, sidecars)
sendShutdownSignal(pod, sidecars)
}
}
}
// ObjectDeleted is called when an object is deleted
func (t *SidecarShutdownHandler) ObjectDeleted(obj interface{}) {
log.Debug("SidecarShutdownHandler.ObjectDeleted")
}
// ObjectUpdated is called when an object is updated.
// Note that the controller in this repo will never call this function properly.
// It uses only ObjectCreated
func (t *SidecarShutdownHandler) ObjectUpdated(objOld, objNew interface{}) {
log.Debug("SidecarShutdownHandler.ObjectUpdated")
}