Skip to content

Commit

Permalink
Move ValidateResult to common package
Browse files Browse the repository at this point in the history
  • Loading branch information
fabi200123 committed Sep 2, 2024
1 parent 0824416 commit 7074f01
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 40 deletions.
23 changes: 23 additions & 0 deletions runner/providers/common/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package common

import (
garmErrors "github.com/cloudbase/garm-provider-common/errors"
commonParams "github.com/cloudbase/garm-provider-common/params"
"github.com/cloudbase/garm/runner/providers/util"
)

func ValidateResult(inst commonParams.ProviderInstance) error {
if inst.ProviderID == "" {
return garmErrors.NewProviderError("missing provider ID")
}

if inst.Name == "" {
return garmErrors.NewProviderError("missing instance name")
}

if !util.IsValidProviderStatus(inst.Status) {
return garmErrors.NewProviderError("invalid status returned (%s)", inst.Status)
}

return nil
}
24 changes: 4 additions & 20 deletions runner/providers/v0.1.0/external.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"github.com/cloudbase/garm/metrics"
"github.com/cloudbase/garm/params"
"github.com/cloudbase/garm/runner/common"
"github.com/cloudbase/garm/runner/providers/util"
commonExternal "github.com/cloudbase/garm/runner/providers/common"
)

var _ common.Provider = (*external)(nil)
Expand Down Expand Up @@ -57,22 +57,6 @@ type external struct {
environmentVariables []string
}

func (e *external) validateResult(inst commonParams.ProviderInstance) error {
if inst.ProviderID == "" {
return garmErrors.NewProviderError("missing provider ID")
}

if inst.Name == "" {
return garmErrors.NewProviderError("missing instance name")
}

if !util.IsValidProviderStatus(inst.Status) {
return garmErrors.NewProviderError("invalid status returned (%s)", inst.Status)
}

return nil
}

// CreateInstance creates a new compute instance in the provider.
func (e *external) CreateInstance(ctx context.Context, bootstrapParams commonParams.BootstrapInstance, _ common.CreateInstanceParams) (commonParams.ProviderInstance, error) {
asEnv := []string{
Expand Down Expand Up @@ -111,7 +95,7 @@ func (e *external) CreateInstance(ctx context.Context, bootstrapParams commonPar
return commonParams.ProviderInstance{}, garmErrors.NewProviderError("failed to decode response from binary: %s", err)
}

if err := e.validateResult(param); err != nil {
if err := commonExternal.ValidateResult(param); err != nil {
metrics.InstanceOperationFailedCount.WithLabelValues(
"CreateInstance", // label: operation
e.cfg.Name, // label: provider
Expand Down Expand Up @@ -189,7 +173,7 @@ func (e *external) GetInstance(ctx context.Context, instance string, _ common.Ge
return commonParams.ProviderInstance{}, garmErrors.NewProviderError("failed to decode response from binary: %s", err)
}

if err := e.validateResult(param); err != nil {
if err := commonExternal.ValidateResult(param); err != nil {
metrics.InstanceOperationFailedCount.WithLabelValues(
"GetInstance", // label: operation
e.cfg.Name, // label: provider
Expand Down Expand Up @@ -235,7 +219,7 @@ func (e *external) ListInstances(ctx context.Context, poolID string, _ common.Li

ret := make([]commonParams.ProviderInstance, len(param))
for idx, inst := range param {
if err := e.validateResult(inst); err != nil {
if err := commonExternal.ValidateResult(inst); err != nil {
metrics.InstanceOperationFailedCount.WithLabelValues(
"ListInstances", // label: operation
e.cfg.Name, // label: provider
Expand Down
32 changes: 12 additions & 20 deletions runner/providers/v0.1.1/external.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"github.com/cloudbase/garm/metrics"
"github.com/cloudbase/garm/params"
"github.com/cloudbase/garm/runner/common"
"github.com/cloudbase/garm/runner/providers/util"
commonExternal "github.com/cloudbase/garm/runner/providers/common"
)

var _ common.Provider = (*external)(nil)
Expand Down Expand Up @@ -56,29 +56,21 @@ type external struct {
environmentVariables []string
}

func (e *external) validateResult(inst commonParams.ProviderInstance) error {
if inst.ProviderID == "" {
return garmErrors.NewProviderError("missing provider ID")
}

if inst.Name == "" {
return garmErrors.NewProviderError("missing instance name")
}

if !util.IsValidProviderStatus(inst.Status) {
return garmErrors.NewProviderError("invalid status returned (%s)", inst.Status)
}

return nil
}

// CreateInstance creates a new compute instance in the provider.
func (e *external) CreateInstance(ctx context.Context, bootstrapParams commonParams.BootstrapInstance, _ common.CreateInstanceParams) (commonParams.ProviderInstance, error) {
extraspecs := bootstrapParams.ExtraSpecs
extraspecsValue, err := json.Marshal(extraspecs)
if err != nil {
return commonParams.ProviderInstance{}, errors.Wrap(err, "serializing extraspecs")
}
// Encode the extraspecs as base64 to avoid issues with special characters.
base64EncodedExtraSpecs := base64.StdEncoding.EncodeToString(extraspecsValue)
asEnv := []string{
fmt.Sprintf("GARM_COMMAND=%s", commonExecution.CreateInstanceCommand),
fmt.Sprintf("GARM_CONTROLLER_ID=%s", e.controllerID),
fmt.Sprintf("GARM_POOL_ID=%s", bootstrapParams.PoolID),
fmt.Sprintf("GARM_PROVIDER_CONFIG_FILE=%s", e.cfg.External.ConfigFile),
fmt.Sprintf("GARM_POOL_EXTRASPECS=%s", base64EncodedExtraSpecs),
}
asEnv = append(asEnv, e.environmentVariables...)

Expand Down Expand Up @@ -110,7 +102,7 @@ func (e *external) CreateInstance(ctx context.Context, bootstrapParams commonPar
return commonParams.ProviderInstance{}, garmErrors.NewProviderError("failed to decode response from binary: %s", err)
}

if err := e.validateResult(param); err != nil {
if err := commonExternal.ValidateResult(param); err != nil {
metrics.InstanceOperationFailedCount.WithLabelValues(
"CreateInstance", // label: operation
e.cfg.Name, // label: provider
Expand Down Expand Up @@ -206,7 +198,7 @@ func (e *external) GetInstance(ctx context.Context, instance string, getInstance
return commonParams.ProviderInstance{}, garmErrors.NewProviderError("failed to decode response from binary: %s", err)
}

if err := e.validateResult(param); err != nil {
if err := commonExternal.ValidateResult(param); err != nil {
metrics.InstanceOperationFailedCount.WithLabelValues(
"GetInstance", // label: operation
e.cfg.Name, // label: provider
Expand Down Expand Up @@ -260,7 +252,7 @@ func (e *external) ListInstances(ctx context.Context, poolID string, listInstanc

ret := make([]commonParams.ProviderInstance, len(param))
for idx, inst := range param {
if err := e.validateResult(inst); err != nil {
if err := commonExternal.ValidateResult(inst); err != nil {
metrics.InstanceOperationFailedCount.WithLabelValues(
"ListInstances", // label: operation
e.cfg.Name, // label: provider
Expand Down

0 comments on commit 7074f01

Please sign in to comment.