Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: sign state to deprecated state #1031

Merged
merged 1 commit into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions pkg/apis/api.kusion.io/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ type Resources []Resource

// Resource is the representation of a resource in the state.
type Resource struct {
// ID is the unique key of this resource in the whole State.
// ID is the unique key of this resource in the whole DeprecatedState.
// ApiVersion:Kind:Namespace:Name is an idiomatic way for Kubernetes resources.
// providerNamespace:providerName:resourceType:resourceName for Terraform resources
ID string `json:"id" yaml:"id"`
Expand All @@ -303,10 +303,11 @@ type Resource struct {
Extensions map[string]interface{} `json:"extensions,omitempty" yaml:"extensions,omitempty"`
}

// State is a record of an operation's result. It is a mapping between resources in KCL and the actual infra
// DeprecatedState 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
// Deprecated: DeprecatedState will not in use in time
type DeprecatedState struct {
// DeprecatedState ID
ID int64 `json:"id" yaml:"id"`

// Project name
Expand All @@ -318,13 +319,13 @@ type State struct {
// Workspace name
Workspace string `json:"workspace" yaml:"workspace"`

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

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

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

// Operator represents the person who triggered this operation
Expand All @@ -333,15 +334,15 @@ type State struct {
// Resources records all resources in this operation
Resources Resources `json:"resources" yaml:"resources"`

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

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

func NewState() *State {
s := &State{
func NewState() *DeprecatedState {
s := &DeprecatedState{
KusionVersion: version.ReleaseVersion(),
Version: 1,
Resources: []Resource{},
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/destroy/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (o *Options) Run() error {
priorState, err := storage.Get()
if err != nil || priorState == nil {
log.Infof("can't find state with project: %s, stack: %s, workspace: %s", proj.Name, stack.Name, o.Workspace)
return fmt.Errorf("can not find State in this stack")
return fmt.Errorf("can not find DeprecatedState in this stack")
}
destroyResources := priorState.Resources

Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/destroy/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func mockDetectProjectAndStack() {
}

func mockGetState() {
mockey.Mock((*statestorages.LocalStorage).Get).Return(&apiv1.State{Resources: []apiv1.Resource{sa1}}, nil).Build()
mockey.Mock((*statestorages.LocalStorage).Get).Return(&apiv1.DeprecatedState{Resources: []apiv1.Resource{sa1}}, nil).Build()
}

func Test_preview(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions pkg/engine/operation/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ type ApplyRequest struct {
}

type ApplyResponse struct {
State *apiv1.State
State *apiv1.DeprecatedState
}

func NewApplyGraph(m *apiv1.Spec, priorState *apiv1.State) (*dag.AcyclicGraph, v1.Status) {
func NewApplyGraph(m *apiv1.Spec, priorState *apiv1.DeprecatedState) (*dag.AcyclicGraph, v1.Status) {
intentParser := parser.NewIntentParser(m)
g := &dag.AcyclicGraph{}
g.Add(&graph.RootNode{})
Expand Down
4 changes: 2 additions & 2 deletions pkg/engine/operation/apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func TestOperation_Apply(t *testing.T) {
RuntimeMap map[apiv1.Type]runtime.Runtime
Stack *apiv1.Stack
MsgCh chan models.Message
resultState *apiv1.State
resultState *apiv1.DeprecatedState
lock *sync.Mutex
}
type args struct {
Expand All @@ -87,7 +87,7 @@ func TestOperation_Apply(t *testing.T) {
},
}}

rs := &apiv1.State{
rs := &apiv1.DeprecatedState{
ID: 0,
Stack: "fake-stack",
Project: "fake-project",
Expand Down
4 changes: 2 additions & 2 deletions pkg/engine/operation/destory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func TestOperation_Destroy(t *testing.T) {

mockey.PatchConvey("destroy success", t, func() {
mockey.Mock((*graph.ResourceNode).Execute).Return(nil).Build()
mockey.Mock((*storages.LocalStorage).Get).Return(&apiv1.State{Resources: []apiv1.Resource{resourceState}}, nil).Build()
mockey.Mock((*storages.LocalStorage).Get).Return(&apiv1.DeprecatedState{Resources: []apiv1.Resource{resourceState}}, nil).Build()
mockey.Mock(kubernetes.NewKubernetesRuntime).To(func() (runtime.Runtime, error) {
return &fakerRuntime{}, nil
}).Build()
Expand All @@ -72,7 +72,7 @@ func TestOperation_Destroy(t *testing.T) {

mockey.PatchConvey("destroy failed", t, func() {
mockey.Mock((*graph.ResourceNode).Execute).Return(v1.NewErrorStatus(errors.New("mock error"))).Build()
mockey.Mock((*storages.LocalStorage).Get).Return(&apiv1.State{Resources: []apiv1.Resource{resourceState}}, nil).Build()
mockey.Mock((*storages.LocalStorage).Get).Return(&apiv1.DeprecatedState{Resources: []apiv1.Resource{resourceState}}, nil).Build()
mockey.Mock(kubernetes.NewKubernetesRuntime).Return(&fakerRuntime{}, nil).Build()

o.MsgCh = make(chan models.Message, 1)
Expand Down
2 changes: 1 addition & 1 deletion pkg/engine/operation/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (d *Diff) Diff(request *DiffRequest) (string, error) {
return DiffWithRequestResourceAndState(plan, priorState)
}

func DiffWithRequestResourceAndState(plan *v1.Spec, priorState *v1.State) (string, error) {
func DiffWithRequestResourceAndState(plan *v1.Spec, priorState *v1.DeprecatedState) (string, error) {
planString := json.MustMarshal2String(plan.Resources)
var report *dyff.Report
var err error
Expand Down
2 changes: 1 addition & 1 deletion pkg/engine/operation/graph/resource_node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func TestResourceNode_Execute(t *testing.T) {
return &runtime.ReadResponse{Resource: request.PriorResource}
}).Build()
mockey.Mock(mockey.GetMethod(tt.args.operation.StateStorage, "Apply")).To(
func(f *storages.LocalStorage, state *apiv1.State) error {
func(f *storages.LocalStorage, state *apiv1.DeprecatedState) error {
return nil
}).Build()

Expand Down
12 changes: 6 additions & 6 deletions pkg/engine/operation/models/operation_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ type Operation struct {
// Lock is the operation-wide mutex
Lock *sync.Mutex

// ResultState is the final State build by this operation, and this State will be saved in the StateStorage
ResultState *v1.State
// ResultState is the final DeprecatedState build by this operation, and this DeprecatedState will be saved in the StateStorage
ResultState *v1.DeprecatedState
}

type Message struct {
Expand Down Expand Up @@ -95,7 +95,7 @@ func (o *Operation) RefreshResourceIndex(resourceKey string, resource *v1.Resour
return nil
}

func (o *Operation) InitStates(request *Request) (*v1.State, *v1.State) {
func (o *Operation) InitStates(request *Request) (*v1.DeprecatedState, *v1.DeprecatedState) {
priorState, err := o.StateStorage.Get()
util.CheckNotError(err, fmt.Sprintf("get state failed with request: %v", json.Marshal2PrettyString(request)))
if priorState == nil {
Expand All @@ -105,7 +105,7 @@ func (o *Operation) InitStates(request *Request) (*v1.State, *v1.State) {
resultState := v1.NewState()
resultState.Serial = priorState.Serial
err = copier.Copy(resultState, request)
util.CheckNotError(err, fmt.Sprintf("copy request to result State failed, request:%v", json.Marshal2PrettyString(request)))
util.CheckNotError(err, fmt.Sprintf("copy request to result DeprecatedState failed, request:%v", json.Marshal2PrettyString(request)))
resultState.Stack = request.Stack.Name
resultState.Project = request.Project.Name

Expand Down Expand Up @@ -139,8 +139,8 @@ func (o *Operation) UpdateState(resourceIndex map[string]*v1.Resource) error {
resultState.ModifiedTime = now
err := o.StateStorage.Apply(resultState)
if err != nil {
return fmt.Errorf("apply State failed. %w", err)
return fmt.Errorf("apply DeprecatedState failed. %w", err)
}
log.Infof("update State:%v success", resultState.ID)
log.Infof("update DeprecatedState:%v success", resultState.ID)
return nil
}
2 changes: 1 addition & 1 deletion pkg/engine/operation/preview.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (po *PreviewOperation) Preview(request *PreviewRequest) (rsp *PreviewRespon
}

var (
priorState, resultState *apiv1.State
priorState, resultState *apiv1.DeprecatedState
priorStateResourceIndex map[string]*apiv1.Resource
ag *dag.AcyclicGraph
)
Expand Down
2 changes: 1 addition & 1 deletion pkg/engine/operation/preview_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func TestOperation_Preview(t *testing.T) {
Order *models.ChangeOrder
RuntimeMap map[apiv1.Type]runtime.Runtime
MsgCh chan models.Message
resultState *apiv1.State
resultState *apiv1.DeprecatedState
lock *sync.Mutex
}
type args struct {
Expand Down
2 changes: 1 addition & 1 deletion pkg/engine/runtime/terraform/tfops/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ type resource struct {
// resource, whose structure depends on the resource type schema.
type attributeValues map[string]interface{}

// ConvertTFState convert Terraform State to kusion State
// ConvertTFState convert Terraform DeprecatedState to kusion DeprecatedState
func ConvertTFState(tfState *StateRepresentation, providerAddr string) v1.Resource {
if tfState == nil || tfState.Values == nil {
return v1.Resource{}
Expand Down
2 changes: 1 addition & 1 deletion pkg/engine/runtime/terraform/tfops/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ func (w *WorkSpace) show(ctx context.Context, fileName string) ([]byte, error) {
return out, nil
}

// RefreshOnly refresh Terraform State
// RefreshOnly refresh Terraform DeprecatedState
func (w *WorkSpace) RefreshOnly(ctx context.Context) (*StateRepresentation, error) {
chdir := fmt.Sprintf("-chdir=%s", w.tfCacheDir)
err := w.CleanAndInitWorkspace(ctx)
Expand Down
4 changes: 2 additions & 2 deletions pkg/engine/state/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
// which is determined by the combination of project name, stack name and workspace name.
type Storage interface {
// Get returns the state, if the state does not exist, return nil.
Get() (*v1.State, error)
Get() (*v1.DeprecatedState, error)

// Apply updates the state if already exists, or create a new state.
Apply(state *v1.State) error
Apply(state *v1.DeprecatedState) error
}
6 changes: 3 additions & 3 deletions pkg/engine/state/storages/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ func NewLocalStorage(path string) *LocalStorage {
return &LocalStorage{path: path}
}

func (s *LocalStorage) Get() (*v1.State, error) {
func (s *LocalStorage) Get() (*v1.DeprecatedState, error) {
content, err := os.ReadFile(s.path)
if err != nil && !os.IsNotExist(err) {
return nil, err
}

if len(content) != 0 {
state := &v1.State{}
state := &v1.DeprecatedState{}
err = yaml.Unmarshal(content, state)
if err != nil {
return nil, err
Expand All @@ -39,7 +39,7 @@ func (s *LocalStorage) Get() (*v1.State, error) {
}
}

func (s *LocalStorage) Apply(state *v1.State) error {
func (s *LocalStorage) Apply(state *v1.DeprecatedState) error {
if err := os.MkdirAll(filepath.Dir(s.path), os.ModePerm); err != nil {
fmt.Println(err)
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/engine/state/storages/local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
v1 "kusionstack.io/kusion/pkg/apis/api.kusion.io/v1"
)

func mockState() *v1.State {
return &v1.State{
func mockState() *v1.DeprecatedState {
return &v1.DeprecatedState{
ID: 1,
Project: "wordpress",
Stack: "dev",
Expand Down Expand Up @@ -69,7 +69,7 @@ func TestLocalStorage_Get(t *testing.T) {
name string
success bool
content []byte
state *v1.State
state *v1.DeprecatedState
}{
{
name: "get local state successfully",
Expand Down Expand Up @@ -100,7 +100,7 @@ func TestLocalStorage_Apply(t *testing.T) {
testcases := []struct {
name string
success bool
state *v1.State
state *v1.DeprecatedState
}{
{
name: "apply local state successfully",
Expand Down
10 changes: 5 additions & 5 deletions pkg/engine/state/storages/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func NewMysqlStorage(db *gorm.DB, project, stack, workspace string) *MysqlStorag
}
}

func (s *MysqlStorage) Get() (*v1.State, error) {
func (s *MysqlStorage) Get() (*v1.DeprecatedState, error) {
stateDO, err := getStateFromMysql(s.db, s.project, s.stack, s.workspace)
if err != nil {
return nil, err
Expand All @@ -35,7 +35,7 @@ func (s *MysqlStorage) Get() (*v1.State, error) {
return convertFromMysqlDO(stateDO)
}

func (s *MysqlStorage) Apply(state *v1.State) error {
func (s *MysqlStorage) Apply(state *v1.DeprecatedState) error {
exist, err := checkStateExistenceInMysql(s.db, s.project, s.stack, s.workspace)
if err != nil {
return err
Expand Down Expand Up @@ -106,7 +106,7 @@ func updateStateInMysql(db *gorm.DB, s *StateMysqlDO) error {
return db.Where(q).Updates(s).Error
}

func convertToMysqlDO(state *v1.State) (*StateMysqlDO, error) {
func convertToMysqlDO(state *v1.DeprecatedState) (*StateMysqlDO, error) {
content, err := yaml.Marshal(state)
if err != nil {
return nil, err
Expand All @@ -119,8 +119,8 @@ func convertToMysqlDO(state *v1.State) (*StateMysqlDO, error) {
}, nil
}

func convertFromMysqlDO(s *StateMysqlDO) (*v1.State, error) {
state := &v1.State{}
func convertFromMysqlDO(s *StateMysqlDO) (*v1.DeprecatedState, error) {
state := &v1.DeprecatedState{}
if err := yaml.Unmarshal([]byte(s.Content), state); err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/engine/state/storages/mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestMysqlStorage_Get(t *testing.T) {
name string
success bool
stateDO *StateMysqlDO
state *v1.State
state *v1.DeprecatedState
}{
{
name: "get mysql state successfully",
Expand Down Expand Up @@ -60,7 +60,7 @@ func TestMysqlStorage_Apply(t *testing.T) {
name string
success bool
lastStateDO *StateMysqlDO
state *v1.State
state *v1.DeprecatedState
}{
{
name: "update mysql state successfully",
Expand Down
6 changes: 3 additions & 3 deletions pkg/engine/state/storages/oss.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func NewOssStorage(bucket *oss.Bucket, key string) *OssStorage {
}
}

func (s *OssStorage) Get() (*v1.State, error) {
func (s *OssStorage) Get() (*v1.DeprecatedState, error) {
var exist bool
body, err := s.bucket.GetObject(s.key)
if err != nil {
Expand All @@ -51,15 +51,15 @@ func (s *OssStorage) Get() (*v1.State, error) {
return nil, nil
}

state := &v1.State{}
state := &v1.DeprecatedState{}
err = yaml.Unmarshal(content, state)
if err != nil {
return nil, err
}
return state, nil
}

func (s *OssStorage) Apply(state *v1.State) error {
func (s *OssStorage) Apply(state *v1.DeprecatedState) error {
content, err := yaml.Marshal(state)
if err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions pkg/engine/state/storages/oss_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestOssStorage_Get(t *testing.T) {
name string
success bool
content []byte
state *v1.State
state *v1.DeprecatedState
}{
{
name: "get oss state successfully",
Expand Down Expand Up @@ -53,7 +53,7 @@ func TestOssStorage_Apply(t *testing.T) {
testcases := []struct {
name string
success bool
state *v1.State
state *v1.DeprecatedState
}{
{
name: "apply oss state successfully",
Expand Down
Loading
Loading