-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathclient.go
116 lines (98 loc) · 2.8 KB
/
client.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
// Copyright © 2022 by PACE Telematics GmbH. All rights reserved.
package k8sapi
import (
"bytes"
"context"
"crypto/tls"
"crypto/x509"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"strings"
"github.com/caarlos0/env/v11"
"github.com/pace/bricks/http/transport"
"github.com/pace/bricks/maintenance/log"
)
// Client minimal client for the kubernetes API
type Client struct {
Podname string
Namespace string
CACert []byte
Token string
cfg Config
HttpClient *http.Client
}
// NewClient create new api client
func NewClient() (*Client, error) {
cl := Client{
HttpClient: &http.Client{},
}
// lookup hostname (for pod update)
hostname, err := os.Hostname()
if err != nil {
return nil, err
}
cl.Podname = hostname
// parse environment including secrets mounted by kubernetes
err = env.Parse(&cl.cfg)
if err != nil {
return nil, err
}
caData, err := os.ReadFile(cl.cfg.CACertFile)
if err != nil {
return nil, fmt.Errorf("failed to read %q: %v", cl.cfg.CACertFile, err)
}
cl.CACert = []byte(strings.TrimSpace(string(caData)))
namespaceData, err := os.ReadFile(cl.cfg.NamespaceFile)
if err != nil {
return nil, fmt.Errorf("failed to read %q: %v", cl.cfg.NamespaceFile, err)
}
cl.Namespace = strings.TrimSpace(string(namespaceData))
tokenData, err := os.ReadFile(cl.cfg.TokenFile)
if err != nil {
return nil, fmt.Errorf("failed to read %q: %v", cl.cfg.CACertFile, err)
}
cl.Token = strings.TrimSpace(string(tokenData))
// add kubernetes api server cert
chain := transport.NewDefaultTransportChain()
pool := x509.NewCertPool()
ok := pool.AppendCertsFromPEM(cl.CACert)
if !ok {
return nil, fmt.Errorf("failed to load kubernetes ca cert")
}
chain.Final(&http.Transport{
TLSClientConfig: &tls.Config{
RootCAs: pool,
},
})
cl.HttpClient.Transport = chain
return &cl, nil
}
// SimpleRequest send a simple http request to kubernetes with the passed
// method, url and requestObj, decoding the result into responseObj
func (c *Client) SimpleRequest(ctx context.Context, method, url string, requestObj, responseObj interface{}) error {
data, err := json.Marshal(requestObj)
if err != nil {
panic(err)
}
req, err := http.NewRequestWithContext(ctx, method, url, bytes.NewReader(data))
if err != nil {
panic(err)
}
req.Header.Set("Content-Type", "application/json-patch+json")
req.Header.Set("Authorization", "Bearer "+c.Token)
resp, err := c.HttpClient.Do(req)
if err != nil {
log.Ctx(ctx).Debug().Err(err).Msg("failed to do api request")
return err
}
defer resp.Body.Close()
if resp.StatusCode > 299 {
body, _ := io.ReadAll(resp.Body) // nolint: errcheck
log.Ctx(ctx).Debug().Msgf("failed to do api request, due to: %s", string(body))
return fmt.Errorf("k8s request failed with %s", resp.Status)
}
return json.NewDecoder(resp.Body).Decode(responseObj)
}