From 1b05c31804609525952f53277fea4b161e60223a Mon Sep 17 00:00:00 2001 From: Shiming Zhang Date: Thu, 6 Apr 2023 09:59:46 +0800 Subject: [PATCH] Add subcommand `snapshot export` of kwokctl --- pkg/kwokctl/cmd/snapshot/export/export.go | 76 +++++++++++++++++++ pkg/kwokctl/cmd/snapshot/snapshot.go | 2 + .../en/docs/generated/kwokctl_snapshot.md | 1 + .../docs/generated/kwokctl_snapshot_export.md | 29 +++++++ site/content/en/docs/user/kwokctl-snapshot.md | 18 +++++ 5 files changed, 126 insertions(+) create mode 100644 pkg/kwokctl/cmd/snapshot/export/export.go create mode 100644 site/content/en/docs/generated/kwokctl_snapshot_export.md diff --git a/pkg/kwokctl/cmd/snapshot/export/export.go b/pkg/kwokctl/cmd/snapshot/export/export.go new file mode 100644 index 0000000000..f4e7c2664a --- /dev/null +++ b/pkg/kwokctl/cmd/snapshot/export/export.go @@ -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 +} diff --git a/pkg/kwokctl/cmd/snapshot/snapshot.go b/pkg/kwokctl/cmd/snapshot/snapshot.go index 703bf9c6f1..c2cb41d521 100644 --- a/pkg/kwokctl/cmd/snapshot/snapshot.go +++ b/pkg/kwokctl/cmd/snapshot/snapshot.go @@ -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" ) @@ -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 } diff --git a/site/content/en/docs/generated/kwokctl_snapshot.md b/site/content/en/docs/generated/kwokctl_snapshot.md index 2f0d7d3a1b..e3ec6d7f49 100644 --- a/site/content/en/docs/generated/kwokctl_snapshot.md +++ b/site/content/en/docs/generated/kwokctl_snapshot.md @@ -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 diff --git a/site/content/en/docs/generated/kwokctl_snapshot_export.md b/site/content/en/docs/generated/kwokctl_snapshot_export.md new file mode 100644 index 0000000000..2f74e63725 --- /dev/null +++ b/site/content/en/docs/generated/kwokctl_snapshot_export.md @@ -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 + diff --git a/site/content/en/docs/user/kwokctl-snapshot.md b/site/content/en/docs/user/kwokctl-snapshot.md index 533fb03fc3..7a874f27b6 100644 --- a/site/content/en/docs/user/kwokctl-snapshot.md +++ b/site/content/en/docs/user/kwokctl-snapshot.md @@ -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 +```