Skip to content

Commit

Permalink
docs: add new flags to the docs of kowkctl snapshot export and save
Browse files Browse the repository at this point in the history
Signed-off-by: AhmedGrati <ahmedgrati1999@gmail.com>
  • Loading branch information
TessaIO committed May 30, 2023
1 parent e9c22da commit 3b1c2e7
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 29 deletions.
13 changes: 12 additions & 1 deletion pkg/kwokctl/cmd/snapshot/export/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
14 changes: 5 additions & 9 deletions pkg/kwokctl/cmd/snapshot/save/save.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
}

Expand Down Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/kwokctl/runtime/binary/cluster_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
7 changes: 5 additions & 2 deletions pkg/kwokctl/runtime/cluster_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions pkg/kwokctl/runtime/compose/cluster_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/kwokctl/runtime/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions pkg/kwokctl/runtime/kind/cluster_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
30 changes: 26 additions & 4 deletions pkg/kwokctl/snapshot/save.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down
14 changes: 8 additions & 6 deletions site/content/en/docs/generated/kwokctl_snapshot_export.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3b1c2e7

Please sign in to comment.