forked from linkerd/linkerd2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
186 lines (150 loc) · 5.1 KB
/
api.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package k8s
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
"time"
"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/version"
"k8s.io/client-go/rest"
// Load all the auth plugins for the cloud providers.
_ "k8s.io/client-go/plugin/pkg/client/auth"
)
var minApiVersion = [3]int{1, 8, 0}
type KubernetesAPI struct {
*rest.Config
}
func (kubeAPI *KubernetesAPI) NewClient() (*http.Client, error) {
secureTransport, err := rest.TransportFor(kubeAPI.Config)
if err != nil {
return nil, fmt.Errorf("error instantiating Kubernetes API client: %v", err)
}
return &http.Client{
Transport: secureTransport,
}, nil
}
func (kubeAPI *KubernetesAPI) GetVersionInfo(client *http.Client) (*version.Info, error) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
rsp, err := kubeAPI.getRequest(ctx, client, "/version")
if err != nil {
return nil, err
}
defer rsp.Body.Close()
if rsp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("Unexpected Kubernetes API response: %s", rsp.Status)
}
bytes, err := ioutil.ReadAll(rsp.Body)
if err != nil {
return nil, err
}
var versionInfo version.Info
err = json.Unmarshal(bytes, &versionInfo)
return &versionInfo, err
}
func (kubeAPI *KubernetesAPI) CheckVersion(versionInfo *version.Info) error {
apiVersion, err := getK8sVersion(versionInfo.String())
if err != nil {
return err
}
if !isCompatibleVersion(minApiVersion, apiVersion) {
return fmt.Errorf("Kubernetes is on version [%d.%d.%d], but version [%d.%d.%d] or more recent is required",
apiVersion[0], apiVersion[1], apiVersion[2],
minApiVersion[0], minApiVersion[1], minApiVersion[2])
}
return nil
}
func (kubeAPI *KubernetesAPI) CheckProxyVersion(pods []v1.Pod, version string) error {
for _, pod := range pods {
for _, container := range pod.Spec.Containers {
if container.Name == ProxyContainerName {
parts := strings.Split(container.Image, ":")
if len(parts) == 2 && parts[1] != version {
return fmt.Errorf("%s is running version %s but the latest version is %s",
pod.Name, parts[1], version)
}
}
}
}
return nil
}
func (kubeAPI *KubernetesAPI) NamespaceExists(client *http.Client, namespace string) (bool, error) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
rsp, err := kubeAPI.getRequest(ctx, client, "/api/v1/namespaces/"+namespace)
if err != nil {
return false, err
}
defer rsp.Body.Close()
if rsp.StatusCode != http.StatusOK && rsp.StatusCode != http.StatusNotFound {
return false, fmt.Errorf("Unexpected Kubernetes API response: %s", rsp.Status)
}
return rsp.StatusCode == http.StatusOK, nil
}
// GetPodsByNamespace returns all pods in a given namespace
func (kubeAPI *KubernetesAPI) GetPodsByNamespace(client *http.Client, namespace string) ([]v1.Pod, error) {
return kubeAPI.getPods(client, "/api/v1/namespaces/"+namespace+"/pods")
}
// GetPodsByControllerNamespace returns all pods that have been injected to
// interface with a given controllerNamespace. If targetNamespace is provided,
// only pods from that namespace are returned.
func (kubeAPI *KubernetesAPI) GetPodsByControllerNamespace(client *http.Client, controllerNamespace, targetNamespace string) ([]v1.Pod, error) {
selector := url.QueryEscape(fmt.Sprintf("%s=%s", ControllerNSLabel, controllerNamespace))
var path string
if targetNamespace == "" {
path = "/api/v1/pods"
} else {
path = "/api/v1/namespaces/" + targetNamespace + "/pods"
}
return kubeAPI.getPods(client, fmt.Sprintf("%s?labelSelector=%s", path, selector))
}
func (kubeAPI *KubernetesAPI) getPods(client *http.Client, path string) ([]v1.Pod, error) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
rsp, err := kubeAPI.getRequest(ctx, client, path)
if err != nil {
return nil, err
}
defer rsp.Body.Close()
if rsp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("Unexpected Kubernetes API response: %s", rsp.Status)
}
bytes, err := ioutil.ReadAll(rsp.Body)
if err != nil {
return nil, err
}
var podList v1.PodList
err = json.Unmarshal(bytes, &podList)
if err != nil {
return nil, err
}
return podList.Items, nil
}
// UrlFor generates a URL based on the Kubernetes config.
func (kubeAPI *KubernetesAPI) UrlFor(namespace string, extraPathStartingWithSlash string) (*url.URL, error) {
return generateKubernetesApiBaseUrlFor(kubeAPI.Host, namespace, extraPathStartingWithSlash)
}
func (kubeAPI *KubernetesAPI) getRequest(ctx context.Context, client *http.Client, path string) (*http.Response, error) {
endpoint, err := url.Parse(kubeAPI.Host + path)
if err != nil {
return nil, err
}
req, err := http.NewRequest("GET", endpoint.String(), nil)
if err != nil {
return nil, err
}
return client.Do(req.WithContext(ctx))
}
// NewAPI validates a Kubernetes config and returns a client for accessing the
// configured cluster
func NewAPI(configPath string) (*KubernetesAPI, error) {
config, err := getConfig(configPath)
if err != nil {
return nil, fmt.Errorf("error configuring Kubernetes API client: %v", err)
}
return &KubernetesAPI{Config: config}, nil
}