Skip to content

Commit

Permalink
feat(hatchery:openstack): default flavor (#6385)
Browse files Browse the repository at this point in the history
  • Loading branch information
richardlt committed Dec 6, 2022
1 parent 5d708b3 commit b7f98e6
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
14 changes: 14 additions & 0 deletions engine/hatchery/openstack/openstack.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,20 @@ func (h *HatcheryOpenstack) WorkerModelsEnabled() ([]sdk.Model, error) {
sort.Slice(filteredModels, func(i, j int) bool {
flavorI, _ := h.flavor(filteredModels[i].ModelVirtualMachine.Flavor)
flavorJ, _ := h.flavor(filteredModels[j].ModelVirtualMachine.Flavor)

// If same flavor sort by model name
if flavorI.Name == flavorJ.Name {
return filteredModels[i].Name < filteredModels[j].Name
}

// Models with the default flavor should be at the beginning of the list
if flavorI.Name == h.Config.DefaultFlavor {
return true
}
if flavorJ.Name == h.Config.DefaultFlavor {
return false
}

return flavorI.VCPUs < flavorJ.VCPUs
})

Expand Down
19 changes: 16 additions & 3 deletions engine/hatchery/openstack/openstack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ func TestHatcheryOpenstack_CanSpawn(t *testing.T) {
func TestHatcheryOpenstack_WorkerModelsEnabled(t *testing.T) {
log.Factory = log.NewTestingWrapper(t)

h := &HatcheryOpenstack{}
h := &HatcheryOpenstack{
Config: HatcheryConfiguration{
DefaultFlavor: "b2-7",
},
}

ctrl := gomock.NewController(t)
mockClient := mock_cdsclient.NewMockInterface(ctrl)
Expand Down Expand Up @@ -73,19 +77,28 @@ func TestHatcheryOpenstack_WorkerModelsEnabled(t *testing.T) {
Group: &sdk.Group{ID: 1, Name: "mygroup"},
ModelVirtualMachine: sdk.ModelVirtualMachine{Flavor: "unknown"},
},
{
ID: 5,
Type: sdk.Openstack,
Name: "my-model-5",
Group: &sdk.Group{ID: 1, Name: "mygroup"},
ModelVirtualMachine: sdk.ModelVirtualMachine{Flavor: "d2-2"},
},
}, nil
})

h.flavors = []flavors.Flavor{
{Name: "b2-7", VCPUs: 2},
{Name: "b2-30", VCPUs: 16},
{Name: "b2-120", VCPUs: 32},
{Name: "d2-2", VCPUs: 1},
}

// Only model that match a known flavor should be returned and sorted by CPUs asc
ms, err := h.WorkerModelsEnabled()
require.NoError(t, err)
require.Len(t, ms, 2)
require.Len(t, ms, 3)
assert.Equal(t, "my-model-3", ms[0].Name)
assert.Equal(t, "my-model-2", ms[1].Name)
assert.Equal(t, "my-model-5", ms[1].Name)
assert.Equal(t, "my-model-2", ms[2].Name)
}
3 changes: 3 additions & 0 deletions engine/hatchery/openstack/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ type HatcheryConfiguration struct {
// This count will prevent big flavor to take all the CPUs available for the hatchery and will keep some available for smaller flavors.
// Ex: if two flavors are available with 8 and 4 cpus and count to keep equals 2 the hatchery will need 8+4*2=16cpus available to start a 8cpus flavor.
CountSmallerFlavorToKeep int `mapstructure:"countSmallerFlavorToKeep" toml:"countSmallerFlavorToKeep" default:"" commented:"true" comment:"Count of smaller flavors that the hatchery should be able to boot when booting a larger flavor." json:"countSmallerFlavorToKeep"`

// DefaultFlavor, if set the hatchery will use a model with the default flavor in priority to start jobs without model requirement
DefaultFlavor string `mapstructure:"defaultFlavor" toml:"defaultFlavor" default:"" commented:"true" comment:"If set the hatchery will use a model with the default flavor in priority to start jobs without model requirement" json:"defaultFlavor"`
}

// HatcheryOpenstack spawns instances of worker model with type 'ISO'
Expand Down

0 comments on commit b7f98e6

Please sign in to comment.