Andrey Smirnov 2bf8540855 test: provision Talos clusters via Firecracker VMs
This is initial PR to push the initial code, it has several known
problems which are going to be addressed in follow-up PRs:

1. there's no "cluster destroy", so the only way to stop the VMs is to
`pkill firecracker`

2. provisioner creates state in `/tmp` and never deletes it, that is
required to keep cluster running when `osctl cluster create` finishes

3. doesn't run any controller process around firecracker to support
reboots/CNI cleanup (vethxyz interfaces are lingering on the host as
they're never cleaned up)

The plan is to create some structure in `~/.talos` to manage cluster
state, e.g. `~/.talos/clusters/<name>` which will contain all the
required files (disk images, file sockets, VM logs, etc.). This
directory structure will also work as a way to detect running clusters
and clean them up.

For point number 3, `osctl cluster create` is going to exec lightweight
process to control the firecracker VM process and to simulate VM reboots
if firecracker finishes cleanly (when VM reboots).

Signed-off-by: Andrey Smirnov <smirnov.andrey@gmail.com>
2020-01-16 00:27:08 +03:00

86 lines
1.7 KiB
Go

// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
// Package inmemhttp implements temporary HTTP server which is based off memory fs.
package inmemhttp
import (
"context"
"net"
"net/http"
"strconv"
)
// Server is an in-memory http web server.
type Server interface {
AddFile(filename string, contents []byte) error
GetAddr() net.Addr
Serve()
Shutdown(ctx context.Context) error
}
type server struct {
l net.Listener
addr net.Addr
srv *http.Server
mux *http.ServeMux
}
// NewServer creates in-mem HTTP server.
func NewServer(address string) (Server, error) {
s := &server{
mux: http.NewServeMux(),
}
var err error
s.l, err = net.Listen("tcp", address)
if err != nil {
return nil, err
}
s.addr = s.l.Addr()
s.srv = &http.Server{
Handler: s.mux,
}
return s, nil
}
func (s *server) AddFile(filename string, contents []byte) error {
contentsCopy := append([]byte(nil), contents...)
s.mux.Handle("/"+filename, http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
switch req.Method {
case http.MethodHead:
w.Header().Add("Content-Length", strconv.Itoa(len(contentsCopy)))
w.WriteHeader(http.StatusOK)
case http.MethodGet:
w.Header().Add("Content-Length", strconv.Itoa(len(contentsCopy)))
w.WriteHeader(http.StatusOK)
w.Write(contentsCopy) //nolint: errcheck
default:
w.WriteHeader(http.StatusNotImplemented)
}
}))
return nil
}
func (s *server) GetAddr() net.Addr {
return s.addr
}
func (s *server) Serve() {
go s.srv.Serve(s.l) //nolint: errcheck
}
func (s *server) Shutdown(ctx context.Context) error {
return s.srv.Shutdown(ctx)
}