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

feat: clean the usage of deprecated state and its storage #1133

Merged
merged 1 commit into from
May 27, 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
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ _python37_home_/
/VERSION
/kclopenapi
.kclvm/
kusion_state*.json
kusion_state*.yaml

# Vscode
__debug_bin
Expand Down
51 changes: 1 addition & 50 deletions pkg/apis/api.kusion.io/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ import (

"gopkg.in/yaml.v2"
v1 "k8s.io/api/core/v1"

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

// Project is a definition of Kusion project resource.
Expand Down Expand Up @@ -820,7 +818,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 DeprecatedState.
// ID is the unique key of this resource.
// ApiVersion:Kind:Namespace:Name is an idiomatic way for Kubernetes resources.
// providerNamespace:providerName:resourceType:resourceName for Terraform resources
ID string `yaml:"id" json:"id"`
Expand Down Expand Up @@ -908,50 +906,3 @@ type Release struct {
// ModifiedTime is the time that the Release is modified.
ModifiedTime time.Time `yaml:"modifiedTime" json:"modifiedTime"`
}

// 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.
// Deprecated: DeprecatedState will not in use in time
type DeprecatedState struct {
// DeprecatedState ID
ID int64 `yaml:"id" json:"id"`

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

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

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

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

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

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

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

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

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

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

func NewState() *DeprecatedState {
s := &DeprecatedState{
KusionVersion: version.ReleaseVersion(),
Version: 1,
Resources: []Resource{},
}
return s
}
4 changes: 0 additions & 4 deletions pkg/backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"kusionstack.io/kusion/pkg/backend/storages"
"kusionstack.io/kusion/pkg/config"
"kusionstack.io/kusion/pkg/engine/release"
"kusionstack.io/kusion/pkg/engine/state"
"kusionstack.io/kusion/pkg/workspace"
)

Expand All @@ -16,9 +15,6 @@ type Backend interface {
// WorkspaceStorage returns the workspace storage and init default workspace.
WorkspaceStorage() (workspace.Storage, error)

// StateStorage returns the state storage.
StateStorage(project, workspace string) state.Storage

// ReleaseStorage returns the release storage.
ReleaseStorage(project, workspace string) (release.Storage, error)
}
Expand Down
6 changes: 0 additions & 6 deletions pkg/backend/storages/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
v1 "kusionstack.io/kusion/pkg/apis/api.kusion.io/v1"
"kusionstack.io/kusion/pkg/engine/release"
releasestorages "kusionstack.io/kusion/pkg/engine/release/storages"
"kusionstack.io/kusion/pkg/engine/state"
statestorages "kusionstack.io/kusion/pkg/engine/state/storages"
"kusionstack.io/kusion/pkg/workspace"
workspacestorages "kusionstack.io/kusion/pkg/workspace/storages"
)
Expand All @@ -21,10 +19,6 @@ func NewLocalStorage(config *v1.BackendLocalConfig) *LocalStorage {
return &LocalStorage{path: config.Path}
}

func (s *LocalStorage) StateStorage(project, workspace string) state.Storage {
return statestorages.NewLocalStorage(statestorages.GenStateFilePath(s.path, project, workspace))
}

func (s *LocalStorage) WorkspaceStorage() (workspace.Storage, error) {
return workspacestorages.NewLocalStorage(workspacestorages.GenWorkspaceDirPath(s.path))
}
Expand Down
31 changes: 0 additions & 31 deletions pkg/backend/storages/local_test.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
package storages

import (
"path/filepath"
"testing"

"github.com/bytedance/mockey"
"github.com/stretchr/testify/assert"

v1 "kusionstack.io/kusion/pkg/apis/api.kusion.io/v1"
releasestorages "kusionstack.io/kusion/pkg/engine/release/storages"
"kusionstack.io/kusion/pkg/engine/state"
statestorages "kusionstack.io/kusion/pkg/engine/state/storages"
workspacestorages "kusionstack.io/kusion/pkg/workspace/storages"
)

Expand All @@ -35,34 +32,6 @@ func TestNewLocalStorage(t *testing.T) {
}
}

func TestLocalStorage_StateStorage(t *testing.T) {
testcases := []struct {
name string
localStorage *LocalStorage
project, workspace string
stateStorage state.Storage
}{
{
name: "state storage from local backend",
localStorage: &LocalStorage{
path: "kusion",
},
project: "wordpress",
workspace: "dev",
stateStorage: statestorages.NewLocalStorage(
filepath.Join("kusion", "states", "wordpress", "dev", "state.yaml"),
),
},
}

for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
stateStorage := tc.localStorage.StateStorage(tc.project, tc.workspace)
assert.Equal(t, tc.stateStorage, stateStorage)
})
}
}

