Always require a client for running a worker (#52)
* Always require a client for running a worker * Actually validate roles * Delete worker Fixes #46 * Update internal/worker/worker.go Co-authored-by: Nikolay Edigaryev <edigaryev@gmail.com> --------- Co-authored-by: Nikolay Edigaryev <edigaryev@gmail.com>
This commit is contained in:
parent
af074f499d
commit
362ea85b4f
|
|
@ -10,7 +10,7 @@ func NewCommand() *cobra.Command {
|
|||
Short: "Delete resources from the controller",
|
||||
}
|
||||
|
||||
command.AddCommand(newDeleteVMCommand(), newDeleteServiceComandCommand())
|
||||
command.AddCommand(newDeleteVMCommand(), newDeleteServiceComandCommand(), newDeleteWorkerCommand())
|
||||
|
||||
return command
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,26 @@
|
|||
package deletecmd
|
||||
|
||||
import (
|
||||
"github.com/cirruslabs/orchard/pkg/client"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func newDeleteWorkerCommand() *cobra.Command {
|
||||
return &cobra.Command{
|
||||
Use: "worker NAME",
|
||||
Short: "Delete a worker",
|
||||
Args: cobra.ExactArgs(1),
|
||||
RunE: runDeleteWorker,
|
||||
}
|
||||
}
|
||||
|
||||
func runDeleteWorker(cmd *cobra.Command, args []string) error {
|
||||
name := args[0]
|
||||
|
||||
client, err := client.New()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return client.Workers().Delete(cmd.Context(), name)
|
||||
}
|
||||
|
|
@ -3,6 +3,7 @@ package dev
|
|||
import (
|
||||
"github.com/cirruslabs/orchard/internal/controller"
|
||||
"github.com/cirruslabs/orchard/internal/worker"
|
||||
"github.com/cirruslabs/orchard/pkg/client"
|
||||
"github.com/spf13/cobra"
|
||||
"go.uber.org/zap"
|
||||
"os"
|
||||
|
|
@ -80,7 +81,11 @@ func CreateDevControllerAndWorker(devDataDirPath string) (*controller.Controller
|
|||
return nil, nil, err
|
||||
}
|
||||
|
||||
devWorker, err := worker.New(worker.WithDataDirPath(devDataDirPath), worker.WithLogger(logger))
|
||||
defaultClient, err := client.New()
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
devWorker, err := worker.New(defaultClient, worker.WithLogger(logger))
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -59,7 +59,7 @@ func runWorker(cmd *cobra.Command, args []string) (err error) {
|
|||
}
|
||||
}()
|
||||
|
||||
workerInstance, err := worker.New(worker.WithClient(controllerClient), worker.WithLogger(logger))
|
||||
workerInstance, err := worker.New(controllerClient, worker.WithLogger(logger))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -26,6 +26,14 @@ func (controller *Controller) createServiceAccount(ctx *gin.Context) responder.R
|
|||
return responder.Code(http.StatusPreconditionFailed)
|
||||
}
|
||||
|
||||
// validate roles
|
||||
for _, role := range serviceAccount.Roles {
|
||||
_, err := v1.NewServiceAccountRole(string(role))
|
||||
if err != nil {
|
||||
return responder.Code(http.StatusPreconditionFailed)
|
||||
}
|
||||
}
|
||||
|
||||
if serviceAccount.Token == "" {
|
||||
serviceAccount.Token = uuid.New().String()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,26 +1,13 @@
|
|||
package worker
|
||||
|
||||
import (
|
||||
"github.com/cirruslabs/orchard/pkg/client"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type Option func(*Worker)
|
||||
|
||||
func WithDataDirPath(dataDir string) Option {
|
||||
return func(worker *Worker) {
|
||||
worker.dataDirPath = dataDir
|
||||
}
|
||||
}
|
||||
|
||||
func WithLogger(logger *zap.Logger) Option {
|
||||
return func(worker *Worker) {
|
||||
worker.logger = logger.Sugar()
|
||||
}
|
||||
}
|
||||
|
||||
func WithClient(client *client.Client) Option {
|
||||
return func(worker *Worker) {
|
||||
worker.client = client
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,18 +20,19 @@ import (
|
|||
const pollInterval = 5 * time.Second
|
||||
|
||||
var ErrPollFailed = errors.New("failed to poll controller")
|
||||
var ErrRegistrationFailed = errors.New("failed to register worker on the controller")
|
||||
|
||||
type Worker struct {
|
||||
dataDirPath string
|
||||
name string
|
||||
vmm *vmmanager.VMManager
|
||||
client *client.Client
|
||||
logger *zap.SugaredLogger
|
||||
name string
|
||||
vmm *vmmanager.VMManager
|
||||
client *client.Client
|
||||
logger *zap.SugaredLogger
|
||||
}
|
||||
|
||||
func New(opts ...Option) (*Worker, error) {
|
||||
func New(client *client.Client, opts ...Option) (*Worker, error) {
|
||||
worker := &Worker{
|
||||
vmm: vmmanager.New(),
|
||||
client: client,
|
||||
vmm: vmmanager.New(),
|
||||
}
|
||||
|
||||
// Apply options
|
||||
|
|
@ -52,13 +53,6 @@ func New(opts ...Option) (*Worker, error) {
|
|||
worker.logger = zap.NewNop().Sugar()
|
||||
}
|
||||
|
||||
// Instantiate worker
|
||||
client, err := client.New()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
worker.client = client
|
||||
|
||||
return worker, nil
|
||||
}
|
||||
|
||||
|
|
@ -77,7 +71,7 @@ func (worker *Worker) runNewSession(ctx context.Context) error {
|
|||
if err := worker.registerWorker(subCtx); err != nil {
|
||||
worker.logger.Warnf("failed to register worker: %v", err)
|
||||
|
||||
return nil
|
||||
return ErrRegistrationFailed
|
||||
}
|
||||
|
||||
go func() {
|
||||
|
|
|
|||
Loading…
Reference in New Issue