Skip to content

Commit

Permalink
microcloud/service: Update package usages
Browse files Browse the repository at this point in the history
Signed-off-by: Max Asnaashari <[email protected]>
  • Loading branch information
masnax committed Dec 16, 2024
1 parent 8513478 commit 7227a83
Show file tree
Hide file tree
Showing 15 changed files with 44 additions and 33 deletions.
2 changes: 1 addition & 1 deletion microcloud/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
2 changes: 1 addition & 1 deletion microcloud/cmd/microcloud/ask.go
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ func askRemotePool(systems map[string]InitSystem, autoSetup bool, wipeAllDisks b
system.MicroCephDisks = []cephTypes.DisksPost{}
}

system.MicroCephDisks = append(system.MicroCephDisks, cephTypes.DisksPost{Path: path, Wipe: wipeMap[entry]})
system.MicroCephDisks = append(system.MicroCephDisks, cephTypes.DisksPost{Path: []string{path}, Wipe: wipeMap[entry]})

systems[target] = system
}
Expand Down
3 changes: 2 additions & 1 deletion microcloud/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 Down Expand Up @@ -34,7 +35,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
9 changes: 8 additions & 1 deletion microcloud/cmd/microcloud/main_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"errors"
"fmt"
"net"
"os"
Expand Down Expand Up @@ -445,10 +446,16 @@ func setupCluster(s *service.Handler, systems map[string]InitSystem) error {
}

logger.Debug("Adding disk to MicroCeph", logger.Ctx{"peer": s.Name, "disk": disk.Path})
err = cephClient.AddDisk(context.Background(), c, &disk)
resp, err := cephClient.AddDisk(context.Background(), c, &disk)
if err != nil {
return err
}