func TestLocalStorage_WorkspaceStorage(t *testing.T) {
testcases := []struct {
name string
Expand Down
6 changes: 0 additions & 6 deletions pkg/backend/storages/oss.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
v1 "kusionstack.io/kusion/pkg/apis/api.kusion.io/v1"
"kusionstack.io/kusion/pkg/engine/release"
releasestorages "kusionstack.io/kusion/pkg/engine/release/storages"
"kusionstack.io/kusion/pkg/engine/state"
statestorages "kusionstack.io/kusion/pkg/engine/state/storages"
"kusionstack.io/kusion/pkg/workspace"
workspacestorages "kusionstack.io/kusion/pkg/workspace/storages"
)
Expand All @@ -33,10 +31,6 @@ func NewOssStorage(config *v1.BackendOssConfig) (*OssStorage, error) {
return &OssStorage{bucket: bucket, prefix: config.Prefix}, nil
}

func (s *OssStorage) StateStorage(project, workspace string) state.Storage {
return statestorages.NewOssStorage(s.bucket, statestorages.GenGenericOssStateFileKey(s.prefix, project, workspace))
}

func (s *OssStorage) WorkspaceStorage() (workspace.Storage, error) {
return workspacestorages.NewOssStorage(s.bucket, workspacestorages.GenGenericOssWorkspacePrefixKey(s.prefix))
}
Expand Down
32 changes: 0 additions & 32 deletions pkg/backend/storages/oss_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import (

v1 "kusionstack.io/kusion/pkg/apis/api.kusion.io/v1"
releasestorages "kusionstack.io/kusion/pkg/engine/release/storages"
"kusionstack.io/kusion/pkg/engine/state"
statestorages "kusionstack.io/kusion/pkg/engine/state/storages"
workspacestorages "kusionstack.io/kusion/pkg/workspace/storages"
)

Expand Down Expand Up @@ -45,36 +43,6 @@ func TestNewOssStorage(t *testing.T) {
}
}

func TestOssStorage_StateStorage(t *testing.T) {
testcases := []struct {
name string
ossStorage *OssStorage
project, workspace string
stateStorage state.Storage
}{
{
name: "state storage from oss backend",
ossStorage: &OssStorage{
bucket: &oss.Bucket{},
prefix: "kusion",
},
project: "wordpress",
workspace: "dev",
stateStorage: statestorages.NewOssStorage(
&oss.Bucket{},
"kusion/states/wordpress/dev/state.yaml",
),
},
}

for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
stateStorage := tc.ossStorage.StateStorage(tc.project, tc.workspace)
assert.Equal(t, tc.stateStorage, stateStorage)
})
}
}

func TestOssStorage_WorkspaceStorage(t *testing.T) {
testcases := []struct {
name string
Expand Down
6 changes: 0 additions & 6 deletions pkg/backend/storages/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import (
v1 "kusionstack.io/kusion/pkg/apis/api.kusion.io/v1"
"kusionstack.io/kusion/pkg/engine/release"
releasestorages "kusionstack.io/kusion/pkg/engine/release/storages"
"kusionstack.io/kusion/pkg/engine/state"
statestorages "kusionstack.io/kusion/pkg/engine/state/storages"
"kusionstack.io/kusion/pkg/workspace"
workspacestorages "kusionstack.io/kusion/pkg/workspace/storages"
)
Expand Down Expand Up @@ -46,10 +44,6 @@ func NewS3Storage(config *v1.BackendS3Config) (*S3Storage, error) {
}, nil
}

func (s *S3Storage) StateStorage(project, workspace string) state.Storage {
return statestorages.NewS3Storage(s.s3, s.bucket, statestorages.GenGenericOssStateFileKey(s.prefix, project, workspace))
}

func (s *S3Storage) WorkspaceStorage() (workspace.Storage, error) {
return workspacestorages.NewS3Storage(s.s3, s.bucket, workspacestorages.GenGenericOssWorkspacePrefixKey(s.prefix))
}
Expand Down
34 changes: 0 additions & 34 deletions pkg/backend/storages/s3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import (

v1 "kusionstack.io/kusion/pkg/apis/api.kusion.io/v1"
releasestorages "kusionstack.io/kusion/pkg/engine/release/storages"
"kusionstack.io/kusion/pkg/engine/state"
statestorages "kusionstack.io/kusion/pkg/engine/state/storages"
workspacestorages "kusionstack.io/kusion/pkg/workspace/storages"
)

Expand Down Expand Up @@ -47,38 +45,6 @@ func TestNewS3Storage(t *testing.T) {
}
}

