Skip to content

Commit

Permalink
refactor: expose runtime parameters in preview and apply, fix unuse…
Browse files Browse the repository at this point in the history
…d parameters
  • Loading branch information
elliotxx committed Jun 16, 2022
1 parent 3738899 commit 802c993
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 27 deletions.
76 changes: 57 additions & 19 deletions pkg/kusionctl/cmd/apply/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"kusionstack.io/kusion/pkg/status"
)

// ApplyOptions defines flags for the `apply` command
type ApplyOptions struct {
compilecmd.CompileOptions
Operator string
Expand All @@ -32,6 +33,7 @@ type ApplyOptions struct {
OnlyPreview bool
}

// NewApplyOptions returns a new ApplyOptions instance
func NewApplyOptions() *ApplyOptions {
return &ApplyOptions{
CompileOptions: compilecmd.CompileOptions{
Expand Down Expand Up @@ -75,7 +77,12 @@ func (o *ApplyOptions) Run() error {

// Compute changes for preview
stateStorage := &states.FileSystemState{Path: filepath.Join(o.WorkDir, states.KusionState)}
changes, err := Preview(o, stateStorage, planResources, project, stack, os.Stdout)
kubernetesRuntime, err := runtime.NewKubernetesRuntime()
if err != nil {
return err
}

changes, err := Preview(o, kubernetesRuntime, stateStorage, planResources, project, stack, os.Stdout)
if err != nil {
return err
}
Expand Down Expand Up @@ -118,7 +125,7 @@ func (o *ApplyOptions) Run() error {

if !o.OnlyPreview {
fmt.Println("Start applying diffs ...")
if err := Apply(o, stateStorage, planResources, changes, os.Stdout); err != nil {
if err := Apply(o, kubernetesRuntime, stateStorage, planResources, changes, os.Stdout); err != nil {
return err
}

Expand All @@ -131,9 +138,29 @@ func (o *ApplyOptions) Run() error {
return nil
}

// The preview function will preview for all resources changes
// The Preview function calculates the upcoming actions of each resource
// through the execution Kusion Engine, and you can customize the
// runtime of engine and the state storage through `runtime` and
// `storage` parameters.
//
// Example:
// o := NewApplyOptions()
// stateStorage := &states.FileSystemState{
// Path: filepath.Join(o.WorkDir, states.KusionState)
// }
// kubernetesRuntime, err := runtime.NewKubernetesRuntime()
// if err != nil {
// return err
// }
//
// changes, err := Preview(o, kubernetesRuntime, stateStorage,
// planResources, project, stack, os.Stdout)
// if err != nil {
// return err
// }
func Preview(
o *ApplyOptions,
runtime runtime.Runtime,
storage states.StateStorage,
planResources *models.Spec,
project *projectstack.Project,
Expand All @@ -142,16 +169,12 @@ func Preview(
) (*operation.Changes, error) {
log.Info("Start compute preview changes ...")

kubernetesRuntime, err := runtime.NewKubernetesRuntime()
if err != nil {
return nil, err
}

// Construct the preview operation
pc := &operation.PreviewOperation{
Operation: operation.Operation{
OperationType: operation.ApplyPreview,
Runtime: kubernetesRuntime,
StateStorage: &states.FileSystemState{Path: filepath.Join(o.WorkDir, states.KusionState)},
Runtime: runtime,
StateStorage: storage,
Order: &operation.ChangeOrder{StepKeys: []string{}, ChangeSteps: map[string]*operation.ChangeStep{}},
},
}
Expand All @@ -174,24 +197,39 @@ func Preview(
return operation.NewChanges(project, stack, rsp.Order), nil
}

// The apply function will apply the resources changes,
// and will save the state to specified storage.
// The Apply function will apply the resources changes
// through the execution Kusion Engine, and will save
// the state to specified storage.
//
// You can customize the runtime of engine and the state
// storage through `runtime` and `storage` parameters.
//
// Example:
// o := NewApplyOptions()
// stateStorage := &states.FileSystemState{
// Path: filepath.Join(o.WorkDir, states.KusionState)
// }
// kubernetesRuntime, err := runtime.NewKubernetesRuntime()
// if err != nil {
// return err
// }
//
// err = Apply(o, kubernetesRuntime, stateStorage, planResources, changes, os.Stdout)
// if err != nil {
// return err
// }
func Apply(
o *ApplyOptions,
runtime runtime.Runtime,
storage states.StateStorage,
planResources *models.Spec,
changes *operation.Changes,
out io.Writer,
) error {
// Build apply operation
kubernetesRuntime, err := runtime.NewKubernetesRuntime()
if err != nil {
return err
}

// Construct the apply operation
ac := &operation.ApplyOperation{
Operation: operation.Operation{
Runtime: kubernetesRuntime,
Runtime: runtime,
StateStorage: storage,
MsgCh: make(chan operation.Message),
},
Expand Down
12 changes: 4 additions & 8 deletions pkg/kusionctl/cmd/apply/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,10 @@ func Test_preview(t *testing.T) {
stateStorage := &states.FileSystemState{Path: filepath.Join("", states.KusionState)}
t.Run("preview success", func(t *testing.T) {
defer monkey.UnpatchAll()
mockNewKubernetesRuntime()
mockOperationPreview()

o := NewApplyOptions()
_, err := Preview(o, stateStorage, &models.Spec{Resources: []models.Resource{sa1, sa2, sa3}}, project, stack, os.Stdout)
_, err := Preview(o, &fakerRuntime{}, stateStorage, &models.Spec{Resources: []models.Resource{sa1, sa2, sa3}}, project, stack, os.Stdout)
assert.Nil(t, err)
})
}
Expand Down Expand Up @@ -216,7 +215,6 @@ func Test_apply(t *testing.T) {
stateStorage := &states.FileSystemState{Path: filepath.Join("", states.KusionState)}
t.Run("dry run", func(t *testing.T) {
defer monkey.UnpatchAll()
mockNewKubernetesRuntime()

planResources := &models.Spec{Resources: []models.Resource{sa1}}
order := &operation.ChangeOrder{
Expand All @@ -233,12 +231,11 @@ func Test_apply(t *testing.T) {
changes := operation.NewChanges(project, stack, order)
o := NewApplyOptions()
o.DryRun = true
err := Apply(o, stateStorage, planResources, changes, os.Stdout)
err := Apply(o, &fakerRuntime{}, stateStorage, planResources, changes, os.Stdout)
assert.Nil(t, err)
})
t.Run("apply success", func(t *testing.T) {
defer monkey.UnpatchAll()
mockNewKubernetesRuntime()
mockOperationApply(operation.Success)

o := NewApplyOptions()
Expand All @@ -262,12 +259,11 @@ func Test_apply(t *testing.T) {
}
changes := operation.NewChanges(project, stack, order)

err := Apply(o, stateStorage, planResources, changes, os.Stdout)
err := Apply(o, &fakerRuntime{}, stateStorage, planResources, changes, os.Stdout)
assert.Nil(t, err)
})
t.Run("apply failed", func(t *testing.T) {
defer monkey.UnpatchAll()
mockNewKubernetesRuntime()
mockOperationApply(operation.Failed)

o := NewApplyOptions()
Expand All @@ -285,7 +281,7 @@ func Test_apply(t *testing.T) {
}
changes := operation.NewChanges(project, stack, order)

err := Apply(o, stateStorage, planResources, changes, os.Stdout)
err := Apply(o, &fakerRuntime{}, stateStorage, planResources, changes, os.Stdout)
assert.NotNil(t, err)
})
}
Expand Down

0 comments on commit 802c993

Please sign in to comment.