-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsidecar.go
81 lines (71 loc) · 1.69 KB
/
sidecar.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
package api
import (
"fmt"
"strconv"
)
type Sidecar struct {
CloudSQLProxy *CloudSQLProxySidecar `json:"cloudSqlProxy" yaml:"cloudSqlProxy"`
}
func (s *Sidecar) Valid() error {
var n int
if s.CloudSQLProxy != nil {
n++
if err := s.CloudSQLProxy.Valid(); err != nil {
return fmt.Errorf("cloudSqlProxy: %w", err)
}
}
if n != 1 {
return fmt.Errorf("only 1 sidecar config per item is allowed")
}
return nil
}
func (s *Sidecar) Config() *SidecarConfig {
switch {
case s.CloudSQLProxy != nil:
return s.CloudSQLProxy.config()
}
return nil
}
type SidecarConfig struct {
Name string
Image string
Env map[string]string
Command []string
Args []string
Port *int
MountData map[string]string
}
type CloudSQLProxySidecar struct {
Instance string `json:"instance" yaml:"instance"`
Port int `json:"port" yaml:"port"`
Credentials string `json:"credentials" yaml:"credentials"`
}
func (s *CloudSQLProxySidecar) Valid() error {
if s.Instance == "" {
return fmt.Errorf("instance is required")
}
return nil
}
func (s *CloudSQLProxySidecar) config() *SidecarConfig {
port := s.Port
if port <= 0 {
port = 3300
}
cfg := SidecarConfig{
Name: "cloudsql-proxy",
Image: "gcr.io/cloud-sql-connectors/cloud-sql-proxy:2.7.0",
Port: &port,
Args: []string{
s.Instance,
"-p=" + strconv.Itoa(port),
"--max-sigterm-delay=30s",
},
MountData: map[string]string{},
}
if s.Credentials != "" {
// cfg.Args = append(cfg.Args, "--json-credentials="+s.Credentials)
cfg.Args = append(cfg.Args, "--credentials-file=/sidecar/cloudsqlproxy/credentials.json")
cfg.MountData["/sidecar/cloudsqlproxy/credentials.json"] = s.Credentials
}
return &cfg
}