Skip to content

Commit

Permalink
feat: refactor the old usage of workspace and backend
Browse files Browse the repository at this point in the history
  • Loading branch information
healthjyk committed Mar 19, 2024
1 parent 3b2af58 commit 4e4cb74
Show file tree
Hide file tree
Showing 21 changed files with 345 additions and 329 deletions.
80 changes: 43 additions & 37 deletions pkg/cmd/apply/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,12 @@ func (o *Options) Validate() error {
}

func (o *Options) Run() error {
// Set no style
// set no style
if o.NoStyle {
pterm.DisableStyling()
pterm.DisableColor()
}

// Parse project and stack of work directory
proj, stack, err := project.DetectProjectAndStack(o.Options.WorkDir)
if err != nil {
return err
}

options := &builders.Options{
IsKclPkg: o.IsKclPkg,
WorkDir: o.WorkDir,
Expand All @@ -73,12 +67,32 @@ func (o *Options) Run() error {
NoStyle: o.NoStyle,
}

// Generate Intent
// parse project and stack of work directory
proj, stack, err := project.DetectProjectAndStack(o.Options.WorkDir)
if err != nil {
return err
}

// get workspace configurations
bk, err := backend.NewBackend(o.Backend)
if err != nil {
return err
}
wsStorage, err := bk.WorkspaceStorage()
if err != nil {
return err
}
ws, err := wsStorage.Get(o.Workspace)
if err != nil {
return err
}

// generate Intent
var sp *apiv1.Intent
if o.IntentFile != "" {
sp, err = build.IntentFromFile(o.IntentFile)
} else {
sp, err = build.IntentWithSpinner(options, proj, stack)
sp, err = build.IntentWithSpinner(options, proj, stack, ws)
}
if err != nil {
return err
Expand All @@ -90,16 +104,9 @@ func (o *Options) Run() error {
return nil
}

// new state storage
ws := "default" // todo: use default workspace for tmp
bk, err := backend.NewBackend("") // todo: use current backend for tmp
if err != nil {
return err
}
storage := bk.StateStorage(proj.Name, stack.Name, ws)

// Compute changes for preview
changes, err := preview.Preview(&o.Options, storage, sp, proj, stack, ws)
// compute changes for preview
storage := bk.StateStorage(proj.Name, stack.Name, ws.Name)
changes, err := preview.Preview(&o.Options, storage, sp, proj, stack)
if err != nil {
return err
}
Expand All @@ -109,18 +116,18 @@ func (o *Options) Run() error {
return nil
}

// Summary preview table
// summary preview table
changes.Summary(os.Stdout)

// Detail detection
// detail detection
if o.Detail && o.All {
changes.OutputDiff("all")
if !o.Yes {
return nil
}
}

// Prompt
// prompt
if !o.Yes {
for {
input, err := prompt()
Expand All @@ -147,7 +154,7 @@ func (o *Options) Run() error {
return err
}

// If dry run, print the hint
// if dry run, print the hint
if o.DryRun {
fmt.Printf("\nNOTE: Currently running in the --dry-run mode, the above configuration does not really take effect\n")
return nil
Expand Down Expand Up @@ -192,7 +199,7 @@ func Apply(
changes *models.Changes,
out io.Writer,
) error {
// Construct the apply operation
// construct the apply operation
ac := &operation.ApplyOperation{
Operation: models.Operation{
Stack: changes.Stack(),
Expand All @@ -202,10 +209,10 @@ func Apply(
},
}

// Line summary
// line summary
var ls lineSummary

// Progress bar, print dag walk detail
// progress bar, print dag walk detail
progressbar, err := pterm.DefaultProgressbar.
WithMaxWidth(0). // Set to 0, the terminal width will be used
WithTotal(len(changes.StepKeys)).
Expand All @@ -214,9 +221,9 @@ func Apply(
if err != nil {
return err
}
// Wait msgCh close
// wait msgCh close
var wg sync.WaitGroup
// Receive msg and print detail
// receive msg and print detail
go func() {
defer func() {
if p := recover(); p != nil {
Expand Down Expand Up @@ -286,21 +293,20 @@ func Apply(
// parse cluster in arguments
_, st := ac.Apply(&operation.ApplyRequest{
Request: models.Request{
Project: changes.Project(),
Stack: changes.Stack(),
Workspace: changes.Workspace(),
Operator: o.Operator,
Intent: planResources,
Project: changes.Project(),
Stack: changes.Stack(),
Operator: o.Operator,
Intent: planResources,
},
})
if v1.IsErr(st) {
return fmt.Errorf("apply failed, status:\n%v", st)
}
}

// Wait for msgCh closed
// wait for msgCh closed
wg.Wait()
// Print summary
// print summary
pterm.Fprintln(out, fmt.Sprintf("Apply complete! Resources: %d created, %d updated, %d deleted.", ls.created, ls.updated, ls.deleted))
return nil
}
Expand Down Expand Up @@ -330,15 +336,15 @@ func Watch(
return nil
}

// Filter out unchanged resources
// filter out unchanged resources
toBeWatched := apiv1.Resources{}
for _, res := range planResources.Resources {
if changes.ChangeOrder.ChangeSteps[res.ResourceKey()].Action != models.UnChanged {
toBeWatched = append(toBeWatched, res)
}
}

// Watch operation
// watch operation
wo := &operation.WatchOperation{}
if err := wo.Watch(&operation.WatchRequest{
Request: models.Request{
Expand Down
28 changes: 18 additions & 10 deletions pkg/cmd/apply/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"kusionstack.io/kusion/pkg/engine/runtime/kubernetes"
statestorages "kusionstack.io/kusion/pkg/engine/state/storages"
"kusionstack.io/kusion/pkg/project"
workspacestorages "kusionstack.io/kusion/pkg/workspace/storages"
)

func TestApplyOptions_Run(t *testing.T) {
Expand All @@ -33,6 +34,7 @@ func TestApplyOptions_Run(t *testing.T) {
mockPatchBuildIntent()
mockPatchNewKubernetesRuntime()
mockNewBackend()
mockWorkspaceStorage()
mockPatchOperationPreview()

o := NewApplyOptions()
Expand All @@ -48,6 +50,7 @@ func TestApplyOptions_Run(t *testing.T) {
mockPatchBuildIntent()
mockPatchNewKubernetesRuntime()
mockNewBackend()
mockWorkspaceStorage()
mockPatchOperationPreview()
mockOperationApply(models.Success)

Expand All @@ -60,28 +63,28 @@ func TestApplyOptions_Run(t *testing.T) {
}

var (
p = &apiv1.Project{
proj = &apiv1.Project{
Name: "testdata",
}
s = &apiv1.Stack{
stack = &apiv1.Stack{
Name: "dev",
}
ws = "dev"
)

func mockPatchDetectProjectAndStack() *mockey.Mocker {
return mockey.Mock(project.DetectProjectAndStack).To(func(stackDir string) (*apiv1.Project, *apiv1.Stack, error) {
p.Path = stackDir
s.Path = stackDir
return p, s, nil
proj.Path = stackDir
stack.Path = stackDir
return proj, stack, nil
}).Build()
}

func mockPatchBuildIntent() *mockey.Mocker {
return mockey.Mock(build.Intent).To(func(
o *builders.Options,
project *apiv1.Project,
proj *apiv1.Project,
stack *apiv1.Stack,
ws *apiv1.Workspace,
) (*apiv1.Intent, error) {
return &apiv1.Intent{Resources: []apiv1.Resource{sa1, sa2, sa3}}, nil
}).Build()
Expand All @@ -97,6 +100,11 @@ func mockNewBackend() *mockey.Mocker {
return mockey.Mock(backend.NewBackend).Return(&storages.LocalStorage{}, nil).Build()
}

func mockWorkspaceStorage() {
mockey.Mock((*storages.LocalStorage).WorkspaceStorage).Return(&workspacestorages.LocalStorage{}, nil).Build()
mockey.Mock((*workspacestorages.LocalStorage).Get).Return(&apiv1.Workspace{}, nil).Build()
}

var _ runtime.Runtime = (*fakerRuntime)(nil)

type fakerRuntime struct{}
Expand Down Expand Up @@ -204,7 +212,7 @@ func Test_apply(t *testing.T) {
},
},
}
changes := models.NewChanges(p, s, ws, order)
changes := models.NewChanges(proj, stack, order)
o := NewApplyOptions()
o.DryRun = true
err := Apply(o, stateStorage, planResources, changes, os.Stdout)
Expand All @@ -229,7 +237,7 @@ func Test_apply(t *testing.T) {
},
},
}
changes := models.NewChanges(p, s, ws, order)
changes := models.NewChanges(proj, stack, order)

err := Apply(o, stateStorage, planResources, changes, os.Stdout)
assert.Nil(t, err)
Expand All @@ -249,7 +257,7 @@ func Test_apply(t *testing.T) {
},
},
}
changes := models.NewChanges(p, s, ws, order)
changes := models.NewChanges(proj, stack, order)

err := Apply(o, stateStorage, planResources, changes, os.Stdout)
assert.NotNil(t, err)
Expand Down
4 changes: 4 additions & 0 deletions pkg/cmd/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,8 @@ func (o *Options) AddBuildFlags(cmd *cobra.Command) {
i18n.T("Specify the command line setting files"))
cmd.Flags().StringToStringVarP(&o.Arguments, "argument", "D", map[string]string{},
i18n.T("Specify the top-level argument"))
cmd.Flags().StringVarP(&o.Backend, "backend", "", "",
i18n.T("the backend name"))
cmd.Flags().StringVarP(&o.Backend, "workspace", "", "",
i18n.T("the workspace name"))
}
6 changes: 3 additions & 3 deletions pkg/cmd/build/builders/appconfig_builder.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package builders

import (
"kusionstack.io/kusion/pkg/apis/core/v1"
v1 "kusionstack.io/kusion/pkg/apis/core/v1"
"kusionstack.io/kusion/pkg/modules"
"kusionstack.io/kusion/pkg/modules/generators"
)
Expand All @@ -13,7 +13,7 @@ type AppsConfigBuilder struct {

func (acg *AppsConfigBuilder) Build(
_ *Options,
project *v1.Project,
proj *v1.Project,
stack *v1.Stack,
) (*v1.Intent, error) {
i := &v1.Intent{
Expand All @@ -22,7 +22,7 @@ func (acg *AppsConfigBuilder) Build(

var gfs []modules.NewGeneratorFunc
err := modules.ForeachOrdered(acg.Apps, func(appName string, app v1.AppConfiguration) error {
gfs = append(gfs, generators.NewAppConfigurationGeneratorFunc(project.Name, stack.Name, appName, &app, acg.Workspace))
gfs = append(gfs, generators.NewAppConfigurationGeneratorFunc(proj.Name, stack.Name, appName, &app, acg.Workspace))
return nil
})
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/build/builders/builder.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package builders

import (
"kusionstack.io/kusion/pkg/apis/core/v1"
v1 "kusionstack.io/kusion/pkg/apis/core/v1"
)

// Builder represents a method to build an Intent. Typically, it is implemented by the AppConfigureBuilder,
Expand Down
18 changes: 16 additions & 2 deletions pkg/cmd/build/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
yamlv2 "gopkg.in/yaml.v2"
"kcl-lang.io/kpm/pkg/api"

"kusionstack.io/kusion/pkg/backend"
"kusionstack.io/kusion/pkg/cmd/build/builders"
"kusionstack.io/kusion/pkg/project"
)
Expand All @@ -29,6 +30,8 @@ type Flags struct {
Settings []string
Arguments map[string]string
NoStyle bool
Backend string
Workspace string
}

const Stdout = "stdout"
Expand Down Expand Up @@ -69,7 +72,17 @@ func (o *Options) Run() error {
}

// Parse project and stack of work directory
project, stack, err := project.DetectProjectAndStack(o.WorkDir)
proj, stack, err := project.DetectProjectAndStack(o.WorkDir)
if err != nil {
return err
}

// Get workspace from backend
storage, err := backend.NewWorkspaceStorage(o.Backend)
if err != nil {
return err
}
ws, err := storage.Get(o.Workspace)
if err != nil {
return err
}
Expand All @@ -83,8 +96,9 @@ func (o *Options) Run() error {
Arguments: o.Arguments,
NoStyle: o.NoStyle,
},
project,
proj,
stack,
ws,
)
if err != nil {
return err
Expand Down
Loading

0 comments on commit 4e4cb74

Please sign in to comment.