-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.go
105 lines (84 loc) · 2.4 KB
/
run.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
package run
import (
"context"
"fmt"
"net/url"
"github.com/barpilot/cloud-run-controller/pkg/utils"
"github.com/spf13/pflag"
"google.golang.org/api/option"
runApi "google.golang.org/api/run/v1alpha1"
)
type RunManager struct {
project string
service *runApi.APIService
}
func NewRunManager(projectName string) (*RunManager, error) {
if projectName == "" {
return &RunManager{}, fmt.Errorf("empty project")
}
ctx := context.Background()
rm := &RunManager{project: projectName}
api := pflag.Lookup("apikey").Value.String()
runService, err := runApi.NewService(ctx, option.WithAPIKey(api))
if err != nil {
return rm, err
}
rm.service = runService
return rm, err
}
func (rm *RunManager) getAllLocations() ([]runApi.Location, error) {
locations := []runApi.Location{}
list, err := rm.service.Projects.Locations.List(fmt.Sprintf("projects/%s", rm.project)).Do()
if err != nil {
return locations, err
}
for _, location := range list.Locations {
locations = append(locations, *location)
}
return locations, nil
}
type RunService struct {
Name string
Hostname string
}
func (rm *RunManager) GetAllServices() ([]RunService, error) {
runServices := []RunService{}
locations, err := rm.getAllLocations()
if err != nil {
return runServices, err
}
for _, location := range locations {
services, err := rm.service.Projects.Locations.Services.List(location.Name).Do()
if err != nil {
return runServices, err
}
for _, item := range services.Items {
u, err := url.Parse(item.Status.Address.Hostname)
if err != nil {
return runServices, err
}
runServices = append(runServices, RunService{Name: item.Metadata.Name, Hostname: u.Host})
}
}
return runServices, nil
}
func (rm *RunManager) CreateOrUpdate(parent string, service Service) error {
svc := runApi.Service(service)
name := utils.ServiceName(parent, service.Metadata.Name)
_, err := rm.service.Projects.Locations.Services.Get(name).Do()
if err != nil {
if _, err := rm.service.Projects.Locations.Services.Create(parent, &svc).Do(); err != nil {
return err
}
} else {
if _, err := rm.service.Projects.Locations.Services.ReplaceService(name, &svc).Do(); err != nil {
return err
}
}
return nil
}
func (rm *RunManager) Delete(parent string, service Service) error {
name := utils.ServiceName(parent, service.Metadata.Name)
_, err := rm.service.Projects.Locations.Services.Delete(name).Do()
return err
}