mirror of
https://github.com/cirruslabs/orchard.git
synced 2026-10-22 06:18:16 +02:00
Resources support (#63)
* Resources support * Ability to provide VM and worker resources via the CLI * orchard dev: always listen on :6120 * orchard dev: support --resources * REST API: provide resource defaults when creating VM * OpenAPI: document "resources" field * orchard dev: serve Swagger API documentation on /v1/ * Integration guide
This commit is contained in:
@@ -1,16 +1,21 @@
|
||||
package create
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/cirruslabs/orchard/pkg/client"
|
||||
v1 "github.com/cirruslabs/orchard/pkg/resource/v1"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var ErrVMFailed = errors.New("failed to create VM")
|
||||
|
||||
var image string
|
||||
var cpu uint64
|
||||
var memory uint64
|
||||
var softnet bool
|
||||
var headless bool
|
||||
var stringToStringResources map[string]string
|
||||
|
||||
func newCreateVMCommand() *cobra.Command {
|
||||
command := &cobra.Command{
|
||||
@@ -25,6 +30,8 @@ func newCreateVMCommand() *cobra.Command {
|
||||
command.PersistentFlags().Uint64Var(&memory, "memory", 8*1024, "megabytes of memory to use")
|
||||
command.PersistentFlags().BoolVar(&softnet, "softnet", false, "whether to use Softnet network isolation")
|
||||
command.PersistentFlags().BoolVar(&headless, "headless", true, "whether to run without graphics")
|
||||
command.PersistentFlags().StringToStringVar(&stringToStringResources, "resources", map[string]string{},
|
||||
"resources to request for this VM")
|
||||
|
||||
return command
|
||||
}
|
||||
@@ -32,6 +39,12 @@ func newCreateVMCommand() *cobra.Command {
|
||||
func runCreateVM(cmd *cobra.Command, args []string) error {
|
||||
name := args[0]
|
||||
|
||||
// Convert resources
|
||||
resources, err := v1.NewResourcesFromStringToString(stringToStringResources)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%w: %v", ErrVMFailed, err)
|
||||
}
|
||||
|
||||
client, err := client.New()
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -41,10 +54,11 @@ func runCreateVM(cmd *cobra.Command, args []string) error {
|
||||
Meta: v1.Meta{
|
||||
Name: name,
|
||||
},
|
||||
Image: image,
|
||||
CPU: cpu,
|
||||
Memory: memory,
|
||||
Softnet: softnet,
|
||||
Headless: headless,
|
||||
Image: image,
|
||||
CPU: cpu,
|
||||
Memory: memory,
|
||||
Softnet: softnet,
|
||||
Headless: headless,
|
||||
Resources: resources,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1,10 +1,14 @@
|
||||
package dev
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/cirruslabs/orchard/internal/config"
|
||||
"github.com/cirruslabs/orchard/internal/controller"
|
||||
"github.com/cirruslabs/orchard/internal/netconstants"
|
||||
"github.com/cirruslabs/orchard/internal/worker"
|
||||
"github.com/cirruslabs/orchard/pkg/client"
|
||||
v1 "github.com/cirruslabs/orchard/pkg/resource/v1"
|
||||
"github.com/spf13/cobra"
|
||||
"go.uber.org/zap"
|
||||
"os"
|
||||
@@ -12,7 +16,10 @@ import (
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
var ErrFailed = errors.New("failed to run development controller and worker")
|
||||
|
||||
var devDataDirPath string
|
||||
var stringToStringResources map[string]string
|
||||
|
||||
func NewCommand() *cobra.Command {
|
||||
command := &cobra.Command{
|
||||
@@ -23,6 +30,8 @@ func NewCommand() *cobra.Command {
|
||||
|
||||
command.PersistentFlags().StringVarP(&devDataDirPath, "data-dir", "d", ".dev-data",
|
||||
"path to persist data between runs")
|
||||
command.PersistentFlags().StringToStringVar(&stringToStringResources, "resources", map[string]string{},
|
||||
"resources that the development worker will provide")
|
||||
|
||||
return command
|
||||
}
|
||||
@@ -36,7 +45,14 @@ func runDev(cmd *cobra.Command, args []string) error {
|
||||
devDataDirPath = path.Join(pwd, devDataDirPath)
|
||||
}
|
||||
|
||||
devController, devWorker, err := CreateDevControllerAndWorker(devDataDirPath)
|
||||
// Convert resources
|
||||
resources, err := v1.NewResourcesFromStringToString(stringToStringResources)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%w: %v", ErrFailed, err)
|
||||
}
|
||||
|
||||
devController, devWorker, err := CreateDevControllerAndWorker(devDataDirPath,
|
||||
fmt.Sprintf(":%d", netconstants.DefaultControllerPort), resources)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -59,7 +75,11 @@ func runDev(cmd *cobra.Command, args []string) error {
|
||||
return <-errChan
|
||||
}
|
||||
|
||||
func CreateDevControllerAndWorker(devDataDirPath string) (*controller.Controller, *worker.Worker, error) {
|
||||
func CreateDevControllerAndWorker(
|
||||
devDataDirPath string,
|
||||
controllerListenAddr string,
|
||||
resources v1.Resources,
|
||||
) (*controller.Controller, *worker.Worker, error) {
|
||||
// Initialize the logger
|
||||
logger, err := zap.NewDevelopment()
|
||||
if err != nil {
|
||||
@@ -78,8 +98,9 @@ func CreateDevControllerAndWorker(devDataDirPath string) (*controller.Controller
|
||||
|
||||
devController, err := controller.New(
|
||||
controller.WithDataDir(dataDir),
|
||||
controller.WithListenAddr(":0"),
|
||||
controller.WithListenAddr(controllerListenAddr),
|
||||
controller.WithInsecureAuthDisabled(),
|
||||
controller.WithSwaggerDocs(),
|
||||
controller.WithLogger(logger),
|
||||
)
|
||||
if err != nil {
|
||||
@@ -90,7 +111,7 @@ func CreateDevControllerAndWorker(devDataDirPath string) (*controller.Controller
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
devWorker, err := worker.New(defaultClient, worker.WithLogger(logger))
|
||||
devWorker, err := worker.New(defaultClient, worker.WithResources(resources), worker.WithLogger(logger))
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
@@ -2,20 +2,25 @@ package worker
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/cirruslabs/orchard/internal/bootstraptoken"
|
||||
"github.com/cirruslabs/orchard/internal/netconstants"
|
||||
"github.com/cirruslabs/orchard/internal/worker"
|
||||
"github.com/cirruslabs/orchard/pkg/client"
|
||||
v1 "github.com/cirruslabs/orchard/pkg/resource/v1"
|
||||
"github.com/spf13/cobra"
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zapcore"
|
||||
"gopkg.in/natefinch/lumberjack.v2"
|
||||
)
|
||||
|
||||
var ErrRunFailed = errors.New("failed to run worker")
|
||||
|
||||
var ErrBootstrapTokenNotProvided = errors.New("no bootstrap token provided")
|
||||
|
||||
var bootstrapTokenRaw string
|
||||
var logFilePath string
|
||||
var stringToStringResources map[string]string
|
||||
|
||||
func newRunCommand() *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
@@ -24,10 +29,14 @@ func newRunCommand() *cobra.Command {
|
||||
RunE: runWorker,
|
||||
Args: cobra.ExactArgs(1),
|
||||
}
|
||||
|
||||
cmd.PersistentFlags().StringVar(&bootstrapTokenRaw, "bootstrap-token", "",
|
||||
"a bootstrap token retrieved via `orchard get bootstrap-token <service-account-name-for-workers>`")
|
||||
cmd.PersistentFlags().StringVar(&logFilePath, "log-file", "",
|
||||
"optional path to a file where logs (up to 100 Mb) will be written.")
|
||||
cmd.PersistentFlags().StringToStringVar(&stringToStringResources, "resources", map[string]string{},
|
||||
"resources that this worker provides")
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
||||
@@ -44,6 +53,12 @@ func runWorker(cmd *cobra.Command, args []string) (err error) {
|
||||
return err
|
||||
}
|
||||
|
||||
// Convert resources
|
||||
resources, err := v1.NewResourcesFromStringToString(stringToStringResources)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%w: %v", ErrRunFailed, err)
|
||||
}
|
||||
|
||||
controllerClient, err := client.New(
|
||||
client.WithAddress(controllerURL.String()),
|
||||
client.WithTrustedCertificate(bootstrapToken.Certificate()),
|
||||
@@ -65,7 +80,11 @@ func runWorker(cmd *cobra.Command, args []string) (err error) {
|
||||
}
|
||||
}()
|
||||
|
||||
workerInstance, err := worker.New(controllerClient, worker.WithLogger(logger))
|
||||
workerInstance, err := worker.New(
|
||||
controllerClient,
|
||||
worker.WithResources(resources),
|
||||
worker.WithLogger(logger),
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user