diff --git a/pkg/engine/operation/graph/resource_node.go b/pkg/engine/operation/graph/resource_node.go index 93437a9c..33f54462 100644 --- a/pkg/engine/operation/graph/resource_node.go +++ b/pkg/engine/operation/graph/resource_node.go @@ -142,11 +142,6 @@ func (rn *ResourceNode) applyResource(operation *opsmodels.Operation, priorState return s } - // compatible with delete action - if res != nil { - res.DependsOn = planedState.DependsOn - res.Extensions = planedState.Extensions - } key := rn.state.ResourceKey() if e := operation.RefreshResourceIndex(key, res, rn.Action); e != nil { return status.NewErrorStatus(e) diff --git a/pkg/engine/runtime/kubernetes_runtime.go b/pkg/engine/runtime/kubernetes_runtime.go index b8faebd2..7e26c39d 100644 --- a/pkg/engine/runtime/kubernetes_runtime.go +++ b/pkg/engine/runtime/kubernetes_runtime.go @@ -118,8 +118,10 @@ func (k *KubernetesRuntime) Apply(ctx context.Context, request *ApplyRequest) *A return &ApplyResponse{&models.Resource{ ID: planState.ResourceKey(), + Type: planState.Type, Attributes: res.Object, DependsOn: planState.DependsOn, + Extensions: planState.Extensions, }, nil} } @@ -149,8 +151,10 @@ func (k *KubernetesRuntime) Read(ctx context.Context, request *ReadRequest) *Rea return &ReadResponse{&models.Resource{ ID: requestResource.ResourceKey(), + Type: requestResource.Type, Attributes: v.Object, DependsOn: requestResource.DependsOn, + Extensions: requestResource.Extensions, }, nil} } diff --git a/pkg/engine/states/local/filesystem_state.go b/pkg/engine/states/local/filesystem_state.go index 203f00f3..93cff2e1 100644 --- a/pkg/engine/states/local/filesystem_state.go +++ b/pkg/engine/states/local/filesystem_state.go @@ -49,7 +49,7 @@ func (f *FileSystemState) Configure(obj cty.Value) error { } func (f *FileSystemState) GetLatestState(query *states.StateQuery) (*states.State, error) { - // parse state + // create a new state file if no file exists file, err := os.OpenFile(f.Path, os.O_RDWR|os.O_CREATE, fs.ModePerm) if err != nil { return nil, err @@ -81,7 +81,19 @@ func (f *FileSystemState) GetLatestState(query *states.StateQuery) (*states.Stat func (f *FileSystemState) Apply(state *states.State) error { now := time.Now() - state.CreateTime = now + + // don't change createTime in the state + oldState, err := f.GetLatestState(nil) + if err != nil { + return err + } + + if oldState == nil || oldState.CreateTime.IsZero() { + state.CreateTime = now + } else { + state.CreateTime = oldState.CreateTime + } + state.ModifiedTime = now jsonByte, err := json.MarshalIndent(state, "", " ") if err != nil { diff --git a/pkg/engine/states/state.go b/pkg/engine/states/state.go index 088a0282..04784dd5 100644 --- a/pkg/engine/states/state.go +++ b/pkg/engine/states/state.go @@ -48,40 +48,40 @@ type StateQuery struct { // datasource for 3-way merge/diff in operations like Apply or Preview. type State struct { // State ID - ID int64 `json:"id"` + ID int64 `json:"id" yaml:"id"` // Tenant is designed for multi-tenant scenario - Tenant string `json:"tenant,omitempty"` + Tenant string `json:"tenant,omitempty" yaml:"tenant,omitempty"` // Project name - Project string `json:"project"` + Project string `json:"project" yaml:"project"` // Stack name - Stack string `json:"stack"` + Stack string `json:"stack" yaml:"stack"` // Cluster is a logical concept to separate states in one stack. - Cluster string `json:"cluster,omitempty"` + Cluster string `json:"cluster,omitempty" yaml:"cluster,omitempty"` // State version - Version int `json:"version"` + Version int `json:"version" yaml:"version"` // KusionVersion represents the Kusion's version when this State is created - KusionVersion string `json:"kusionVersion"` + 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"` + Serial uint64 `json:"serial" yaml:"serial"` // Operator represents the person who triggered this operation - Operator string `json:"operator,omitempty"` + Operator string `json:"operator,omitempty" yaml:"operator,omitempty"` // Resources records all resources in this operation - Resources models.Resources `json:"resources"` + Resources models.Resources `json:"resources" yaml:"resources"` // CreateTime is the time State is created - CreateTime time.Time `json:"createTime"` + CreateTime time.Time `json:"createTime" yaml:"createTime"` // ModifiedTime is the time State is modified each time - ModifiedTime time.Time `json:"modifiedTime,omitempty"` + ModifiedTime time.Time `json:"modifiedTime,omitempty" yaml:"modifiedTime"` } func NewState() *State {