Skip to content

Commit

Permalink
Add subcommand snapshot export of kwokctl
Browse files Browse the repository at this point in the history
  • Loading branch information
wzshiming committed Apr 6, 2023
1 parent c1cd7da commit 1b05c31
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 0 deletions.
76 changes: 76 additions & 0 deletions pkg/kwokctl/cmd/snapshot/export/export.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
Copyright 2023 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// Package export is the export of external cluster
package export

import (
"context"
"fmt"
"os"

"github.com/spf13/cobra"

"sigs.k8s.io/kwok/pkg/kwokctl/snapshot"
)

type flagpole struct {
Path string
Kubeconfig string
Filters []string
}

// NewCommand returns a new cobra.Command for cluster exporting.
func NewCommand(ctx context.Context) *cobra.Command {
flags := &flagpole{}

cmd := &cobra.Command{
Args: cobra.NoArgs,
Use: "export",
Short: "Export the snapshots of external clusters",
RunE: func(cmd *cobra.Command, args []string) error {
return runE(cmd.Context(), flags)
},
}
cmd.Flags().StringVar(&flags.Kubeconfig, "kubeconfig", flags.Kubeconfig, "Path to the kubeconfig file to use")
cmd.Flags().StringVar(&flags.Path, "path", "", "Path to the snapshot")
cmd.Flags().StringSliceVar(&flags.Filters, "filter", snapshot.Resources, "Filter the resources to export")
return cmd
}

func runE(ctx context.Context, flags *flagpole) error {
if flags.Path == "" {
return fmt.Errorf("path is required")
}
if _, err := os.Stat(flags.Path); err == nil {
return fmt.Errorf("file %q already exists", flags.Path)
}

file, err := os.Create(flags.Path)
if err != nil {
return err
}
defer func() {
_ = file.Close()
}()

err = snapshot.Save(ctx, flags.Kubeconfig, file, flags.Filters)
if err != nil {
return err
}

return nil
}
2 changes: 2 additions & 0 deletions pkg/kwokctl/cmd/snapshot/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/spf13/cobra"

"sigs.k8s.io/kwok/pkg/kwokctl/cmd/snapshot/export"
"sigs.k8s.io/kwok/pkg/kwokctl/cmd/snapshot/restore"
"sigs.k8s.io/kwok/pkg/kwokctl/cmd/snapshot/save"
)
Expand All @@ -38,5 +39,6 @@ func NewCommand(ctx context.Context) *cobra.Command {
}
cmd.AddCommand(save.NewCommand(ctx))
cmd.AddCommand(restore.NewCommand(ctx))
cmd.AddCommand(export.NewCommand(ctx))
return cmd
}
1 change: 1 addition & 0 deletions site/content/en/docs/generated/kwokctl_snapshot.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ kwokctl snapshot [command] [flags]
### SEE ALSO

* [kwokctl](kwokctl.md) - kwokctl is a tool to streamline the creation and management of clusters, with nodes simulated by kwok
* [kwokctl snapshot export](kwokctl_snapshot_export.md) - Export the snapshots of external clusters
* [kwokctl snapshot restore](kwokctl_snapshot_restore.md) - Restore the snapshot of the cluster
* [kwokctl snapshot save](kwokctl_snapshot_save.md) - Save the snapshot of the cluster

29 changes: 29 additions & 0 deletions site/content/en/docs/generated/kwokctl_snapshot_export.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
## kwokctl snapshot export

Export the snapshots of external clusters

```
kwokctl snapshot export [flags]
```

### Options

```
--filter strings Filter the resources to export (default [namespace,node,serviceaccount,configmap,secret,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
```

### Options inherited from parent commands

```
-c, --config stringArray config path (default [~/.kwok/kwok.yaml])
--name string cluster name (default "kwok")
-v, --v log-level number for the log level verbosity (DEBUG, INFO, WARN, ERROR) or (-4, 0, 4, 8) (default INFO)
```

### SEE ALSO

* [kwokctl snapshot](kwokctl_snapshot.md) - Snapshot [save, restore] one of cluster

18 changes: 18 additions & 0 deletions site/content/en/docs/user/kwokctl-snapshot.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,21 @@ which will handle the ownerReference so that the resources remain relative to ea
``` bash
kwokctl snapshot restore --path cluster.yaml --format k8s
```

## Export external cluster

This like `kwokctl snapshot save --format k8s` but it will use the kubeconfig to connect to the cluster.
This is useful when you want to save a cluster that is not managed by `kwokctl` and restore it on a cluster managed by `kwokctl`.

``` bash
kwokctl snapshot export --path external-snapshot.yaml --kubeconfig /path/to/kubeconfig
```

### Restore cluster

Let's restore the cluster we just exported.

``` bash
kwokctl create cluster
kwokctl snapshot restore --path external-snapshot.yaml --format k8s
```

0 comments on commit 1b05c31

Please sign in to comment.