@@ -15,76 +15,84 @@ import (
15
15
"strings"
16
16
17
17
"github.com/caarlos0/env/v10"
18
+
18
19
"github.com/pace/bricks/http/transport"
19
20
"github.com/pace/bricks/maintenance/log"
20
21
)
21
22
22
- // Client minimal client for the kubernetes API
23
+ // Client minimal client for the kubernetes API.
23
24
type Client struct {
24
25
Podname string
25
26
Namespace string
26
27
CACert []byte
27
28
Token string
28
29
cfg Config
29
- HttpClient * http.Client
30
+ HTTPClient * http.Client
30
31
}
31
32
32
- // NewClient create new api client
33
+ // NewClient create new api client.
33
34
func NewClient () (* Client , error ) {
34
35
cl := Client {
35
- HttpClient : & http.Client {},
36
+ HTTPClient : & http.Client {},
36
37
}
37
38
38
39
// lookup hostname (for pod update)
39
40
hostname , err := os .Hostname ()
40
41
if err != nil {
41
42
return nil , err
42
43
}
44
+
43
45
cl .Podname = hostname
44
46
45
47
// parse environment including secrets mounted by kubernetes
46
- err = env .Parse (& cl .cfg )
47
- if err != nil {
48
+ if err := env .Parse (& cl .cfg ); err != nil {
48
49
return nil , err
49
50
}
50
51
51
52
caData , err := os .ReadFile (cl .cfg .CACertFile )
52
53
if err != nil {
53
- return nil , fmt .Errorf ("failed to read %q: %v " , cl .cfg .CACertFile , err )
54
+ return nil , fmt .Errorf ("failed to read %q: %w " , cl .cfg .CACertFile , err )
54
55
}
56
+
55
57
cl .CACert = []byte (strings .TrimSpace (string (caData )))
56
58
57
59
namespaceData , err := os .ReadFile (cl .cfg .NamespaceFile )
58
60
if err != nil {
59
- return nil , fmt .Errorf ("failed to read %q: %v " , cl .cfg .NamespaceFile , err )
61
+ return nil , fmt .Errorf ("failed to read %q: %w " , cl .cfg .NamespaceFile , err )
60
62
}
63
+
61
64
cl .Namespace = strings .TrimSpace (string (namespaceData ))
62
65
63
66
tokenData , err := os .ReadFile (cl .cfg .TokenFile )
64
67
if err != nil {
65
- return nil , fmt .Errorf ("failed to read %q: %v " , cl .cfg .CACertFile , err )
68
+ return nil , fmt .Errorf ("failed to read %q: %w " , cl .cfg .CACertFile , err )
66
69
}
70
+
67
71
cl .Token = strings .TrimSpace (string (tokenData ))
68
72
69
73
// add kubernetes api server cert
70
74
chain := transport .NewDefaultTransportChain ()
71
75
pool := x509 .NewCertPool ()
76
+
72
77
ok := pool .AppendCertsFromPEM (cl .CACert )
73
78
if ! ok {
74
79
return nil , fmt .Errorf ("failed to load kubernetes ca cert" )
75
80
}
81
+
76
82
chain .Final (& http.Transport {
77
83
TLSClientConfig : & tls.Config {
78
- RootCAs : pool ,
84
+ RootCAs : pool ,
85
+ MinVersion : tls .VersionTLS12 ,
79
86
},
80
87
})
81
- cl .HttpClient .Transport = chain
88
+
89
+ cl .HTTPClient .Transport = chain
82
90
83
91
return & cl , nil
84
92
}
85
93
86
94
// SimpleRequest send a simple http request to kubernetes with the passed
87
- // method, url and requestObj, decoding the result into responseObj
95
+ // method, url and requestObj, decoding the result into responseObj.
88
96
func (c * Client ) SimpleRequest (ctx context.Context , method , url string , requestObj , responseObj interface {}) error {
89
97
data , err := json .Marshal (requestObj )
90
98
if err != nil {
@@ -99,16 +107,22 @@ func (c *Client) SimpleRequest(ctx context.Context, method, url string, requestO
99
107
req .Header .Set ("Content-Type" , "application/json-patch+json" )
100
108
req .Header .Set ("Authorization" , "Bearer " + c .Token )
101
109
102
- resp , err := c .HttpClient .Do (req )
110
+ resp , err := c .HTTPClient .Do (req )
103
111
if err != nil {
104
112
log .Ctx (ctx ).Debug ().Err (err ).Msg ("failed to do api request" )
105
113
return err
106
114
}
107
- defer resp .Body .Close ()
115
+
116
+ defer func () {
117
+ if err := resp .Body .Close (); err != nil {
118
+ log .Ctx (ctx ).Debug ().Err (err ).Msg ("failed to close response body" )
119
+ }
120
+ }()
108
121
109
122
if resp .StatusCode > 299 {
110
- body , _ := io .ReadAll (resp .Body ) // nolint: errcheck
123
+ body , _ := io .ReadAll (resp .Body )
111
124
log .Ctx (ctx ).Debug ().Msgf ("failed to do api request, due to: %s" , string (body ))
125
+
112
126
return fmt .Errorf ("k8s request failed with %s" , resp .Status )
113
127
}
114
128
0 commit comments