Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cdsctl): worker model list - image field #5267

Merged
merged 1 commit into from
Jun 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 54 additions & 1 deletion cli/cdsctl/worker_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,53 @@ var workerModelListCmd = cli.Command{
},
}

type workerModelDisplay struct {
Name string `json:"name" cli:"name,key"`
Type string `json:"type" cli:"type"`
Disabled bool `json:"disabled" cli:"disabled"`
Restricted bool `json:"restricted" cli:"restricted"`
NeedRegistration bool `json:"need_registration" cli:"need_registration"`
NbSpawnErr int64 `json:"nb_spawn_err" cli:"nb_spawn_err"`
IsDeprecated bool `json:"is_deprecated" cli:"deprecated"`
IsOfficial bool `json:"is_official" cli:"official"`
Image string `json:"image" cli:"image"`
Flavor string `json:"flavor" cli:"flavor"`
}

func newWorkerModelDisplay(wm sdk.Model) workerModelDisplay {
name := wm.Name
if wm.Group != nil {
name = fmt.Sprintf("%s/%s", wm.Group.Name, wm.Name)
} else {
name = wm.Name
}

var image, flavor string

switch wm.Type {
case sdk.Docker:
image = wm.ModelDocker.Image
case sdk.Openstack, sdk.VSphere:
image = wm.ModelVirtualMachine.Image
flavor = wm.ModelVirtualMachine.Flavor
}

w := workerModelDisplay{
Name: name,
Type: wm.Type,
Disabled: wm.Disabled,
Restricted: wm.Restricted,
NeedRegistration: wm.NeedRegistration,
NbSpawnErr: wm.NbSpawnErr,
IsDeprecated: wm.IsDeprecated,
Image: image,
Flavor: flavor,
IsOfficial: wm.IsOfficial,
}

return w
}

