forked from monzo/envoy-preflight
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
196 lines (164 loc) · 4.6 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package main
import (
"context"
"errors"
"fmt"
"os"
"os/exec"
"os/signal"
"strings"
log "github.com/sirupsen/logrus"
"github.com/cenk/backoff"
"github.com/monzo/typhon"
)
type ServerInfo struct {
State string `json:"state"`
}
var (
config Config
)
func main() {
log.SetFormatter(&log.JSONFormatter{})
config = getConfig()
// Check if logging is enabled
if config.LoggingEnabled {
log.Debug("logging is now enabled")
}
// If an envoy API was set and config is set to wait on envoy
if config.EnvoyAdminAPI != "" && !config.StartWithoutEnvoy {
if config.LoggingEnabled {
log.Info("blocking until envoy starts")
}
block()
}
if len(os.Args) < 2 {
log.Info("no arguments received, exiting")
return
}
binary, err := exec.LookPath(os.Args[1])
if err != nil {
log.Error(err.Error())
os.Exit(1)
}
var proc *os.Process
// Pass signals to the child process
go func() {
stop := make(chan os.Signal, 2)
signal.Notify(stop)
for sig := range stop {
if proc != nil {
proc.Signal(sig)
} else {
// Signal received before the process even started. Let's just exit.
log.Info("received exit signal, exiting")
os.Exit(1)
}
}
}()
// Start process passed in by user
proc, err = os.StartProcess(binary, os.Args[1:], &os.ProcAttr{
Files: []*os.File{os.Stdin, os.Stdout, os.Stderr},
})
if err != nil {
log.Error(err.Error())
os.Exit(1)
}
state, err := proc.Wait()
if err != nil {
log.Error(err.Error())
os.Exit(1)
}
exitCode := state.ExitCode()
kill(exitCode)
os.Exit(exitCode)
}
func kill(exitCode int) {
switch {
case config.EnvoyAdminAPI == "":
// We don't have an ENVOY_ADMIN_API env var, do nothing
log.Info("no ENVOY_ADMIN_API, doing nothing")
case !strings.Contains(config.EnvoyAdminAPI, "127.0.0.1") && !strings.Contains(config.EnvoyAdminAPI, "localhost"):
// Envoy is not local; do nothing
log.Info("ENVOY_ADMIN_API is not localhost or 127.0.0.1, doing nothing")
case config.NeverKillIstio:
// We're configured never to kill envoy, do nothing
log.Info("NEVER_KILL_ISTIO is true, doing nothing")
case config.NeverKillIstioOnFailure && exitCode != 0:
log.Info("NEVER_KILL_ISTIO_ON_FAILURE is true, exiting without killing Istio")
case config.IstioQuitAPI == "":
// No istio API sent, fallback to Pkill method
killGenericEndpoints()
killIstioWithPkill()
default:
// Stop istio using api
killGenericEndpoints()
status, error := killIstioWithAPI()
if (error != nil || status != 200) && config.IstioFallbackPkill {
log.Error("quitquitquit failed, will attempt pkill method")
killIstioWithPkill()
}
}
}
func killGenericEndpoints() {
if len(config.GenericQuitEndpoints) == 0 {
return
}
for _, genericEndpoint := range config.GenericQuitEndpoints {
genericEndpoint = strings.Trim(genericEndpoint, " ")
resp := typhon.NewRequest(context.Background(), "POST", genericEndpoint, nil).Send().Response()
if resp.Error != nil {
log.Warnf("sent POST to '%s', error: %s", genericEndpoint, resp.Error)
continue
} else {
log.Infof("sent POST to '%s', status code: %v", genericEndpoint, resp.StatusCode)
}
}
}
func killIstioWithAPI() (int, error) {
if config.LoggingEnabled {
log.Infof("stopping Istio using Istio API '%s' (intended for Istio >v1.2)", config.IstioQuitAPI)
}
url := fmt.Sprintf("%s/quitquitquit", config.IstioQuitAPI)
resp := typhon.NewRequest(context.Background(), "POST", url, nil).Send().Response()
if resp.Error != nil {
log.Warnf("sent POST to '%s', error: %s", url, resp.Error)
return 200, resp.Error
}
if config.LoggingEnabled {
log.Infof("sent quitquitquit to Istio, status code: %d", resp.StatusCode)
}
return resp.StatusCode, resp.Error
}
func killIstioWithPkill() error {
log.Info("stopping Istio using pkill command (intended for Istio <v1.3)")
cmd := exec.Command("sh", "-c", "pkill -SIGINT pilot-agent")
_, err := cmd.Output()
if err == nil {
log.Info("process pilot-agent successfully stopped")
} else {
errorMessage := err.Error()
log.Errorf("pilot-agent could not be stopped, err: " + errorMessage)
}
return err
}
func block() {
if config.StartWithoutEnvoy {
return
}
url := fmt.Sprintf("%s/server_info", config.EnvoyAdminAPI)
b := backoff.NewExponentialBackOff()
// We wait forever for envoy to start. In practice k8s will kill the pod if we take too long.
b.MaxElapsedTime = 0
_ = backoff.Retry(func() error {
rsp := typhon.NewRequest(context.Background(), "GET", url, nil).Send().Response()
info := &ServerInfo{}
err := rsp.Decode(info)
if err != nil {
return err
}
if info.State != "LIVE" {
return errors.New("not live yet")
}
return nil
}, b)
}