Skip to content

Commit

Permalink
fix: unsupported resource type (#186)
Browse files Browse the repository at this point in the history
  • Loading branch information
SparkYuan committed Dec 5, 2022
1 parent 6bb5eed commit 16946d2
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 7 deletions.
11 changes: 9 additions & 2 deletions pkg/kusionctl/cmd/apply/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
runtimeInit "kusionstack.io/kusion/pkg/engine/runtime/init"
"kusionstack.io/kusion/pkg/engine/states"
previewcmd "kusionstack.io/kusion/pkg/kusionctl/cmd/preview"
"kusionstack.io/kusion/pkg/kusionctl/cmd/util"
"kusionstack.io/kusion/pkg/log"
"kusionstack.io/kusion/pkg/projectstack"
"kusionstack.io/kusion/pkg/status"
Expand Down Expand Up @@ -94,13 +95,19 @@ func (o *ApplyOptions) Run() error {
return err
}

// Compute changes for preview
runtimes := runtimeInit.InitRuntime()
r, err := runtimes[planResources.Resources[0].Type]()
// validate resource type
runtimeInitFun, err := util.ValidateResourceType(runtimes, planResources.Resources[0].Type)
if err != nil {
return err
}

r, err := runtimeInitFun()
if err != nil {
return err
}

// Compute changes for preview
changes, err := previewcmd.Preview(&o.PreviewOptions, r, stateStorage, planResources, project, stack)
if err != nil {
return err
Expand Down
14 changes: 10 additions & 4 deletions pkg/kusionctl/cmd/destroy/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
runtimeInit "kusionstack.io/kusion/pkg/engine/runtime/init"
"kusionstack.io/kusion/pkg/engine/states"
compilecmd "kusionstack.io/kusion/pkg/kusionctl/cmd/compile"
"kusionstack.io/kusion/pkg/kusionctl/cmd/util"
"kusionstack.io/kusion/pkg/log"
"kusionstack.io/kusion/pkg/projectstack"
"kusionstack.io/kusion/pkg/status"
Expand Down Expand Up @@ -76,19 +77,24 @@ func (o *DestroyOptions) Run() error {
}

runtimes := runtimeInit.InitRuntime()
runtime, err := runtimes[planResources.Resources[0].Type]()
runtimeInitFun, err := util.ValidateResourceType(runtimes, planResources.Resources[0].Type)
if err != nil {
return err
}

r, err := runtimeInitFun()
if err != nil {
return nil
}

// Get stateStroage from backend config to manage state
// Get stateStorage from backend config to manage state
stateStorage, err := backend.BackendFromConfig(project.Backend, o.BackendOps, o.WorkDir)
if err != nil {
return err
}

// Compute changes for preview
changes, err := o.preview(planResources, project, stack, runtime, stateStorage)
changes, err := o.preview(planResources, project, stack, r, stateStorage)
if err != nil {
return err
}
Expand Down Expand Up @@ -126,7 +132,7 @@ func (o *DestroyOptions) Run() error {

// Destroy
fmt.Println("Start destroying resources......")
if err := o.destroy(planResources, changes, runtime, stateStorage); err != nil {
if err := o.destroy(planResources, changes, r, stateStorage); err != nil {
return err
}
return nil
Expand Down
8 changes: 7 additions & 1 deletion pkg/kusionctl/cmd/preview/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
runtimeInit "kusionstack.io/kusion/pkg/engine/runtime/init"
"kusionstack.io/kusion/pkg/engine/states"
compilecmd "kusionstack.io/kusion/pkg/kusionctl/cmd/compile"
"kusionstack.io/kusion/pkg/kusionctl/cmd/util"
"kusionstack.io/kusion/pkg/log"
"kusionstack.io/kusion/pkg/projectstack"
"kusionstack.io/kusion/pkg/status"
Expand Down Expand Up @@ -92,7 +93,12 @@ func (o *PreviewOptions) Run() error {

// Compute changes for preview
runtimes := runtimeInit.InitRuntime()
r, err := runtimes[planResources.Resources[0].Type]()
runtimeInitFun, err := util.ValidateResourceType(runtimes, planResources.Resources[0].Type)
if err != nil {
return err
}

r, err := runtimeInitFun()
if err != nil {
return err
}
Expand Down
16 changes: 16 additions & 0 deletions pkg/kusionctl/cmd/util/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ package util

import (
"errors"
"fmt"

"kusionstack.io/kusion/pkg/engine/models"
runtimeInit "kusionstack.io/kusion/pkg/engine/runtime/init"
)

func RecoverErr(err *error) {
Expand All @@ -22,3 +26,15 @@ func CheckErr(err error) {
panic(err)
}
}

func ValidateResourceType(runtimes map[models.Type]runtimeInit.InitFn, t models.Type) (runtimeInit.InitFn, error) {
supportTypes := make([]models.Type, 0, len(runtimes))
for k := range runtimes {
supportTypes = append(supportTypes, k)
}
typeFun := runtimes[t]
if typeFun == nil {
return nil, fmt.Errorf("unknow resource type: %s. Currently supported resource types are: %v", t, supportTypes)
}
return typeFun, nil
}

0 comments on commit 16946d2

Please sign in to comment.