func workerModelListRun(v cli.Values) (cli.ListResult, error) {
var err error
var workerModels []sdk.Model
Expand All @@ -63,7 +110,13 @@ func workerModelListRun(v cli.Values) (cli.ListResult, error) {
if err != nil {
return nil, err
}
return cli.AsListResult(workerModels), nil

wms := make([]workerModelDisplay, len(workerModels))
for i := range workerModels {
wms[i] = newWorkerModelDisplay(workerModels[i])
}

return cli.AsListResult(wms), nil
}

var workerModelImportCmd = cli.Command{
Expand Down
12 changes: 3 additions & 9 deletions engine/hatchery/swarm/swarm_util_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,7 @@ func TestHatcherySwarm_createAndStartContainer(t *testing.T) {
// RegisterOnly = true, this will pull image if image is not found
spawnArgs := hatchery.SpawnArguments{
RegisterOnly: true,
Model: &sdk.Model{
Image: args.image,
},
Model: &sdk.Model{},
}
err := h.createAndStartContainer(context.TODO(), h.dockerClients["default"], args, spawnArgs)
require.NoError(t, err)
Expand Down Expand Up @@ -195,9 +193,7 @@ func TestHatcherySwarm_createAndStartContainerWithMount(t *testing.T) {

spawnArgs := hatchery.SpawnArguments{
RegisterOnly: false,
Model: &sdk.Model{
Image: args.image,
},
Model: &sdk.Model{},
}
err = h.createAndStartContainer(context.TODO(), h.dockerClients["default"], args, spawnArgs)
require.NoError(t, err)
Expand Down Expand Up @@ -230,9 +226,7 @@ func TestHatcherySwarm_createAndStartContainerWithNetwork(t *testing.T) {

spawnArgs := hatchery.SpawnArguments{
RegisterOnly: false,
Model: &sdk.Model{
Image: args.image,
},
Model: &sdk.Model{},
}
err = h.createAndStartContainer(context.TODO(), h.dockerClients["default"], args, spawnArgs)
require.NoError(t, err)
Expand Down
7 changes: 7 additions & 0 deletions engine/sql/208_worker_model.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- +migrate Up

ALTER TABLE "worker_model" DROP COLUMN IF EXISTS image;

-- +migrate Down

ALTER TABLE "worker_model" ADD COLUMN IF NOT EXISTS image TEXT;
36 changes: 17 additions & 19 deletions sdk/exportentities/worker_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,23 @@ import (

// WorkerModel is the as code format of a worker model
type WorkerModel struct {
Name string `json:"name" yaml:"name"`
Group string `json:"group" yaml:"group"`
Communication string `json:"communication,omitempty" yaml:"communication,omitempty"`
Image string `json:"image" yaml:"image"`
Registry string `json:"registry,omitempty" yaml:"registry,omitempty"`
Username string `json:"username,omitempty" yaml:"username,omitempty"`
Password string `json:"password,omitempty" yaml:"password,omitempty"`
Description string `json:"description" yaml:"description"`
Type string `json:"type" yaml:"type"`
Flavor string `json:"flavor,omitempty" yaml:"flavor,omitempty"`
Envs map[string]string `json:"envs,omitempty" yaml:"envs,omitempty"`
PatternName string `json:"pattern_name,omitempty" yaml:"pattern_name,omitempty"`
Shell string `json:"shell,omitempty" yaml:"shell,omitempty"`
PreCmd string `json:"pre_cmd,omitempty" yaml:"pre_cmd,omitempty"`
Cmd string `json:"cmd,omitempty" yaml:"cmd,omitempty"`
PostCmd string `json:"post_cmd,omitempty" yaml:"post_cmd,omitempty"`
Restricted bool `json:"restricted,omitempty" yaml:"restricted,omitempty"`
IsDeprecated bool `json:"is_deprecated,omitempty" yaml:"is_deprecated,omitempty"`
Name string `json:"name" yaml:"name"`
Group string `json:"group" yaml:"group"`
Image string `json:"image" yaml:"image"`
Registry string `json:"registry,omitempty" yaml:"registry,omitempty"`
Username string `json:"username,omitempty" yaml:"username,omitempty"`
Password string `json:"password,omitempty" yaml:"password,omitempty"`
Description string `json:"description" yaml:"description"`
Type string `json:"type" yaml:"type"`
Flavor string `json:"flavor,omitempty" yaml:"flavor,omitempty"`
Envs map[string]string `json:"envs,omitempty" yaml:"envs,omitempty"`
PatternName string `json:"pattern_name,omitempty" yaml:"pattern_name,omitempty"`
Shell string `json:"shell,omitempty" yaml:"shell,omitempty"`
PreCmd string `json:"pre_cmd,omitempty" yaml:"pre_cmd,omitempty"`
Cmd string `json:"cmd,omitempty" yaml:"cmd,omitempty"`
PostCmd string `json:"post_cmd,omitempty" yaml:"post_cmd,omitempty"`
Restricted bool `json:"restricted,omitempty" yaml:"restricted,omitempty"`
IsDeprecated bool `json:"is_deprecated,omitempty" yaml:"is_deprecated,omitempty"`
}

type WorkerModelOption func(sdk.Model, *WorkerModel) error
Expand Down Expand Up @@ -53,7 +52,6 @@ func NewWorkerModel(wm sdk.Model, opts ...WorkerModelOption) WorkerModel {
IsDeprecated: wm.IsDeprecated,
Description: wm.Description,
Restricted: wm.Restricted,
Image: wm.Image,
}

switch wm.Type {
Expand Down
1 change: 0 additions & 1 deletion sdk/worker_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ type Model struct {
Name string `json:"name" db:"name" cli:"name,key"`
Description string `json:"description" db:"description" cli:"description"`
Type string `json:"type" db:"type" cli:"type"`
Image string `json:"image" db:"image" cli:"image"` // TODO: DELETE after migration done
Disabled bool `json:"disabled" db:"disabled" cli:"disabled"`
Restricted bool `json:"restricted" db:"restricted" cli:"restricted"`
RegisteredOS *string `json:"registered_os" db:"registered_os" cli:"-"`
Expand Down