Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor snapshot for format k8s #450

Merged
merged 1 commit into from
Apr 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the future, we may seek a fine-grained interface via componentconfig, so it can:

  • adapt to lower-version k8s clusters as which may employ different GVK (like v1beta1 runtimeclass vs. v1)
  • fine-grained behavior on fields stripping (like, whether or not strip a pod's nodeName, some particular annotations, security contexts, controllerRef, finalizers, etc.)
  • ......

}

// 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