Skip to content

Commit 9a29a7e

Browse files
authored
1. return Resource.Type and Resource.Extensions in K8s Runtime (#101)
2. fix filesystem_state createTime bug
1 parent c28949c commit 9a29a7e

File tree

4 files changed

+30
-19
lines changed

4 files changed

+30
-19
lines changed

pkg/engine/operation/graph/resource_node.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,6 @@ func (rn *ResourceNode) applyResource(operation *opsmodels.Operation, priorState
142142
return s
143143
}
144144

145-
// compatible with delete action
146-
if res != nil {
147-
res.DependsOn = planedState.DependsOn
148-
res.Extensions = planedState.Extensions
149-
}
150145
key := rn.state.ResourceKey()
151146
if e := operation.RefreshResourceIndex(key, res, rn.Action); e != nil {
152147
return status.NewErrorStatus(e)

pkg/engine/runtime/kubernetes_runtime.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,10 @@ func (k *KubernetesRuntime) Apply(ctx context.Context, request *ApplyRequest) *A
118118

119119
return &ApplyResponse{&models.Resource{
120120
ID: planState.ResourceKey(),
121+
Type: planState.Type,
121122
Attributes: res.Object,
122123
DependsOn: planState.DependsOn,
124+
Extensions: planState.Extensions,
123125
}, nil}
124126
}
125127

@@ -149,8 +151,10 @@ func (k *KubernetesRuntime) Read(ctx context.Context, request *ReadRequest) *Rea
149151

150152
return &ReadResponse{&models.Resource{
151153
ID: requestResource.ResourceKey(),
154+
Type: requestResource.Type,
152155
Attributes: v.Object,
153156
DependsOn: requestResource.DependsOn,
157+
Extensions: requestResource.Extensions,
154158
}, nil}
155159
}
156160

pkg/engine/states/local/filesystem_state.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func (f *FileSystemState) Configure(obj cty.Value) error {
4949
}
5050

5151
func (f *FileSystemState) GetLatestState(query *states.StateQuery) (*states.State, error) {
52-
// parse state
52+
// create a new state file if no file exists
5353
file, err := os.OpenFile(f.Path, os.O_RDWR|os.O_CREATE, fs.ModePerm)
5454
if err != nil {
5555
return nil, err
@@ -81,7 +81,19 @@ func (f *FileSystemState) GetLatestState(query *states.StateQuery) (*states.Stat
8181

8282
func (f *FileSystemState) Apply(state *states.State) error {
8383
now := time.Now()
84-
state.CreateTime = now
84+
85+
// don't change createTime in the state
86+
oldState, err := f.GetLatestState(nil)
87+
if err != nil {
88+
return err
89+
}
90+
91+
if oldState == nil || oldState.CreateTime.IsZero() {
92+
state.CreateTime = now
93+
} else {
94+
state.CreateTime = oldState.CreateTime
95+
}
96+
8597
state.ModifiedTime = now
8698
jsonByte, err := json.MarshalIndent(state, "", " ")
8799
if err != nil {

pkg/engine/states/state.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,40 +48,40 @@ type StateQuery struct {
4848
// datasource for 3-way merge/diff in operations like Apply or Preview.
4949
type State struct {
5050
// State ID
51-
ID int64 `json:"id"`
51+
ID int64 `json:"id" yaml:"id"`
5252

5353
// Tenant is designed for multi-tenant scenario
54-
Tenant string `json:"tenant,omitempty"`
54+
Tenant string `json:"tenant,omitempty" yaml:"tenant,omitempty"`
5555

5656
// Project name
57-
Project string `json:"project"`
57+
Project string `json:"project" yaml:"project"`
5858

5959
// Stack name
60-
Stack string `json:"stack"`
60+
Stack string `json:"stack" yaml:"stack"`
6161

6262
// Cluster is a logical concept to separate states in one stack.
63-
Cluster string `json:"cluster,omitempty"`
63+
Cluster string `json:"cluster,omitempty" yaml:"cluster,omitempty"`
6464

6565
// State version
66-
Version int `json:"version"`
66+
Version int `json:"version" yaml:"version"`
6767

6868
// KusionVersion represents the Kusion's version when this State is created
69-
KusionVersion string `json:"kusionVersion"`
69+
KusionVersion string `json:"kusionVersion" yaml:"kusionVersion"`
7070

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

7474
// Operator represents the person who triggered this operation
75-
Operator string `json:"operator,omitempty"`
75+
Operator string `json:"operator,omitempty" yaml:"operator,omitempty"`
7676

7777
// Resources records all resources in this operation
78-
Resources models.Resources `json:"resources"`
78+
Resources models.Resources `json:"resources" yaml:"resources"`
7979

8080
// CreateTime is the time State is created
81-
CreateTime time.Time `json:"createTime"`
81+
CreateTime time.Time `json:"createTime" yaml:"createTime"`
8282

8383
// ModifiedTime is the time State is modified each time
84-
ModifiedTime time.Time `json:"modifiedTime,omitempty"`
84+
ModifiedTime time.Time `json:"modifiedTime,omitempty" yaml:"modifiedTime"`
8585
}
8686

8787
func NewState() *State {

0 commit comments

Comments
 (0)