Skip to content

Commit 31033b9

Browse files
authored
Merge pull request #605 from ginglis13/resolver-metrics-port
Update HTTP resolver to return metrics port
2 parents 056e169 + 453bcf6 commit 31033b9

File tree

6 files changed

+49
-7
lines changed

6 files changed

+49
-7
lines changed

snapshotter/app/service.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"context"
1818
"fmt"
1919
"net"
20+
"net/url"
2021
"os"
2122
"os/signal"
2223
"strconv"
@@ -134,17 +135,33 @@ func initSnapshotter(ctx context.Context, config config.Config) (snapshots.Snaps
134135
if err != nil {
135136
return nil, err
136137
}
137-
host, portstr, err := net.SplitHostPort(response.Address)
138+
u, err := url.Parse(response.Address)
138139
if err != nil {
139140
return nil, err
140141
}
141-
port, err := strconv.ParseUint(portstr, base10, bits32)
142+
host := u.Hostname()
143+
port, err := strconv.ParseUint(response.SnapshotterPort, base10, bits32)
142144
if err != nil {
143145
return nil, err
144146
}
145147
snapshotterDialer := func(ctx context.Context, namespace string) (net.Conn, error) {
146148
return vsock.DialContext(ctx, host, uint32(port), vsock.WithLogger(log.G(ctx)))
147149
}
150+
151+
if config.Snapshotter.Metrics.Enable {
152+
metricsPort, err := strconv.ParseUint(response.MetricsPort, base10, bits32)
153+
if err != nil {
154+
return nil, err
155+
}
156+
157+
// TODO (ginglis13) metricsDialer func to be defined here using metricsPort. It will dial
158+
// the same host but connect via its own port. The metrics proxy will be configured in NewProxySnapshotter
159+
// task 2 of https://github.com/firecracker-microvm/firecracker-containerd/issues/602
160+
_ = func(ctx context.Context, _ string) (net.Conn, error) {
161+
return vsock.DialContext(ctx, host, uint32(metricsPort), vsock.WithLogger(log.G(ctx)))
162+
}
163+
}
164+
148165
return proxy.NewProxySnapshotter(ctx, host, snapshotterDialer)
149166
}
150167

snapshotter/config/config.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ type Config struct {
3333
type snapshotter struct {
3434
Listener listener `toml:"listener"`
3535
Proxy proxy `toml:"proxy"`
36+
Metrics metrics `toml:"metrics"`
3637
}
3738

3839
type listener struct {
@@ -57,6 +58,10 @@ type debug struct {
5758
LogLevel string `toml:"logLevel" default:"info"`
5859
}
5960

61+
type metrics struct {
62+
Enable bool `toml:"enable" default:"false"`
63+
}
64+
6065
// Load parses application configuration from a specified file path.
6166
func Load(filePath string) (Config, error) {
6267
file, err := os.Open(filePath)

snapshotter/config/config.toml.example

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,8 @@
66
type = "http"
77
address = "127.0.0.1:10001"
88

9+
[snapshotter.metrics]
10+
enable = true
11+
912
[debug]
1013
logLevel = "info"

snapshotter/config/config_test.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ func defaultConfig() error {
6262
Network: "unix",
6363
Address: "/var/lib/demux-snapshotter/snapshotter.sock",
6464
},
65+
Metrics: metrics{
66+
Enable: false,
67+
},
6568
},
6669
Debug: debug{
6770
LogLevel: "info",
@@ -79,6 +82,8 @@ func parseExampleConfig() error {
7982
[snapshotter.proxy.address.resolver]
8083
type = "http"
8184
address = "localhost:10001"
85+
[snapshotter.metrics]
86+
enable = true
8287
8388
[debug]
8489
logLevel = "debug"
@@ -97,6 +102,9 @@ func parseExampleConfig() error {
97102
},
98103
},
99104
},
105+
Metrics: metrics{
106+
Enable: true,
107+
},
100108
},
101109
Debug: debug{
102110
LogLevel: "debug",
@@ -116,7 +124,7 @@ func parseConfig(input []byte, expected Config) error {
116124
return errors.New("expected no error on parse")
117125
}
118126
if actual != expected {
119-
return fmt.Errorf("expected %s actual %s", expected, actual)
127+
return fmt.Errorf("expected %v actual %v", expected, actual)
120128
}
121129
return nil
122130
}

snapshotter/demux/proxy/address/http_resolver_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func returnErrorOnJSONParserError() error {
5858
}
5959

6060
func happyPath() error {
61-
reader := mockReader{response: []byte(`{"network": "unix", "address": "/path/to/snapshotter.vsock"}`), err: io.EOF}
61+
reader := mockReader{response: []byte(`{"network": "unix", "address": "/path/to/snapshotter.vsock", "snapshotter_port": "10000", "metrics_port": "10001"}`), err: io.EOF}
6262
client := mockClient{getError: nil, getResponse: http.Response{Body: &reader}}
6363
uut := HTTPResolver{url: "localhost:10001", client: &client}
6464

@@ -73,6 +73,12 @@ func happyPath() error {
7373
if actual.Address != "/path/to/snapshotter.vsock" {
7474
return fmt.Errorf("Expected address '/path/to/snapshotter.vsock' but actual %s", actual.Address)
7575
}
76+
if actual.SnapshotterPort != "10000" {
77+
return fmt.Errorf("Expected metrics port '10000' but actual %s", actual.MetricsPort)
78+
}
79+
if actual.MetricsPort != "10001" {
80+
return fmt.Errorf("Expected metrics port '10001' but actual %s", actual.MetricsPort)
81+
}
7682
return nil
7783
}
7884

snapshotter/demux/proxy/address/resolver.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,13 @@ type Response struct {
2121
Network string `json:"network"`
2222

2323
// Network address used in net.Dial.
24-
// Assumes the host port forms documented in net.SplitHostPort.
25-
//
26-
// Reference: https://pkg.go.dev/net#SplitHostPort
2724
Address string `json:"address"`
25+
26+
// SnapshotterPort is the port used in vsock.DialContext for sending snapshotter API requests to the remote snapshotter.
27+
SnapshotterPort string `json:"snapshotter_port"`
28+
29+
// MetricsPort is the port used in vsock.DialContext for sending metrics requests to the remote snapshotter.
30+
MetricsPort string `json:"metrics_port"`
2831
}
2932

3033
// Resolver for the proxy network address.

0 commit comments

Comments
 (0)