Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move MicroCloud to microcluster v2 #398

Merged
merged 9 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 1 addition & 18 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -214,29 +214,12 @@ jobs:
sudo snap install lxd --channel latest/edge || sudo snap refresh lxd --channel latest/edge
sudo lxd init --auto

- name: Build MicroOVN from commit 803dc03ea847d214941330c74cc0ae0027550dc0
run: |
set -eux

sudo snap refresh
sudo snap set lxd daemon.group=adm
sudo snap install snapcraft --classic
git clone https://github.com/canonical/microovn
cd microovn
git checkout 803dc03ea847d214941330c74cc0ae0027550dc0
# disable golangci-lint as the latest version is not happy with this commit.
sed -e "s/golangci-lint run/# &/" -i snap/snapcraft.yaml
make microovn.snap
export MICROOVN_SNAP_PATH="$(readlink -f microovn.snap)"
echo "MICROOVN_SNAP_PATH=${MICROOVN_SNAP_PATH}" >> "${GITHUB_ENV}"
cd -

- name: "Prepare for system tests"
run: |
set -eux
chmod +x ~
cd test
sudo --preserve-env=DEBUG,GITHUB_ACTIONS,MICROCLOUD_DEBUG_PATH,MICROCLOUDD_DEBUG_PATH,SKIP_VM_LAUNCH,SNAPSHOT_RESTORE,TEST_STORAGE_SOURCE,TESTBED_READY,MICROOVN_SNAP_PATH ./main.sh setup
sudo --preserve-env=DEBUG,GITHUB_ACTIONS,MICROCLOUD_DEBUG_PATH,MICROCLOUDD_DEBUG_PATH,SKIP_VM_LAUNCH,SNAPSHOT_RESTORE,TEST_STORAGE_SOURCE,TESTBED_READY ./main.sh setup
echo "TESTBED_READY=1" >> "${GITHUB_ENV}"

- name: "Run system tests (${{ matrix.go }}, ${{ matrix.suite }})"
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
GOMIN=1.22.4
GOMIN=1.22.5

.PHONY: default
default: build
Expand Down
2 changes: 1 addition & 1 deletion api/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func NewResponse(response *http.Response) response.Response {
}

