Skip to content

Commit

Permalink
1. return Resource.Type and Resource.Extensions in K8s Runtime (#101)
Browse files Browse the repository at this point in the history
2. fix filesystem_state createTime bug
  • Loading branch information
SparkYuan committed Jul 19, 2022
1 parent c28949c commit 9a29a7e
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 19 deletions.
5 changes: 0 additions & 5 deletions pkg/engine/operation/graph/resource_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions pkg/engine/runtime/kubernetes_runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
}

Expand Down Expand Up @@ -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}
}

Expand Down
16 changes: 14 additions & 2 deletions pkg/engine/states/local/filesystem_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
24 changes: 12 additions & 12 deletions pkg/engine/states/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 9a29a7e

Please sign in to comment.