Skip to content

Commit

Permalink
chore: refact the machine and fs
Browse files Browse the repository at this point in the history
  • Loading branch information
hsinhoyeh committed Feb 22, 2022
1 parent fe10fcc commit 89d655c
Show file tree
Hide file tree
Showing 26 changed files with 590 additions and 271 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
vendor
.vagrant
70 changes: 57 additions & 13 deletions cmd/multikind/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package multikind

import (
goflag "flag"
"fmt"
"os"
"path/filepath"

"github.com/footprintai/multikind/pkg/runtime"
vagrantmachine "github.com/footprintai/multikind/pkg/machine/vagrant"
"github.com/footprintai/multikind/pkg/version"
log "github.com/golang/glog"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -88,17 +89,16 @@ func newRunCmd() (*runCmd, error) {
// CPUs: cpus,
// Memory: memoryInG * 1024, // in M egabytes
//}
vag := runtime.NewVagrantMachines(vagrantRootDir, verbose)
vag := vagrantmachine.NewVagrantMachines(vagrantRootDir, verbose)
return &runCmd{vag: vag}, nil
}

type runCmd struct {
vag *runtime.VagrantMachines
vag *vagrantmachine.VagrantMachines
}

func (r *runCmd) Add(name string, cpus, memoryInG int) error {
m := r.vag.NewMachine(name)
m.AddConfig(&runtime.VagrantMachineConfig{
m := r.vag.NewMachine(name, &vagrantmachine.VagrantMachineConfig{
CPUs: cpus,
Memory: memoryInG * 1024, // in M egabytes
})
Expand All @@ -113,7 +113,7 @@ func (r *runCmd) Export(name string, path string) error {
if path == "" {
path = filepath.Join(vagrantRootDir, name, "kubeconfig")
}
m := r.vag.NewMachine(name)
m := r.vag.NewMachine(name, nil)
if err := m.ExportKubeConfig(path, forceOverwrite); err != nil {
log.Errorf("runcmd: export vagrant node (%s) failed, err:%+v\n", name, err)
return err
Expand All @@ -122,27 +122,71 @@ func (r *runCmd) Export(name string, path string) error {
}

func (r *runCmd) Delete(name string) error {
m := r.vag.NewMachine(name)
m := r.vag.NewMachine(name, nil)
if err := m.Destroy(forceDelete); err != nil {
log.Errorf("runcmd: delete vagrant node (%s) failed, err:%+v\n", name, err)
return err
}
return nil
}

var dummyRow = &runtime.OutputVagrantMachine{}
// OutputMachineInfo defines the output format returned for each Machine
type OutputMachineInfo struct {
Name string `json:"name"`
MachineDir string `json:"dir"`
Status string `json:"status"`
Cpus string `json:"cpus"`
Memory string `json:"memory"`
}

func (o *OutputMachineInfo) Headers() []string {
return []string{
"name",
"dir",
"status",
"cpus",
"memory",
}
}

func (o *OutputMachineInfo) Values() []string {
return []string{
o.Name,
o.MachineDir,
o.Status,
o.Cpus,
o.Memory,
}
}

var dummyRow = &OutputMachineInfo{}

func (r *runCmd) List() error {
w := NewFormatWriter(os.Stdout, Table)
var listvalues [][]string
respList, err := r.vag.ListMachines()
machineList, err := r.vag.ListMachines()
if err != nil {
return err
}
for _, resp := range respList {
listvalues = append(listvalues, resp.Values())
machineNamesMap := map[string]*OutputMachineInfo{}
for _, m := range machineList {
info, err := m.Info()
if err != nil {
return err
}
machineNamesMap[m.Name()] = &OutputMachineInfo{
Name: m.Name(),
MachineDir: m.HostDir(),
Status: info.Status,
Cpus: fmt.Sprintf("%d", info.CpuInfo.NumCPUs()),
Memory: fmt.Sprintf("%d/%d", info.MemInfo.Free(), info.MemInfo.Total()),
}
}

var csvValues [][]string
for _, v := range machineNamesMap {
csvValues = append(csvValues, v.Values())
}
return w.WriteAndClose(dummyRow.Headers(), listvalues)
return w.WriteAndClose(dummyRow.Headers(), csvValues)
}

func Main() {
Expand Down
2 changes: 1 addition & 1 deletion hack/Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Vagrant.configure("2") do |config|

# Every Vagrant development environment requires a box. You can search for
# boxes at https://vagrantcloud.com/search.
config.vm.box = "ubuntu/focal64"
config.vm.box = "generic/ubuntu2004"
config.vm.provision "file", source: "kind-config.yaml", destination: "/tmp/kind-config.yaml"
config.vm.provision :shell, path: "bootstrap/bootstrap.sh"
config.vm.provision :shell, path: "bootstrap/provision-cluster.sh"
Expand Down
58 changes: 51 additions & 7 deletions pkg/runtime/vagrant_cli.go → pkg/client/vagrant/vagrant_cli.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
package runtime
package vagrantclient

import (
"fmt"
"io/ioutil"

govagrant "github.com/bmatcuk/go-vagrant"
fssh "github.com/footprintai/multikind/pkg/ssh"
log "github.com/golang/glog"
"golang.org/x/crypto/ssh"
)

func (v *VagrantMachine) NewVagrantCli() (*VagrantCli, error) {
cli, err := govagrant.NewVagrantClient(v.vagrantMachineDir)
func NewVagrantCli(machineName string, vagrantMachineDir string, verbose bool) (*VagrantCli, error) {
cli, err := govagrant.NewVagrantClient(vagrantMachineDir)
if err != nil {
return nil, err
}
return &VagrantCli{
name: v.name,
name: machineName,
client: cli,
Verbose: v.verbose,
Verbose: verbose,
}, nil
}

Expand Down Expand Up @@ -94,6 +99,45 @@ func (v *VagrantCli) SSHConfig() (SSHConfigFile, error) {
return SSHConfigFile{cmd.SSHConfigResponse.Configs[v.name]}, nil
}

type SSHConfigFile struct {
govagrant.SSHConfig
}

func (s SSHConfigFile) Addr() string {
return fmt.Sprintf("%s:%d", s.SSHConfig.HostName, s.SSHConfig.Port)
}

func (s SSHConfigFile) PrivateKeySigner() (ssh.Signer, error) {
key, err := ioutil.ReadFile(s.IdentityFile)
if err != nil {
return nil, err
}

// Create the Signer for this private key.
signer, err := ssh.ParsePrivateKey(key)
if err != nil {
return nil, err
}
return signer, nil
}

func (s SSHConfigFile) SSHClientConfig() (*ssh.ClientConfig, error) {
signer, err := s.PrivateKeySigner()
if err != nil {
return nil, err
}
config := &ssh.ClientConfig{
User: s.User,
Auth: []ssh.AuthMethod{
// Use the PublicKeys method for remote authentication.
ssh.PublicKeys(signer),
},
//HostKeyCallback: ssh.FixedHostKey(hostKey),
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
return config, nil
}

func (v *VagrantCli) Scp(fromRemotePath string, toHostPath string) error {
sshconfg, err := v.SSHConfig()
if err != nil {
Expand All @@ -104,7 +148,7 @@ func (v *VagrantCli) Scp(fromRemotePath string, toHostPath string) error {
return err
}

conn, err := NewSSHConn(sshconfg.Addr(), clientconfig)
conn, err := fssh.NewSSHConn(sshconfg.Addr(), clientconfig)
if err != nil {
return err
}
Expand All @@ -122,7 +166,7 @@ func (v *VagrantCli) SshExec(command string) (string, error) {
return "", err
}

conn, err := NewSSHConn(sshconfg.Addr(), clientconfig)
conn, err := fssh.NewSSHConn(sshconfg.Addr(), clientconfig)
if err != nil {
return "", err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/runtime/cpuinfo.go → pkg/machine/cpuinfo.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package runtime
package machine

import (
"regexp"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package runtime
package machine

import (
"testing"
Expand Down
6 changes: 3 additions & 3 deletions pkg/runtime/freeport.go → pkg/machine/freeport.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package runtime
package machine

import (
"errors"
Expand All @@ -8,7 +8,7 @@ import (
log "github.com/golang/glog"
)

func findFreeSSHPort() (int, error) {
func FindFreeSSHPort() (int, error) {
start := 2022
nextIncr := 100
for {
Expand All @@ -24,7 +24,7 @@ func findFreeSSHPort() (int, error) {
}
}

func findFreeKubeApiPort() (int, error) {
func FindFreeKubeApiPort() (int, error) {
start := 16443
nextIncr := 1000
for {
Expand Down
17 changes: 17 additions & 0 deletions pkg/machine/freeport_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package machine

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestFreePorts(t *testing.T) {
sshport, err := FindFreeSSHPort()
assert.NoError(t, err)
assert.True(t, sshport >= 2022)

kubeport, err := FindFreeKubeApiPort()
assert.NoError(t, err)
assert.True(t, kubeport >= 16443)
}
Loading

0 comments on commit 89d655c

Please sign in to comment.