Skip to content

Commit

Permalink
Refactor snapshot for format k8s
Browse files Browse the repository at this point in the history
  • Loading branch information
wzshiming committed Apr 7, 2023
1 parent 63d409b commit 2099e55
Show file tree
Hide file tree
Showing 21 changed files with 1,334 additions and 420 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: "[experimental] 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
}
4 changes: 3 additions & 1 deletion 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 @@ -31,12 +32,13 @@ func NewCommand(ctx context.Context) *cobra.Command {
cmd := &cobra.Command{
Args: cobra.NoArgs,
Use: "snapshot [command]",
Short: "Snapshot [save, restore] one of cluster",
Short: "Snapshot [save, restore, export] one of cluster",
RunE: func(cmd *cobra.Command, args []string) error {
return cmd.Help()
},
}
cmd.AddCommand(save.NewCommand(ctx))
cmd.AddCommand(restore.NewCommand(ctx))
cmd.AddCommand(export.NewCommand(ctx))
return cmd
}
6 changes: 4 additions & 2 deletions pkg/kwokctl/runtime/cluster_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ func (c *Cluster) SnapshotSaveWithYAML(ctx context.Context, path string, filters
defer func() {
_ = file.Close()
}()
err = snapshot.Save(ctx, c, file, filters)
kubeconfigPath := c.GetWorkdirPath(InHostKubeconfigName)
err = snapshot.Save(ctx, kubeconfigPath, file, filters)
if err != nil {
return err
}
Expand All @@ -46,7 +47,8 @@ func (c *Cluster) SnapshotRestoreWithYAML(ctx context.Context, path string, filt
if err != nil {
return err
}
err = snapshot.Load(ctx, c, bytes.NewBuffer(data), filters)
kubeconfigPath := c.GetWorkdirPath(InHostKubeconfigName)
err = snapshot.Load(ctx, kubeconfigPath, bytes.NewBuffer(data), filters)
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit 2099e55

Please sign in to comment.