Skip to content

Commit

Permalink
refactor: reorganize states directory
Browse files Browse the repository at this point in the history
fix: add yaml tags to Spec
  • Loading branch information
SparkYuan committed Jul 4, 2022
1 parent b02b252 commit 8944778
Show file tree
Hide file tree
Showing 22 changed files with 116 additions and 111 deletions.
10 changes: 5 additions & 5 deletions pkg/engine/models/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ type Resources []Resource

type Resource struct {
// ID is the unique key of this resource in the whole State. ApiVersion:Kind:Namespace:Name is an idiomatic way of Kubernetes resources.
ID string `json:"id"`
ID string `json:"id" yaml:"id"`

// Type represents all Runtimes we supported
Type Type `json:"type"`
Type Type `json:"type" yaml:"type"`

// Attributes represents all specified attributes of this resource
Attributes map[string]interface{} `json:"attributes"`
Attributes map[string]interface{} `json:"attributes" yaml:"attributes"`

// DependsOn contains all resources this resource depends on
DependsOn []string `json:"dependsOn,omitempty"`
DependsOn []string `json:"dependsOn,omitempty" yaml:"dependsOn,omitempty"`

// Extensions specifies arbitrary metadata of this resource
Extensions map[string]interface{} `json:"extensions,omitempty"`
Extensions map[string]interface{} `json:"extensions,omitempty" yaml:"extensions,omitempty"`
}

