-
Notifications
You must be signed in to change notification settings - Fork 1
/
services.go
73 lines (62 loc) · 1.61 KB
/
services.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
package main
import (
"crypto/tls"
"crypto/x509"
"encoding/json"
"fmt"
"github.com/rs/zerolog/log"
"net/http"
)
type K8sService struct {
baseUrl string
token string
client http.Client
}
func newK8sService(config *Config) K8sService {
token := string(readFile(config.K8s.Token))
// Load ca cert file
caCert := readFile(config.K8s.Tls.CaCert)
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(caCert)
// Create http client
httpClient := http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
RootCAs: caCertPool,
},
},
}
return K8sService{baseUrl: config.K8s.URL, token: token, client: httpClient}
}
func (k8s K8sService) getSts(namespace string, name string) *Statefulset {
// get api path
url := fmt.Sprintf(
"%s/apis/apps/v1/namespaces/%s/statefulsets/%s", k8s.baseUrl, namespace, name)
req, _ := http.NewRequest("GET", url, nil)
// add authorization header
authorization := fmt.Sprintf("Bearer %s", k8s.token)
req.Header.Add("Authorization", authorization)
resp, connectionErr := k8s.client.Do(req)
if connectionErr != nil {
log.Error().
Err(connectionErr).
Msgf("Request to %s failed. Cause %s", url, connectionErr.Error())
return nil
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
log.Error().
Msgf("Request to %s failed. Returned with status %s", url, resp.Status)
return nil
}
// parse the response
sts := &Statefulset{}
decodeErr := json.NewDecoder(resp.Body).Decode(sts)
if decodeErr != nil {
log.Error().
Err(decodeErr).
Msgf("Failed to decode Statefulset %s by struct", resp.Body)
return nil
}
return sts
}