-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
service.go
393 lines (336 loc) · 11.7 KB
/
service.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*
Copyright 2016 The Kubernetes Authors All rights reserved.
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 service
import (
"bytes"
"fmt"
"io"
"net/url"
"os"
"strings"
"text/template"
"time"
"github.com/docker/machine/libmachine"
"github.com/golang/glog"
"github.com/olekukonko/tablewriter"
"github.com/pkg/browser"
"github.com/pkg/errors"
"github.com/spf13/viper"
core "k8s.io/api/core/v1"
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/client-go/kubernetes"
typed_core "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/client-go/tools/clientcmd"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
"k8s.io/minikube/pkg/minikube/cluster"
"k8s.io/minikube/pkg/minikube/config"
"k8s.io/minikube/pkg/minikube/out"
"k8s.io/minikube/pkg/minikube/proxy"
"k8s.io/minikube/pkg/util/retry"
)
const (
defaultK8sClientTimeout = 60 * time.Second
// DefaultWait is the default wait time, in seconds
DefaultWait = 20
// DefaultInterval is the default interval, in seconds
DefaultInterval = 6
)
// K8sClient represents a kubernetes client
type K8sClient interface {
GetCoreClient() (typed_core.CoreV1Interface, error)
GetClientset(timeout time.Duration) (*kubernetes.Clientset, error)
}
// K8sClientGetter can get a K8sClient
type K8sClientGetter struct{}
// K8s is the current K8sClient
var K8s K8sClient
func init() {
K8s = &K8sClientGetter{}
}
// GetCoreClient returns a core client
func (k *K8sClientGetter) GetCoreClient() (typed_core.CoreV1Interface, error) {
client, err := k.GetClientset(defaultK8sClientTimeout)
if err != nil {
return nil, errors.Wrap(err, "getting clientset")
}
return client.CoreV1(), nil
}
// GetClientset returns a clientset
func (*K8sClientGetter) GetClientset(timeout time.Duration) (*kubernetes.Clientset, error) {
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
profile := viper.GetString(config.MachineProfile)
configOverrides := &clientcmd.ConfigOverrides{
Context: clientcmdapi.Context{
Cluster: profile,
AuthInfo: profile,
},
}
kubeConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, configOverrides)
clientConfig, err := kubeConfig.ClientConfig()
if err != nil {
return nil, fmt.Errorf("kubeConfig: %v", err)
}
clientConfig.Timeout = timeout
clientConfig = proxy.UpdateTransport(clientConfig)
client, err := kubernetes.NewForConfig(clientConfig)
if err != nil {
return nil, errors.Wrap(err, "client from config")
}
return client, nil
}
// SvcURL represents a service URL. Each item in the URLs field combines the service URL with one of the configured
// node ports. The PortNames field contains the configured names of the ports in the URLs field (sorted correspondingly -
// first item in PortNames belongs to the first item in URLs).
type SvcURL struct {
Namespace string
Name string
URLs []string
PortNames []string
}
// URLs represents a list of URL
type URLs []SvcURL
// GetServiceURLs returns a SvcURL object for every service in a particular namespace.
// Accepts a template for formatting
func GetServiceURLs(api libmachine.API, namespace string, t *template.Template) (URLs, error) {
host, err := cluster.CheckIfHostExistsAndLoad(api, config.GetMachineName())
if err != nil {
return nil, err
}
ip, err := host.Driver.GetIP()
if err != nil {
return nil, err
}
client, err := K8s.GetCoreClient()
if err != nil {
return nil, err
}
serviceInterface := client.Services(namespace)
svcs, err := serviceInterface.List(meta.ListOptions{})
if err != nil {
return nil, err
}
var serviceURLs []SvcURL
for _, svc := range svcs.Items {
svcURL, err := printURLsForService(client, ip, svc.Name, svc.Namespace, t)
if err != nil {
return nil, err
}
serviceURLs = append(serviceURLs, svcURL)
}
return serviceURLs, nil
}
// GetServiceURLsForService returns a SvcUrl object for a service in a namespace. Supports optional formatting.
func GetServiceURLsForService(api libmachine.API, namespace, service string, t *template.Template) (SvcURL, error) {
host, err := cluster.CheckIfHostExistsAndLoad(api, config.GetMachineName())
if err != nil {
return SvcURL{}, errors.Wrap(err, "Error checking if api exist and loading it")
}
ip, err := host.Driver.GetIP()
if err != nil {
return SvcURL{}, errors.Wrap(err, "Error getting ip from host")
}
client, err := K8s.GetCoreClient()
if err != nil {
return SvcURL{}, err
}
return printURLsForService(client, ip, service, namespace, t)
}
func printURLsForService(c typed_core.CoreV1Interface, ip, service, namespace string, t *template.Template) (SvcURL, error) {
if t == nil {
return SvcURL{}, errors.New("Error, attempted to generate service url with nil --format template")
}
svc, err := c.Services(namespace).Get(service, meta.GetOptions{})
if err != nil {
return SvcURL{}, errors.Wrapf(err, "service '%s' could not be found running", service)
}
endpoints, err := c.Endpoints(namespace).Get(service, meta.GetOptions{})
m := make(map[int32]string)
if err == nil && endpoints != nil && len(endpoints.Subsets) > 0 {
for _, ept := range endpoints.Subsets {
for _, p := range ept.Ports {
m[p.Port] = p.Name
}
}
}
urls := []string{}
portNames := []string{}
for _, port := range svc.Spec.Ports {
if port.NodePort > 0 {
var doc bytes.Buffer
err = t.Execute(&doc, struct {
IP string
Port int32
Name string
}{
ip,
port.NodePort,
m[port.TargetPort.IntVal],
})
if err != nil {
return SvcURL{}, err
}
urls = append(urls, doc.String())
portNames = append(portNames, m[port.TargetPort.IntVal])
}
}
return SvcURL{Namespace: svc.Namespace, Name: svc.Name, URLs: urls, PortNames: portNames}, nil
}
// CheckService checks if a service is listening on a port.
func CheckService(namespace string, service string) error {
client, err := K8s.GetCoreClient()
if err != nil {
return errors.Wrap(err, "Error getting kubernetes client")
}
svc, err := client.Services(namespace).Get(service, meta.GetOptions{})
if err != nil {
return &retry.RetriableError{
Err: errors.Wrapf(err, "Error getting service %s", service),
}
}
if len(svc.Spec.Ports) == 0 {
return fmt.Errorf("%s:%s has no ports", namespace, service)
}
glog.Infof("Found service: %+v", svc)
return nil
}
// OptionallyHTTPSFormattedURLString returns a formatted URL string, optionally HTTPS
func OptionallyHTTPSFormattedURLString(bareURLString string, https bool) (string, bool) {
httpsFormattedString := bareURLString
isHTTPSchemedURL := false
if u, parseErr := url.Parse(bareURLString); parseErr == nil {
isHTTPSchemedURL = u.Scheme == "http"
}
if isHTTPSchemedURL && https {
httpsFormattedString = strings.Replace(bareURLString, "http", "https", 1)
}
return httpsFormattedString, isHTTPSchemedURL
}
// PrintServiceList prints a list of services as a table which has
// "Namespace", "Name" and "URL" columns to a writer
func PrintServiceList(writer io.Writer, data [][]string) {
table := tablewriter.NewWriter(writer)
table.SetHeader([]string{"Namespace", "Name", "Target Port", "URL"})
table.SetBorders(tablewriter.Border{Left: true, Top: true, Right: true, Bottom: true})
table.SetCenterSeparator("|")
table.AppendBulk(data)
table.Render()
}
// WaitAndMaybeOpenService waits for a service, and opens it when running
func WaitAndMaybeOpenService(api libmachine.API, namespace string, service string, urlTemplate *template.Template, urlMode bool, https bool,
wait int, interval int) error {
// Convert "Amount of time to wait" and "interval of each check" to attempts
if interval == 0 {
interval = 1
}
chkSVC := func() error { return CheckService(namespace, service) }
if err := retry.Expo(chkSVC, time.Duration(interval)*time.Second, time.Duration(wait)*time.Second); err != nil {
return errors.Wrapf(err, "Service %s was not found in %q namespace. You may select another namespace by using 'minikube service %s -n <namespace>", service, namespace, service)
}
serviceURL, err := GetServiceURLsForService(api, namespace, service, urlTemplate)
if err != nil {
return errors.Wrap(err, "Check that minikube is running and that you have specified the correct namespace")
}
if !urlMode {
var data [][]string
if len(serviceURL.URLs) == 0 {
data = append(data, []string{namespace, service, "", "No node port"})
} else {
data = append(data, []string{namespace, service, strings.Join(serviceURL.PortNames, "\n"), strings.Join(serviceURL.URLs, "\n")})
}
PrintServiceList(os.Stdout, data)
}
if len(serviceURL.URLs) == 0 {
out.T(out.Sad, "service {{.namespace_name}}/{{.service_name}} has no node port", out.V{"namespace_name": namespace, "service_name": service})
return nil
}
for _, bareURLString := range serviceURL.URLs {
urlString, isHTTPSchemedURL := OptionallyHTTPSFormattedURLString(bareURLString, https)
if urlMode || !isHTTPSchemedURL {
out.T(out.Empty, urlString)
} else {
out.T(out.Celebrate, "Opening kubernetes service {{.namespace_name}}/{{.service_name}} in default browser...", out.V{"namespace_name": namespace, "service_name": service})
if err := browser.OpenURL(urlString); err != nil {
out.ErrT(out.Empty, "browser failed to open url: {{.error}}", out.V{"error": err})
}
}
}
return nil
}
// GetServiceListByLabel returns a ServiceList by label
func GetServiceListByLabel(namespace string, key string, value string) (*core.ServiceList, error) {
client, err := K8s.GetCoreClient()
if err != nil {
return &core.ServiceList{}, &retry.RetriableError{Err: err}
}
return getServiceListFromServicesByLabel(client.Services(namespace), key, value)
}
func getServiceListFromServicesByLabel(services typed_core.ServiceInterface, key string, value string) (*core.ServiceList, error) {
selector := labels.SelectorFromSet(labels.Set(map[string]string{key: value}))
serviceList, err := services.List(meta.ListOptions{LabelSelector: selector.String()})
if err != nil {
return &core.ServiceList{}, &retry.RetriableError{Err: err}
}
return serviceList, nil
}
// CreateSecret creates or modifies secrets
func CreateSecret(namespace, name string, dataValues map[string]string, labels map[string]string) error {
client, err := K8s.GetCoreClient()
if err != nil {
return &retry.RetriableError{Err: err}
}
secrets := client.Secrets(namespace)
secret, err := secrets.Get(name, meta.GetOptions{})
if err != nil {
return &retry.RetriableError{Err: err}
}
// Delete existing secret
if len(secret.Name) > 0 {
err = DeleteSecret(namespace, name)
if err != nil {
return &retry.RetriableError{Err: err}
}
}
// convert strings to data secrets
data := map[string][]byte{}
for key, value := range dataValues {
data[key] = []byte(value)
}
// Create Secret
secretObj := &core.Secret{
ObjectMeta: meta.ObjectMeta{
Name: name,
Labels: labels,
},
Data: data,
Type: core.SecretTypeOpaque,
}
_, err = secrets.Create(secretObj)
if err != nil {
return &retry.RetriableError{Err: err}
}
return nil
}
// DeleteSecret deletes a secret from a namespace
func DeleteSecret(namespace, name string) error {
client, err := K8s.GetCoreClient()
if err != nil {
return &retry.RetriableError{Err: err}
}
secrets := client.Secrets(namespace)
err = secrets.Delete(name, &meta.DeleteOptions{})
if err != nil {
return &retry.RetriableError{Err: err}
}
return nil
}