forked from play-with-docker/play-with-docker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfactory.go
140 lines (114 loc) · 3.24 KB
/
factory.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
package k8s
import (
"crypto/tls"
"encoding/json"
"fmt"
"net"
"net/http"
"net/url"
"time"
"github.com/docker/go-connections/tlsconfig"
"github.com/play-with-docker/play-with-docker/pwd/types"
"github.com/play-with-docker/play-with-docker/router"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/runtime/serializer"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
)
type FactoryApi interface {
GetForInstance(instance *types.Instance) (*kubernetes.Clientset, error)
GetKubeletForInstance(instance *types.Instance) (*KubeletClient, error)
}
func NewClient(instance *types.Instance, proxyHost string) (*kubernetes.Clientset, error) {
var durl string
host := router.EncodeHost(instance.SessionId, instance.RoutableIP, router.HostOpts{EncodedPort: 6443})
var tlsConfig *tls.Config
tlsConfig = tlsconfig.ClientDefault()
tlsConfig.InsecureSkipVerify = true
tlsConfig.ServerName = host
var transport http.RoundTripper
transport = &http.Transport{
DialContext: (&net.Dialer{
Timeout: 1 * time.Second,
KeepAlive: 30 * time.Second,
}).DialContext,
TLSClientConfig: tlsConfig,
MaxIdleConnsPerHost: 5,
}
durl = fmt.Sprintf("https://%s", proxyHost)
cc := rest.ContentConfig{
ContentType: "application/json",
GroupVersion: &schema.GroupVersion{Version: "v1"},
NegotiatedSerializer: serializer.DirectCodecFactory{CodecFactory: scheme.Codecs},
}
restConfig := &rest.Config{
Host: durl,
APIPath: "/api/",
BearerToken: "31ada4fd-adec-460c-809a-9e56ceb75269",
ContentConfig: cc,
}
transport, err := rest.HTTPWrappersForConfig(restConfig, transport)
if err != nil {
return nil, fmt.Errorf("Error wrapping transport %v", err)
}
cli := &http.Client{
Transport: transport,
}
rc, err := rest.RESTClientFor(restConfig)
rc.Client = cli
if err != nil {
return nil, fmt.Errorf("Error creating K8s client %v", err)
}
return kubernetes.New(rc), nil
}
func NewKubeletClient(instance *types.Instance, proxyHost string) (*KubeletClient, error) {
var durl string
host := router.EncodeHost(instance.SessionId, instance.RoutableIP, router.HostOpts{EncodedPort: 10255})
transport := &http.Transport{
DialContext: (&net.Dialer{
Timeout: 1 * time.Second,
KeepAlive: 30 * time.Second,
}).DialContext,
MaxIdleConnsPerHost: 5,
}
durl = fmt.Sprintf("http://%s", host)
transport.Proxy = http.ProxyURL(&url.URL{Host: proxyHost})
cli := &http.Client{
Transport: transport,
}
kc := &KubeletClient{client: cli, baseURL: durl}
return kc, nil
}
type KubeletClient struct {
client *http.Client
baseURL string
}
func (c *KubeletClient) Get(path string) (*http.Response, error) {
return c.client.Get(c.baseURL + path)
}
type metadata struct {
Labels map[string]string
}
type item struct {
Metadata metadata
}
type kubeletPodsResponse struct {
Items []item
}
func (c *KubeletClient) IsManager() (bool, error) {
res, err := c.client.Get(c.baseURL + "/pods")
if err != nil {
return false, err
}
podsData := &kubeletPodsResponse{}
json.NewDecoder(res.Body).Decode(podsData)
for _, i := range podsData.Items {
for _, v := range i.Metadata.Labels {
if v == "kube-apiserver" {
return true, nil
}
}
}
return false, nil
}