Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use the new state storage to replace the old one #912

Merged
merged 1 commit into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions pkg/backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ func NewBackend(name string) (Backend, error) {
emptyCfg = true
} else if err != nil {
return nil, err
}
if cfg.Backends == nil {
} else if cfg.Backends == nil {
emptyCfg = true
}

Expand Down
1 change: 0 additions & 1 deletion pkg/cmd/apply/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ func NewCmdApply() *cobra.Command {

o.AddBuildFlags(cmd)
o.AddPreviewFlags(cmd)
o.AddBackendFlags(cmd)

cmd.Flags().BoolVarP(&o.Yes, "yes", "y", false,
i18n.T("Automatically approve and perform the update after previewing it"))
Expand Down
89 changes: 44 additions & 45 deletions pkg/cmd/apply/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,21 @@ import (

apiv1 "kusionstack.io/kusion/pkg/apis/core/v1"
v1 "kusionstack.io/kusion/pkg/apis/status/v1"
"kusionstack.io/kusion/pkg/backend"
"kusionstack.io/kusion/pkg/cmd/build"
cmdintent "kusionstack.io/kusion/pkg/cmd/build/builders"
previewcmd "kusionstack.io/kusion/pkg/cmd/preview"
"kusionstack.io/kusion/pkg/engine/backend"
_ "kusionstack.io/kusion/pkg/engine/backend/init"
"kusionstack.io/kusion/pkg/cmd/build/builders"
"kusionstack.io/kusion/pkg/cmd/preview"
"kusionstack.io/kusion/pkg/engine/operation"
opsmodels "kusionstack.io/kusion/pkg/engine/operation/models"
"kusionstack.io/kusion/pkg/engine/states"
"kusionstack.io/kusion/pkg/engine/operation/models"
"kusionstack.io/kusion/pkg/engine/state"
"kusionstack.io/kusion/pkg/log"
"kusionstack.io/kusion/pkg/project"
"kusionstack.io/kusion/pkg/util/pretty"
)

// Options defines flags for the `apply` command
type Options struct {
previewcmd.Options
preview.Options
Flag
}

Expand All @@ -40,7 +39,7 @@ type Flag struct {
// NewApplyOptions returns a new ApplyOptions instance
func NewApplyOptions() *Options {
return &Options{
Options: *previewcmd.NewPreviewOptions(),
Options: *preview.NewPreviewOptions(),
}
}

Expand All @@ -60,12 +59,12 @@ func (o *Options) Run() error {
}

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

options := &cmdintent.Options{
options := &builders.Options{
IsKclPkg: o.IsKclPkg,
WorkDir: o.WorkDir,
Filenames: o.Filenames,
Expand All @@ -79,7 +78,7 @@ func (o *Options) Run() error {
if o.IntentFile != "" {
sp, err = build.IntentFromFile(o.IntentFile)
} else {
sp, err = build.IntentWithSpinner(options, project, stack)
sp, err = build.IntentWithSpinner(options, proj, stack)
}
if err != nil {
return err
Expand All @@ -91,14 +90,16 @@ func (o *Options) Run() error {
return nil
}

// Get state storage from cli backend options, environment variables, workspace backend configs
stateStorage, err := backend.NewStateStorage(stack, &o.BackendOptions)
// 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 := previewcmd.Preview(&o.Options, stateStorage, sp, project, stack)
changes, err := preview.Preview(&o.Options, storage, sp, proj, stack, ws)
if err != nil {
return err
}
Expand Down Expand Up @@ -142,7 +143,7 @@ func (o *Options) Run() error {
}

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

Expand All @@ -154,7 +155,7 @@ func (o *Options) Run() error {

if o.Watch {
fmt.Println("\nStart watching changes ...")
if err := Watch(o, sp, changes); err != nil {
if err = Watch(o, sp, changes); err != nil {
return err
}
}
Expand Down Expand Up @@ -186,17 +187,17 @@ func (o *Options) Run() error {
// }
func Apply(
o *Options,
storage states.StateStorage,
storage state.Storage,
planResources *apiv1.Intent,
changes *opsmodels.Changes,
changes *models.Changes,
out io.Writer,
) error {
// Construct the apply operation
ac := &operation.ApplyOperation{
Operation: opsmodels.Operation{
Operation: models.Operation{
Stack: changes.Stack(),
StateStorage: storage,
MsgCh: make(chan opsmodels.Message),
MsgCh: make(chan models.Message),
IgnoreFields: o.IgnoreFields,
},
}
Expand Down Expand Up @@ -234,13 +235,13 @@ func Apply(
changeStep := changes.Get(msg.ResourceID)

switch msg.OpResult {
case opsmodels.Success, opsmodels.Skip:
case models.Success, models.Skip:
var title string
if changeStep.Action == opsmodels.UnChanged {
if changeStep.Action == models.UnChanged {
title = fmt.Sprintf("%s %s, %s",
changeStep.Action.String(),
pterm.Bold.Sprint(changeStep.ID),
strings.ToLower(string(opsmodels.Skip)),
strings.ToLower(string(models.Skip)),
)
} else {
title = fmt.Sprintf("%s %s %s",
Expand All @@ -253,7 +254,7 @@ func Apply(
progressbar.UpdateTitle(title)
progressbar.Increment()
ls.Count(changeStep.Action)
case opsmodels.Failed:
case models.Failed:
title := fmt.Sprintf("%s %s %s",
changeStep.Action.String(),
pterm.Bold.Sprint(changeStep.ID),
Expand All @@ -274,24 +275,22 @@ func Apply(

if o.DryRun {
for _, r := range planResources.Resources {
ac.MsgCh <- opsmodels.Message{
ac.MsgCh <- models.Message{
ResourceID: r.ResourceKey(),
OpResult: opsmodels.Success,
OpResult: models.Success,
OpErr: nil,
}
}
close(ac.MsgCh)
} else {
// parse cluster in arguments
cluster := o.Arguments["cluster"]
_, st := ac.Apply(&operation.ApplyRequest{
Request: opsmodels.Request{
Tenant: "",
Project: changes.Project(),
Stack: changes.Stack(),
Cluster: cluster,
Operator: o.Operator,
Intent: planResources,
Request: models.Request{
Project: changes.Project(),
Stack: changes.Stack(),
Workspace: changes.Workspace(),
Operator: o.Operator,
Intent: planResources,
},
})
if v1.IsErr(st) {
Expand Down Expand Up @@ -324,7 +323,7 @@ func Apply(
func Watch(
o *Options,
planResources *apiv1.Intent,
changes *opsmodels.Changes,
changes *models.Changes,
) error {
if o.DryRun {
fmt.Println("NOTE: Watch doesn't work in DryRun mode")
Expand All @@ -334,15 +333,15 @@ func Watch(
// Filter out unchanged resources
toBeWatched := apiv1.Resources{}
for _, res := range planResources.Resources {
if changes.ChangeOrder.ChangeSteps[res.ResourceKey()].Action != opsmodels.UnChanged {
if changes.ChangeOrder.ChangeSteps[res.ResourceKey()].Action != models.UnChanged {
toBeWatched = append(toBeWatched, res)
}
}

// Watch operation
wo := &operation.WatchOperation{}
if err := wo.Watch(&operation.WatchRequest{
Request: opsmodels.Request{
Request: models.Request{
Project: changes.Project(),
Stack: changes.Stack(),
Intent: &apiv1.Intent{Resources: toBeWatched},
Expand All @@ -359,20 +358,20 @@ type lineSummary struct {
created, updated, deleted int
}

func (ls *lineSummary) Count(op opsmodels.ActionType) {
func (ls *lineSummary) Count(op models.ActionType) {
switch op {
case opsmodels.Create:
case models.Create:
ls.created++
case opsmodels.Update:
case models.Update:
ls.updated++
case opsmodels.Delete:
case models.Delete:
ls.deleted++
}
}

func allUnChange(changes *opsmodels.Changes) bool {
func allUnChange(changes *models.Changes) bool {
for _, v := range changes.ChangeSteps {
if v.Action != opsmodels.UnChanged {
if v.Action != models.UnChanged {
return false
}
}
Expand All @@ -384,14 +383,14 @@ func prompt() (string, error) {
// don`t display yes item when only preview
options := []string{"yes", "details", "no"}

prompt := &survey.Select{
p := &survey.Select{
Message: `Do you want to apply these diffs?`,
Options: options,
Default: "details",
}

var input string
err := survey.AskOne(prompt, &input)
err := survey.AskOne(p, &input)
if err != nil {
fmt.Printf("Prompt failed %v\n", err)
return "", err
Expand Down
Loading
Loading