ci/cd: create liveness probe for go mod download (#1520)

Co-authored-by: Aaron Schlesinger <70865+arschles@users.noreply.github.com>
This commit is contained in:
Marwan Sulaiman
2020-01-10 11:42:29 -05:00
committed by Aaron Schlesinger
parent 519a69adf9
commit 9510893177
3 changed files with 65 additions and 2 deletions
+3 -2
View File
@@ -7,7 +7,9 @@ steps:
image: golang:1.13
commands:
# wait for services to be ready.
- sleep 10
- cd scripts/liveness_probe
- go run .
- cd ../..
- go mod download
- go mod vendor # this is for when the Dockerfile gets built
environment:
@@ -17,7 +19,6 @@ steps:
- name: cache
path: /go
- name: build
image: golang:1.13
commands:
+3
View File
@@ -0,0 +1,3 @@
module liveness_probe
go 1.13
+59
View File
@@ -0,0 +1,59 @@
// Package main is a simple script for our CI/CD workflow
// that ensures our sidecar proxy is running before proceeding
package main
import (
"context"
"errors"
"fmt"
"net"
"net/http"
"os"
"time"
)
var goproxy = os.Getenv("GOPROXY")
func main() {
timeout := time.After(time.Minute)
for {
select {
case <-timeout:
fmt.Println("liveness probe timed out")
os.Exit(1)
default:
}
isLive, err := probe()
if err != nil {
shouldPrintErr := true
// connection-refused errors are expected, don't print them
var opErr *net.OpError
if errors.As(err, &opErr) && opErr.Op == "read" {
shouldPrintErr = false
}
if shouldPrintErr {
fmt.Println(err)
}
}
if isLive {
fmt.Println("proxy is live")
return
}
time.Sleep(time.Second)
}
}
func probe() (bool, error) {
req, err := http.NewRequest(http.MethodGet, goproxy, nil)
if err != nil {
return false, err
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
req = req.WithContext(ctx)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return false, err
}
return resp.StatusCode == http.StatusOK, nil
}