// Render implements response.Response for Response, enabling use with a rest.EndpointAction Handler function.
func (r *Response) Render(w http.ResponseWriter) error {
func (r *Response) Render(w http.ResponseWriter, _ *http.Request) error {
decoder := json.NewDecoder(r.response.Body)

var responseRaw *api.ResponseRaw
Expand Down
8 changes: 4 additions & 4 deletions api/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (

"github.com/canonical/lxd/lxd/response"
"github.com/canonical/lxd/lxd/util"
"github.com/canonical/microcluster/rest"
"github.com/canonical/microcluster/state"
"github.com/canonical/microcluster/v2/rest"
"github.com/canonical/microcluster/v2/state"

"github.com/canonical/microcloud/microcloud/api/types"
"github.com/canonical/microcloud/microcloud/service"
Expand All @@ -28,7 +28,7 @@ var ServicesCmd = func(sh *service.Handler) rest.Endpoint {
}

// servicesPut updates the cluster status of the MicroCloud peer.
func servicesPut(state *state.State, r *http.Request) response.Response {
func servicesPut(state state.State, r *http.Request) response.Response {
// Parse the request.
req := types.ServicesPut{}

Expand All @@ -50,7 +50,7 @@ func servicesPut(state *state.State, r *http.Request) response.Response {
addr = req.Address
}

sh, err := service.NewHandler(state.Name(), addr, state.OS.StateDir, false, false, services...)
sh, err := service.NewHandler(state.Name(), addr, state.FileSystem().StateDir, services...)
if err != nil {
return response.SmartError(err)
}
Expand Down
6 changes: 3 additions & 3 deletions api/services_auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ import (
"github.com/canonical/lxd/lxd/response"
"github.com/canonical/lxd/lxd/util"
"github.com/canonical/lxd/shared/logger"
"github.com/canonical/microcluster/state"
"github.com/canonical/microcluster/v2/state"

"github.com/canonical/microcloud/microcloud/service"
)

// endpointHandler is just a convenience for writing clean return types.
type endpointHandler func(*state.State, *http.Request) response.Response
type endpointHandler func(state.State, *http.Request) response.Response

// authHandler ensures a request has been authenticated with the mDNS broadcast secret.
func authHandler(sh *service.Handler, f endpointHandler) endpointHandler {
return func(s *state.State, r *http.Request) response.Response {
return func(s state.State, r *http.Request) response.Response {
if r.RemoteAddr == "@" {
logger.Debug("Allowing unauthenticated request through unix socket")

Expand Down
8 changes: 4 additions & 4 deletions api/services_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"github.com/canonical/lxd/lxd/response"
"github.com/canonical/lxd/shared/api"
"github.com/canonical/lxd/shared/logger"
"github.com/canonical/microcluster/rest"
"github.com/canonical/microcluster/state"
"github.com/canonical/microcluster/v2/rest"
"github.com/canonical/microcluster/v2/state"
"github.com/gorilla/mux"

"github.com/canonical/microcloud/microcloud/api/types"
Expand All @@ -29,7 +29,7 @@ var ServicesClusterCmd = func(sh *service.Handler) rest.Endpoint {
}

// removeClusterMember removes the given cluster member from all services that it exists in.
func removeClusterMember(state *state.State, r *http.Request) response.Response {
func removeClusterMember(state state.State, r *http.Request) response.Response {
force := r.URL.Query().Get("force") == "1"
name, err := url.PathUnescape(mux.Vars(r)["name"])
if err != nil {
Expand All @@ -54,7 +54,7 @@ func removeClusterMember(state *state.State, r *http.Request) response.Response
return response.SmartError(fmt.Errorf("State address %q is invalid: %w", state.Address().String(), err))
}

sh, err := service.NewHandler(state.Name(), addr, state.OS.StateDir, false, false, existingServices...)
sh, err := service.NewHandler(state.Name(), addr, state.FileSystem().StateDir, existingServices...)
if err != nil {
return response.SmartError(err)
}
Expand Down
12 changes: 6 additions & 6 deletions api/services_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (
"github.com/canonical/lxd/client"
"github.com/canonical/lxd/lxd/response"
"github.com/canonical/lxd/shared/ws"
"github.com/canonical/microcluster/microcluster"
"github.com/canonical/microcluster/rest"
"github.com/canonical/microcluster/state"
"github.com/canonical/microcluster/v2/microcluster"
"github.com/canonical/microcluster/v2/rest"
"github.com/canonical/microcluster/v2/state"

"github.com/canonical/microcloud/microcloud/service"
)
Expand Down Expand Up @@ -56,7 +56,7 @@ func proxy(sh *service.Handler, name, path string, handler endpointHandler) rest
}

// lxdHandler forwards a request made to /1.0/services/lxd/<rest> to /1.0/<rest> on the LXD unix socket.
func lxdHandler(s *state.State, r *http.Request) response.Response {
func lxdHandler(s state.State, r *http.Request) response.Response {
_, path, ok := strings.Cut(r.URL.Path, "/1.0/services/lxd")
if !ok {
return response.SmartError(fmt.Errorf("Invalid path %q", r.URL.Path))
Expand Down Expand Up @@ -110,8 +110,8 @@ func lxdHandler(s *state.State, r *http.Request) response.Response {
}

// microHandler forwards a request made to /1.0/services/<microcluster-service>/<rest> to /1.0/<rest> on the service unix socket.
func microHandler(service string, stateDir string) func(*state.State, *http.Request) response.Response {
return func(s *state.State, r *http.Request) response.Response {
func microHandler(service string, stateDir string) func(state.State, *http.Request) response.Response {
return func(s state.State, r *http.Request) response.Response {
_, path, ok := strings.Cut(r.URL.Path, fmt.Sprintf("/1.0/services/%s", service))
if !ok {
return response.SmartError(fmt.Errorf("Invalid path %q", r.URL.Path))
Expand Down
10 changes: 5 additions & 5 deletions api/services_tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"net/url"

"github.com/canonical/lxd/lxd/response"
"github.com/canonical/microcluster/rest"
"github.com/canonical/microcluster/state"
"github.com/canonical/microcluster/v2/rest"
"github.com/canonical/microcluster/v2/state"
"github.com/gorilla/mux"

"github.com/canonical/microcloud/microcloud/api/types"
Expand All @@ -29,7 +29,7 @@ var ServiceTokensCmd = func(sh *service.Handler) rest.Endpoint {
// serviceTokensPost issues a token for service using the MicroCloud proxy.
// Normally a token request to a service would be restricted to trusted systems,
// so this endpoint validates the mDNS auth token and then proxies the request to the local unix socket of the remote system.
func serviceTokensPost(s *state.State, r *http.Request) response.Response {
func serviceTokensPost(s state.State, r *http.Request) response.Response {
serviceType, err := url.PathUnescape(mux.Vars(r)["serviceType"])
if err != nil {
return response.SmartError(err)
Expand All @@ -43,12 +43,12 @@ func serviceTokensPost(s *state.State, r *http.Request) response.Response {
return response.BadRequest(err)
}

sh, err := service.NewHandler(s.Name(), req.ClusterAddress, s.OS.StateDir, false, false, types.ServiceType(serviceType))
sh, err := service.NewHandler(s.Name(), req.ClusterAddress, s.FileSystem().StateDir, types.ServiceType(serviceType))
if err != nil {
return response.SmartError(err)
}

token, err := sh.Services[types.ServiceType(serviceType)].IssueToken(s.Context, req.JoinerName)
token, err := sh.Services[types.ServiceType(serviceType)].IssueToken(r.Context(), req.JoinerName)
if err != nil {
return response.SmartError(fmt.Errorf("Failed to issue %s token for peer %q: %w", serviceType, req.JoinerName, err))
}
Expand Down
2 changes: 1 addition & 1 deletion client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"time"

"github.com/canonical/lxd/shared/api"
"github.com/canonical/microcluster/client"
"github.com/canonical/microcluster/v2/client"

"github.com/canonical/microcloud/microcloud/api/types"
)
Expand Down
2 changes: 1 addition & 1 deletion client/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"strings"

"github.com/canonical/lxd/shared"
"github.com/canonical/microcluster/client"
"github.com/canonical/microcluster/v2/client"

"github.com/canonical/microcloud/microcloud/api/types"
)
Expand Down
4 changes: 2 additions & 2 deletions cmd/microcloud/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"time"

"github.com/canonical/microcluster/microcluster"
"github.com/canonical/microcluster/v2/microcluster"
"github.com/spf13/cobra"

"github.com/canonical/microcloud/microcloud/api"
Expand Down Expand Up @@ -94,7 +94,7 @@ func (c *cmdAdd) Run(cmd *cobra.Command, args []string) error {
return err
}

s, err := service.NewHandler(cfg.name, cfg.address, c.common.FlagMicroCloudDir, c.common.FlagLogDebug, c.common.FlagLogVerbose, services...)
s, err := service.NewHandler(cfg.name, cfg.address, c.common.FlagMicroCloudDir, services...)
if err != nil {
return err
}
Expand Down
12 changes: 6 additions & 6 deletions cmd/microcloud/cluster_members.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import (
"github.com/canonical/lxd/shared"
cli "github.com/canonical/lxd/shared/cmd"
"github.com/canonical/lxd/shared/termios"
"github.com/canonical/microcluster/client"
"github.com/canonical/microcluster/cluster"
"github.com/canonical/microcluster/microcluster"
"github.com/canonical/microcluster/v2/client"
"github.com/canonical/microcluster/v2/cluster"
"github.com/canonical/microcluster/v2/microcluster"
"github.com/spf13/cobra"
"golang.org/x/sys/unix"
"gopkg.in/yaml.v2"
Expand Down Expand Up @@ -95,7 +95,7 @@ func (c *cmdClusterMembersList) Run(cmd *cobra.Command, args []string) error {
}

// Get all state information.
options := microcluster.Args{StateDir: c.common.FlagMicroCloudDir, Verbose: c.common.FlagLogVerbose, Debug: c.common.FlagLogDebug}
options := microcluster.Args{StateDir: c.common.FlagMicroCloudDir}
m, err := microcluster.App(options)
if err != nil {
return err
Expand Down Expand Up @@ -185,7 +185,7 @@ func (c *cmdClusterMemberRemove) Run(cmd *cobra.Command, args []string) error {
return cmd.Help()
}

options := microcluster.Args{StateDir: c.common.FlagMicroCloudDir, Verbose: c.common.FlagLogVerbose, Debug: c.common.FlagLogDebug}
options := microcluster.Args{StateDir: c.common.FlagMicroCloudDir}
m, err := microcluster.App(options)
if err != nil {
return err
Expand Down Expand Up @@ -219,7 +219,7 @@ func (c *cmdClusterRecover) command() *cobra.Command {
}

func (c *cmdClusterRecover) run(cmd *cobra.Command, args []string) error {
m, err := microcluster.App(microcluster.Args{StateDir: c.common.FlagMicroCloudDir, Verbose: c.common.FlagLogVerbose, Debug: c.common.FlagLogDebug})
m, err := microcluster.App(microcluster.Args{StateDir: c.common.FlagMicroCloudDir})
if err != nil {
return err
}
Expand Down
7 changes: 2 additions & 5 deletions cmd/microcloud/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"

cli "github.com/canonical/lxd/shared/cmd"
"github.com/canonical/lxd/shared/logger"
"github.com/spf13/cobra"

"github.com/canonical/microcloud/microcloud/version"
Expand All @@ -19,8 +20,6 @@ type CmdControl struct {

FlagHelp bool
FlagVersion bool
FlagLogDebug bool
FlagLogVerbose bool
MggMuggins marked this conversation as resolved.
Show resolved Hide resolved
FlagMicroCloudDir string

asker cli.Asker
Expand All @@ -34,7 +33,7 @@ func main() {
}

// common flags.
commonCmd := CmdControl{asker: cli.NewAsker(bufio.NewReader(os.Stdin))}
commonCmd := CmdControl{asker: cli.NewAsker(bufio.NewReader(os.Stdin), logger.Log)}

useTestConsole := os.Getenv("TEST_CONSOLE")
if useTestConsole == "1" {
Expand Down Expand Up @@ -70,8 +69,6 @@ EOF`)
app.PersistentFlags().StringVar(&commonCmd.FlagMicroCloudDir, "state-dir", "", "Path to store MicroCloud state information"+"``")
app.PersistentFlags().BoolVarP(&commonCmd.FlagHelp, "help", "h", false, "Print help")
app.PersistentFlags().BoolVar(&commonCmd.FlagVersion, "version", false, "Print version number")
app.PersistentFlags().BoolVarP(&commonCmd.FlagLogDebug, "debug", "d", false, "Show all debug messages")
app.PersistentFlags().BoolVarP(&commonCmd.FlagLogVerbose, "verbose", "v", false, "Show all information messages")

app.SetVersionTemplate("{{.Version}}\n")

Expand Down
4 changes: 2 additions & 2 deletions cmd/microcloud/main_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"github.com/canonical/lxd/shared/validate"
cephTypes "github.com/canonical/microceph/microceph/api/types"
cephClient "github.com/canonical/microceph/microceph/client"
"github.com/canonical/microcluster/client"
"github.com/canonical/microcluster/v2/client"
ovnClient "github.com/canonical/microovn/microovn/client"
"github.com/spf13/cobra"

Expand Down Expand Up @@ -213,7 +213,7 @@ func (c *initConfig) RunInteractive(cmd *cobra.Command, args []string) error {
return err
}

s, err := service.NewHandler(c.name, c.address, c.common.FlagMicroCloudDir, c.common.FlagLogDebug, c.common.FlagLogVerbose, services...)
s, err := service.NewHandler(c.name, c.address, c.common.FlagMicroCloudDir, services...)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/microcloud/main_init_preseed.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func (c *initConfig) RunPreseed(cmd *cobra.Command) error {

c.name = hostname
c.address = listenIP.String()
s, err := service.NewHandler(c.name, c.address, c.common.FlagMicroCloudDir, c.common.FlagLogDebug, c.common.FlagLogVerbose, services...)
s, err := service.NewHandler(c.name, c.address, c.common.FlagMicroCloudDir, services...)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/microcloud/main_init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func newSystemWithUplinkNetConfig(address string, config map[string]string) Init
}

func newTestHandler(addr string, t *testing.T) *service.Handler {
handler, err := service.NewHandler("testSystem", addr, "/tmp/microcloud_test_hander", true, true)
handler, err := service.NewHandler("testSystem", addr, "/tmp/microcloud_test_hander")
if err != nil {
t.Fatalf("Failed to create test service handler: %s", err)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/microcloud/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package main
import (
"context"

"github.com/canonical/microcluster/microcluster"
"github.com/canonical/microcluster/v2/microcluster"
"github.com/spf13/cobra"

cloudClient "github.com/canonical/microcloud/microcloud/client"
Expand Down Expand Up @@ -33,7 +33,7 @@ func (c *cmdRemove) Run(cmd *cobra.Command, args []string) error {
return cmd.Help()
}

options := microcluster.Args{StateDir: c.common.FlagMicroCloudDir, Verbose: c.common.FlagLogVerbose, Debug: c.common.FlagLogDebug}
options := microcluster.Args{StateDir: c.common.FlagMicroCloudDir}
m, err := microcluster.App(options)
if err != nil {
return err
Expand Down
8 changes: 4 additions & 4 deletions cmd/microcloud/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
"github.com/canonical/lxd/client"
lxdAPI "github.com/canonical/lxd/shared/api"
cli "github.com/canonical/lxd/shared/cmd"
"github.com/canonical/microcluster/client"
"github.com/canonical/microcluster/microcluster"
"github.com/canonical/microcluster/v2/client"
"github.com/canonical/microcluster/v2/microcluster"
"github.com/spf13/cobra"

"github.com/canonical/microcloud/microcloud/api"
Expand Down Expand Up @@ -99,7 +99,7 @@ func (c *cmdServiceList) Run(cmd *cobra.Command, args []string) error {
}

// Instantiate a handler for the services.
s, err := service.NewHandler(status.Name, status.Address.Addr().String(), c.common.FlagMicroCloudDir, c.common.FlagLogDebug, c.common.FlagLogVerbose, services...)
s, err := service.NewHandler(status.Name, status.Address.Addr().String(), c.common.FlagMicroCloudDir, services...)
if err != nil {
return err
}
Expand Down Expand Up @@ -255,7 +255,7 @@ func (c *cmdServiceAdd) Run(cmd *cobra.Command, args []string) error {
}

// Instantiate a handler for the services.
s, err := service.NewHandler(cfg.name, cfg.address, c.common.FlagMicroCloudDir, c.common.FlagLogDebug, c.common.FlagLogVerbose, services...)
s, err := service.NewHandler(cfg.name, cfg.address, c.common.FlagMicroCloudDir, services...)
if err != nil {
return err
}
Expand Down
Loading
Loading