-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathservice.go
103 lines (86 loc) · 2.47 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
package run
import (
"context"
"fmt"
"net/http"
"strings"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
)
const (
// serviceAPIBaseURL is the base URL for Cloud Run service.
serviceAPIBaseURL = "https://run.googleapis.com/v2/projects/%s/locations/%s/services"
// revisionAPIBaseURL is the base URL for Cloud Run revision.
revisionAPIBaseURL = "https://run.googleapis.com/v2/%s"
managedByGCF = "cloudfunctions"
runtimeGCF = "gcf"
runtimeRun = "run"
)
type serviceList struct {
Services []*service `json:"services"`
}
type service struct {
Name string `json:"name"`
FullName string `json:"fullName"`
Revision string `json:"latestReadyRevision"`
Containers []*container `json:"containers"`
Runtime string `json:"runtime"`
}
type revision struct {
Labels struct {
ManagedBy string `json:"goog-managed-by"` //nolint:tagliatelle
} `json:"labels"`
Conditions []*container `json:"containers"`
}
type container struct {
Name string `json:"name"`
Image string `json:"image"`
}
func getServices(ctx context.Context, projectID, region string) ([]*service, error) {
if projectID == "" {
return nil, errors.New("projectID is empty")
}
if region == "" {
return nil, errors.New("region is empty")
}
u := fmt.Sprintf(serviceAPIBaseURL, projectID, region)
req, err := http.NewRequest(http.MethodGet, u, nil)
if err != nil {
return nil, errors.Wrap(err, "error client creating service request")
}
var list serviceList
if err := apiClient.Get(ctx, req, &list); err != nil {
return nil, errors.Wrap(err, "error decoding service response")
}
if len(list.Services) == 0 {
return list.Services, nil
}
// add revision images
for _, s := range list.Services {
if s.Revision == "" {
log.Logger.Debug().Msgf("service %s has no revision", s.Name)
continue
}
u := fmt.Sprintf(revisionAPIBaseURL, s.Revision)
req, err = http.NewRequest(http.MethodGet, u, nil)
if err != nil {
return nil, errors.Wrap(err, "error client creating revision request")
}
var rev revision
if err := apiClient.Get(ctx, req, &rev); err != nil {
return nil, errors.Wrapf(err, "error decoding revision response from: %s", u)
}
s.FullName = s.Name
s.Name = parseServiceName(s.Name)
s.Containers = rev.Conditions
if rev.Labels.ManagedBy == managedByGCF {
s.Runtime = runtimeGCF
} else {
s.Runtime = runtimeRun
}
}
return list.Services, nil
}
func parseServiceName(s string) string {
return s[strings.LastIndex(s, "/")+1:]
}