Skip to content

Commit 1786792

Browse files
committed
initial commit
Signed-off-by: Richard Case <richard@weave.works>
1 parent f537ffb commit 1786792

File tree

16 files changed

+1314
-0
lines changed

16 files changed

+1314
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,5 @@
1313

1414
# Dependency directories (remove the comment below to include it)
1515
# vendor/
16+
17+
.vscode/

cmd/fl/main.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"log"
6+
7+
"github.com/spf13/cobra"
8+
"github.com/spf13/viper"
9+
10+
"github.com/weaveworks-experiments/fl/internal/cmd"
11+
)
12+
13+
func main() {
14+
ctx := context.Background()
15+
16+
cobra.OnInitialize(initConfig)
17+
18+
rootCmd := cmd.NewRootCmd()
19+
if err := rootCmd.ExecuteContext(ctx); err != nil {
20+
log.Fatal("failed executing root command")
21+
}
22+
}
23+
24+
func initConfig() {
25+
viper.SetEnvPrefix("FL")
26+
viper.AutomaticEnv()
27+
}

go.mod

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
module github.com/weaveworks-experiments/fl
2+
3+
go 1.17
4+
5+
require (
6+
github.com/moby/moby v20.10.14+incompatible
7+
github.com/spf13/cobra v1.4.0
8+
github.com/spf13/pflag v1.0.5
9+
github.com/spf13/viper v1.10.1
10+
github.com/weaveworks/flintlock/api v0.0.0-20220304105853-8fcb8aa2bafb
11+
github.com/yitsushi/macpot v1.0.2
12+
go.uber.org/zap v1.21.0
13+
google.golang.org/grpc v1.44.0
14+
gopkg.in/yaml.v2 v2.4.0
15+
)
16+
17+
require (
18+
github.com/fsnotify/fsnotify v1.5.1 // indirect
19+
github.com/golang/protobuf v1.5.2 // indirect
20+
github.com/grpc-ecosystem/grpc-gateway/v2 v2.6.0 // indirect
21+
github.com/hashicorp/hcl v1.0.0 // indirect
22+
github.com/inconshreveable/mousetrap v1.0.0 // indirect
23+
github.com/magiconair/properties v1.8.6 // indirect
24+
github.com/mitchellh/mapstructure v1.4.3 // indirect
25+
github.com/pelletier/go-toml v1.9.4 // indirect
26+
github.com/spf13/afero v1.8.2 // indirect
27+
github.com/spf13/cast v1.4.1 // indirect
28+
github.com/spf13/jwalterweatherman v1.1.0 // indirect
29+
github.com/subosito/gotenv v1.2.0 // indirect
30+
github.com/weaveworks/flintlock/client v0.0.0-20220304105853-8fcb8aa2bafb // indirect
31+
go.uber.org/atomic v1.9.0 // indirect
32+
go.uber.org/multierr v1.8.0 // indirect
33+
golang.org/x/net v0.0.0-20211209124913-491a49abca63 // indirect
34+
golang.org/x/sys v0.0.0-20220405210540-1e041c57c461 // indirect
35+
golang.org/x/text v0.3.7 // indirect
36+
google.golang.org/genproto v0.0.0-20211208223120-3a66f561d7aa // indirect
37+
google.golang.org/protobuf v1.28.0 // indirect
38+
gopkg.in/ini.v1 v1.66.4 // indirect
39+
)

go.sum

Lines changed: 555 additions & 0 deletions
Large diffs are not rendered by default.

