-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
93 lines (78 loc) · 1.87 KB
/
main.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
package main
import (
"fmt"
"log"
"os"
"strings"
"cloud.google.com/go/run/apiv2"
"cloud.google.com/go/run/apiv2/runpb"
"github.com/urfave/cli/v2"
)
func matchImageName(a, b string) bool {
return strings.Split(a, ":")[0] == strings.Split(b, ":")[0] ||
strings.Split(a, "@")[0] == strings.Split(b, "@")[0] ||
strings.Split(a, ":")[0] == strings.Split(b, "@")[0] ||
strings.Split(a, "@")[0] == strings.Split(b, ":")[0]
}
func main() {
app := &cli.App{
Name: "gcloud-run-deploy-multi",
Usage: "update images on Cloud Run service with multiple containers",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "name",
Usage: "name of the service",
Required: true,
},
&cli.StringSliceFlag{
Name: "image",
Usage: "image used in the service to update",
Required: true,
},
},
Action: func(ctx *cli.Context) error {
client, err := run.NewServicesClient(ctx.Context)
if err != nil {
return err
}
fmt.Println("Fetching the service")
service, err := client.GetService(
ctx.Context,
&runpb.GetServiceRequest{
Name: ctx.String("name"),
},
)
if err != nil {
return err
}
service.GetTemplate().Revision = ""
for _, container := range service.GetTemplate().GetContainers() {
for _, image := range ctx.StringSlice("image") {
if matchImageName(image, container.GetImage()) {
fmt.Printf("%s -> %s\n", container.GetImage(), image)
container.Image = image
}
}
}
fmt.Println("Modifying the service")
future, err := client.UpdateService(
ctx.Context,
&runpb.UpdateServiceRequest{
Service: service,
},
)
if err != nil {
return err
}
newService, err := future.Wait(ctx.Context)
if err != nil {
return err
}
fmt.Printf("%+v\n", newService)
return nil
},
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}