-
Notifications
You must be signed in to change notification settings - Fork 0
/
cluster.go
83 lines (74 loc) · 2.09 KB
/
cluster.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
package main
import (
"bytes"
"crypto/tls"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
"time"
)
const (
kubernetesBaseURI = "https://kubernetes:6443"
tokenPath = "/var/run/secrets/kubernetes.io/serviceaccount/token"
)
var (
webhookSecret string = os.Getenv(dockerSecretEnvironmentVar)
)
// try: cat /var/run/secrets/kubernetes.io/serviceaccount/token
func authToken() string {
f, err := ioutil.ReadFile(tokenPath)
if err != nil {
panic(fmt.Sprintf("cannot read token file %s", tokenPath))
}
return string(f)
}
// Send PATCH request to the k8s API to update deployment
func deploy(namespace, deployment string) error {
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{Transport: tr}
params := patchArgs()
req, err := http.NewRequest("PATCH", kubernetesDeploymentEndpoint(namespace, deployment), bytes.NewBuffer([]byte(params)))
if err != nil {
return fmt.Errorf("error building request: %v", err)
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", authToken()))
req.Header.Set("Accept", "application/json, */*")
req.Header.Set("Content-Type", "application/strategic-merge-patch+json")
log.Printf("DEBUG:sending request: %v", req)
resp, err := client.Do(req)
log.Printf("DEBUG:request response: %v", resp)
if err != nil {
return fmt.Errorf("error sending request: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
body := new(strings.Builder)
_, _ = io.Copy(body, resp.Body)
return fmt.Errorf("error interacting with k8s API: %s", body.String())
}
return nil
}
func patchArgs() string {
now := fmt.Sprint(time.Now().Format(time.RFC3339))
res := fmt.Sprintf(`{
"spec": {
"template": {
"metadata": {
"annotations": {
"kubectl.kubernetes.io/restartedAt": "%s"
}
}
}
}
}`, now)
return res
}
func kubernetesDeploymentEndpoint(namespace, deployment string) string {
return fmt.Sprintf("%s/apis/apps/v1/namespaces/%s/deployments/%s", kubernetesBaseURI, namespace, deployment)
}