/
routingapi.go
134 lines (120 loc) · 3.29 KB
/
routingapi.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
package runners
import (
"fmt"
"os"
"os/exec"
"strconv"
"time"
"gopkg.in/yaml.v2"
routing_api "code.cloudfoundry.org/routing-api"
apiconfig "code.cloudfoundry.org/routing-api/config"
"code.cloudfoundry.org/routing-api/models"
"code.cloudfoundry.org/tlsconfig"
ginkgomon "github.com/tedsuo/ifrit/ginkgomon_v2"
)
type Config struct {
apiconfig.Config
DevMode bool
IP string
}
type RoutingAPIRunner struct {
Config Config
configPath, binPath string
}
type SQLConfig struct {
Port int
DBName string
DriverName string
Username string
Password string
}
func NewRoutingAPIRunner(binPath string, adminPort int, sqlConfig SQLConfig, fs ...func(*Config)) (*RoutingAPIRunner, error) {
cfg := Config{
DevMode: true,
Config: apiconfig.Config{
AdminPort: adminPort,
// required fields
MetricsReportingIntervalString: "500ms",
StatsdClientFlushIntervalString: "10ms",
SystemDomain: "example.com",
LogGuid: "routing-api-logs",
RouterGroups: models.RouterGroups{
{
Name: "default-tcp",
Type: "tcp",
ReservablePorts: "1024-65535",
},
},
// end of required fields
SqlDB: apiconfig.SqlDB{
Host: "localhost",
Port: sqlConfig.Port,
Schema: sqlConfig.DBName,
Type: sqlConfig.DriverName,
Username: sqlConfig.Username,
Password: sqlConfig.Password,
},
UUID: "routing-api-uuid",
},
}
for _, f := range fs {
f(&cfg)
}
f, err := os.CreateTemp(os.TempDir(), "routing-api-config")
if err != nil {
return nil, err
}
defer f.Close()
configBytes, err := yaml.Marshal(cfg.Config)
if err != nil {
return nil, err
}
_, err = f.Write(configBytes)
if err != nil {
return nil, err
}
return &RoutingAPIRunner{
Config: cfg,
configPath: f.Name(),
binPath: binPath,
}, nil
}
func (runner *RoutingAPIRunner) Run(signals <-chan os.Signal, ready chan<- struct{}) error {
// Create a new ginkgomon runner here instead in New() so that we can restart
// the same runner without having to worry about messing the state of the
// ginkgomon Runner
args := []string{
"-ip", "localhost",
"-config", runner.configPath,
"-logLevel=debug",
"-devMode=" + strconv.FormatBool(runner.Config.DevMode),
}
r := ginkgomon.New(ginkgomon.Config{
Name: "routing-api",
Command: exec.Command(runner.binPath, args...),
StartCheck: "routing-api.started",
StartCheckTimeout: 20 * time.Second,
})
return r.Run(signals, ready)
}
type RoutingAPIClientConfig struct {
Port int
CACertFile string
ClientCertFile string
ClientKeyFile string
}
func NewRoutingAPIClient(config RoutingAPIClientConfig) (routing_api.Client, error) {
if config.CACertFile != "" && config.ClientCertFile != "" && config.ClientKeyFile != "" {
tlsConfig, err := tlsconfig.Build(
tlsconfig.WithInternalServiceDefaults(),
tlsconfig.WithIdentityFromFile(config.ClientCertFile, config.ClientKeyFile),
).Client(
tlsconfig.WithAuthorityFromFile(config.CACertFile),
)
if err != nil {
return nil, err
}
return routing_api.NewClientWithTLSConfig(fmt.Sprintf("https://127.0.0.1:%d", config.Port), tlsConfig), nil
}
return routing_api.NewClient(fmt.Sprintf("http://127.0.0.1:%d", config.Port), false), nil
}