Skip to content

Commit

Permalink
migrate: Make finalize more stylish
Browse files Browse the repository at this point in the history
Co-authored-by: Trong Nguyen <trong.huu.nguyen@nav.no>
  • Loading branch information
mortenlj and tronghn committed Oct 4, 2024
1 parent df1a140 commit 35ac7ab
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 29 deletions.
3 changes: 2 additions & 1 deletion cmd/postgrescmd/migratecmd/finalizecmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package migratecmd
import (
"context"
"fmt"
"github.com/pterm/pterm"

"github.com/nais/cli/pkg/k8s"
"github.com/nais/cli/pkg/postgres/migrate"
Expand All @@ -25,7 +26,7 @@ func finalizeCommand() *cli.Command {
cfg := makeConfig(cCtx)
cluster := cCtx.String(contextFlagName)

fmt.Println(cCtx.Command.Description)
pterm.Println(cCtx.Command.Description)

client := k8s.SetupClient(k8s.WithKubeContext(cluster))
migrator := migrate.NewMigrator(client, cfg, cCtx.Bool(dryRunFlagName))
Expand Down
3 changes: 2 additions & 1 deletion cmd/postgrescmd/migratecmd/promotecmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package migratecmd
import (
"context"
"fmt"
"github.com/pterm/pterm"

"github.com/nais/cli/pkg/k8s"
"github.com/nais/cli/pkg/postgres/migrate"
Expand All @@ -25,7 +26,7 @@ func promoteCommand() *cli.Command {
cfg := makeConfig(cCtx)
cluster := cCtx.String(contextFlagName)

fmt.Println(cCtx.Command.Description)
pterm.Println(cCtx.Command.Description)

client := k8s.SetupClient(k8s.WithKubeContext(cluster))
migrator := migrate.NewMigrator(client, cfg, cCtx.Bool(dryRunFlagName))
Expand Down
3 changes: 2 additions & 1 deletion cmd/postgrescmd/migratecmd/rollbackcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package migratecmd
import (
"context"
"fmt"
"github.com/pterm/pterm"

"github.com/nais/cli/pkg/k8s"
"github.com/nais/cli/pkg/postgres/migrate"
Expand All @@ -25,7 +26,7 @@ func rollbackCommand() *cli.Command {
cfg := makeConfig(cCtx)
cluster := cCtx.String(contextFlagName)

fmt.Println(cCtx.Command.Description)
pterm.Println(cCtx.Command.Description)

client := k8s.SetupClient(k8s.WithKubeContext(cluster))
migrator := migrate.NewMigrator(client, cfg, cCtx.Bool(dryRunFlagName))
Expand Down
40 changes: 16 additions & 24 deletions pkg/postgres/migrate/finalize.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,18 @@ package migrate

import (
"context"
"fmt"
"github.com/pterm/pterm"
)

const FinalizeStartedMessage = `
Finalize has been started successfully.
To monitor the finalize, run the following command in a separate terminal:
kubectl logs -f -l %s -n %s
Pausing to wait for finalize job to complete in order to do final finalize actions ...
`

const FinalizeSuccessMessage = `
Finalize has completed successfully.
The old instance has been deleted and the migration is complete.
Congratulations, you're all done! 🎉
`

func (m *Migrator) Finalize(ctx context.Context) error {
fmt.Println("Resolving config")
pterm.Println("Resolving config ...")
cfgMap, err := m.cfg.PopulateFromConfigMap(ctx, m.client)
if err != nil {
return err
}

m.printConfig()
fmt.Print(`
This will delete the old database instance. Rollback after this point is not possible.
pterm.Warning.Print(`This will delete the old database instance. Rollback after this point is not possible.
Only proceed if you are sure that the migration was successful and that your application is working as expected.
`)

Expand All @@ -47,7 +28,13 @@ Only proceed if you are sure that the migration was successful and that your app
}

label := m.kubectlLabelSelector(CommandFinalize)
fmt.Printf(FinalizeStartedMessage, label, m.cfg.Namespace)

pterm.DefaultHeader.Println("Finalize has been started successfully")
pterm.Println()
pterm.Println("To monitor the finalize, run the following command in a separate terminal:")
cmdStyle.Printfln("\tkubectl logs -f -l %s -n %s", label, m.cfg.Namespace)
pterm.Println()
pterm.Println("Pausing to wait for finalize job to complete in order to do final finalize actions ...")

err = m.waitForJobCompletion(ctx, jobName, CommandFinalize)
if err != nil {
Expand All @@ -59,6 +46,11 @@ Only proceed if you are sure that the migration was successful and that your app
return err
}

fmt.Print(FinalizeSuccessMessage)
pterm.DefaultHeader.Println("Finalize has completed successfully")
pterm.Println()
pterm.Println("The old instance has been deleted and the migration is complete.")
pterm.Println()
pterm.Println("Congratulations, you're all done! 🎉")

return nil
}
4 changes: 2 additions & 2 deletions pkg/postgres/migrate/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func NewMigrator(client ctrl.Client, cfg config.Config, dryRun bool) *Migrator {
func (m *Migrator) Create(ctx context.Context, obj ctrl.Object) error {
if m.dryRun {
v := reflect.Indirect(reflect.ValueOf(obj))
fmt.Printf("Dry run: Skipping creation of %s: %s\n", v.Type().Name(), obj.GetName())
pterm.Printf("Dry run: Skipping creation of %s: %s\n", v.Type().Name(), obj.GetName())
return nil
}
return m.client.Create(ctx, obj)
Expand All @@ -66,7 +66,7 @@ func (m *Migrator) Create(ctx context.Context, obj ctrl.Object) error {
func (m *Migrator) Delete(ctx context.Context, obj ctrl.Object) error {
if m.dryRun {
v := reflect.Indirect(reflect.ValueOf(obj))
fmt.Printf("Dry run: Skipping deletion of %s: %s\n", v.Type().Name(), obj.GetName())
pterm.Printf("Dry run: Skipping deletion of %s: %s\n", v.Type().Name(), obj.GetName())
return nil
}
return m.client.Delete(ctx, obj)
Expand Down

0 comments on commit 35ac7ab

Please sign in to comment.