internal/cmd/microvm/create.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package microvm
2+
3+
import (
4+
"github.com/moby/moby/pkg/namesgenerator"
5+
"github.com/spf13/cobra"
6+
"go.uber.org/zap"
7+
8+
"github.com/weaveworks-experiments/fl/pkg/app"
9+
"github.com/weaveworks-experiments/fl/pkg/flags"
10+
)
11+
12+
const (
13+
defaultNamespace = "default"
14+
defaultVCPU = 2
15+
defaultMemoryMb = 2048
16+
defaultKernelImage = "ghcr.io/weaveworks/flintlock-kernel:5.10.77"
17+
defaultKernelFile = "boot/vmlinux"
18+
defaultRootImage = "ghcr.io/weaveworks/capmvm-kubernetes:1.21.8"
19+
)
20+
21+
func newCreateCommand() *cobra.Command {
22+
createInput := &app.CreateInput{}
23+
24+
cmd := &cobra.Command{
25+
Use: "create",
26+
Short: "create a new microvm",
27+
PreRun: func(cmd *cobra.Command, args []string) {
28+
flags.BindFlags(cmd)
29+
},
30+
Run: func(c *cobra.Command, _ []string) {
31+
a := app.New(zap.S().With("action", "create"))
32+
if err := a.Create(c.Context(), createInput); err != nil {
33+
zap.S().Errorw("failed creating microvm", "error", err)
34+
}
35+
},
36+
}
37+
38+
cmd.Flags().StringVar(&createInput.Host, "host", "", "the flintlock host to create the microvm on")
39+
cmd.MarkFlagRequired("host")
40+
41+
defaultName := namesgenerator.GetRandomName(10)
42+
cmd.Flags().StringVar(&createInput.Name, "name", defaultName, "the name of the microvm, auto-generated if not supplied")
43+
cmd.Flags().StringVar(&createInput.Namespace, "namespace", defaultNamespace, "the namespace for the microvm")
44+
cmd.Flags().IntVar(&createInput.VCPU, "vcpu", defaultVCPU, "the number of vcpus")
45+
cmd.Flags().IntVar(&createInput.MemoryInMb, "memory", defaultMemoryMb, "the memory in mb")
46+
cmd.Flags().StringVar(&createInput.KernelImage, "kernel-image", defaultKernelImage, "the image to use for the kernel")
47+
cmd.Flags().BoolVar(&createInput.KernelAddNetConf, "add-netconf", true, "automatically add network configuration to the kernel cmd line")
48+
cmd.Flags().StringVar(&createInput.KernelFileName, "kernel-filename", defaultKernelFile, "name of the kernel file in the image")
49+
cmd.Flags().StringVar(&createInput.RootImage, "root-image", defaultRootImage, "the image to use for the root volume")
50+
cmd.Flags().StringVar(&createInput.InitrdImage, "initrd-image", "", "the image to use for the initial ramdisk")
51+
cmd.Flags().StringVar(&createInput.InitrdFilename, "initrd-filename", "", "name of the file in the image to use for the initial ramdisk")
52+
cmd.Flags().StringSliceVar(&createInput.NetworkInterfaces, "network-interface", nil, "specify the network interfaces to attach. In the following format: name:type:[macaddress]:[ipaddress]")
53+
cmd.Flags().StringSliceVar(&createInput.MetadataFromFile, "metadata-from-file", nil, "specify metadata to be available to your microvm. In the following format key=pathtofile")
54+
cmd.Flags().StringVar(&createInput.Hostname, "hostname", "", "the hostname of the the microvm")
55+
cmd.Flags().StringVar(&createInput.SSHKeyFile, "ssh-key-file", "", "an ssh key to use")
56+
//TODO: additional command line args for kernel
57+
//TODO: add additional volumes
58+
59+
return cmd
60+
}

internal/cmd/microvm/delete.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package microvm
2+
3+
import (
4+
"github.com/spf13/cobra"
5+
"go.uber.org/zap"
6+
7+
"github.com/weaveworks-experiments/fl/pkg/app"
8+
"github.com/weaveworks-experiments/fl/pkg/flags"
9+
)
10+
11+
const (
12+
deleteExamples = `
13+
# Delete a microvm
14+
fl microvm delete --host host1:9090 01FZZJV1XD2FKH2KY0NDB4MBRQ
15+
`
16+
)
17+
18+
func newDeleteCommand() *cobra.Command {
19+
deleteInput := &app.DeleteInput{}
20+
21+
cmd := &cobra.Command{
22+
Use: "delete",
23+
Short: "delete a microvm from a host",
24+
Example: deleteExamples,
25+
Args: cobra.ExactValidArgs(1),
26+
PreRun: func(cmd *cobra.Command, args []string) {
27+
flags.BindFlags(cmd)
28+
},
29+
Run: func(c *cobra.Command, args []string) {
30+
deleteInput.UID = args[0]
31+
32+
a := app.New(zap.S().With("action", "delete"))
33+
err := a.Delete(c.Context(), deleteInput)
34+
if err != nil {
35+
zap.S().Errorw("failed deleting microvm", "error", err)
36+
return
37+
}
38+
},
39+
}
40+
41+
cmd.Flags().StringVar(&deleteInput.Host, "host", "", "the flintlock host to get the microvms from")
42+
cmd.MarkFlagRequired("host")
43+
44+
return cmd
45+
}

