Skip to content

Commit 44270a2

Browse files
committed
refactor: internal HTTP address resolver
Change the internal mechanics to query firecracker-containerd instead of using the requests namespace as the VM ID. This internal mechanism will be used for integration testing. Signed-off-by: Austin Vazquez <[email protected]>
1 parent c4f9c73 commit 44270a2

File tree

3 files changed

+27
-41
lines changed

3 files changed

+27
-41
lines changed

snapshotter/.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
demux-snapshotter
2-
http-resolver
2+
http-address-resolver

snapshotter/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ all: demux-snapshotter
2424
demux-snapshotter: $(SOURCES) $(GOMOD) $(GOSUM)
2525
go build $(EXTRAGOARGS) -ldflags "-X main.revision=$(REVISION)" -o $@
2626

27-
http-resolver: $(SOURCES) $(GOMOD) $(GOSUM)
27+
http-address-resolver: $(SOURCES) $(GOMOD) $(GOSUM)
2828
go build $(EXTRAGOARGS) -ldflags "-X main.revision=$(REVISION)" -o $@ internal/http_address_resolver.go
2929

3030
test:

snapshotter/internal/http_address_resolver.go

Lines changed: 25 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
"encoding/json"
1919
"flag"
2020
"fmt"
21-
"io/ioutil"
2221
"net/http"
2322
"os"
2423
"os/signal"
@@ -27,24 +26,20 @@ import (
2726
"github.com/sirupsen/logrus"
2827
"golang.org/x/sync/errgroup"
2928

29+
"github.com/firecracker-microvm/firecracker-containerd/firecracker-control/client"
30+
"github.com/firecracker-microvm/firecracker-containerd/proto"
3031
proxyaddress "github.com/firecracker-microvm/firecracker-containerd/snapshotter/demux/proxy/address"
3132
)
3233

3334
var (
34-
port int
35-
filepath string
36-
logger *logrus.Logger
37-
store map[string]networkAddress
35+
port int
36+
containerdSockPath string
37+
logger *logrus.Logger
3838
)
3939

40-
type networkAddress struct {
41-
Network string `json:"network"`
42-
Address string `json:"address"`
43-
}
44-
4540
func init() {
46-
flag.IntVar(&port, "port", 10001, "port to be used in the network address")
47-
flag.StringVar(&filepath, "map", "map.json", "filepath to map configuration")
41+
flag.IntVar(&port, "port", 10001, "service port for address resolver")
42+
flag.StringVar(&containerdSockPath, "containerdSocket", "/run/firecracker-containerd/containerd.sock", "filepath to the containerd socket")
4843
logger = logrus.New()
4944
}
5045

@@ -56,8 +51,8 @@ func init() {
5651
//
5752
// Response:
5853
// {
59-
// "network": "tcp",
60-
// "address": "192.168.0.1:80"
54+
// "network": "unix",
55+
// "address": "/var/lib/firecracker-containerd/shim-base/default#cbfad871-0862-4dd6-ae7a-52e9b1c16ede/firecracker.vsock"
6156
// }
6257
func main() {
6358
ctx, cancel := context.WithCancel(context.Background())
@@ -74,17 +69,12 @@ func main() {
7469
flag.Parse()
7570
}
7671

77-
if err := loadStoreFromFile(filepath); err != nil {
78-
logger.WithError(err).Error()
79-
os.Exit(1)
80-
}
81-
8272
http.HandleFunc("/address", queryAddress)
8373
httpServer := &http.Server{
8474
Addr: fmt.Sprintf(":%d", port),
8575
}
8676

87-
logger.Info(fmt.Sprintf("http server serving at port %d", port))
77+
logger.Info(fmt.Sprintf("http resolver serving at port %d", port))
8878
g, gCtx := errgroup.WithContext(ctx)
8979
g.Go(func() error {
9080
return httpServer.ListenAndServe()
@@ -102,20 +92,6 @@ func main() {
10292
logger.Info("http: server closed")
10393
}
10494

105-
func loadStoreFromFile(filepath string) error {
106-
store = make(map[string]networkAddress)
107-
contextLogger := logger.WithField("filepath", filepath)
108-
contextLogger.Info("opening file for read...")
109-
110-
fileStream, err := ioutil.ReadFile(filepath)
111-
if err != nil {
112-
return err
113-
}
114-
115-
contextLogger.Info("reading JSON from file...")
116-
return json.Unmarshal(fileStream, &store)
117-
}
118-
11995
func queryAddress(writ http.ResponseWriter, req *http.Request) {
12096
if req.Method != http.MethodGet {
12197
http.Error(writ, fmt.Sprintf("%s method not allowed", req.Method), http.StatusForbidden)
@@ -132,17 +108,27 @@ func queryAddress(writ http.ResponseWriter, req *http.Request) {
132108
}
133109

134110
namespace := keys[0]
135-
networkAddress, ok := store[namespace]
136-
if !ok {
137-
http.Error(writ, "Socket path not found", http.StatusNotFound)
111+
112+
fcClient, err := client.New(containerdSockPath + ".ttrpc")
113+
if err != nil {
114+
logger.WithError(err).Error("could not create firecracker client")
115+
http.Error(writ, "Internal server error", http.StatusInternalServerError)
116+
return
117+
}
118+
defer fcClient.Close()
119+
120+
vmInfo, err := fcClient.GetVMInfo(req.Context(), &proto.GetVMInfoRequest{VMID: namespace})
121+
if err != nil {
122+
logger.WithField("VMID", namespace).WithError(err).Error("unable to retrieve VM Info")
123+
http.Error(writ, "Internal server error", http.StatusInternalServerError)
138124
return
139125
}
140126

141127
writ.WriteHeader(http.StatusOK)
142128

143129
response, err := json.Marshal(proxyaddress.Response{
144-
Network: networkAddress.Network,
145-
Address: networkAddress.Address,
130+
Network: "unix",
131+
Address: vmInfo.VSockPath,
146132
})
147133
if err != nil {
148134
http.Error(writ, "Internal server error", http.StatusInternalServerError)

0 commit comments

Comments
 (0)