Skip to content

Commit

Permalink
Update execution
Browse files Browse the repository at this point in the history
  • Loading branch information
fabi200123 committed Sep 6, 2024
1 parent d5373f3 commit 51370ba
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 35 deletions.
29 changes: 17 additions & 12 deletions execution/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@ import (
executionv011 "github.com/cloudbase/garm-provider-common/execution/v0.1.1"
)

type ExternalProvider interface {
executionv010.ExternalProvider
executionv011.ExternalProvider
}

type Environment struct {
EnvironmentV010 executionv010.EnvironmentV010
EnvironmentV011 executionv011.EnvironmentV011
Expand All @@ -41,7 +36,7 @@ func GetEnvironment() (Environment, error) {
interfaceVersion := os.Getenv("GARM_INTERFACE_VERSION")

switch interfaceVersion {
case common.Version010:
case common.Version010, "":
env, err := executionv010.GetEnvironment()
if err != nil {
return Environment{}, err
Expand All @@ -68,13 +63,23 @@ func GetEnvironment() (Environment, error) {
}
}

func Run(ctx context.Context, provider ExternalProvider, env Environment) (string, error) {
switch env.InterfaceVersion {
case common.Version010:
return executionv010.Run(ctx, provider, env.EnvironmentV010)
func (e Environment) Run(ctx context.Context, provider interface{}) (string, error) {
switch e.InterfaceVersion {
case common.Version010, "":
prov, ok := provider.(executionv010.ExternalProvider)
if !ok {
return "", fmt.Errorf("provider does not implement %s ExternalProvider", e.InterfaceVersion)
}
return e.EnvironmentV010.Run(ctx, prov)

case common.Version011:
return executionv011.Run(ctx, provider, env.EnvironmentV011)
prov, ok := provider.(executionv011.ExternalProvider)
if !ok {
return "", fmt.Errorf("provider does not implement %s ExternalProvider", e.InterfaceVersion)
}
return e.EnvironmentV011.Run(ctx, prov)

default:
return "", fmt.Errorf("unsupported interface version: %s", env.InterfaceVersion)
return "", fmt.Errorf("unsupported interface version: %s", e.InterfaceVersion)
}
}
29 changes: 10 additions & 19 deletions execution/v0.1.0/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ import (
"fmt"
"os"

"golang.org/x/mod/semver"

common "github.com/cloudbase/garm-provider-common/execution/common"
"github.com/cloudbase/garm-provider-common/params"
)
Expand All @@ -43,10 +41,6 @@ func GetEnvironment() (EnvironmentV010, error) {
}
env.BootstrapParams = boostrapParams

if env.InterfaceVersion == "" {
env.InterfaceVersion = common.Version010
}

if err := env.Validate(); err != nil {
return EnvironmentV010{}, fmt.Errorf("failed to validate execution environment: %w", err)
}
Expand All @@ -60,7 +54,6 @@ type EnvironmentV010 struct {
PoolID string
ProviderConfigFile string
InstanceID string
InterfaceVersion string
BootstrapParams params.BootstrapInstance
}

Expand Down Expand Up @@ -106,20 +99,18 @@ func (e EnvironmentV010) Validate() error {
return fmt.Errorf("missing controller ID")
}
case common.GetVersionCommand:
if !semver.IsValid(e.InterfaceVersion) {
return fmt.Errorf("invalid interface version: %s", e.InterfaceVersion)
}
return nil
default:
return fmt.Errorf("unknown GARM_COMMAND: %s", e.Command)
}
return nil
}

func Run(ctx context.Context, provider ExternalProvider, env EnvironmentV010) (string, error) {
func (e EnvironmentV010) Run(ctx context.Context, provider ExternalProvider) (string, error) {
var ret string
switch env.Command {
switch e.Command {
case common.CreateInstanceCommand:
instance, err := provider.CreateInstance(ctx, env.BootstrapParams)
instance, err := provider.CreateInstance(ctx, e.BootstrapParams)
if err != nil {
return "", fmt.Errorf("failed to create instance in provider: %w", err)
}
Expand All @@ -130,7 +121,7 @@ func Run(ctx context.Context, provider ExternalProvider, env EnvironmentV010) (s
}
ret = string(asJs)
case common.GetInstanceCommand:
instance, err := provider.GetInstance(ctx, env.InstanceID)
instance, err := provider.GetInstance(ctx, e.InstanceID)
if err != nil {
return "", fmt.Errorf("failed to get instance from provider: %w", err)
}
Expand All @@ -140,7 +131,7 @@ func Run(ctx context.Context, provider ExternalProvider, env EnvironmentV010) (s
}
ret = string(asJs)
case common.ListInstancesCommand:
instances, err := provider.ListInstances(ctx, env.PoolID)
instances, err := provider.ListInstances(ctx, e.PoolID)
if err != nil {
return "", fmt.Errorf("failed to list instances from provider: %w", err)
}
Expand All @@ -150,26 +141,26 @@ func Run(ctx context.Context, provider ExternalProvider, env EnvironmentV010) (s
}
ret = string(asJs)
case common.DeleteInstanceCommand:
if err := provider.DeleteInstance(ctx, env.InstanceID); err != nil {
if err := provider.DeleteInstance(ctx, e.InstanceID); err != nil {
return "", fmt.Errorf("failed to delete instance from provider: %w", err)
}
case common.RemoveAllInstancesCommand:
if err := provider.RemoveAllInstances(ctx); err != nil {
return "", fmt.Errorf("failed to destroy environment: %w", err)
}
case common.StartInstanceCommand:
if err := provider.Start(ctx, env.InstanceID); err != nil {
if err := provider.Start(ctx, e.InstanceID); err != nil {
return "", fmt.Errorf("failed to start instance: %w", err)
}
case common.StopInstanceCommand:
if err := provider.Stop(ctx, env.InstanceID, true); err != nil {
if err := provider.Stop(ctx, e.InstanceID, true); err != nil {
return "", fmt.Errorf("failed to stop instance: %w", err)
}
case common.GetVersionCommand:
version := provider.GetVersion(ctx)
ret = string(version)
default:
return "", fmt.Errorf("invalid command: %s", env.Command)
return "", fmt.Errorf("invalid command: %s", e.Command)
}
return ret, nil
}
2 changes: 1 addition & 1 deletion execution/v0.1.0/execution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ func TestRun(t *testing.T) {
mockInstance: tc.providerInstance,
}

out, err := Run(context.Background(), &testExternalProvider, tc.providerEnv)
out, err := tc.providerEnv.Run(context.Background(), &testExternalProvider)

if tc.expectedErrMsg == "" {
require.NoError(t, err)
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ require (
github.com/stretchr/testify v1.9.0
github.com/teris-io/shortid v0.0.0-20220617161101-71ec9f2aa569
golang.org/x/crypto v0.26.0
golang.org/x/mod v0.20.0
golang.org/x/sys v0.24.0
gopkg.in/natefinch/lumberjack.v2 v2.2.1
gopkg.in/yaml.v3 v3.0.1
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ github.com/teris-io/shortid v0.0.0-20220617161101-71ec9f2aa569 h1:xzABM9let0HLLq
github.com/teris-io/shortid v0.0.0-20220617161101-71ec9f2aa569/go.mod h1:2Ly+NIftZN4de9zRmENdYbvPQeaVIYKWpLFStLFEBgI=
golang.org/x/crypto v0.26.0 h1:RrRspgV4mU+YwB4FYnuBoKsUapNIL5cohGAmSH3azsw=
golang.org/x/crypto v0.26.0/go.mod h1:GY7jblb9wI+FOo5y8/S2oY4zWP07AkOJ4+jxCqdqn54=
golang.org/x/mod v0.20.0 h1:utOm6MM3R3dnawAiJgn0y+xvuYRsm1RKM/4giyfDgV0=
golang.org/x/mod v0.20.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/net v0.28.0 h1:a9JDOJc5GMUJ0+UDqmLT86WiEy7iWyIhz8gz8E4e5hE=
golang.org/x/net v0.28.0/go.mod h1:yqtgsTWOOnlGLG9GFRrK3++bGOUEkNBoHZc8MEDWPNg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand Down

0 comments on commit 51370ba

Please sign in to comment.