func TestS3Storage_StateStorage(t *testing.T) {
testcases := []struct {
name string
s3Storage *S3Storage
project, workspace string
stateStorage state.Storage
}{
{
name: "state storage from s3 backend",
s3Storage: &S3Storage{
s3: &s3.S3{},
bucket: "infra",
prefix: "kusion",
},
project: "wordpress",
workspace: "dev",
stateStorage: statestorages.NewS3Storage(
&s3.S3{},
"infra",
"kusion/states/wordpress/dev/state.yaml",
),
},
}

for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
stateStorage := tc.s3Storage.StateStorage(tc.project, tc.workspace)
assert.Equal(t, tc.stateStorage, stateStorage)
})
}
}

func TestS3Storage_WorkspaceStorage(t *testing.T) {
testcases := []struct {
name string
Expand Down
6 changes: 1 addition & 5 deletions pkg/cmd/preview/preview_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package preview

import (
"context"
"path/filepath"
"testing"

"github.com/bytedance/mockey"
Expand Down Expand Up @@ -93,15 +92,12 @@ var (
)

func newPreviewOptions() *PreviewOptions {
storageBackend := storages.NewLocalStorage(&apiv1.BackendLocalConfig{
Path: filepath.Join("", "state.yaml"),
})
return &PreviewOptions{
MetaOptions: &meta.MetaOptions{
RefProject: proj,
RefStack: stack,
RefWorkspace: workspace,
Backend: storageBackend,
Backend: &storages.LocalStorage{},
},
}
}
Expand Down
16 changes: 0 additions & 16 deletions pkg/engine/api/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,6 @@ import (
//
// You can customize the runtime of engine and the state
// storage through `runtime` and `storage` parameters.
//
// Example:
//
// o := NewApplyOptions()
// stateStorage := &states.FileSystemState{
// Path: filepath.Join(o.WorkDir, states.KusionState)
// }
// kubernetesRuntime, err := runtime.NewKubernetesRuntime()
// if err != nil {
// return err
// }
//
// err = Apply(o, kubernetesRuntime, stateStorage, planResources, changes, os.Stdout)
// if err != nil {
// return err
// }
func Apply(
o *APIOptions,
storage release.Storage,
Expand Down
2 changes: 1 addition & 1 deletion pkg/engine/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
//
// 2. Walk this DAG:
// 1. Get the latest `State` from the actual infra by the `Runtime`
// 2. Get the last operation `State` from the `State` storage medium
// 2. Get the last operation `State` from the `Release` storage medium
//
// 3. Merge/Diff three states: desired state described in Intent, live state from `Runtime` and prior state from `State` storage medium, and return the diff result to the console
package engine
4 changes: 2 additions & 2 deletions pkg/engine/runtime/terraform/tfops/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

"github.com/zclconf/go-cty/cty"

"kusionstack.io/kusion/pkg/apis/api.kusion.io/v1"
v1 "kusionstack.io/kusion/pkg/apis/api.kusion.io/v1"
)

// StateRepresentation is the top-level representation of the json format of a terraform
Expand Down 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 DeprecatedState to kusion DeprecatedState
// ConvertTFState convert Terraform State to kusion State
func ConvertTFState(tfState *StateRepresentation, providerAddr string) v1.Resource {
if tfState == nil || tfState.Values == nil {
return v1.Resource{}
Expand Down
4 changes: 2 additions & 2 deletions pkg/engine/runtime/terraform/tfops/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/hashicorp/hcl/v2/hclparse"
"github.com/spf13/afero"

"kusionstack.io/kusion/pkg/apis/api.kusion.io/v1"
v1 "kusionstack.io/kusion/pkg/apis/api.kusion.io/v1"
"kusionstack.io/kusion/pkg/log"
"kusionstack.io/kusion/pkg/util/io"
jsonutil "kusionstack.io/kusion/pkg/util/json"
Expand Down Expand Up @@ -294,7 +294,7 @@ func (w *WorkSpace) show(ctx context.Context, fileName string) ([]byte, error) {
return out, nil
}

// RefreshOnly refresh Terraform DeprecatedState
// RefreshOnly refresh Terraform State
func (w *WorkSpace) RefreshOnly(ctx context.Context) (*StateRepresentation, error) {
chdir := fmt.Sprintf("-chdir=%s", w.tfCacheDir)
err := w.CleanAndInitWorkspace(ctx)
Expand Down
Loading
Loading