Skip to content

Commit

Permalink
feat: state backend using workspace, and upgrade of state (#669)
Browse files Browse the repository at this point in the history
  • Loading branch information
healthjyk committed Dec 14, 2023
1 parent efa145e commit 1dd8aa2
Show file tree
Hide file tree
Showing 40 changed files with 1,258 additions and 430 deletions.
4 changes: 0 additions & 4 deletions pkg/apis/project/types.go
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/pterm/pterm"

"kusionstack.io/kusion/pkg/apis/stack"
"kusionstack.io/kusion/pkg/engine/backend"
"kusionstack.io/kusion/pkg/log"
)

Expand Down Expand Up @@ -50,9 +49,6 @@ type Configuration struct {
// Tenant name
Tenant string `json:"tenant,omitempty" yaml:"tenant,omitempty"`

// Backend storage config
Backend *backend.Storage `json:"backend,omitempty" yaml:"backend,omitempty"`

// SpecGenerator configs
Generator *GeneratorConfig `json:"generator,omitempty" yaml:"generator,omitempty"`

Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/apply/options.go
Expand Up @@ -91,8 +91,8 @@ func (o *Options) Run() error {
return nil
}

// Get state storage from backend config to manage state
stateStorage, err := backend.BackendFromConfig(project.Backend, o.BackendOps, o.WorkDir)
// Get state storage from cli backend options, environment variables, workspace backend configs
stateStorage, err := backend.NewStateStorage(stack, &o.BackendOptions)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/apply/options_test.go
Expand Up @@ -188,7 +188,7 @@ func newSA(name string) intent.Resource {
}

func Test_apply(t *testing.T) {
stateStorage := &local.FileSystemState{Path: filepath.Join("", local.KusionState)}
stateStorage := &local.FileSystemState{Path: filepath.Join("", local.KusionStateFileFile)}
mockey.PatchConvey("dry run", t, func() {
planResources := &intent.Intent{Resources: []intent.Resource{sa1}}
order := &opsmodels.ChangeOrder{
Expand Down
16 changes: 12 additions & 4 deletions pkg/cmd/destroy/options.go
Expand Up @@ -28,7 +28,7 @@ type Options struct {
Operator string
Yes bool
Detail bool
backend.BackendOps
backend.BackendOptions
}

func NewDestroyOptions() *Options {
Expand All @@ -42,7 +42,15 @@ func (o *Options) Complete(args []string) {
}

func (o *Options) Validate() error {
return o.Options.Validate()
if err := o.Options.Validate(); err != nil {
return err
}
if !o.BackendOptions.IsEmpty() {
if err := o.BackendOptions.Validate(); err != nil {
return err
}
}
return nil
}

func (o *Options) Run() error {
Expand All @@ -54,8 +62,8 @@ func (o *Options) Run() error {
return err
}

// Get stateStorage from backend config to manage state
stateStorage, err := backend.BackendFromConfig(project.Backend, o.BackendOps, o.WorkDir)
// Get state storage from cli backend options, environment variables, workspace backend configs
stateStorage, err := backend.NewStateStorage(stack, &o.BackendOptions)
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/cmd/destroy/options_test.go
Expand Up @@ -100,7 +100,7 @@ func Test_preview(t *testing.T) {
mockOperationPreview()

o := NewDestroyOptions()
stateStorage := &local.FileSystemState{Path: filepath.Join(o.WorkDir, local.KusionState)}
stateStorage := &local.FileSystemState{Path: filepath.Join(o.WorkDir, local.KusionStateFileFile)}
_, err := o.preview(&intent.Intent{Resources: []intent.Resource{sa1}}, p, s, stateStorage)
assert.Nil(t, err)
})
Expand Down Expand Up @@ -217,7 +217,7 @@ func Test_destroy(t *testing.T) {
}
changes := opsmodels.NewChanges(p, s, order)

stateStorage := &local.FileSystemState{Path: filepath.Join(o.WorkDir, local.KusionState)}
stateStorage := &local.FileSystemState{Path: filepath.Join(o.WorkDir, local.KusionStateFileFile)}

err := o.destroy(planResources, changes, stateStorage)
assert.Nil(t, err)
Expand All @@ -239,7 +239,7 @@ func Test_destroy(t *testing.T) {
},
}
changes := opsmodels.NewChanges(p, s, order)
stateStorage := &local.FileSystemState{Path: filepath.Join(o.WorkDir, local.KusionState)}
stateStorage := &local.FileSystemState{Path: filepath.Join(o.WorkDir, local.KusionStateFileFile)}

err := o.destroy(planResources, changes, stateStorage)
assert.NotNil(t, err)
Expand Down
11 changes: 8 additions & 3 deletions pkg/cmd/preview/options.go
Expand Up @@ -29,7 +29,7 @@ const jsonOutput = "json"
type Options struct {
build.Options
Flags
backend.BackendOps
backend.BackendOptions
}

type Flags struct {
Expand Down Expand Up @@ -62,6 +62,11 @@ func (o *Options) Validate() error {
if err := o.ValidateIntentFile(); err != nil {
return err
}
if !o.BackendOptions.IsEmpty() {
if err := o.BackendOptions.Validate(); err != nil {
return err
}
}
return nil
}

Expand Down Expand Up @@ -152,8 +157,8 @@ func (o *Options) Run() error {
return nil
}

// Get state storage from backend config to manage state
stateStorage, err := backend.BackendFromConfig(project.Backend, o.BackendOps, o.WorkDir)
// Get state storage from cli backend options, environment variables, workspace backend configs
stateStorage, err := backend.NewStateStorage(stack, &o.BackendOptions)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/preview/options_test.go
Expand Up @@ -47,7 +47,7 @@ var (
)

func Test_preview(t *testing.T) {
stateStorage := &local.FileSystemState{Path: filepath.Join("", local.KusionState)}
stateStorage := &local.FileSystemState{Path: filepath.Join("", local.KusionStateFileFile)}
t.Run("preview success", func(t *testing.T) {
m := mockOperationPreview()
defer m.UnPatch()
Expand Down
127 changes: 0 additions & 127 deletions pkg/engine/backend/backend.go

This file was deleted.

0 comments on commit 1dd8aa2

Please sign in to comment.