Skip to content

Commit

Permalink
add Stack in operation context and runtime interface (#216)
Browse files Browse the repository at this point in the history
  • Loading branch information
SparkYuan authored Jan 9, 2023
1 parent 331a4b2 commit 3729ccb
Show file tree
Hide file tree
Showing 14 changed files with 107 additions and 35 deletions.
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ linters:
- dupl
- exportloopref
- gocritic
- misspell
# - misspell
- nolintlint
- prealloc
- predeclared
Expand Down
1 change: 1 addition & 0 deletions pkg/engine/operation/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ func (ao *ApplyOperation) Apply(request *ApplyRequest) (rsp *ApplyResponse, st s
PriorStateResourceIndex: priorStateResourceIndex,
StateResourceIndex: priorStateResourceIndex,
Runtime: o.Runtime,
Stack: o.Stack,
MsgCh: o.MsgCh,
ResultState: resultState,
Lock: &sync.Mutex{},
Expand Down
21 changes: 19 additions & 2 deletions pkg/engine/operation/apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"kusionstack.io/kusion/pkg/engine/runtime"
"kusionstack.io/kusion/pkg/engine/states"
"kusionstack.io/kusion/pkg/engine/states/local"
"kusionstack.io/kusion/pkg/projectstack"
"kusionstack.io/kusion/pkg/status"
)

Expand Down Expand Up @@ -67,6 +68,7 @@ func TestOperation_Apply(t *testing.T) {
StateResourceIndex map[string]*models.Resource
Order *opsmodels.ChangeOrder
Runtime runtime.Runtime
Stack *projectstack.Stack
MsgCh chan opsmodels.Message
resultState *states.State
lock *sync.Mutex
Expand Down Expand Up @@ -106,6 +108,20 @@ func TestOperation_Apply(t *testing.T) {
},
}

stack := &projectstack.Stack{
StackConfiguration: projectstack.StackConfiguration{Name: "fakeStack"},
Path: "fakePath",
}
project := &projectstack.Project{
ProjectConfiguration: projectstack.ProjectConfiguration{
Name: "fakeProject",
Tenant: "fakeTenant",
Backend: nil,
},
Path: "fakePath",
Stacks: []*projectstack.Stack{stack},
}

tests := []struct {
name string
fields fields
Expand All @@ -123,8 +139,8 @@ func TestOperation_Apply(t *testing.T) {
},
args: args{applyRequest: &ApplyRequest{opsmodels.Request{
Tenant: "fakeTenant",
Stack: "fakeStack",
Project: "fakeProject",
Stack: stack,
Project: project,
Operator: "faker",
Spec: mf,
}}},
Expand All @@ -143,6 +159,7 @@ func TestOperation_Apply(t *testing.T) {
StateResourceIndex: tt.fields.StateResourceIndex,
ChangeOrder: tt.fields.Order,
Runtime: tt.fields.Runtime,
Stack: tt.fields.Stack,
MsgCh: tt.fields.MsgCh,
ResultState: tt.fields.resultState,
Lock: tt.fields.lock,
Expand Down
1 change: 1 addition & 0 deletions pkg/engine/operation/destory.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ func (do *DestroyOperation) Destroy(request *DestroyRequest) (st status.Status)
PriorStateResourceIndex: priorStateResourceIndex,
StateResourceIndex: priorStateResourceIndex,
Runtime: o.Runtime,
Stack: o.Stack,
MsgCh: o.MsgCh,
ResultState: resultState,
Lock: &sync.Mutex{},
Expand Down
18 changes: 16 additions & 2 deletions pkg/engine/operation/destory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,30 @@ import (
opsmodels "kusionstack.io/kusion/pkg/engine/operation/models"
"kusionstack.io/kusion/pkg/engine/runtime"
"kusionstack.io/kusion/pkg/engine/states/local"
"kusionstack.io/kusion/pkg/projectstack"
"kusionstack.io/kusion/pkg/status"
)

func TestOperation_Destroy(t *testing.T) {
var (
tenant = "tenant_name"
stack = "dev"
project = "project_name"
operator = "foo_user"
)

stack := &projectstack.Stack{
StackConfiguration: projectstack.StackConfiguration{Name: "fake-name"},
Path: "fake-path",
}
project := &projectstack.Project{
ProjectConfiguration: projectstack.ProjectConfiguration{
Name: "fake-name",
Tenant: "fake-tenant",
Backend: nil,
},
Path: "fake-path",
Stacks: []*projectstack.Stack{stack},
}

resourceState := models.Resource{
ID: "id1",

Expand Down
4 changes: 2 additions & 2 deletions pkg/engine/operation/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ func (d *Diff) Diff(request *DiffRequest) (string, error) {
latestState, err := d.StateStorage.GetLatestState(
&states.StateQuery{
Tenant: request.Tenant,
Stack: request.Stack,
Project: request.Project,
Stack: request.Stack.Name,
Project: request.Project.Name,
},
)
if err != nil {
Expand Down
9 changes: 6 additions & 3 deletions pkg/engine/operation/graph/resource_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (rn *ResourceNode) Execute(operation *opsmodels.Operation) status.Status {
priorState := operation.PriorStateResourceIndex[key]

// 3. get the latest resource from runtime
readRequest := &runtime.ReadRequest{PlanResource: planedState, PriorResource: priorState}
readRequest := &runtime.ReadRequest{PlanResource: planedState, PriorResource: priorState, Stack: operation.Stack}

response := operation.Runtime.Read(context.Background(), readRequest)
liveState := response.Resource
Expand All @@ -78,6 +78,7 @@ func (rn *ResourceNode) Execute(operation *opsmodels.Operation) status.Status {
dryRunResp := operation.Runtime.Apply(context.Background(), &runtime.ApplyRequest{
PriorResource: priorState,
PlanResource: planedState,
Stack: operation.Stack,
DryRun: true,
})
if status.IsErr(dryRunResp.Status) {
Expand Down Expand Up @@ -128,15 +129,17 @@ func (rn *ResourceNode) applyResource(operation *opsmodels.Operation, priorState

switch rn.Action {
case opsmodels.Create, opsmodels.Update:
response := operation.Runtime.Apply(context.Background(), &runtime.ApplyRequest{PriorResource: priorState, PlanResource: planedState})
response := operation.Runtime.Apply(context.Background(), &runtime.ApplyRequest{
PriorResource: priorState, PlanResource: planedState, Stack: operation.Stack,
})
res = response.Resource
s = response.Status
log.Debugf("apply resource:%s, result: %v", planedState.ID, jsonutil.Marshal2String(res))
if s != nil {
log.Debugf("apply status: %v", s.String())
}
case opsmodels.Delete:
response := operation.Runtime.Delete(context.Background(), &runtime.DeleteRequest{Resource: priorState})
response := operation.Runtime.Delete(context.Background(), &runtime.DeleteRequest{Resource: priorState, Stack: operation.Stack})
s = response.Status
if s != nil {
log.Debugf("delete state: %v", s.String())
Expand Down
23 changes: 15 additions & 8 deletions pkg/engine/operation/models/operation_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"kusionstack.io/kusion/pkg/engine/runtime"
"kusionstack.io/kusion/pkg/engine/states"
"kusionstack.io/kusion/pkg/log"
"kusionstack.io/kusion/pkg/projectstack"
"kusionstack.io/kusion/pkg/util"
jsonutil "kusionstack.io/kusion/pkg/util/json"
)
Expand Down Expand Up @@ -40,6 +41,9 @@ type Operation struct {
// Runtime is the resource infrastructure runtime of this operation
Runtime runtime.Runtime

// Stack contains info about where this command is invoked
Stack *projectstack.Stack

// MsgCh is used to send operation status like Success, Failed or Skip to Kusion CTl,
// and this message will be displayed in the terminal
MsgCh chan Message
Expand All @@ -58,12 +62,12 @@ type Message struct {
}

type Request struct {
Tenant string `json:"tenant"`
Project string `json:"project"`
Stack string `json:"stack"`
Cluster string `json:"cluster"`
Operator string `json:"operator"`
Spec *models.Spec `json:"spec"`
Tenant string `json:"tenant"`
Project *projectstack.Project `json:"project"`
Stack *projectstack.Stack `json:"stack"`
Cluster string `json:"cluster"`
Operator string `json:"operator"`
Spec *models.Spec `json:"spec"`
}

type OpResult string
Expand Down Expand Up @@ -96,8 +100,8 @@ func (o *Operation) RefreshResourceIndex(resourceKey string, resource *models.Re
func (o *Operation) InitStates(request *Request) (*states.State, *states.State) {
query := &states.StateQuery{
Tenant: request.Tenant,
Stack: request.Stack,
Project: request.Project,
Stack: request.Stack.Name,
Project: request.Project.Name,
Cluster: request.Cluster,
}
latestState, err := o.StateStorage.GetLatestState(
Expand All @@ -112,6 +116,9 @@ func (o *Operation) InitStates(request *Request) (*states.State, *states.State)
resultState.Serial = latestState.Serial
err = copier.Copy(resultState, request)
util.CheckNotError(err, fmt.Sprintf("copy request to result State failed, request:%v", jsonutil.Marshal2PrettyString(request)))
resultState.Stack = request.Stack.Name
resultState.Project = request.Project.Name

resultState.Resources = nil

return latestState, resultState
Expand Down
1 change: 1 addition & 0 deletions pkg/engine/operation/preview.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ func (po *PreviewOperation) Preview(request *PreviewRequest) (rsp *PreviewRespon
IgnoreFields: o.IgnoreFields,
ChangeOrder: o.ChangeOrder,
Runtime: o.Runtime, // preview need get the latest spec from runtime
Stack: o.Stack,
ResultState: resultState,
Lock: &sync.Mutex{},
},
Expand Down
28 changes: 21 additions & 7 deletions pkg/engine/operation/preview_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"kusionstack.io/kusion/pkg/engine/runtime"
"kusionstack.io/kusion/pkg/engine/states"
"kusionstack.io/kusion/pkg/engine/states/local"
"kusionstack.io/kusion/pkg/projectstack"
"kusionstack.io/kusion/pkg/status"
"kusionstack.io/kusion/pkg/util/kdump"
)
Expand Down Expand Up @@ -91,6 +92,19 @@ func TestOperation_Preview(t *testing.T) {
type args struct {
request *PreviewRequest
}
stack := &projectstack.Stack{
StackConfiguration: projectstack.StackConfiguration{Name: "fake-name"},
Path: "fake-path",
}
project := &projectstack.Project{
ProjectConfiguration: projectstack.ProjectConfiguration{
Name: "fake-name",
Tenant: "fake-tenant",
Backend: nil,
},
Path: "fake-path",
Stacks: []*projectstack.Stack{stack},
}
tests := []struct {
name string
fields fields
Expand All @@ -110,8 +124,8 @@ func TestOperation_Preview(t *testing.T) {
request: &PreviewRequest{
Request: opsmodels.Request{
Tenant: "fake-tenant",
Stack: "fake-stack",
Project: "fake-project",
Stack: stack,
Project: project,
Operator: "fake-operator",
Spec: &models.Spec{
Resources: []models.Resource{
Expand Down Expand Up @@ -148,8 +162,8 @@ func TestOperation_Preview(t *testing.T) {
request: &PreviewRequest{
Request: opsmodels.Request{
Tenant: "fake-tenant",
Stack: "fake-stack",
Project: "fake-project",
Stack: stack,
Project: project,
Operator: "fake-operator",
Spec: &models.Spec{
Resources: []models.Resource{
Expand Down Expand Up @@ -203,9 +217,9 @@ func TestOperation_Preview(t *testing.T) {
args: args{
request: &PreviewRequest{
Request: opsmodels.Request{
Tenant: "fake-tennat",
Stack: "fake-stack",
Project: "fake-project",
Tenant: "fake-tenant",
Stack: stack,
Project: project,
Operator: "fake-operator",
Spec: &models.Spec{
Resources: []models.Resource{
Expand Down
10 changes: 10 additions & 0 deletions pkg/engine/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"k8s.io/apimachinery/pkg/watch"

"kusionstack.io/kusion/pkg/engine/models"
"kusionstack.io/kusion/pkg/projectstack"
"kusionstack.io/kusion/pkg/status"
)

Expand Down Expand Up @@ -45,6 +46,9 @@ type ApplyRequest struct {
// PlanResource is the resource we want to apply in this request
PlanResource *models.Resource

// Stack contains info about where this command is invoked
Stack *projectstack.Stack

// DryRun means this a dry-run request and will not make any changes in actual infra
DryRun bool
}
Expand All @@ -63,6 +67,9 @@ type ReadRequest struct {

// PlanResource is the resource we want to apply in this request
PlanResource *models.Resource

// Stack contains info about where this command is invoked
Stack *projectstack.Stack
}

type ReadResponse struct {
Expand All @@ -76,6 +83,9 @@ type ReadResponse struct {
type DeleteRequest struct {
// Resource represents the resource we want to delete from the actual infra
Resource *models.Resource

// Stack contains info about where this command is invoked
Stack *projectstack.Stack
}

type DeleteResponse struct {
Expand Down
9 changes: 5 additions & 4 deletions pkg/kusionctl/cmd/apply/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ func Apply(
ac := &operation.ApplyOperation{
Operation: opsmodels.Operation{
Runtime: runtime,
Stack: changes.Stack(),
StateStorage: storage,
MsgCh: make(chan opsmodels.Message),
},
Expand Down Expand Up @@ -295,8 +296,8 @@ func Apply(
_, st := ac.Apply(&operation.ApplyRequest{
Request: opsmodels.Request{
Tenant: changes.Project().Tenant,
Project: changes.Project().Name,
Stack: changes.Stack().Name,
Project: changes.Project(),
Stack: changes.Stack(),
Cluster: cluster,
Operator: o.Operator,
Spec: planResources,
Expand Down Expand Up @@ -356,8 +357,8 @@ func Watch(o *ApplyOptions,
wo := &operation.WatchOperation{Runtime: r}
if err := wo.Watch(&operation.WatchRequest{
Request: opsmodels.Request{
Project: changes.Project().Name,
Stack: changes.Stack().Name,
Project: changes.Project(),
Stack: changes.Stack(),
Spec: &models.Spec{Resources: toBeWatched},
},
}); err != nil {
Expand Down
10 changes: 6 additions & 4 deletions pkg/kusionctl/cmd/destroy/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ func (o *DestroyOptions) preview(planResources *models.Spec,
Operation: opsmodels.Operation{
OperationType: opsmodels.DestroyPreview,
Runtime: runtime,
Stack: stack,
StateStorage: stateStorage,
ChangeOrder: &opsmodels.ChangeOrder{StepKeys: []string{}, ChangeSteps: map[string]*opsmodels.ChangeStep{}},
},
Expand All @@ -156,9 +157,9 @@ func (o *DestroyOptions) preview(planResources *models.Spec,
rsp, s := pc.Preview(&operation.PreviewRequest{
Request: opsmodels.Request{
Tenant: project.Tenant,
Project: project.Name,
Project: project,
Operator: o.Operator,
Stack: stack.Name,
Stack: stack,
Spec: planResources,
},
})
Expand All @@ -175,6 +176,7 @@ func (o *DestroyOptions) destroy(planResources *models.Spec, changes *opsmodels.
do := &operation.DestroyOperation{
Operation: opsmodels.Operation{
Runtime: runtime,
Stack: changes.Stack(),
StateStorage: stateStorage,
MsgCh: make(chan opsmodels.Message),
},
Expand Down Expand Up @@ -250,9 +252,9 @@ func (o *DestroyOptions) destroy(planResources *models.Spec, changes *opsmodels.
st := do.Destroy(&operation.DestroyRequest{
Request: opsmodels.Request{
Tenant: changes.Project().Tenant,
Project: changes.Project().Name,
Project: changes.Project(),
Operator: o.Operator,
Stack: changes.Stack().Name,
Stack: changes.Stack(),
Spec: planResources,
},
})
Expand Down
Loading

0 comments on commit 3729ccb

Please sign in to comment.