forked from Azure/aks-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
367 lines (339 loc) · 9.88 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
package service
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os/exec"
"regexp"
"time"
"github.com/Azure/aks-engine/test/e2e/kubernetes/util"
"github.com/pkg/errors"
)
// List holds a list of services returned from kubectl get svc
type List struct {
Services []Service `json:"items"`
}
// Service represents a kubernetes service
type Service struct {
Metadata Metadata `json:"metadata"`
Spec Spec `json:"spec"`
Status Status `json:"status"`
}
// Metadata holds information like name, namespace, and labels
type Metadata struct {
CreatedAt time.Time `json:"creationTimestamp"`
Labels map[string]string `json:"labels"`
Name string `json:"name"`
Namespace string `json:"namespace"`
}
// Spec holds information like clusterIP and port
type Spec struct {
ClusterIP string `json:"clusterIP"`
Ports []Port `json:"ports"`
Type string `json:"type"`
}
// Port represents a service port definition
type Port struct {
NodePort int `json:"nodePort"`
Port int `json:"port"`
Protocol string `json:"protocol"`
TargetPort int `json:"targetPort"`
}
// Status holds the load balancer definition
type Status struct {
LoadBalancer LoadBalancer `json:"loadBalancer"`
}
// LoadBalancer holds the ingress definitions
type LoadBalancer struct {
Ingress []map[string]string `json:"ingress"`
}
// GetResult is a return struct for GetAsync
type GetResult struct {
svc *Service
err error
}
// GetAsync wraps Get with a struct response for goroutine + channel usage
func GetAsync(name, namespace string) GetResult {
svc, err := Get(name, namespace)
return GetResult{
svc: svc,
err: err,
}
}
// Get returns the service definition specified in a given namespace
func Get(name, namespace string) (*Service, error) {
cmd := exec.Command("k", "get", "svc", "-o", "json", "-n", namespace, name)
out, err := cmd.CombinedOutput()
if err != nil {
log.Printf("Error getting svc:\n")
util.PrintCommand(cmd)
return nil, err
}
s := Service{}
err = json.Unmarshal(out, &s)
if err != nil {
log.Printf("Error unmarshalling service json:%s\n", err)
return nil, err
}
return &s, nil
}
// GetAll will return all services in a given namespace
func GetAll(namespace string) (*List, error) {
cmd := exec.Command("k", "get", "svc", "-n", namespace, "-o", "json")
util.PrintCommand(cmd)
out, err := cmd.CombinedOutput()
if err != nil {
log.Printf("Error getting all services:\n")
return nil, err
}
sl := List{}
err = json.Unmarshal(out, &sl)
if err != nil {
log.Printf("Error unmarshalling services json:%s\n", err)
return nil, err
}
return &sl, nil
}
// GetAllByPrefixResult is a return struct for GetAllByPrefixAsync
type GetAllByPrefixResult struct {
svcs []Service
err error
}
// GetAllByPrefixAsync wraps Get with a struct response for goroutine + channel usage
func GetAllByPrefixAsync(prefix, namespace string) GetAllByPrefixResult {
svcs, err := GetAllByPrefix(prefix, namespace)
return GetAllByPrefixResult{
svcs: svcs,
err: err,
}
}
// GetAllByPrefix will return all services in a given namespace that match a prefix
func GetAllByPrefix(prefix, namespace string) ([]Service, error) {
sl, err := GetAll(namespace)
if err != nil {
return nil, err
}
services := []Service{}
for _, s := range sl.Services {
matched, err := regexp.MatchString(prefix+"-.*", s.Metadata.Name)
if err != nil {
log.Printf("Error trying to match service name:%s\n", err)
return nil, err
}
if matched {
services = append(services, s)
}
}
return services, nil
}
// Delete will delete a service in a given namespace
func (s *Service) Delete(retries int) error {
var zeroValueDuration time.Duration
var kubectlOutput []byte
var kubectlError error
for i := 0; i < retries; i++ {
cmd := exec.Command("k", "delete", "svc", "-n", s.Metadata.Namespace, s.Metadata.Name)
kubectlOutput, kubectlError = util.RunAndLogCommand(cmd, zeroValueDuration)
if kubectlError != nil {
log.Printf("Error while trying to delete service %s in namespace %s:%s\n", s.Metadata.Namespace, s.Metadata.Name, kubectlError)
log.Printf("%s\n", string(kubectlOutput))
continue
}
break
}
return kubectlError
}
// DescribeServices describes all service resources whose name matches a substring
func DescribeServices(svcPrefix, namespace string) {
svcs, err := GetAllByPrefix(svcPrefix, namespace)
if err != nil {
log.Printf("Unable to get services matching prefix %s in namespace %s: %s", svcPrefix, namespace, err)
}
for _, svc := range svcs {
err := svc.Describe()
if err != nil {
log.Printf("Unable to describe service %s: %s", svc.Metadata.Name, err)
}
}
}
// Describe will describe a service resource
func (s *Service) Describe() error {
var commandTimeout time.Duration
cmd := exec.Command("k", "describe", "svc", s.Metadata.Name, "-n", s.Metadata.Namespace)
out, err := util.RunAndLogCommand(cmd, commandTimeout)
log.Printf("\n%s\n", string(out))
return err
}
// GetNodePort will return the node port for a given pod
func (s *Service) GetNodePort(port int) int {
for _, p := range s.Spec.Ports {
if p.Port == port {
return p.NodePort
}
}
return 0
}
// WaitForIngress waits for an Ingress to be provisioned
func (s *Service) WaitForIngress(timeout, sleep time.Duration) error {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
var mostRecentWaitForIngressError error
ch := make(chan GetResult)
go func() {
for {
select {
case <-ctx.Done():
return
case ch <- GetAsync(s.Metadata.Name, s.Metadata.Namespace):
time.Sleep(sleep)
}
}
}()
for {
select {
case result := <-ch:
mostRecentWaitForIngressError = result.err
svc := result.svc
if mostRecentWaitForIngressError == nil {
if svc != nil && svc.Status.LoadBalancer.Ingress != nil {
s.Status.LoadBalancer.Ingress = svc.Status.LoadBalancer.Ingress
return nil
}
}
case <-ctx.Done():
err := s.Describe()
if err != nil {
log.Printf("Unable to describe service\n: %s", err)
}
return errors.Errorf("WaitForIngress timed out: %s\n", mostRecentWaitForIngressError)
}
}
}
// WaitOnDeleted returns when a service resource is successfully deleted
func WaitOnDeleted(servicePrefix, namespace string, sleep, timeout time.Duration) (bool, error) {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
ch := make(chan GetAllByPrefixResult)
var mostRecentWaitOnDeletedError error
var svcs []Service
go func() {
for {
select {
case <-ctx.Done():
return
case ch <- GetAllByPrefixAsync(servicePrefix, namespace):
time.Sleep(sleep)
}
}
}()
for {
select {
case result := <-ch:
mostRecentWaitOnDeletedError = result.err
svcs = result.svcs
if mostRecentWaitOnDeletedError == nil {
if len(svcs) == 0 {
return true, nil
}
}
case <-ctx.Done():
DescribeServices(servicePrefix, namespace)
return false, errors.Errorf("WaitOnDeleted timed out: %s\n", mostRecentWaitOnDeletedError)
}
}
}
// ValidateWithRetry waits for an Ingress to be provisioned
func (s *Service) ValidateWithRetry(bodyResponseTextMatch string, sleep, timeout time.Duration) error {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
var mostRecentValidateWithRetryError error
ch := make(chan error)
go func() {
for {
select {
case <-ctx.Done():
return
case ch <- s.Validate(bodyResponseTextMatch):
time.Sleep(sleep)
}
}
}()
for {
select {
case result := <-ch:
mostRecentValidateWithRetryError = result
if mostRecentValidateWithRetryError == nil {
return nil
}
case <-ctx.Done():
err := s.Describe()
if err != nil {
log.Printf("Unable to describe service\n: %s", err)
}
return errors.Errorf("ValidateWithRetry timed out: %s\n", mostRecentValidateWithRetryError)
}
}
}
// Validate will attempt to run an http.Get against the root service url
func (s *Service) Validate(bodyResponseTextMatch string) error {
if len(s.Status.LoadBalancer.Ingress) < 1 {
return errors.Errorf("No LB ingress IP for service %s", s.Metadata.Name)
}
var resp *http.Response
url := fmt.Sprintf("http://%s", s.Status.LoadBalancer.Ingress[0]["ip"])
resp, err := http.Get(url)
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
return errors.Errorf("Unable to call service at URL %s: %s", url, err)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return errors.Errorf("Unable to parse response body: %s", err)
}
matched, err := regexp.MatchString(bodyResponseTextMatch, string(body))
if err != nil {
return errors.Errorf("Unable to evalute response body against a regular expression match: %s", err)
}
if matched {
return nil
}
return errors.Errorf("Got unexpected URL body, expected to find %s, got:\n%s\n", bodyResponseTextMatch, string(body))
}
// CreateServiceFromFile will create a Service from file with a name
func CreateServiceFromFile(filename, name, namespace string) (*Service, error) {
cmd := exec.Command("k", "create", "-f", filename)
util.PrintCommand(cmd)
out, err := cmd.CombinedOutput()
if err != nil {
log.Printf("Error trying to create Service %s:%s\n", name, string(out))
return nil, err
}
svc, err := Get(name, namespace)
if err != nil {
log.Printf("Error while trying to fetch Service %s:%s\n", name, err)
return nil, err
}
return svc, nil
}
// CreateServiceFromFileDeleteIfExist will create a Service from file, deleting any pre-existing service with the same name
func CreateServiceFromFileDeleteIfExist(filename, name, namespace string) (*Service, error) {
s, _ := Get(name, namespace)
if s != nil {
err := s.Delete(util.DefaultDeleteRetries)
if err != nil {
return nil, err
}
_, err = WaitOnDeleted(name, namespace, 10*time.Second, 1*time.Minute)
if err != nil {
return nil, err
}
}
return CreateServiceFromFile(filename, name, namespace)
}