Skip to content

Commit

Permalink
feat: remove state model to pkg apis/core/v1 and stateStorage interfa…
Browse files Browse the repository at this point in the history
…ce to pkg state
  • Loading branch information
healthjyk committed Mar 11, 2024
1 parent c0c6265 commit a529b96
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 214 deletions.
53 changes: 53 additions & 0 deletions pkg/apis/core/v1/state.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package v1

import (
"time"

"kusionstack.io/kusion/pkg/version"
)

// State is a record of an operation's result. It is a mapping between resources in KCL and the actual infra
// resource and often used as a datasource for 3-way merge/diff in operations like Apply or Preview.
type State struct {
// State ID
ID int64 `json:"id" yaml:"id"`

// Project name
Project string `json:"project" yaml:"project"`

// Stack name
Stack string `json:"stack" yaml:"stack"`

// Workspace name
Workspace string `json:"workspace" yaml:"workspace"`

// State version
Version int `json:"version" yaml:"version"`

// KusionVersion represents the Kusion's version when this State is created
KusionVersion string `json:"kusionVersion" yaml:"kusionVersion"`

// Serial is an auto-increase number that represents how many times this State is modified
Serial uint64 `json:"serial" yaml:"serial"`

// Operator represents the person who triggered this operation
Operator string `json:"operator,omitempty" yaml:"operator,omitempty"`

// Resources records all resources in this operation
Resources Resources `json:"resources" yaml:"resources"`

// CreateTime is the time State is created
CreateTime time.Time `json:"createTime" yaml:"createTime"`

// ModifiedTime is the time State is modified each time
ModifiedTime time.Time `json:"modifiedTime,omitempty" yaml:"modifiedTime"`
}

func NewState() *State {
s := &State{
KusionVersion: version.ReleaseVersion(),
Version: 1,
Resources: []Resource{},
}
return s
}
14 changes: 14 additions & 0 deletions pkg/engine/state/storage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package state

import (
v1 "kusionstack.io/kusion/pkg/apis/core/v1"
)

// Storage represents the set of methods to manipulate State in a specified storage.
type Storage interface {
// Get returns the latest state, if the state does not exist, return nil.
Get() (*v1.State, error)

// Apply updates the state if already exists, or create a new state.
Apply(state *v1.State) error
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,10 @@ package states
import (
"time"

apiv1 "kusionstack.io/kusion/pkg/apis/core/v1"
v1 "kusionstack.io/kusion/pkg/apis/core/v1"
"kusionstack.io/kusion/pkg/version"
)

// StateStorage represents the set of methods to manipulate State in a specified storage
type StateStorage interface {
// GetLatestState return nil if state not exists
GetLatestState(query *StateQuery) (*State, error)

// Apply means update this state if it already exists or create a new one
Apply(state *State) error

// Delete State by id
Delete(id string) error
}

type StateQuery struct {
// Tenant name
Tenant string `json:"tenant"`
Expand Down Expand Up @@ -64,7 +52,7 @@ type State struct {
Operator string `json:"operator,omitempty" yaml:"operator,omitempty"`

// Resources records all resources in this operation
Resources apiv1.Resources `json:"resources" yaml:"resources"`
Resources v1.Resources `json:"resources" yaml:"resources"`

// CreateTime is the time State is created
CreateTime time.Time `json:"createTime" yaml:"createTime"`
Expand All @@ -77,7 +65,19 @@ func NewState() *State {
s := &State{
KusionVersion: version.ReleaseVersion(),
Version: 1,
Resources: []apiv1.Resource{},
Resources: []v1.Resource{},
}
return s
}

// StateStorage represents the set of methods to manipulate State in a specified storage
type StateStorage interface {
// GetLatestState return nil if state not exists
GetLatestState(query *StateQuery) (*State, error)

// Apply means update this state if it already exists or create a new one
Apply(state *State) error

// Delete State by id
Delete(id string) error
}
199 changes: 0 additions & 199 deletions pkg/engine/states/state_test.go

This file was deleted.

0 comments on commit a529b96

Please sign in to comment.