Skip to content

Commit

Permalink
Allow for drivers instantinating by using <driver>/<suffix>
Browse files Browse the repository at this point in the history
  • Loading branch information
sparshev committed Jun 7, 2024
1 parent efcaabc commit 0621527
Show file tree
Hide file tree
Showing 12 changed files with 395 additions and 389 deletions.
2 changes: 1 addition & 1 deletion docs/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ servers:
info:
description: API of the Fish node/cluster
version: 1.0.0
title: Aqurium Fish
title: Aquarium Fish
contact:
name: Sergei Parshev
url: 'https://github.com/adobe/aquarium-fish'
Expand Down
19 changes: 15 additions & 4 deletions lib/drivers/aws/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,21 @@ import (
"github.com/adobe/aquarium-fish/lib/util"
)

// Implements drivers.ResourceDriverFabric interface
type Fabric struct{}

func (f *Fabric) Name() string {
return "aws"
}

func (f *Fabric) NewResourceDriver() drivers.ResourceDriver {
return &Driver{}
}

func init() {
drivers.FabricsList = append(drivers.FabricsList, &Fabric{})
}

// Implements drivers.ResourceDriver interface
type Driver struct {
cfg Config
Expand All @@ -47,10 +62,6 @@ type Driver struct {
quotas_next_update time.Time
}

func init() {
drivers.DriversList = append(drivers.DriversList, &Driver{})
}

func (d *Driver) Name() string {
return "aws"
}
Expand Down
19 changes: 15 additions & 4 deletions lib/drivers/docker/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,21 @@ import (
"github.com/adobe/aquarium-fish/lib/openapi/types"
)

// Implements drivers.ResourceDriverFabric interface
type Fabric struct{}

func (f *Fabric) Name() string {
return "docker"
}

func (f *Fabric) NewResourceDriver() drivers.ResourceDriver {
return &Driver{}
}

func init() {
drivers.FabricsList = append(drivers.FabricsList, &Fabric{})
}

// Implements drivers.ResourceDriver interface
type Driver struct {
cfg Config
Expand All @@ -43,10 +58,6 @@ type Driver struct {
docker_usage types.Resources // Used when the docker is remote
}

func init() {
drivers.DriversList = append(drivers.DriversList, &Driver{})
}

func (d *Driver) Name() string {
return "docker"
}
Expand Down
11 changes: 10 additions & 1 deletion lib/drivers/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,16 @@ const (
StatusAllocated = "ALLOCATED"
)

var DriversList []ResourceDriver
var FabricsList []ResourceDriverFabric

// Fabric allows to generate new instances of the drivers
type ResourceDriverFabric interface {
// Name of the driver
Name() string

// Generates new resource driver
NewResourceDriver() ResourceDriver
}

type ResourceDriver interface {
// Name of the driver
Expand Down
19 changes: 15 additions & 4 deletions lib/drivers/native/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,21 @@ import (
"github.com/adobe/aquarium-fish/lib/openapi/types"
)

// Implements drivers.ResourceDriverFabric interface
type Fabric struct{}

func (f *Fabric) Name() string {
return "native"
}

func (f *Fabric) NewResourceDriver() drivers.ResourceDriver {
return &Driver{}
}

func init() {
drivers.FabricsList = append(drivers.FabricsList, &Fabric{})
}

// Implements drivers.ResourceDriver interface
type Driver struct {
cfg Config
Expand All @@ -41,10 +56,6 @@ type EnvData struct {
Disks map[string]string // Map with disk_name = mount_path
}

func init() {
drivers.DriversList = append(drivers.DriversList, &Driver{})
}

func (d *Driver) Name() string {
return "native"
}
Expand Down
19 changes: 15 additions & 4 deletions lib/drivers/test/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,28 @@ import (
"github.com/adobe/aquarium-fish/lib/openapi/types"
)

// Implements drivers.ResourceDriverFabric interface
type Fabric struct{}

func (f *Fabric) Name() string {
return "test"
}

func (f *Fabric) NewResourceDriver() drivers.ResourceDriver {
return &Driver{}
}

func init() {
drivers.FabricsList = append(drivers.FabricsList, &Fabric{})
}

// Implements drivers.ResourceDriver interface
type Driver struct {
cfg Config
// Contains the available tasks of the driver
tasks_list []drivers.ResourceDriverTask
}

func init() {
drivers.DriversList = append(drivers.DriversList, &Driver{})
}

func (d *Driver) Name() string {
return "test"
}
Expand Down
19 changes: 15 additions & 4 deletions lib/drivers/vmx/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,21 @@ import (
"github.com/adobe/aquarium-fish/lib/util"
)

// Implements drivers.ResourceDriverFabric interface
type Fabric struct{}

func (f *Fabric) Name() string {
return "vmx"
}

func (f *Fabric) NewResourceDriver() drivers.ResourceDriver {
return &Driver{}
}

func init() {
drivers.FabricsList = append(drivers.FabricsList, &Fabric{})
}

// Implements drivers.ResourceDriver interface
type Driver struct {
cfg Config
Expand All @@ -40,10 +55,6 @@ type Driver struct {
total_ram uint // In RAM GB
}

func init() {
drivers.DriversList = append(drivers.DriversList, &Driver{})
}

func (d *Driver) Name() string {
return "vmx"
}
Expand Down
5 changes: 4 additions & 1 deletion lib/fish/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ type Config struct {

DefaultResourceLifetime string `json:"default_resource_lifetime"` // Sets the lifetime of the resource which will be used if label definition one is not set

Drivers []ConfigDriver `json:"drivers"` // If specified - only the listed plugins will be loaded
// Configuration for the node drivers, if defined - only the listed plugins will be loaded
// Each configuration could instantinate the same driver multiple times by adding instance name
// separated from driver by slash symbol (like "<driver>/prod" - will create "prod" instance).
Drivers []ConfigDriver `json:"drivers"`
}

type ConfigDriver struct {
Expand Down
64 changes: 32 additions & 32 deletions lib/fish/drivers.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ package fish

import (
"fmt"
"strings"

"github.com/adobe/aquarium-fish/lib/drivers"
"github.com/adobe/aquarium-fish/lib/log"
Expand All @@ -27,57 +28,56 @@ import (
_ "github.com/adobe/aquarium-fish/lib/drivers/test"
)

var drivers_enabled_list []drivers.ResourceDriver
var drivers_instances map[string]drivers.ResourceDriver

func (f *Fish) DriverGet(name string) drivers.ResourceDriver {
for _, drv := range drivers_enabled_list {
if drv.Name() == name {
return drv
}
if drivers_instances == nil {
log.Error("Fish: Resource drivers are not initialized to request the driver instance:", name)
return nil
}
return nil
drv, _ := drivers_instances[name]
return drv
}

// Making the drivers instances map with specified names
func (f *Fish) DriversSet() error {
var list []drivers.ResourceDriver
instances := make(map[string]drivers.ResourceDriver)

for _, drv := range drivers.DriversList {
en := false
if len(f.cfg.Drivers) == 0 {
// If no drivers is specified in the config - load all
en = true
} else {
for _, res := range f.cfg.Drivers {
if res.Name == drv.Name() {
en = true
break
if len(f.cfg.Drivers) == 0 {
// If no drivers instances are specified in the config - load all the drivers
for _, fbr := range drivers.FabricsList {
instances[fbr.Name()] = fbr.NewResourceDriver()
log.Info("Fish: Resource driver enabled:", fbr.Name())
}
} else {
for _, fbr := range drivers.FabricsList {
// One driver could be used multiple times by config suffixes
for _, cfg := range f.cfg.Drivers {
log.Debug("Fish: Processing driver config:", cfg.Name, "vs", fbr.Name())
if cfg.Name == fbr.Name() || strings.HasPrefix(cfg.Name, fbr.Name()+"/") {
instances[cfg.Name] = fbr.NewResourceDriver()
log.Info("Fish: Resource driver enabled:", fbr.Name(), "as", cfg.Name)
}
}
}
if en {
log.Info("Fish: Resource driver enabled:", drv.Name())
list = append(list, drv)
} else {
log.Info("Fish: Resource driver disabled:", drv.Name())
}
}

if len(f.cfg.Drivers) > len(list) {
return fmt.Errorf("Unable to enable all the required drivers %s", f.cfg.Drivers)
if len(f.cfg.Drivers) > len(instances) {
return fmt.Errorf("Unable to enable all the required drivers %s", f.cfg.Drivers)
}
}

drivers_enabled_list = list
drivers_instances = instances

return nil
}

func (f *Fish) DriversPrepare(configs []ConfigDriver) (errs []error) {
not_skipped_drivers := drivers_enabled_list[:0]
for _, drv := range drivers_enabled_list {
activated_drivers_instances := make(map[string]drivers.ResourceDriver)
for name, drv := range drivers_instances {
// Looking for the driver config
var json_cfg []byte
for _, cfg := range configs {
if drv.Name() == cfg.Name {
if name == cfg.Name {
json_cfg = []byte(cfg.Cfg)
break
}
Expand All @@ -87,12 +87,12 @@ func (f *Fish) DriversPrepare(configs []ConfigDriver) (errs []error) {
errs = append(errs, err)
log.Warn("Fish: Resource driver prepare failed:", drv.Name(), err)
} else {
not_skipped_drivers = append(not_skipped_drivers, drv)
activated_drivers_instances[name] = drv
log.Info("Fish: Resource driver activated:", drv.Name())
}
}

drivers_enabled_list = not_skipped_drivers
drivers_instances = activated_drivers_instances

return errs
}
Loading

0 comments on commit 0621527

Please sign in to comment.