diff --git a/pkg/kwokctl/cmd/snapshot/export/export.go b/pkg/kwokctl/cmd/snapshot/export/export.go index aff66b37a1..dd4d160185 100644 --- a/pkg/kwokctl/cmd/snapshot/export/export.go +++ b/pkg/kwokctl/cmd/snapshot/export/export.go @@ -83,7 +83,18 @@ func runE(ctx context.Context, flags *flagpole) error { UserName: flags.ImpersonateUser, Groups: flags.ImpersonateGroups, } - err = snapshot.Save(ctx, flags.Kubeconfig, file, flags.Filters, impersonateConfig, flags.PageSize, flags.PageBufferSize) + + pagerConfig := &snapshot.PagerConfig{ + PageSize: flags.PageSize, + PageBufferSize: flags.PageBufferSize, + } + + snapshotSaveConfig := snapshot.SnapshotSaveConfig{ + PagerConfig: pagerConfig, + ImpersonationConfig: impersonateConfig, + } + + err = snapshot.Save(ctx, flags.Kubeconfig, file, flags.Filters, snapshotSaveConfig) if err != nil { return err } diff --git a/pkg/kwokctl/cmd/snapshot/save/save.go b/pkg/kwokctl/cmd/snapshot/save/save.go index 796e2f4341..9fc45ae2e2 100644 --- a/pkg/kwokctl/cmd/snapshot/save/save.go +++ b/pkg/kwokctl/cmd/snapshot/save/save.go @@ -33,12 +33,10 @@ import ( ) type flagpole struct { - Name string - Path string - Format string - Filters []string - PageSize int64 - PageBufferSize int32 + Name string + Path string + Format string + Filters []string } // NewCommand returns a new cobra.Command for cluster snapshotting. @@ -57,8 +55,6 @@ func NewCommand(ctx context.Context) *cobra.Command { cmd.Flags().StringVar(&flags.Path, "path", "", "Path to the snapshot") cmd.Flags().StringVar(&flags.Format, "format", "etcd", "Format of the snapshot file (etcd, k8s)") cmd.Flags().StringSliceVar(&flags.Filters, "filter", snapshot.Resources, "Filter the resources to save, only support for k8s format") - cmd.Flags().Int64Var(&flags.PageSize, "page-size", 500, "Define the page size") - cmd.Flags().Int32Var(&flags.PageBufferSize, "page-buffer-size", 10, "Define the number of pages to buffer") return cmd } @@ -91,7 +87,7 @@ func runE(ctx context.Context, flags *flagpole) error { return err } case "k8s": - err = rt.SnapshotSaveWithYAML(ctx, flags.Path, flags.Filters, flags.PageSize, flags.PageBufferSize) + err = rt.SnapshotSaveWithYAML(ctx, flags.Path, 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 8cfe2bcf19..1ecf27c528 100644 --- a/pkg/kwokctl/runtime/binary/cluster_snapshot.go +++ b/pkg/kwokctl/runtime/binary/cluster_snapshot.go @@ -103,8 +103,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, pageSize int64, pageBufferSize int32) error { - err := c.Cluster.SnapshotSaveWithYAML(ctx, path, filters, pageSize, pageBufferSize) +func (c *Cluster) SnapshotSaveWithYAML(ctx context.Context, path string, filters []string) error { + err := c.Cluster.SnapshotSaveWithYAML(ctx, path, filters) if err != nil { return err } diff --git a/pkg/kwokctl/runtime/cluster_snapshot.go b/pkg/kwokctl/runtime/cluster_snapshot.go index 1ddeaec290..afefe88129 100644 --- a/pkg/kwokctl/runtime/cluster_snapshot.go +++ b/pkg/kwokctl/runtime/cluster_snapshot.go @@ -27,7 +27,7 @@ import ( ) // SnapshotSaveWithYAML save the snapshot of cluster -func (c *Cluster) SnapshotSaveWithYAML(ctx context.Context, path string, filters []string, pageSize int64, pageBufferSize int32) error { +func (c *Cluster) SnapshotSaveWithYAML(ctx context.Context, path string, filters []string) error { file, err := os.Create(path) if err != nil { return err @@ -38,7 +38,10 @@ func (c *Cluster) SnapshotSaveWithYAML(ctx context.Context, path string, filters kubeconfigPath := c.GetWorkdirPath(InHostKubeconfigName) // 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. - return snapshot.Save(ctx, kubeconfigPath, file, filters, rest.ImpersonationConfig{}, pageSize, pageBufferSize) + snapshotSaveConfig := snapshot.SnapshotSaveConfig{ + ImpersonationConfig: rest.ImpersonationConfig{}, + } + return snapshot.Save(ctx, kubeconfigPath, file, filters, snapshotSaveConfig) } // SnapshotRestoreWithYAML restore the snapshot of cluster diff --git a/pkg/kwokctl/runtime/compose/cluster_snapshot.go b/pkg/kwokctl/runtime/compose/cluster_snapshot.go index 8abef33dee..5d89eb2ce3 100644 --- a/pkg/kwokctl/runtime/compose/cluster_snapshot.go +++ b/pkg/kwokctl/runtime/compose/cluster_snapshot.go @@ -140,8 +140,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, pageSize int64, pageBufferSize int32) error { - err := c.Cluster.SnapshotSaveWithYAML(ctx, path, filters, pageSize, pageBufferSize) +func (c *Cluster) SnapshotSaveWithYAML(ctx context.Context, path string, filters []string) error { + err := c.Cluster.SnapshotSaveWithYAML(ctx, path, filters) if err != nil { return err } diff --git a/pkg/kwokctl/runtime/config.go b/pkg/kwokctl/runtime/config.go index 2560505be6..2734d434dd 100644 --- a/pkg/kwokctl/runtime/config.go +++ b/pkg/kwokctl/runtime/config.go @@ -111,7 +111,7 @@ type Runtime interface { SnapshotRestore(ctx context.Context, path string) error // SnapshotSaveWithYAML save the snapshot of cluster - SnapshotSaveWithYAML(ctx context.Context, path string, filters []string, pageSize int64, pageBufferSize int32) error + SnapshotSaveWithYAML(ctx context.Context, path string, filters []string) error // SnapshotRestoreWithYAML restore the snapshot of cluster SnapshotRestoreWithYAML(ctx context.Context, path string, filters []string) error diff --git a/pkg/kwokctl/runtime/kind/cluster_snapshot.go b/pkg/kwokctl/runtime/kind/cluster_snapshot.go index 386c9c4758..4c34895363 100644 --- a/pkg/kwokctl/runtime/kind/cluster_snapshot.go +++ b/pkg/kwokctl/runtime/kind/cluster_snapshot.go @@ -110,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, pageSize int64, pageBufferSize int32) error { - err := c.Cluster.SnapshotSaveWithYAML(ctx, path, filters, pageSize, pageBufferSize) +func (c *Cluster) SnapshotSaveWithYAML(ctx context.Context, path string, filters []string) error { + err := c.Cluster.SnapshotSaveWithYAML(ctx, path, filters) if err != nil { return err } diff --git a/pkg/kwokctl/snapshot/save.go b/pkg/kwokctl/snapshot/save.go index b5bf8c9ce0..492768d3d4 100644 --- a/pkg/kwokctl/snapshot/save.go +++ b/pkg/kwokctl/snapshot/save.go @@ -36,10 +36,24 @@ import ( "sigs.k8s.io/kwok/pkg/utils/yaml" ) +// PagerConfig is the configuration of the list pager. +// It defines the page size and the page buffer size of the list pager. +type PagerConfig struct { + PageSize int64 + PageBufferSize int32 +} + +// SnapshotSaveConfig is the a combination of the impersonation config +// and the PagerConfig. +type SnapshotSaveConfig struct { + PagerConfig *PagerConfig + ImpersonationConfig rest.ImpersonationConfig +} + // Save saves the snapshot of cluster -func Save(ctx context.Context, kubeconfigPath string, w io.Writer, resources []string, impersonateConfig rest.ImpersonationConfig, pageSize int64, pageBufferSize int32) error { +func Save(ctx context.Context, kubeconfigPath string, w io.Writer, resources []string, snapshotSaveConfig SnapshotSaveConfig) error { clientset, err := client.NewClientset("", kubeconfigPath, - client.WithImpersonate(impersonateConfig), + client.WithImpersonate(snapshotSaveConfig.ImpersonationConfig), ) if err != nil { return fmt.Errorf("failed to create clientset: %w", err) @@ -89,8 +103,16 @@ func Save(ctx context.Context, kubeconfigPath string, w io.Writer, resources []s return list, err }) - listPager.PageSize = pageSize - listPager.PageBufferSize = pageBufferSize + pagerConfig := snapshotSaveConfig.PagerConfig + + if pagerConfig != nil { + if pagerConfig.PageSize > 0 { + listPager.PageSize = pagerConfig.PageSize + } + if pagerConfig.PageBufferSize > 0 { + listPager.PageBufferSize = pagerConfig.PageBufferSize + } + } count := 0 if err := listPager.EachListItem(ctx, metav1.ListOptions{}, func(obj runtime.Object) error { diff --git a/site/content/en/docs/generated/kwokctl_snapshot_export.md b/site/content/en/docs/generated/kwokctl_snapshot_export.md index bfe8d3a649..9999f718b2 100644 --- a/site/content/en/docs/generated/kwokctl_snapshot_export.md +++ b/site/content/en/docs/generated/kwokctl_snapshot_export.md @@ -9,12 +9,14 @@ kwokctl snapshot export [flags] ### Options ``` - --as string Username to impersonate for the operation. User could be a regular user or a service account in a namespace. - --as-group strings Group to impersonate for the operation, this flag can be repeated to specify multiple groups. - --filter strings Filter the resources to export (default [namespace,node,serviceaccount,configmap,secret,limitrange,runtimeclass.node.k8s.io,priorityclass.scheduling.k8s.io,clusterrolebindings.rbac.authorization.k8s.io,clusterroles.rbac.authorization.k8s.io,rolebindings.rbac.authorization.k8s.io,roles.rbac.authorization.k8s.io,daemonset.apps,deployment.apps,replicaset.apps,statefulset.apps,cronjob.batch,job.batch,persistentvolumeclaim,persistentvolume,pod,service,endpoints]) - -h, --help help for export - --kubeconfig string Path to the kubeconfig file to use - --path string Path to the snapshot + --as string Username to impersonate for the operation. User could be a regular user or a service account in a namespace. + --as-group strings Group to impersonate for the operation, this flag can be repeated to specify multiple groups. + --filter strings Filter the resources to export (default [namespace,node,serviceaccount,configmap,secret,limitrange,runtimeclass.node.k8s.io,priorityclass.scheduling.k8s.io,clusterrolebindings.rbac.authorization.k8s.io,clusterroles.rbac.authorization.k8s.io,rolebindings.rbac.authorization.k8s.io,roles.rbac.authorization.k8s.io,daemonset.apps,deployment.apps,replicaset.apps,statefulset.apps,cronjob.batch,job.batch,persistentvolumeclaim,persistentvolume,pod,service,endpoints]) + -h, --help help for export + --kubeconfig string Path to the kubeconfig file to use + --page-buffer-size int32 Define the number of pages to buffer (default 10) + --page-size int Define the page size (default 500) + --path string Path to the snapshot ``` ### Options inherited from parent commands