diff --git a/pkg/kwokctl/dryrun/dryrun.go b/pkg/kwokctl/dryrun/dryrun.go index 92721285f3..f94fa32920 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 117d6ee385..b93a64663b 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 { @@ -282,14 +298,28 @@ func Scale(ctx context.Context, clientset client.Clientset, conf Config) error { }, wantCreate) ctx = log.NewContext(ctx, logger) - err = snapshot.Load(ctx, clientset, gen, snapshot.LoadConfig{}) - if err != nil { - return err + if conf.DryRun { + dryrun.PrintMessage("kubectl apply -f - <