-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
172 lines (151 loc) · 4.15 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
package main
import (
"context"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"os/signal"
"strings"
"syscall"
"time"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"github.com/gin-gonic/gin"
"github.com/go-redis/redis/v8"
)
var color = "red"
var version = os.Getenv("VERSION")
var rdb = &redis.Client{}
var rdbCtx = context.Background()
func main() {
port := ":8080"
rdb = redis.NewClient(&redis.Options{
Addr: getRedisURL(),
Password: "", // no password set
DB: 0, // use default DB
})
flag.Parse()
r := gin.Default()
log.Printf("Backend version: %s\n", version)
r.LoadHTMLGlob("index.html")
r.GET("/", handleIndex)
r.GET("/version", handleVersion)
r.GET("/healthz", handleHealthz)
srv := &http.Server{
Addr: port,
Handler: r,
}
go func() {
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("listen: %s\n", err)
}
}()
// Wait for interrupt signal to gracefully shutdown the server with
// a timeout of 5 seconds.
quit := make(chan os.Signal, 1)
// kill (no param) default send syscall.SIGTERM
// kill -2 is syscall.SIGINT
// kill -9 is syscall.SIGKILL but can't be catch, so don't need add it
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
log.Println("Shutting down server...")
// The context is used to inform the server it has 5 seconds to finish
// the request it is currently handling
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
log.Fatal("Server forced to shutdown:", err)
}
log.Println("Server exiting")
}
func handleIndex(c *gin.Context) {
log.Printf("Received request from %s at %s", c.Request.RemoteAddr, c.Request.URL.EscapedPath())
p := PodMetadata{}
counter, err := incrCounter(c)
if err != nil {
c.String(http.StatusInternalServerError, "%v", err)
return
}
err = p.Populate(version, counter, color)
if err != nil {
c.String(http.StatusInternalServerError, "%v", err)
return
}
c.HTML(http.StatusOK, "index.html", p)
}
func incrCounter(c *gin.Context) (string, error) {
err := rdb.Incr(rdbCtx, "counter").Err()
if err != nil {
return "", err
}
val, err := rdb.Get(rdbCtx, "counter").Result()
if err != nil {
return "", err
}
return val, nil
}
func handleVersion(c *gin.Context) {
c.String(http.StatusOK, "%s", c.Value("version"))
}
func handleHealthz(c *gin.Context) {
status := rdb.Ping(rdbCtx)
if status.Err() == nil {
c.String(http.StatusOK, "", "")
} else {
errorString := fmt.Sprintf("Unable to reach redis at %v", rdb.Options().Addr)
c.String(http.StatusInternalServerError, errorString, "")
}
}
// PodMetadata represents info about an InstanceMetadata in GCE
type PodMetadata struct {
Name string
Namespace string
HostIP string
PodIP string
StartTime string
RawRequest string
Counter string
Version string
Color string
RedisURL string
}
// Populate creates a new instance with info filled out
func (p *PodMetadata) Populate(version string, counter string, color string) error {
hostname := os.Getenv("HOSTNAME")
config, err := rest.InClusterConfig()
if err != nil {
return fmt.Errorf("unable to create InClusterConfig client: %v", err)
}
// creates the clientset
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
return fmt.Errorf("unable to create kubernetes client: %v", err)
}
pod, err := clientset.CoreV1().Pods(getNamespace()).Get(context.TODO(), hostname, v1.GetOptions{})
if err != nil {
return fmt.Errorf("unable to find pod %s: %v", hostname, err)
}
p.Name = pod.Name
p.HostIP = pod.Status.HostIP
p.Namespace = pod.Namespace
p.PodIP = pod.Status.PodIP
p.StartTime = pod.Status.StartTime.String()
p.Counter = counter
p.Version = version
p.Color = color
p.RedisURL = rdb.Options().Addr
return nil
}
func getNamespace() string {
// Fall back to the namespace associated with the service account token, if available
if data, err := ioutil.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/namespace"); err == nil {
if ns := strings.TrimSpace(string(data)); len(ns) > 0 {
return ns
}
}
return "default"
}