Skip to content

Commit

Permalink
Merge pull request #863 from wzshiming/clean/snapshot-yaml
Browse files Browse the repository at this point in the history
Clean up snapshot yaml configuration
  • Loading branch information
wzshiming authored Dec 11, 2023
2 parents 5b4e807 + 5cc6d61 commit 5be4570
Show file tree
Hide file tree
Showing 12 changed files with 56 additions and 34 deletions.
3 changes: 2 additions & 1 deletion pkg/kwokctl/cmd/snapshot/export/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,10 @@ func runE(ctx context.Context, flags *flagpole) error {

snapshotSaveConfig := snapshot.SaveConfig{
PagerConfig: pagerConfig,
Filters: flags.Filters,
}

err = snapshot.Save(ctx, clientset, f, flags.Filters, snapshotSaveConfig)
err = snapshot.Save(ctx, clientset, f, snapshotSaveConfig)
if err != nil {
return err
}
Expand Down
6 changes: 4 additions & 2 deletions pkg/kwokctl/cmd/snapshot/restore/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type flagpole struct {
Filters []string
}

// NewCommand returns a new cobra.Command to save the cluster as a snapshot.
// NewCommand returns a new cobra.Command to restore the cluster as a snapshot.
func NewCommand(ctx context.Context) *cobra.Command {
flags := &flagpole{}

Expand Down Expand Up @@ -88,7 +88,9 @@ func runE(ctx context.Context, flags *flagpole) error {
return err
}
case "k8s":
err = rt.SnapshotRestoreWithYAML(ctx, flags.Path, flags.Filters)
err = rt.SnapshotRestoreWithYAML(ctx, flags.Path, runtime.SnapshotRestoreWithYAMLConfig{
Filters: flags.Filters,
})
if err != nil {
return err
}
Expand Down
4 changes: 3 additions & 1 deletion pkg/kwokctl/cmd/snapshot/save/save.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ func runE(ctx context.Context, flags *flagpole) error {
return err
}
case "k8s":
err = rt.SnapshotSaveWithYAML(ctx, flags.Path, flags.Filters)
err = rt.SnapshotSaveWithYAML(ctx, flags.Path, runtime.SnapshotSaveWithYAMLConfig{
Filters: flags.Filters,
})
if err != nil {
return err
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/kwokctl/runtime/binary/cluster_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,16 @@ func (c *Cluster) SnapshotRestore(ctx context.Context, path string) error {
}

// SnapshotSaveWithYAML save the snapshot of cluster
func (c *Cluster) SnapshotSaveWithYAML(ctx context.Context, path string, filters []string) error {
err := c.Cluster.SnapshotSaveWithYAML(ctx, path, filters)
func (c *Cluster) SnapshotSaveWithYAML(ctx context.Context, path string, conf runtime.SnapshotSaveWithYAMLConfig) error {
err := c.Cluster.SnapshotSaveWithYAML(ctx, path, conf)
if err != nil {
return err
}
return nil
}

// SnapshotRestoreWithYAML restore the snapshot of cluster
func (c *Cluster) SnapshotRestoreWithYAML(ctx context.Context, path string, filters []string) error {
func (c *Cluster) SnapshotRestoreWithYAML(ctx context.Context, path string, conf runtime.SnapshotRestoreWithYAMLConfig) error {
logger := log.FromContext(ctx)
err := wait.Poll(ctx, func(ctx context.Context) (bool, error) {
err := c.StopComponent(ctx, consts.ComponentKubeControllerManager)
Expand All @@ -132,7 +132,7 @@ func (c *Cluster) SnapshotRestoreWithYAML(ctx context.Context, path string, filt
}
}()

err = c.Cluster.SnapshotRestoreWithYAML(ctx, path, filters)
err = c.Cluster.SnapshotRestoreWithYAML(ctx, path, conf)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/kwokctl/runtime/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ func (c *Cluster) InitCRDs(ctx context.Context) error {
logger := log.FromContext(ctx)
ctx = log.NewContext(ctx, logger.With("crds", strings.Join(crds, ",")))

return snapshot.Load(ctx, clientset, buf, nil)
return snapshot.Load(ctx, clientset, buf, snapshot.LoadConfig{})
}

var crdDefines = map[string][]byte{
Expand Down
17 changes: 9 additions & 8 deletions pkg/kwokctl/runtime/cluster_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ import (
)

// SnapshotSaveWithYAML save the snapshot of cluster
func (c *Cluster) SnapshotSaveWithYAML(ctx context.Context, path string, filters []string) error {
func (c *Cluster) SnapshotSaveWithYAML(ctx context.Context, path string, conf SnapshotSaveWithYAMLConfig) error {
if c.IsDryRun() {
dryrun.PrintMessage("kubectl get %s -o yaml >%s", strings.Join(filters, ","), path)
dryrun.PrintMessage("kubectl get %s -o yaml >%s", strings.Join(conf.Filters, ","), path)
return nil
}

Expand All @@ -45,14 +45,13 @@ func (c *Cluster) SnapshotSaveWithYAML(ctx context.Context, path string, filters
_ = f.Close()
}()

// In most cases, the user should have full privileges on the clusters created by kwokctl,
// so no need to expose impersonation args to "snapshot save" command.
snapshotSaveConfig := snapshot.SaveConfig{}
return snapshot.Save(ctx, clientset, f, filters, snapshotSaveConfig)
return snapshot.Save(ctx, clientset, f, snapshot.SaveConfig{
Filters: conf.Filters,
})
}

// SnapshotRestoreWithYAML restore the snapshot of cluster
func (c *Cluster) SnapshotRestoreWithYAML(ctx context.Context, path string, filters []string) error {
func (c *Cluster) SnapshotRestoreWithYAML(ctx context.Context, path string, conf SnapshotRestoreWithYAMLConfig) error {
if c.IsDryRun() {
dryrun.PrintMessage("kubectl create -f %s", path)
return nil
Expand All @@ -71,5 +70,7 @@ func (c *Cluster) SnapshotRestoreWithYAML(ctx context.Context, path string, filt
_ = f.Close()
}()

return snapshot.Load(ctx, clientset, f, filters)
return snapshot.Load(ctx, clientset, f, snapshot.LoadConfig{
Filters: conf.Filters,
})
}
9 changes: 5 additions & 4 deletions pkg/kwokctl/runtime/compose/cluster_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"

"sigs.k8s.io/kwok/pkg/consts"
"sigs.k8s.io/kwok/pkg/kwokctl/runtime"
"sigs.k8s.io/kwok/pkg/log"
)

Expand Down Expand Up @@ -178,16 +179,16 @@ func (c *Cluster) SnapshotRestore(ctx context.Context, path string) error {
}

// SnapshotSaveWithYAML save the snapshot of cluster
func (c *Cluster) SnapshotSaveWithYAML(ctx context.Context, path string, filters []string) error {
err := c.Cluster.SnapshotSaveWithYAML(ctx, path, filters)
func (c *Cluster) SnapshotSaveWithYAML(ctx context.Context, path string, conf runtime.SnapshotSaveWithYAMLConfig) error {
err := c.Cluster.SnapshotSaveWithYAML(ctx, path, conf)
if err != nil {
return err
}
return nil
}

// SnapshotRestoreWithYAML restore the snapshot of cluster
func (c *Cluster) SnapshotRestoreWithYAML(ctx context.Context, path string, filters []string) error {
func (c *Cluster) SnapshotRestoreWithYAML(ctx context.Context, path string, conf runtime.SnapshotRestoreWithYAMLConfig) error {
logger := log.FromContext(ctx)
err := c.StopComponent(ctx, consts.ComponentKubeControllerManager)
if err != nil {
Expand All @@ -200,7 +201,7 @@ func (c *Cluster) SnapshotRestoreWithYAML(ctx context.Context, path string, filt
}
}()

err = c.Cluster.SnapshotRestoreWithYAML(ctx, path, filters)
err = c.Cluster.SnapshotRestoreWithYAML(ctx, path, conf)
if err != nil {
return err
}
Expand Down
12 changes: 10 additions & 2 deletions pkg/kwokctl/runtime/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,10 @@ type Runtime interface {
SnapshotRestore(ctx context.Context, path string) error

// SnapshotSaveWithYAML save the snapshot of cluster
SnapshotSaveWithYAML(ctx context.Context, path string, filters []string) error
SnapshotSaveWithYAML(ctx context.Context, path string, conf SnapshotSaveWithYAMLConfig) error

// SnapshotRestoreWithYAML restore the snapshot of cluster
SnapshotRestoreWithYAML(ctx context.Context, path string, filters []string) error
SnapshotRestoreWithYAML(ctx context.Context, path string, conf SnapshotRestoreWithYAMLConfig) error

// GetWorkdirPath get the workdir path of cluster
GetWorkdirPath(name string) string
Expand All @@ -128,3 +128,11 @@ type Runtime interface {
// IsDryRun returns true if the runtime is in dry-run mode
IsDryRun() bool
}

type SnapshotSaveWithYAMLConfig struct {
Filters []string
}

type SnapshotRestoreWithYAMLConfig struct {
Filters []string
}
9 changes: 5 additions & 4 deletions pkg/kwokctl/runtime/kind/cluster_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"

"sigs.k8s.io/kwok/pkg/consts"
"sigs.k8s.io/kwok/pkg/kwokctl/runtime"
"sigs.k8s.io/kwok/pkg/log"
"sigs.k8s.io/kwok/pkg/utils/wait"
)
Expand Down Expand Up @@ -109,16 +110,16 @@ func (c *Cluster) SnapshotRestore(ctx context.Context, path string) error {
}

// SnapshotSaveWithYAML save the snapshot of cluster
func (c *Cluster) SnapshotSaveWithYAML(ctx context.Context, path string, filters []string) error {
err := c.Cluster.SnapshotSaveWithYAML(ctx, path, filters)
func (c *Cluster) SnapshotSaveWithYAML(ctx context.Context, path string, conf runtime.SnapshotSaveWithYAMLConfig) error {
err := c.Cluster.SnapshotSaveWithYAML(ctx, path, conf)
if err != nil {
return err
}
return nil
}

// SnapshotRestoreWithYAML restore the snapshot of cluster
func (c *Cluster) SnapshotRestoreWithYAML(ctx context.Context, path string, filters []string) error {
func (c *Cluster) SnapshotRestoreWithYAML(ctx context.Context, path string, conf runtime.SnapshotRestoreWithYAMLConfig) error {
logger := log.FromContext(ctx)
err := wait.Poll(ctx, func(ctx context.Context) (bool, error) {
err := c.StopComponent(ctx, consts.ComponentKubeControllerManager)
Expand All @@ -134,7 +135,7 @@ func (c *Cluster) SnapshotRestoreWithYAML(ctx context.Context, path string, filt
}
}()

err = c.Cluster.SnapshotRestoreWithYAML(ctx, path, filters)
err = c.Cluster.SnapshotRestoreWithYAML(ctx, path, conf)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/kwokctl/scale/scale.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ func Scale(ctx context.Context, clientset client.Clientset, conf Config) error {
}, wantCreate)

ctx = log.NewContext(ctx, logger)
err = snapshot.Load(ctx, clientset, gen, nil)
err = snapshot.Load(ctx, clientset, gen, snapshot.LoadConfig{})
if err != nil {
return err
}
Expand Down
11 changes: 8 additions & 3 deletions pkg/kwokctl/snapshot/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,18 @@ import (
"sigs.k8s.io/kwok/pkg/utils/yaml"
)

// LoadConfig is the a combination of the impersonation config
type LoadConfig struct {
Filters []string
}

// Load loads the resources to cluster from the reader
func Load(ctx context.Context, clientset client.Clientset, r io.Reader, filters []string) error {
l, err := newLoader(clientset, filters == nil)
func Load(ctx context.Context, clientset client.Clientset, r io.Reader, loadConfig LoadConfig) error {
l, err := newLoader(clientset, loadConfig.Filters == nil)
if err != nil {
return err
}
l.addResource(ctx, filters)
l.addResource(ctx, loadConfig.Filters)

return l.Load(ctx, r)
}
Expand Down
7 changes: 4 additions & 3 deletions pkg/kwokctl/snapshot/save.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,11 @@ type PagerConfig struct {
// and the PagerConfig.
type SaveConfig struct {
PagerConfig *PagerConfig
Filters []string
}

// Save saves the snapshot of cluster
func Save(ctx context.Context, clientset client.Clientset, w io.Writer, resources []string, saveConfig SaveConfig) error {
func Save(ctx context.Context, clientset client.Clientset, w io.Writer, saveConfig SaveConfig) error {
restMapper, err := clientset.ToRESTMapper()
if err != nil {
return fmt.Errorf("failed to get rest mapper: %w", err)
Expand All @@ -61,8 +62,8 @@ func Save(ctx context.Context, clientset client.Clientset, w io.Writer, resource

logger := log.FromContext(ctx)

gvrs := make([]schema.GroupVersionResource, 0, len(resources))
for _, resource := range resources {
gvrs := make([]schema.GroupVersionResource, 0, len(saveConfig.Filters))
for _, resource := range saveConfig.Filters {
mapping, err := client.MappingFor(restMapper, resource)
if err != nil {
logger.Warn("Failed to get mapping for resource", "resource", resource, "err", err)
Expand Down

0 comments on commit 5be4570

Please sign in to comment.