/
utilities.go
47 lines (41 loc) · 1.04 KB
/
utilities.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
package main
import (
"errors"
"fmt"
"net/http"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/rest"
)
func internalServerError(w http.ResponseWriter, err error, msg string) {
fmt.Printf("Error %s because of %s\n", msg, err.Error())
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("500 - " + msg + " " + err.Error()))
}
func badRequest(w http.ResponseWriter, err error, msg string) {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("400 - " + msg + " " + err.Error()))
}
func validateHeartbeatRequestArgs(hb *HeartbeatRequest) error {
var msg string
if hb.CurrentGameHealth == "" {
msg = "CurrentGameHealth cannot be empty"
}
if hb.CurrentGameState == "" {
msg = fmt.Sprintf("%s - CurrentGameState cannot be empty", msg)
}
if msg != "" {
return errors.New(msg)
}
return nil
}
func initializeKubernetesClient() (dynamic.Interface, error) {
config, err := rest.InClusterConfig()
if err != nil {
return nil, err
}
client, err := dynamic.NewForConfig(config)
if err != nil {
return nil, err
}
return client, nil
}