-
Notifications
You must be signed in to change notification settings - Fork 318
/
kubelet_stub.go
141 lines (121 loc) · 3.47 KB
/
kubelet_stub.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
/*
Copyright 2022 The Koordinator Authors.
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 impl
import (
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"net/url"
"strconv"
"time"
corev1 "k8s.io/api/core/v1"
"k8s.io/client-go/rest"
kubeletconfigv1beta1 "k8s.io/kubelet/config/v1beta1"
"k8s.io/kubernetes/cmd/kubelet/app/options"
kubeletconfiginternal "k8s.io/kubernetes/pkg/kubelet/apis/config"
kubeletscheme "k8s.io/kubernetes/pkg/kubelet/apis/config/scheme"
)
type KubeletStub interface {
GetAllPods() (corev1.PodList, error)
GetKubeletConfiguration() (*kubeletconfiginternal.KubeletConfiguration, error)
}
type kubeletStub struct {
addr string
port int
scheme string
httpClient *http.Client
}
func NewKubeletStub(addr string, port int, scheme string, timeout time.Duration, cfg *rest.Config) (KubeletStub, error) {
client := &http.Client{
Timeout: timeout,
}
if cfg != nil && rest.IsConfigTransportTLS(*cfg) {
transport, err := rest.TransportFor(cfg)
if err != nil {
return nil, err
}
client.Transport = transport
}
return &kubeletStub{
httpClient: client,
addr: addr,
port: port,
scheme: scheme,
}, nil
}
func (k *kubeletStub) GetAllPods() (corev1.PodList, error) {
url := url.URL{
Scheme: k.scheme,
Host: net.JoinHostPort(k.addr, strconv.Itoa(k.port)),
Path: "/pods/",
}
podList := corev1.PodList{}
rsp, err := k.httpClient.Get(url.String())
if err != nil {
return podList, err
}
defer rsp.Body.Close()
if rsp.StatusCode != http.StatusOK {
return podList, fmt.Errorf("request %s failed, code %d", url.String(), rsp.StatusCode)
}
body, err := io.ReadAll(rsp.Body)
if err != nil {
return podList, err
}
// parse json data
err = json.Unmarshal(body, &podList)
if err != nil {
return podList, fmt.Errorf("parse kubelet pod list failed, err: %v", err)
}
return podList, nil
}
type kubeletConfigz struct {
ComponentConfig kubeletconfigv1beta1.KubeletConfiguration `json:"kubeletconfig"`
}
func (k *kubeletStub) GetKubeletConfiguration() (*kubeletconfiginternal.KubeletConfiguration, error) {
configzURL := url.URL{
Scheme: k.scheme,
Host: net.JoinHostPort(k.addr, strconv.Itoa(k.port)),
Path: "/configz",
}
rsp, err := k.httpClient.Get(configzURL.String())
if err != nil {
return nil, err
}
defer rsp.Body.Close()
if rsp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("request %s failed, code %d", configzURL.String(), rsp.StatusCode)
}
body, err := io.ReadAll(rsp.Body)
if err != nil {
return nil, err
}
var configz kubeletConfigz
if err = json.Unmarshal(body, &configz); err != nil {
return nil, fmt.Errorf("failed to unmarshal kubeletConfigz: %v", err)
}
kubeletConfiguration, err := options.NewKubeletConfiguration()
if err != nil {
return nil, err
}
scheme, _, err := kubeletscheme.NewSchemeAndCodecs()
if err != nil {
return nil, err
}
if err = scheme.Convert(&configz.ComponentConfig, kubeletConfiguration, nil); err != nil {
return nil, err
}
return kubeletConfiguration, nil
}