for _, r := range resp.Reports {
if r.Error != "" {
return errors.New(r.Error)
}
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions microcloud/cmd/microcloud/main_init_preseed.go
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ func (p *Preseed) Parse(s *service.Handler, bootstrap bool) (map[string]InitSyst
}

for _, disk := range directCeph {
system.MicroCephDisks = append(system.MicroCephDisks, cephTypes.DisksPost{Path: disk.Path, Wipe: disk.Wipe})
system.MicroCephDisks = append(system.MicroCephDisks, cephTypes.DisksPost{Path: []string{disk.Path}, Wipe: disk.Wipe})
}

// Setup ceph pool for disks specified to MicroCeph.
Expand Down Expand Up @@ -541,7 +541,7 @@ func (p *Preseed) Parse(s *service.Handler, bootstrap bool) (map[string]InitSyst
}

for _, disk := range matched {
system.MicroCephDisks = append(system.MicroCephDisks, cephTypes.DisksPost{Path: parseDiskPath(disk), Wipe: filter.Wipe})
system.MicroCephDisks = append(system.MicroCephDisks, cephTypes.DisksPost{Path: []string{parseDiskPath(disk)}, Wipe: filter.Wipe})
// There should only be one ceph pool per system.
if !addedCephPool {
if bootstrap {
Expand Down
3 changes: 2 additions & 1 deletion microcloud/cmd/microcloud/test_console.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/AlecAivazis/survey/v2/terminal"
"github.com/Netflix/go-expect"
cli "github.com/canonical/lxd/shared/cmd"
"github.com/canonical/lxd/shared/logger"
"github.com/creack/pty"
"github.com/hinshun/vt10x"
)
Expand All @@ -39,7 +40,7 @@ func prepareTestAsker(r io.Reader) cli.Asker {

reader = bufio.NewReader(bytes.NewReader(b.Bytes()))

return cli.NewAsker(reader)
return cli.NewAsker(reader, logger.Log)
}

// NewTestConsole creates a new testConsole, with an underlying expect.Console and virtual terminal.
Expand Down
5 changes: 0 additions & 5 deletions microcloud/cmd/microcloudd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main

import (
"fmt"
"math/rand"
"os"
"time"

Expand Down Expand Up @@ -140,10 +139,6 @@ func (c *cmdDaemon) Run(cmd *cobra.Command, args []string) error {
return s.Services[types.MicroCloud].(*service.CloudService).StartCloud(s, endpoints)
}

func init() {
rand.Seed(time.Now().UnixNano())
}

func main() {
// Only root should run this
if os.Geteuid() != 0 {
Expand Down
4 changes: 2 additions & 2 deletions microcloud/mdns/lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ func (f forwardingWriter) Write(p []byte) (int, error) {

if strings.Contains(logMsg, "[INFO]") {
_, after, _ := strings.Cut(logMsg, "[INFO]")
logger.Infof(after)
logger.Info(after)
} else if strings.Contains(logMsg, "[ERR]") {
_, after, _ := strings.Cut(logMsg, "[ERR]")
logger.Errorf(after)
logger.Error(after)
} else {
return 0, fmt.Errorf("Invalid log %q", logMsg)
}
Expand Down
4 changes: 2 additions & 2 deletions microcloud/mdns/mdns.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ const ServiceSize = 10
const clusterSize = 1000

// NewBroadcast returns a running mdns.Server which broadcasts the service at the given name and address.
func NewBroadcast(name string, addr string, port int, service string, txt []byte) (*mdns.Server, error) {
func NewBroadcast(name string, addr string, port int64, service string, txt []byte) (*mdns.Server, error) {
var sendTXT []string
if txt != nil {
sendTXT = dnsTXTSlice(txt)
}

config, err := mdns.NewMDNSService(name, service, "", "", port, []net.IP{net.ParseIP(addr)}, sendTXT)
config, err := mdns.NewMDNSService(name, service, "", "", int(port), []net.IP{net.ParseIP(addr)}, sendTXT)
if err != nil {
return nil, fmt.Errorf("Failed to create configuration for broadcast: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion microcloud/service/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ type Service interface {
Type() types.ServiceType
Name() string
Address() string
Port() int
Port() int64
}
6 changes: 3 additions & 3 deletions microcloud/service/lxd.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type LXDService struct {

name string
address string
port int
port int64
}

// NewLXDService creates a new LXD service with a client attached.
Expand Down Expand Up @@ -69,7 +69,7 @@ func (s LXDService) Client(ctx context.Context, secret string) (lxd.InstanceServ
}

// remoteClient returns an https client for the given address:port.
func (s LXDService) remoteClient(secret string, address string, port int) (lxd.InstanceServer, error) {
func (s LXDService) remoteClient(secret string, address string, port int64) (lxd.InstanceServer, error) {
c, err := s.m.RemoteClient(util.CanonicalNetworkAddress(address, port))
if err != nil {
return nil, err
Expand Down Expand Up @@ -257,7 +257,7 @@ func (s LXDService) Address() string {
}

// Port returns the port of this Service instance.
func (s LXDService) Port() int {
func (s LXDService) Port() int64 {
return s.port
}

Expand Down
13 changes: 10 additions & 3 deletions microcloud/service/microceph.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package service

import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
Expand All @@ -23,7 +24,7 @@ type CephService struct {

name string
address string
port int
port int64
}

// NewCephService creates a new MicroCeph service with a client attached.
Expand Down Expand Up @@ -121,10 +122,16 @@ func (s CephService) Join(ctx context.Context, joinConfig JoinConfig) error {
}

for _, disk := range joinConfig.CephConfig {
err := cephClient.AddDisk(ctx, c, &disk)
resp, err := cephClient.AddDisk(ctx, c, &disk)
if err != nil {
return err
}

for _, r := range resp.Reports {
if r.Error != "" {
return errors.New(r.Error)
}
}
}

return nil
Expand Down Expand Up @@ -166,6 +173,6 @@ func (s CephService) Address() string {
}

// Port returns the port of this Service instance.
func (s CephService) Port() int {
func (s CephService) Port() int64 {
return s.port
}
6 changes: 3 additions & 3 deletions microcloud/service/microcloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type CloudService struct {

name string
address string
port int
port int64
}

// JoinConfig represents configuration for cluster joining.
Expand All @@ -40,7 +40,7 @@ type JoinConfig struct {

// NewCloudService creates a new MicroCloud service with a client attached.
func NewCloudService(ctx context.Context, name string, addr string, dir string, verbose bool, debug bool) (*CloudService, error) {
client, err := microcluster.App(ctx, microcluster.Args{StateDir: dir, ListenPort: strconv.Itoa(CloudPort), Debug: debug, Verbose: verbose})
client, err := microcluster.App(ctx, microcluster.Args{StateDir: dir, ListenPort: strconv.FormatInt(CloudPort, 10), Debug: debug, Verbose: verbose})
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -159,6 +159,6 @@ func (s CloudService) Address() string {
}

// Port returns the port of this Service instance.
func (s CloudService) Port() int {
func (s CloudService) Port() int64 {
return s.port
}
4 changes: 2 additions & 2 deletions microcloud/service/microovn.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type OVNService struct {

name string
address string
port int
port int64
}

// NewOVNService creates a new MicroOVN service with a client attached.
Expand Down Expand Up @@ -127,6 +127,6 @@ func (s OVNService) Address() string {
}

// Port returns the port of this Service instance.
func (s OVNService) Port() int {
func (s OVNService) Port() int64 {
return s.port
}
10 changes: 5 additions & 5 deletions microcloud/service/service_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ import (

const (
// OVNPort is the efault MicroOVN port.
OVNPort int = 6443
OVNPort int64 = 6443

// CephPort is the efault MicroCeph port.
CephPort int = 7443
CephPort int64 = 7443

// LXDPort is the efault LXD port.
LXDPort int = 8443
LXDPort int64 = 8443

// CloudPort is the efault MicroCloud port.
CloudPort int = 9443
CloudPort int64 = 9443
)

// Handler holds a set of services and an mdns server for communication between them.
Expand All @@ -38,7 +38,7 @@ type Handler struct {
Services map[types.ServiceType]Service
Name string
Address string
Port int
Port int64

AuthSecret string
}
Expand Down

0 comments on commit 7227a83

Please sign in to comment.