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: remove state model to pkg apis/core/v1 and stateStorage interface to pkg state #897

Merged
merged 1 commit into from
Mar 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
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
}
15 changes: 15 additions & 0 deletions pkg/engine/state/storage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package state

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

// Storage is used to provide the state storage for a set of real resources belonging to a specified stack,
// 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)

// 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.

Loading