Skip to content

Commit

Permalink
[Daemon] Create a listener on host virtual IP and add virtualnetwork mux
Browse files Browse the repository at this point in the history
This patch is creating a listener on host virtual IP on port 7777
and then adding handler to it which able to serve the virtual network
service.

From the VM following is now possible. Now it is also possible for
`podman-machine-cni` plugin to add/remove port mapping when a container
created with port expose.

```
$ curl host.crc.testing:7777/services/forwarder/all | jq .
[
  {
    "local": ":2222",
    "remote": "192.168.127.2:22"
  },
  {
    "local": ":6443",
    "remote": "192.168.127.2:6443"
  },
  {
    "local": ":443",
    "remote": "192.168.127.2:443"
  },
  {
    "local": ":80",
    "remote": "192.168.127.2:80"
  }
]

$ podman run -d -p 8080:80 docker.io/httpd:2.4
a29368f56624112de0a1e00c51875a2e9d3125ce6d08991dfec9cfe09c8bb394

$ curl host.crc.testing:7777/services/forwarder/all | jq .
[
  {
    "local": ":443",
    "remote": "192.168.127.2:443"
  },
  {
    "local": ":80",
    "remote": "192.168.127.2:80"
  },
  {
    "local": "0.0.0.0:8080",
    "remote": "192.168.127.2:8080"
  },
  {
    "local": ":2222",
    "remote": "192.168.127.2:22"
  },
  {
    "local": ":6443",
    "remote": "192.168.127.2:6443"
  }
]
```
  • Loading branch information
praveenkumar authored and gbraad committed Dec 10, 2021
1 parent c17ab41 commit 84ad942
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions cmd/crc/cmd/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,17 @@ func run(configuration *types.Configuration) error {
}
}()

networkListener, err := vn.Listen("tcp", fmt.Sprintf("%s:7777", hostVirtualIP))
if err != nil {
return err
}
go func() {
mux := networkAPIMux(vn)
if err := http.Serve(networkListener, handlers.LoggingHandler(os.Stderr, mux)); err != nil {
errCh <- errors.Wrap(err, "host virtual IP http.Serve failed")
}
}()

go func() {
if runtime.GOOS == "darwin" {
for {
Expand Down Expand Up @@ -243,6 +254,12 @@ func gatewayAPIMux() *http.ServeMux {
return mux
}

func networkAPIMux(vn *virtualnetwork.VirtualNetwork) *http.ServeMux {
mux := http.NewServeMux()
mux.Handle("/", vn.Mux())
return mux
}

func acceptJSONStringArray(w http.ResponseWriter, r *http.Request, fun func(hostnames []string) error) {
if r.Method != http.MethodPost {
http.Error(w, "post only", http.StatusBadRequest)
Expand Down

0 comments on commit 84ad942

Please sign in to comment.