This repository has been archived by the owner on Nov 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 152
/
client.go
149 lines (122 loc) · 3.85 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
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
// Copyright 2019 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package kubernetes
import (
"crypto/tls"
"crypto/x509"
"fmt"
"io/ioutil"
"net"
"net/http"
"os"
"strings"
"github.com/google/cloudprober/common/tlsconfig"
"github.com/google/cloudprober/logger"
configpb "github.com/google/cloudprober/rds/kubernetes/proto"
)
// Variables defined by Kubernetes spec to find out local CA cert and token.
var (
LocalCACert = "/var/run/secrets/kubernetes.io/serviceaccount/ca.crt"
LocalTokenFile = "/var/run/secrets/kubernetes.io/serviceaccount/token"
)
// client encapsulates an in-cluster kubeapi client.
type client struct {
cfg *configpb.ProviderConfig
httpC *http.Client
apiHost string
bearer string
l *logger.Logger
}
func (c *client) httpRequest(url string) (*http.Request, error) {
url = fmt.Sprintf("https://%s/%s", c.apiHost, strings.TrimPrefix(url, "/"))
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
if c.bearer != "" {
req.Header.Add("Authorization", c.bearer)
}
return req, nil
}
func (c *client) getURL(url string) ([]byte, error) {
req, err := c.httpRequest(url)
if err != nil {
return nil, err
}
c.l.Infof("kubernetes.client: getting URL: %s", req.URL.String())
resp, err := c.httpC.Do(req)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("HTTP response status code: %d, status: %s", resp.StatusCode, resp.Status)
}
return ioutil.ReadAll(resp.Body)
}
func (c *client) initAPIHost() error {
c.apiHost = c.cfg.GetApiServerAddress()
if c.apiHost != "" {
return nil
}
// If API server address is not given, assume in-cluster operation and try to
// get it from environment variables set by pod.
host, port := os.Getenv("KUBERNETES_SERVICE_HOST"), os.Getenv("KUBERNETES_SERVICE_PORT")
if len(host) == 0 || len(port) == 0 {
return fmt.Errorf("initAPIHost: not running in cluster: KUBERNETES_SERVICE_HOST and KUBERNETES_SERVICE_PORT environment variables not set")
}
c.apiHost = net.JoinHostPort(host, port)
return nil
}
func (c *client) initHTTPClient() error {
transport := http.DefaultTransport.(*http.Transport).Clone()
if transport.TLSClientConfig == nil {
transport.TLSClientConfig = &tls.Config{}
}
if c.cfg.GetTlsConfig() != nil {
if err := tlsconfig.UpdateTLSConfig(transport.TLSClientConfig, c.cfg.GetTlsConfig(), false); err != nil {
return err
}
c.httpC = &http.Client{Transport: transport}
return nil
}
// If TLS config is not provided, assume in-cluster.
certs, err := ioutil.ReadFile(LocalCACert)
if err != nil {
return fmt.Errorf("error while reading local ca.crt file (%s): %v", LocalCACert, err)
}
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(certs)
transport.TLSClientConfig.RootCAs = caCertPool
c.httpC = &http.Client{Transport: transport}
// Read OAuth token from local file.
token, err := ioutil.ReadFile(LocalTokenFile)
if err != nil {
return fmt.Errorf("error while reading in-cluster local token file (%s): %v", LocalTokenFile, err)
}
c.bearer = "Bearer " + string(token)
return nil
}
func newClient(cfg *configpb.ProviderConfig, l *logger.Logger) (*client, error) {
c := &client{
cfg: cfg,
l: l,
}
if err := c.initHTTPClient(); err != nil {
return nil, err
}
if err := c.initAPIHost(); err != nil {
return nil, err
}
return c, nil
}