internal/cmd/microvm/get.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package microvm
2+
3+
import (
4+
"github.com/spf13/cobra"
5+
"go.uber.org/zap"
6+
7+
"github.com/weaveworks-experiments/fl/pkg/app"
8+
"github.com/weaveworks-experiments/fl/pkg/flags"
9+
)
10+
11+
const (
12+
examples = `
13+
# Get all microvms from a host
14+
fl microvm get --host host1:9090
15+
16+
# Get a microvm with a specific id
17+
fl microvm get --host host1:9090 01FZZJV1XD2FKH2KY0NDB4MBRQ
18+
`
19+
)
20+
21+
func newGetCommand() *cobra.Command {
22+
getInput := &app.GetInput{}
23+
24+
cmd := &cobra.Command{
25+
Use: "get",
26+
Short: "get details of a microvm(s) from a host",
27+
Example: examples,
28+
Args: cobra.MaximumNArgs(1),
29+
PreRun: func(cmd *cobra.Command, args []string) {
30+
flags.BindFlags(cmd)
31+
},
32+
Run: func(c *cobra.Command, args []string) {
33+
if len(args) > 0 {
34+
getInput.UID = args[0]
35+
}
36+
37+
a := app.New(zap.S().With("action", "get"))
38+
err := a.Get(c.Context(), getInput)
39+
if err != nil {
40+
zap.S().Errorw("failed getting microvm(s)", "error", err)
41+
return
42+
}
43+
},
44+
}
45+
46+
cmd.Flags().StringVar(&getInput.Host, "host", "", "the flintlock host to get the microvms from")
47+
cmd.Flags().StringVar(&getInput.Namespace, "namespace", defaultNamespace, "the namespace to get the microvms from")
48+
49+
cmd.MarkFlagRequired("host")
50+
51+
return cmd
52+
}

internal/cmd/microvm/microvm.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package microvm
2+
3+
import (
4+
"github.com/spf13/cobra"
5+
"go.uber.org/zap"
6+
)
7+
8+
func NewCommand() *cobra.Command {
9+
cmd := &cobra.Command{
10+
Use: "microvm",
11+
Short: "perform microvm operations",
12+
Run: func(c *cobra.Command, _ []string) {
13+
if err := c.Help(); err != nil {
14+
zap.S().Debugw("ingoring cobra error",
15+
"error",
16+
err.Error())
17+
}
18+
},
19+
}
20+
21+
createCmd := newCreateCommand()
22+
cmd.AddCommand(createCmd)
23+
24+
getCmd := newGetCommand()
25+
cmd.AddCommand(getCmd)
26+
27+
deleteCmd := newDeleteCommand()
28+
cmd.AddCommand(deleteCmd)
29+
30+
return cmd
31+
}

internal/cmd/root.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package cmd
2+
3+
import (
4+
"github.com/spf13/cobra"
5+
"go.uber.org/zap"
6+
7+
"github.com/weaveworks-experiments/fl/internal/cmd/microvm"
8+
"github.com/weaveworks-experiments/fl/pkg/logging"
9+
)
10+
11+
const (
12+
logLevelFlag = "log-level"
13+
)
14+
15+
func NewRootCmd() *cobra.Command {
16+
cmd := &cobra.Command{
17+
Use: "fl",
18+
Short: "The experimental cli for flintlock",
19+
PersistentPreRunE: func(cmd *cobra.Command, _ []string) error {
20+
logLevelFlag, _ := cmd.Flags().GetString(logLevelFlag)
21+
err := logging.Configure(logLevelFlag)
22+
if err != nil {
23+
return err
24+
}
25+
26+
return nil
27+
},
28+
Run: func(c *cobra.Command, _ []string) {
29+
if err := c.Help(); err != nil {
30+
zap.S().Debugw("ingoring cobra error",
31+
"error",
32+
err.Error())
33+
}
34+
},
35+
}
36+
37+
cmd.PersistentFlags().String(logLevelFlag, "debug", "set the level of the logger")
38+
39+
versionCmd := newVersionCommand()
40+
cmd.AddCommand(versionCmd)
41+
42+
microvmCmd := microvm.NewCommand()
43+
cmd.AddCommand(microvmCmd)
44+
45+
return cmd
46+
}

internal/cmd/version.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/spf13/cobra"
7+
)
8+
9+
func newVersionCommand() *cobra.Command {
10+
return &cobra.Command{
11+
Use: "version",
12+
Short: "display version information",
13+
Run: func(c *cobra.Command, _ []string) {
14+
fmt.Println("to do, add version information")
15+
},
16+
}
17+
18+
}

0 commit comments

Comments
 (0)