From 750c17a19e5a20f0ab31d69e12a4f2d7dd0f2a14 Mon Sep 17 00:00:00 2001 From: healthjyk Date: Wed, 10 Apr 2024 20:14:29 +0800 Subject: [PATCH] refactor: sign state to deprecated state --- pkg/apis/api.kusion.io/v1/types.go | 23 ++++++++++--------- pkg/cmd/destroy/options.go | 2 +- pkg/cmd/destroy/options_test.go | 2 +- pkg/engine/operation/apply.go | 4 ++-- pkg/engine/operation/apply_test.go | 4 ++-- pkg/engine/operation/destory_test.go | 4 ++-- pkg/engine/operation/diff.go | 2 +- .../operation/graph/resource_node_test.go | 2 +- .../operation/models/operation_context.go | 12 +++++----- pkg/engine/operation/preview.go | 2 +- pkg/engine/operation/preview_test.go | 2 +- pkg/engine/runtime/terraform/tfops/state.go | 2 +- .../runtime/terraform/tfops/workspace.go | 2 +- pkg/engine/state/storage.go | 4 ++-- pkg/engine/state/storages/local.go | 6 ++--- pkg/engine/state/storages/local_test.go | 8 +++---- pkg/engine/state/storages/mysql.go | 10 ++++---- pkg/engine/state/storages/mysql_test.go | 4 ++-- pkg/engine/state/storages/oss.go | 6 ++--- pkg/engine/state/storages/oss_test.go | 4 ++-- pkg/engine/state/storages/s3.go | 6 ++--- pkg/engine/state/storages/s3_test.go | 4 ++-- 22 files changed, 58 insertions(+), 57 deletions(-) diff --git a/pkg/apis/api.kusion.io/v1/types.go b/pkg/apis/api.kusion.io/v1/types.go index 4a4b3539..0013d370 100644 --- a/pkg/apis/api.kusion.io/v1/types.go +++ b/pkg/apis/api.kusion.io/v1/types.go @@ -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"` @@ -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 @@ -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 @@ -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{}, diff --git a/pkg/cmd/destroy/options.go b/pkg/cmd/destroy/options.go index 13e1d483..c287e491 100644 --- a/pkg/cmd/destroy/options.go +++ b/pkg/cmd/destroy/options.go @@ -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 diff --git a/pkg/cmd/destroy/options_test.go b/pkg/cmd/destroy/options_test.go index 318d41be..3ae4eefc 100644 --- a/pkg/cmd/destroy/options_test.go +++ b/pkg/cmd/destroy/options_test.go @@ -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) { diff --git a/pkg/engine/operation/apply.go b/pkg/engine/operation/apply.go index 46161709..8b1459fa 100644 --- a/pkg/engine/operation/apply.go +++ b/pkg/engine/operation/apply.go @@ -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{}) diff --git a/pkg/engine/operation/apply_test.go b/pkg/engine/operation/apply_test.go index af1d01d9..28aeba5c 100644 --- a/pkg/engine/operation/apply_test.go +++ b/pkg/engine/operation/apply_test.go @@ -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 { @@ -87,7 +87,7 @@ func TestOperation_Apply(t *testing.T) { }, }} - rs := &apiv1.State{ + rs := &apiv1.DeprecatedState{ ID: 0, Stack: "fake-stack", Project: "fake-project", diff --git a/pkg/engine/operation/destory_test.go b/pkg/engine/operation/destory_test.go index 1db22a08..af987ee5 100644 --- a/pkg/engine/operation/destory_test.go +++ b/pkg/engine/operation/destory_test.go @@ -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() @@ -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) diff --git a/pkg/engine/operation/diff.go b/pkg/engine/operation/diff.go index b5c5e2bf..fff3f625 100644 --- a/pkg/engine/operation/diff.go +++ b/pkg/engine/operation/diff.go @@ -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 diff --git a/pkg/engine/operation/graph/resource_node_test.go b/pkg/engine/operation/graph/resource_node_test.go index df9a3e02..28f8a511 100644 --- a/pkg/engine/operation/graph/resource_node_test.go +++ b/pkg/engine/operation/graph/resource_node_test.go @@ -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() diff --git a/pkg/engine/operation/models/operation_context.go b/pkg/engine/operation/models/operation_context.go index b1de40c0..3c199788 100644 --- a/pkg/engine/operation/models/operation_context.go +++ b/pkg/engine/operation/models/operation_context.go @@ -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 { @@ -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 { @@ -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 @@ -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 } diff --git a/pkg/engine/operation/preview.go b/pkg/engine/operation/preview.go index ff80b95c..bf935310 100644 --- a/pkg/engine/operation/preview.go +++ b/pkg/engine/operation/preview.go @@ -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 ) diff --git a/pkg/engine/operation/preview_test.go b/pkg/engine/operation/preview_test.go index f314175e..e0d12877 100644 --- a/pkg/engine/operation/preview_test.go +++ b/pkg/engine/operation/preview_test.go @@ -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 { diff --git a/pkg/engine/runtime/terraform/tfops/state.go b/pkg/engine/runtime/terraform/tfops/state.go index 1ec0efcf..f66ea92d 100644 --- a/pkg/engine/runtime/terraform/tfops/state.go +++ b/pkg/engine/runtime/terraform/tfops/state.go @@ -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{} diff --git a/pkg/engine/runtime/terraform/tfops/workspace.go b/pkg/engine/runtime/terraform/tfops/workspace.go index 7333fa03..b0eb8774 100644 --- a/pkg/engine/runtime/terraform/tfops/workspace.go +++ b/pkg/engine/runtime/terraform/tfops/workspace.go @@ -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) diff --git a/pkg/engine/state/storage.go b/pkg/engine/state/storage.go index 1ef62943..87d201c4 100644 --- a/pkg/engine/state/storage.go +++ b/pkg/engine/state/storage.go @@ -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 } diff --git a/pkg/engine/state/storages/local.go b/pkg/engine/state/storages/local.go index f7672b36..3b5e58d3 100644 --- a/pkg/engine/state/storages/local.go +++ b/pkg/engine/state/storages/local.go @@ -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 @@ -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) } diff --git a/pkg/engine/state/storages/local_test.go b/pkg/engine/state/storages/local_test.go index d39bfa21..a7fa3491 100644 --- a/pkg/engine/state/storages/local_test.go +++ b/pkg/engine/state/storages/local_test.go @@ -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", @@ -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", @@ -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", diff --git a/pkg/engine/state/storages/mysql.go b/pkg/engine/state/storages/mysql.go index 9a899fde..b9125b57 100644 --- a/pkg/engine/state/storages/mysql.go +++ b/pkg/engine/state/storages/mysql.go @@ -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 @@ -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 @@ -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 @@ -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 } diff --git a/pkg/engine/state/storages/mysql_test.go b/pkg/engine/state/storages/mysql_test.go index bdbe1f09..fde543b7 100644 --- a/pkg/engine/state/storages/mysql_test.go +++ b/pkg/engine/state/storages/mysql_test.go @@ -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", @@ -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", diff --git a/pkg/engine/state/storages/oss.go b/pkg/engine/state/storages/oss.go index a957b8dd..3b708395 100644 --- a/pkg/engine/state/storages/oss.go +++ b/pkg/engine/state/storages/oss.go @@ -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 { @@ -51,7 +51,7 @@ 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 @@ -59,7 +59,7 @@ func (s *OssStorage) Get() (*v1.State, error) { 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 diff --git a/pkg/engine/state/storages/oss_test.go b/pkg/engine/state/storages/oss_test.go index 384ce555..b3cf5d77 100644 --- a/pkg/engine/state/storages/oss_test.go +++ b/pkg/engine/state/storages/oss_test.go @@ -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", @@ -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", diff --git a/pkg/engine/state/storages/s3.go b/pkg/engine/state/storages/s3.go index 3872c5e6..ef583fc3 100644 --- a/pkg/engine/state/storages/s3.go +++ b/pkg/engine/state/storages/s3.go @@ -29,7 +29,7 @@ func NewS3Storage(s3 *s3.S3, bucket, key string) *S3Storage { } } -func (s *S3Storage) Get() (*v1.State, error) { +func (s *S3Storage) Get() (*v1.DeprecatedState, error) { input := &s3.GetObjectInput{ Bucket: aws.String(s.bucket), Key: &s.key, @@ -55,7 +55,7 @@ func (s *S3Storage) Get() (*v1.State, error) { return nil, nil } - state := &v1.State{} + state := &v1.DeprecatedState{} err = yaml.Unmarshal(content, state) if err != nil { return nil, err @@ -63,7 +63,7 @@ func (s *S3Storage) Get() (*v1.State, error) { return state, nil } -func (s *S3Storage) Apply(state *v1.State) error { +func (s *S3Storage) Apply(state *v1.DeprecatedState) error { content, err := yaml.Marshal(state) if err != nil { return err diff --git a/pkg/engine/state/storages/s3_test.go b/pkg/engine/state/storages/s3_test.go index 37cb304e..2575231a 100644 --- a/pkg/engine/state/storages/s3_test.go +++ b/pkg/engine/state/storages/s3_test.go @@ -21,7 +21,7 @@ func TestS3Storage_Get(t *testing.T) { name string success bool content []byte - state *v1.State + state *v1.DeprecatedState }{ { name: "get s3 state successfully", @@ -55,7 +55,7 @@ func TestS3Storage_Apply(t *testing.T) { testcases := []struct { name string success bool - state *v1.State + state *v1.DeprecatedState }{ { name: "apply s3 state successfully",