func (r *Resource) ResourceKey() string {
Expand Down
2 changes: 1 addition & 1 deletion pkg/engine/models/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ package models

// Spec represents desired state of resources in one stack and will be applied to the actual infrastructure by the Kusion Engine
type Spec struct {
Resources Resources `json:"resources"`
Resources Resources `json:"resources" yaml:"resources"`
}
4 changes: 3 additions & 1 deletion pkg/engine/operation/apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"sync"
"testing"

"kusionstack.io/kusion/pkg/engine/states/local"

opsmodels "kusionstack.io/kusion/pkg/engine/operation/models"

"kusionstack.io/kusion/pkg/engine/operation/graph"
Expand Down Expand Up @@ -119,7 +121,7 @@ func TestOperation_Apply(t *testing.T) {
name: "apply test",
fields: fields{
OperationType: types.Apply,
StateStorage: &states.FileSystemState{Path: filepath.Join("test_data", states.KusionState)},
StateStorage: &local.FileSystemState{Path: filepath.Join("test_data", local.KusionState)},
Runtime: &runtime.KubernetesRuntime{},
MsgCh: make(chan opsmodels.Message, 5),
},
Expand Down
5 changes: 3 additions & 2 deletions pkg/engine/operation/destory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"path/filepath"
"testing"

"kusionstack.io/kusion/pkg/engine/states/local"

opsmodels "kusionstack.io/kusion/pkg/engine/operation/models"

"kusionstack.io/kusion/pkg/engine/operation/graph"
Expand All @@ -19,7 +21,6 @@ import (

"kusionstack.io/kusion/pkg/engine/models"
"kusionstack.io/kusion/pkg/engine/runtime"
"kusionstack.io/kusion/pkg/engine/states"
"kusionstack.io/kusion/pkg/status"
)

Expand All @@ -42,7 +43,7 @@ func TestOperation_Destroy(t *testing.T) {
o := &DestroyOperation{
opsmodels.Operation{
OperationType: types.Destroy,
StateStorage: &states.FileSystemState{Path: filepath.Join("test_data", states.KusionState)},
StateStorage: &local.FileSystemState{Path: filepath.Join("test_data", local.KusionState)},
Runtime: &runtime.KubernetesRuntime{},
},
}
Expand Down
10 changes: 6 additions & 4 deletions pkg/engine/operation/graph/resource_node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"sync"
"testing"

"kusionstack.io/kusion/pkg/engine/states/local"

opsmodels "kusionstack.io/kusion/pkg/engine/operation/models"

"kusionstack.io/kusion/pkg/engine/operation/types"
Expand Down Expand Up @@ -106,7 +108,7 @@ func TestResourceNode_Execute(t *testing.T) {
},
args: args{operation: opsmodels.Operation{
OperationType: types.Apply,
StateStorage: states.NewFileSystemState(),
StateStorage: local.NewFileSystemState(),
CtxResourceIndex: priorStateResourceIndex,
PriorStateResourceIndex: priorStateResourceIndex,
StateResourceIndex: priorStateResourceIndex,
Expand All @@ -126,7 +128,7 @@ func TestResourceNode_Execute(t *testing.T) {
},
args: args{operation: opsmodels.Operation{
OperationType: types.Apply,
StateStorage: states.NewFileSystemState(),
StateStorage: local.NewFileSystemState(),
CtxResourceIndex: priorStateResourceIndex,
PriorStateResourceIndex: priorStateResourceIndex,
StateResourceIndex: priorStateResourceIndex,
Expand All @@ -146,7 +148,7 @@ func TestResourceNode_Execute(t *testing.T) {
},
args: args{operation: opsmodels.Operation{
OperationType: types.Apply,
StateStorage: states.NewFileSystemState(),
StateStorage: local.NewFileSystemState(),
CtxResourceIndex: priorStateResourceIndex,
PriorStateResourceIndex: priorStateResourceIndex,
StateResourceIndex: priorStateResourceIndex,
Expand Down Expand Up @@ -180,7 +182,7 @@ func TestResourceNode_Execute(t *testing.T) {
return resourceState, nil
})
monkey.PatchInstanceMethod(reflect.TypeOf(tt.args.operation.StateStorage), "Apply",
func(f *states.FileSystemState, state *states.State) error {
func(f *local.FileSystemState, state *states.State) error {
return nil
})
defer monkey.UnpatchAll()
Expand Down
10 changes: 6 additions & 4 deletions pkg/engine/operation/preview_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"sync"
"testing"

"kusionstack.io/kusion/pkg/engine/states/local"

opsmodels "kusionstack.io/kusion/pkg/engine/operation/models"

"kusionstack.io/kusion/pkg/engine/operation/types"
Expand Down Expand Up @@ -90,7 +92,7 @@ func TestOperation_Preview(t *testing.T) {
fields: fields{
OperationType: types.ApplyPreview,
Runtime: &fakePreviewRuntime{},
StateStorage: &states.FileSystemState{Path: states.KusionState},
StateStorage: &local.FileSystemState{Path: local.KusionState},
Order: &opsmodels.ChangeOrder{StepKeys: []string{}, ChangeSteps: map[string]*opsmodels.ChangeStep{}},
},
args: args{
Expand Down Expand Up @@ -129,7 +131,7 @@ func TestOperation_Preview(t *testing.T) {
fields: fields{
OperationType: types.DestroyPreview,
Runtime: &fakePreviewRuntime{},
StateStorage: &states.FileSystemState{Path: states.KusionState},
StateStorage: &local.FileSystemState{Path: local.KusionState},
Order: &opsmodels.ChangeOrder{},
},
args: args{
Expand Down Expand Up @@ -168,7 +170,7 @@ func TestOperation_Preview(t *testing.T) {
fields: fields{
OperationType: types.ApplyPreview,
Runtime: &fakePreviewRuntime{},
StateStorage: &states.FileSystemState{Path: states.KusionState},
StateStorage: &local.FileSystemState{Path: local.KusionState},
Order: &opsmodels.ChangeOrder{},
},
args: args{
Expand All @@ -186,7 +188,7 @@ func TestOperation_Preview(t *testing.T) {
fields: fields{
OperationType: types.ApplyPreview,
Runtime: &fakePreviewRuntime{},
StateStorage: &states.FileSystemState{Path: states.KusionState},
StateStorage: &local.FileSystemState{Path: local.KusionState},
Order: &opsmodels.ChangeOrder{},
},
args: args{
Expand Down
2 changes: 2 additions & 0 deletions pkg/engine/states/backend.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package states

// TODO: We will refactor this file with StateStorage later

var Backends = make(map[string]func() StateStorage)

func AddToBackends(name string, storage func() StateStorage) {
Expand Down
27 changes: 0 additions & 27 deletions pkg/engine/states/backend_test.go

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package states
package local

import (
"encoding/json"
Expand All @@ -7,24 +7,26 @@ import (
"os"
"time"

"kusionstack.io/kusion/pkg/engine/states"

"gopkg.in/yaml.v3"

"github.com/zclconf/go-cty/cty"
"kusionstack.io/kusion/pkg/log"
)

func init() {
AddToBackends("local", NewFileSystemState)
states.AddToBackends("local", NewFileSystemState)
}

var _ StateStorage = &FileSystemState{}
var _ states.StateStorage = &FileSystemState{}

type FileSystemState struct {
// state Path is in the same dir where command line is invoked
Path string
}

func NewFileSystemState() StateStorage {
func NewFileSystemState() states.StateStorage {
return &FileSystemState{}
}

Expand All @@ -47,7 +49,7 @@ func (f *FileSystemState) Configure(obj cty.Value) error {
return nil
}

func (f *FileSystemState) GetLatestState(query *StateQuery) (*State, error) {
func (f *FileSystemState) GetLatestState(query *states.StateQuery) (*states.State, error) {
// parse state
file, err := os.OpenFile(f.Path, os.O_RDWR|os.O_CREATE, fs.ModePerm)
if err != nil {
Expand All @@ -61,7 +63,7 @@ func (f *FileSystemState) GetLatestState(query *StateQuery) (*State, error) {
}

if len(jsonFile) != 0 {
state := &State{}
state := &states.State{}
// JSON is a subset of YAML.
// We are using yaml.Unmarshal here (instead of json.Unmarshal) because the
// Go JSON library doesn't try to pick the right number type (int, float,
Expand All @@ -78,7 +80,7 @@ func (f *FileSystemState) GetLatestState(query *StateQuery) (*State, error) {
}
}

func (f *FileSystemState) Apply(state *State) error {
func (f *FileSystemState) Apply(state *states.State) error {
now := time.Now()
state.CreatTime = now
state.ModifiedTime = now
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//go:build !arm64
// +build !arm64

package states
package local

import (
"io/fs"
Expand All @@ -10,6 +10,8 @@ import (
"reflect"
"testing"

"kusionstack.io/kusion/pkg/engine/states"

"bou.ke/monkey"
"github.com/stretchr/testify/assert"
"github.com/zclconf/go-cty/cty"
Expand All @@ -29,7 +31,7 @@ func TestMain(m *testing.M) {
func TestNewFileSystemState(t *testing.T) {
tests := []struct {
name string
want StateStorage
want states.StateStorage
}{
{
name: "t1",
Expand Down Expand Up @@ -120,13 +122,13 @@ func TestFileSystemState_GetLatestState(t *testing.T) {
Path string
}
type args struct {
query *StateQuery
query *states.StateQuery
}
tests := []struct {
name string
fields fields
args args
want *State
want *states.State
wantErr bool
}{
{
Expand All @@ -135,7 +137,7 @@ func TestFileSystemState_GetLatestState(t *testing.T) {
Path: stateFile,
},
args: args{
query: &StateQuery{},
query: &states.StateQuery{},
},
want: nil,
wantErr: false,
Expand Down Expand Up @@ -173,7 +175,7 @@ func TestFileSystemState(t *testing.T) {
defer monkey.UnpatchAll()
fileSystemState := FileSystemStateSetUp(t)

state := &State{Tenant: "test_global_tenant", Project: "test_project", Stack: "test_env"}
state := &states.State{Tenant: "test_global_tenant", Project: "test_project", Stack: "test_env"}
err := fileSystemState.Apply(state)
assert.NoError(t, err)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package states
package remote

import (
"database/sql"
Expand All @@ -8,6 +8,8 @@ import (
"net/url"
"sort"

"kusionstack.io/kusion/pkg/engine/states"

"kusionstack.io/kusion/pkg/engine/models"

"github.com/didi/gendry/scanner"
Expand All @@ -24,12 +26,12 @@ import (
)

func init() {
AddToBackends("db", NewDBState)
states.AddToBackends("db", NewDBState)
}

var _ StateStorage = &DBState{}
var _ states.StateStorage = &DBState{}

func NewDBState() StateStorage {
func NewDBState() states.StateStorage {
result := &DBState{}
return result
}
Expand Down Expand Up @@ -82,7 +84,7 @@ func (s *DBState) Configure(obj cty.Value) error {
}

// Apply save state in DB by add-only strategy.
func (s *DBState) Apply(state *State) error {
func (s *DBState) Apply(state *states.State) error {
m := make(map[string]interface{})
sort.Stable(state.Resources)
marshal, err := json.Marshal(state)
Expand All @@ -101,7 +103,7 @@ func (s *DBState) Delete(id string) error {
panic("implement me")
}

func (s *DBState) GetLatestState(q *StateQuery) (*State, error) {
func (s *DBState) GetLatestState(q *states.StateQuery) (*states.State, error) {
where := make(map[string]interface{})

if len(q.Tenant) == 0 {
Expand Down Expand Up @@ -131,13 +133,13 @@ func (s *DBState) GetLatestState(q *StateQuery) (*State, error) {
return res, err
}

func do2Bo(dbState *mapper.StateDO) *State {
func do2Bo(dbState *mapper.StateDO) *states.State {
var resStateList []models.Resource

// JSON is a subset of YAML. Please check FileSystemState.GetLatestState for detail explanation
parseErr := yaml.Unmarshal([]byte(dbState.Resources), &resStateList)
util.CheckNotError(parseErr, fmt.Sprintf("marshall stateDO.resources failed:%v", dbState.Resources))
res := NewState()
res := states.NewState()
e := copier.Copy(res, dbState)
util.CheckNotError(e,
fmt.Sprintf("copy db_state to State failed. db_state:%v", jsonutil.MustMarshal2String(dbState)))
Expand Down
Loading

0 comments on commit 8944778

Please sign in to comment.