generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 211
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add subcommand
snapshot export
of kwokctl
- Loading branch information
Showing
5 changed files
with
126 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters