From eae067d51295639f77e72e1b32cac51989a4a21e Mon Sep 17 00:00:00 2001 From: Shiming Zhang Date: Mon, 29 Jan 2024 11:55:52 +0800 Subject: [PATCH] kwokctl: support dryrun for scale --- pkg/kwokctl/dryrun/dryrun.go | 5 +++ pkg/kwokctl/scale/scale.go | 60 +++++++++++++++++++++++++++--------- 2 files changed, 50 insertions(+), 15 deletions(-) diff --git a/pkg/kwokctl/dryrun/dryrun.go b/pkg/kwokctl/dryrun/dryrun.go index 92721285f..f94fa3292 100644 --- a/pkg/kwokctl/dryrun/dryrun.go +++ b/pkg/kwokctl/dryrun/dryrun.go @@ -34,6 +34,11 @@ func PrintMessage(format string, a ...any) { _, _ = fmt.Fprintf(stdout, format+"\n", a...) } +// PrintReader prints the content of the reader to stdout. +func PrintReader(r io.Reader) { + _, _ = io.Copy(stdout, r) +} + type dryRunWriter struct { name string w io.Writer diff --git a/pkg/kwokctl/scale/scale.go b/pkg/kwokctl/scale/scale.go index 609371b4d..614be8bb7 100644 --- a/pkg/kwokctl/scale/scale.go +++ b/pkg/kwokctl/scale/scale.go @@ -164,6 +164,10 @@ func Scale(ctx context.Context, clientset client.Clientset, conf Config) error { deleteCount++ if len(objs) == 0 { + if conf.DryRun { + deleteDryrun(gvr, obj.GetName(), namespace) + return nil + } err = ri.Delete(ctx, obj.GetName(), metav1.DeleteOptions{}) if err != nil { logger.Error("Delete resource", err) @@ -174,6 +178,10 @@ func Scale(ctx context.Context, clientset client.Clientset, conf Config) error { // New object is newer than the end object, delete the new object. endObj := objs[len(objs)-1] if endObj.Less(obj.GetCreationTimestamp(), obj.GetName()) { + if conf.DryRun { + deleteDryrun(gvr, obj.GetName(), namespace) + return nil + } // Delete the last object. err = ri.Delete(ctx, obj.GetName(), metav1.DeleteOptions{}) if err != nil { @@ -182,10 +190,14 @@ func Scale(ctx context.Context, clientset client.Clientset, conf Config) error { return nil } - // Delete the end object. - err = ri.Delete(ctx, endObj.Name, metav1.DeleteOptions{}) - if err != nil { - logger.Error("Delete resource", err) + if conf.DryRun { + deleteDryrun(gvr, endObj.Name, namespace) + } else { + // Delete the end object. + err = ri.Delete(ctx, endObj.Name, metav1.DeleteOptions{}) + if err != nil { + logger.Error("Delete resource", err) + } } // Find the index of the new object to be inserted. @@ -197,6 +209,10 @@ func Scale(ctx context.Context, clientset client.Clientset, conf Config) error { }) if index == len(objs) { + if conf.DryRun { + deleteDryrun(gvr, obj.GetName(), namespace) + return nil + } // Delete the last object. err = ri.Delete(ctx, obj.GetName(), metav1.DeleteOptions{}) if err != nil { @@ -283,23 +299,37 @@ func Scale(ctx context.Context, clientset client.Clientset, conf Config) error { ctx = log.NewContext(ctx, logger) - loader, err := snapshot.NewLoader(snapshot.LoadConfig{ - Clientset: clientset, - NoFilers: true, - }) - if err != nil { - return err - } + if conf.DryRun { + dryrun.PrintMessage("kubectl apply -f - <