From 5cc6d616d34fd99a7104d74ea1f699d9a68ddee2 Mon Sep 17 00:00:00 2001 From: Shiming Zhang Date: Thu, 7 Dec 2023 15:45:17 +0800 Subject: [PATCH] Clean up snapshot yaml configuration --- pkg/kwokctl/cmd/snapshot/export/export.go | 3 ++- pkg/kwokctl/cmd/snapshot/restore/restore.go | 6 ++++-- pkg/kwokctl/cmd/snapshot/save/save.go | 4 +++- pkg/kwokctl/runtime/binary/cluster_snapshot.go | 8 ++++---- pkg/kwokctl/runtime/cluster.go | 2 +- pkg/kwokctl/runtime/cluster_snapshot.go | 17 +++++++++-------- pkg/kwokctl/runtime/compose/cluster_snapshot.go | 9 +++++---- pkg/kwokctl/runtime/config.go | 12 ++++++++++-- pkg/kwokctl/runtime/kind/cluster_snapshot.go | 9 +++++---- pkg/kwokctl/scale/scale.go | 2 +- pkg/kwokctl/snapshot/load.go | 11 ++++++++--- pkg/kwokctl/snapshot/save.go | 7 ++++--- 12 files changed, 56 insertions(+), 34 deletions(-) diff --git a/pkg/kwokctl/cmd/snapshot/export/export.go b/pkg/kwokctl/cmd/snapshot/export/export.go index df96437d8..9a63f6af5 100644 --- a/pkg/kwokctl/cmd/snapshot/export/export.go +++ b/pkg/kwokctl/cmd/snapshot/export/export.go @@ -101,9 +101,10 @@ func runE(ctx context.Context, flags *flagpole) error { snapshotSaveConfig := snapshot.SaveConfig{ PagerConfig: pagerConfig, + Filters: flags.Filters, } - err = snapshot.Save(ctx, clientset, f, flags.Filters, snapshotSaveConfig) + err = snapshot.Save(ctx, clientset, f, snapshotSaveConfig) if err != nil { return err } diff --git a/pkg/kwokctl/cmd/snapshot/restore/restore.go b/pkg/kwokctl/cmd/snapshot/restore/restore.go index cb82f932d..72e98c6db 100644 --- a/pkg/kwokctl/cmd/snapshot/restore/restore.go +++ b/pkg/kwokctl/cmd/snapshot/restore/restore.go @@ -40,7 +40,7 @@ type flagpole struct { Filters []string } -// NewCommand returns a new cobra.Command to save the cluster as a snapshot. +// NewCommand returns a new cobra.Command to restore the cluster as a snapshot. func NewCommand(ctx context.Context) *cobra.Command { flags := &flagpole{} @@ -88,7 +88,9 @@ func runE(ctx context.Context, flags *flagpole) error { return err } case "k8s": - err = rt.SnapshotRestoreWithYAML(ctx, flags.Path, flags.Filters) + err = rt.SnapshotRestoreWithYAML(ctx, flags.Path, runtime.SnapshotRestoreWithYAMLConfig{ + Filters: flags.Filters, + }) if err != nil { return err } diff --git a/pkg/kwokctl/cmd/snapshot/save/save.go b/pkg/kwokctl/cmd/snapshot/save/save.go index 836398330..abc6f1c0b 100644 --- a/pkg/kwokctl/cmd/snapshot/save/save.go +++ b/pkg/kwokctl/cmd/snapshot/save/save.go @@ -88,7 +88,9 @@ func runE(ctx context.Context, flags *flagpole) error { return err } case "k8s": - err = rt.SnapshotSaveWithYAML(ctx, flags.Path, flags.Filters) + err = rt.SnapshotSaveWithYAML(ctx, flags.Path, runtime.SnapshotSaveWithYAMLConfig{ + Filters: flags.Filters, + }) if err != nil { return err } diff --git a/pkg/kwokctl/runtime/binary/cluster_snapshot.go b/pkg/kwokctl/runtime/binary/cluster_snapshot.go index 42864d171..06b85ff83 100644 --- a/pkg/kwokctl/runtime/binary/cluster_snapshot.go +++ b/pkg/kwokctl/runtime/binary/cluster_snapshot.go @@ -99,8 +99,8 @@ func (c *Cluster) SnapshotRestore(ctx context.Context, path string) error { } // SnapshotSaveWithYAML save the snapshot of cluster -func (c *Cluster) SnapshotSaveWithYAML(ctx context.Context, path string, filters []string) error { - err := c.Cluster.SnapshotSaveWithYAML(ctx, path, filters) +func (c *Cluster) SnapshotSaveWithYAML(ctx context.Context, path string, conf runtime.SnapshotSaveWithYAMLConfig) error { + err := c.Cluster.SnapshotSaveWithYAML(ctx, path, conf) if err != nil { return err } @@ -108,7 +108,7 @@ func (c *Cluster) SnapshotSaveWithYAML(ctx context.Context, path string, filters } // SnapshotRestoreWithYAML restore the snapshot of cluster -func (c *Cluster) SnapshotRestoreWithYAML(ctx context.Context, path string, filters []string) error { +func (c *Cluster) SnapshotRestoreWithYAML(ctx context.Context, path string, conf runtime.SnapshotRestoreWithYAMLConfig) error { logger := log.FromContext(ctx) err := wait.Poll(ctx, func(ctx context.Context) (bool, error) { err := c.StopComponent(ctx, consts.ComponentKubeControllerManager) @@ -132,7 +132,7 @@ func (c *Cluster) SnapshotRestoreWithYAML(ctx context.Context, path string, filt } }() - err = c.Cluster.SnapshotRestoreWithYAML(ctx, path, filters) + err = c.Cluster.SnapshotRestoreWithYAML(ctx, path, conf) if err != nil { return err } diff --git a/pkg/kwokctl/runtime/cluster.go b/pkg/kwokctl/runtime/cluster.go index 00b71ed9d..4e97e81fd 100644 --- a/pkg/kwokctl/runtime/cluster.go +++ b/pkg/kwokctl/runtime/cluster.go @@ -558,7 +558,7 @@ func (c *Cluster) InitCRDs(ctx context.Context) error { logger := log.FromContext(ctx) ctx = log.NewContext(ctx, logger.With("crds", strings.Join(crds, ","))) - return snapshot.Load(ctx, clientset, buf, nil) + return snapshot.Load(ctx, clientset, buf, snapshot.LoadConfig{}) } var crdDefines = map[string][]byte{ diff --git a/pkg/kwokctl/runtime/cluster_snapshot.go b/pkg/kwokctl/runtime/cluster_snapshot.go index d11162668..dd00b2990 100644 --- a/pkg/kwokctl/runtime/cluster_snapshot.go +++ b/pkg/kwokctl/runtime/cluster_snapshot.go @@ -26,9 +26,9 @@ import ( ) // SnapshotSaveWithYAML save the snapshot of cluster -func (c *Cluster) SnapshotSaveWithYAML(ctx context.Context, path string, filters []string) error { +func (c *Cluster) SnapshotSaveWithYAML(ctx context.Context, path string, conf SnapshotSaveWithYAMLConfig) error { if c.IsDryRun() { - dryrun.PrintMessage("kubectl get %s -o yaml >%s", strings.Join(filters, ","), path) + dryrun.PrintMessage("kubectl get %s -o yaml >%s", strings.Join(conf.Filters, ","), path) return nil } @@ -45,14 +45,13 @@ func (c *Cluster) SnapshotSaveWithYAML(ctx context.Context, path string, filters _ = f.Close() }() - // In most cases, the user should have full privileges on the clusters created by kwokctl, - // so no need to expose impersonation args to "snapshot save" command. - snapshotSaveConfig := snapshot.SaveConfig{} - return snapshot.Save(ctx, clientset, f, filters, snapshotSaveConfig) + return snapshot.Save(ctx, clientset, f, snapshot.SaveConfig{ + Filters: conf.Filters, + }) } // SnapshotRestoreWithYAML restore the snapshot of cluster -func (c *Cluster) SnapshotRestoreWithYAML(ctx context.Context, path string, filters []string) error { +func (c *Cluster) SnapshotRestoreWithYAML(ctx context.Context, path string, conf SnapshotRestoreWithYAMLConfig) error { if c.IsDryRun() { dryrun.PrintMessage("kubectl create -f %s", path) return nil @@ -71,5 +70,7 @@ func (c *Cluster) SnapshotRestoreWithYAML(ctx context.Context, path string, filt _ = f.Close() }() - return snapshot.Load(ctx, clientset, f, filters) + return snapshot.Load(ctx, clientset, f, snapshot.LoadConfig{ + Filters: conf.Filters, + }) } diff --git a/pkg/kwokctl/runtime/compose/cluster_snapshot.go b/pkg/kwokctl/runtime/compose/cluster_snapshot.go index eb4e42973..76a28dc4c 100644 --- a/pkg/kwokctl/runtime/compose/cluster_snapshot.go +++ b/pkg/kwokctl/runtime/compose/cluster_snapshot.go @@ -20,6 +20,7 @@ import ( "context" "sigs.k8s.io/kwok/pkg/consts" + "sigs.k8s.io/kwok/pkg/kwokctl/runtime" "sigs.k8s.io/kwok/pkg/log" ) @@ -178,8 +179,8 @@ func (c *Cluster) SnapshotRestore(ctx context.Context, path string) error { } // SnapshotSaveWithYAML save the snapshot of cluster -func (c *Cluster) SnapshotSaveWithYAML(ctx context.Context, path string, filters []string) error { - err := c.Cluster.SnapshotSaveWithYAML(ctx, path, filters) +func (c *Cluster) SnapshotSaveWithYAML(ctx context.Context, path string, conf runtime.SnapshotSaveWithYAMLConfig) error { + err := c.Cluster.SnapshotSaveWithYAML(ctx, path, conf) if err != nil { return err } @@ -187,7 +188,7 @@ func (c *Cluster) SnapshotSaveWithYAML(ctx context.Context, path string, filters } // SnapshotRestoreWithYAML restore the snapshot of cluster -func (c *Cluster) SnapshotRestoreWithYAML(ctx context.Context, path string, filters []string) error { +func (c *Cluster) SnapshotRestoreWithYAML(ctx context.Context, path string, conf runtime.SnapshotRestoreWithYAMLConfig) error { logger := log.FromContext(ctx) err := c.StopComponent(ctx, consts.ComponentKubeControllerManager) if err != nil { @@ -200,7 +201,7 @@ func (c *Cluster) SnapshotRestoreWithYAML(ctx context.Context, path string, filt } }() - err = c.Cluster.SnapshotRestoreWithYAML(ctx, path, filters) + err = c.Cluster.SnapshotRestoreWithYAML(ctx, path, conf) if err != nil { return err } diff --git a/pkg/kwokctl/runtime/config.go b/pkg/kwokctl/runtime/config.go index 3c3b72d0d..299990aa5 100644 --- a/pkg/kwokctl/runtime/config.go +++ b/pkg/kwokctl/runtime/config.go @@ -114,10 +114,10 @@ type Runtime interface { SnapshotRestore(ctx context.Context, path string) error // SnapshotSaveWithYAML save the snapshot of cluster - SnapshotSaveWithYAML(ctx context.Context, path string, filters []string) error + SnapshotSaveWithYAML(ctx context.Context, path string, conf SnapshotSaveWithYAMLConfig) error // SnapshotRestoreWithYAML restore the snapshot of cluster - SnapshotRestoreWithYAML(ctx context.Context, path string, filters []string) error + SnapshotRestoreWithYAML(ctx context.Context, path string, conf SnapshotRestoreWithYAMLConfig) error // GetWorkdirPath get the workdir path of cluster GetWorkdirPath(name string) string @@ -128,3 +128,11 @@ type Runtime interface { // IsDryRun returns true if the runtime is in dry-run mode IsDryRun() bool } + +type SnapshotSaveWithYAMLConfig struct { + Filters []string +} + +type SnapshotRestoreWithYAMLConfig struct { + Filters []string +} diff --git a/pkg/kwokctl/runtime/kind/cluster_snapshot.go b/pkg/kwokctl/runtime/kind/cluster_snapshot.go index 1f6fedda4..852836c00 100644 --- a/pkg/kwokctl/runtime/kind/cluster_snapshot.go +++ b/pkg/kwokctl/runtime/kind/cluster_snapshot.go @@ -20,6 +20,7 @@ import ( "context" "sigs.k8s.io/kwok/pkg/consts" + "sigs.k8s.io/kwok/pkg/kwokctl/runtime" "sigs.k8s.io/kwok/pkg/log" "sigs.k8s.io/kwok/pkg/utils/wait" ) @@ -109,8 +110,8 @@ func (c *Cluster) SnapshotRestore(ctx context.Context, path string) error { } // SnapshotSaveWithYAML save the snapshot of cluster -func (c *Cluster) SnapshotSaveWithYAML(ctx context.Context, path string, filters []string) error { - err := c.Cluster.SnapshotSaveWithYAML(ctx, path, filters) +func (c *Cluster) SnapshotSaveWithYAML(ctx context.Context, path string, conf runtime.SnapshotSaveWithYAMLConfig) error { + err := c.Cluster.SnapshotSaveWithYAML(ctx, path, conf) if err != nil { return err } @@ -118,7 +119,7 @@ func (c *Cluster) SnapshotSaveWithYAML(ctx context.Context, path string, filters } // SnapshotRestoreWithYAML restore the snapshot of cluster -func (c *Cluster) SnapshotRestoreWithYAML(ctx context.Context, path string, filters []string) error { +func (c *Cluster) SnapshotRestoreWithYAML(ctx context.Context, path string, conf runtime.SnapshotRestoreWithYAMLConfig) error { logger := log.FromContext(ctx) err := wait.Poll(ctx, func(ctx context.Context) (bool, error) { err := c.StopComponent(ctx, consts.ComponentKubeControllerManager) @@ -134,7 +135,7 @@ func (c *Cluster) SnapshotRestoreWithYAML(ctx context.Context, path string, filt } }() - err = c.Cluster.SnapshotRestoreWithYAML(ctx, path, filters) + err = c.Cluster.SnapshotRestoreWithYAML(ctx, path, conf) if err != nil { return err } diff --git a/pkg/kwokctl/scale/scale.go b/pkg/kwokctl/scale/scale.go index 339cf51fb..117d6ee38 100644 --- a/pkg/kwokctl/scale/scale.go +++ b/pkg/kwokctl/scale/scale.go @@ -282,7 +282,7 @@ func Scale(ctx context.Context, clientset client.Clientset, conf Config) error { }, wantCreate) ctx = log.NewContext(ctx, logger) - err = snapshot.Load(ctx, clientset, gen, nil) + err = snapshot.Load(ctx, clientset, gen, snapshot.LoadConfig{}) if err != nil { return err } diff --git a/pkg/kwokctl/snapshot/load.go b/pkg/kwokctl/snapshot/load.go index 6b6952dca..4c6b39542 100644 --- a/pkg/kwokctl/snapshot/load.go +++ b/pkg/kwokctl/snapshot/load.go @@ -41,13 +41,18 @@ import ( "sigs.k8s.io/kwok/pkg/utils/yaml" ) +// LoadConfig is the a combination of the impersonation config +type LoadConfig struct { + Filters []string +} + // Load loads the resources to cluster from the reader -func Load(ctx context.Context, clientset client.Clientset, r io.Reader, filters []string) error { - l, err := newLoader(clientset, filters == nil) +func Load(ctx context.Context, clientset client.Clientset, r io.Reader, loadConfig LoadConfig) error { + l, err := newLoader(clientset, loadConfig.Filters == nil) if err != nil { return err } - l.addResource(ctx, filters) + l.addResource(ctx, loadConfig.Filters) return l.Load(ctx, r) } diff --git a/pkg/kwokctl/snapshot/save.go b/pkg/kwokctl/snapshot/save.go index 17138a03c..095f92205 100644 --- a/pkg/kwokctl/snapshot/save.go +++ b/pkg/kwokctl/snapshot/save.go @@ -46,10 +46,11 @@ type PagerConfig struct { // and the PagerConfig. type SaveConfig struct { PagerConfig *PagerConfig + Filters []string } // Save saves the snapshot of cluster -func Save(ctx context.Context, clientset client.Clientset, w io.Writer, resources []string, saveConfig SaveConfig) error { +func Save(ctx context.Context, clientset client.Clientset, w io.Writer, saveConfig SaveConfig) error { restMapper, err := clientset.ToRESTMapper() if err != nil { return fmt.Errorf("failed to get rest mapper: %w", err) @@ -61,8 +62,8 @@ func Save(ctx context.Context, clientset client.Clientset, w io.Writer, resource logger := log.FromContext(ctx) - gvrs := make([]schema.GroupVersionResource, 0, len(resources)) - for _, resource := range resources { + gvrs := make([]schema.GroupVersionResource, 0, len(saveConfig.Filters)) + for _, resource := range saveConfig.Filters { mapping, err := client.MappingFor(restMapper, resource) if err != nil { logger.Warn("Failed to get mapping for resource", "resource", resource, "err", err)