Skip to content

Commit

Permalink
Merge pull request #1671 from johannesfrey/vcluster-migrate
Browse files Browse the repository at this point in the history
feat: add "migrate values" command to migrate to new values format
  • Loading branch information
heiko-braun authored Apr 12, 2024
2 parents 40eb7f5 + 1ea7d79 commit df2c75b
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 0 deletions.
22 changes: 22 additions & 0 deletions cmd/vclusterctl/cmd/migrate/migrate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package migrate

import (
"github.com/loft-sh/vcluster/cmd/vclusterctl/flags"
"github.com/spf13/cobra"
)

func NewMigrateCmd(globalFlags *flags.GlobalFlags) *cobra.Command {
migrateCmd := &cobra.Command{
Use: "migrate",
Short: "Migrate vcluster configuration",
Long: `
#######################################################
################## vcluster migrate ###################
#######################################################
`,
Args: cobra.NoArgs,
}

migrateCmd.AddCommand(migrateValues(globalFlags))
return migrateCmd
}
112 changes: 112 additions & 0 deletions cmd/vclusterctl/cmd/migrate/values.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package migrate

import (
"fmt"
"io"
"os"
"strings"

"github.com/loft-sh/log"
"github.com/loft-sh/vcluster/cmd/vclusterctl/flags"
"github.com/loft-sh/vcluster/pkg/config/legacyconfig"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/util/yaml"
)

type valuesCmd struct {
*flags.GlobalFlags
log log.Logger
distro string
filePath string
format string
}

func migrateValues(globalFlags *flags.GlobalFlags) *cobra.Command {
c := &valuesCmd{
GlobalFlags: globalFlags,
log: log.GetInstance(),
}

cobraCmd := &cobra.Command{
Use: "values",
Short: "Migrates current cluster values",
Long: `
#######################################################
############### vcluster migrate values ###############
#######################################################
Migrates values for a vcluster to the 0.20 format
Examples:
vcluster migrate values --distro k8s -f /my/k8s/values.yaml
vcluster migrate values --distro k3s < /my/k3s/values.yaml
cat /my/k0s/values.yaml | vcluster migrate values --distro k0s
#######################################################
`,
RunE: func(_ *cobra.Command, _ []string) error {
return c.Run()
}}

cobraCmd.Flags().StringVarP(&c.filePath, "file", "f", "", "Path to the input file")
cobraCmd.Flags().StringVar(&c.distro, "distro", "", fmt.Sprintf("Kubernetes distro of the values. Allowed distros: %s", strings.Join([]string{"k8s", "k3s", "k0s", "eks"}, ", ")))
cobraCmd.Flags().StringVarP(&c.format, "output", "o", "yaml", "Prints the output in the specified format. Allowed values: yaml, json")

return cobraCmd
}

func (cmd *valuesCmd) Run() error {
var (
migratedConfig string
err error
)

if cmd.distro == "" {
return fmt.Errorf("no distro given: please set \"--distro\" (IMPORTANT: distro must match the given values)")
}

if cmd.filePath != "" {
file, err := os.Open(cmd.filePath)
if err != nil {
return err
}
migratedConfig, err = migrate(cmd.distro, file)
if err != nil {
return fmt.Errorf("unable to migrate config values: %w", err)
}
defer file.Close()
} else {
// If no files provided, read from stdin
migratedConfig, err = migrate(cmd.distro, os.Stdin)
if err != nil {
return fmt.Errorf("unable to migrate config values: %w", err)
}
}

var out string
switch cmd.format {
case "json":
j, err := yaml.ToJSON([]byte(migratedConfig))
if err != nil {
return err
}
out = string(j)
case "yaml":
out = migratedConfig
default:
fmt.Fprintf(os.Stderr, "unsupported output format: %s. falling back to yaml\n", cmd.format)
out = migratedConfig
}

cmd.log.WriteString(logrus.InfoLevel, out)

return nil
}

func migrate(distro string, r io.Reader) (string, error) {
content, err := io.ReadAll(r)
if err != nil {
return "", err
}

return legacyconfig.MigrateLegacyConfig(distro, string(content))
}
2 changes: 2 additions & 0 deletions cmd/vclusterctl/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/loft-sh/log"
"github.com/loft-sh/vcluster/cmd/vclusterctl/cmd/get"
"github.com/loft-sh/vcluster/cmd/vclusterctl/cmd/migrate"
cmdpro "github.com/loft-sh/vcluster/cmd/vclusterctl/cmd/pro"
cmdtelemetry "github.com/loft-sh/vcluster/cmd/vclusterctl/cmd/telemetry"
"github.com/loft-sh/vcluster/cmd/vclusterctl/flags"
Expand Down Expand Up @@ -89,6 +90,7 @@ func BuildRoot(log log.Logger) (*cobra.Command, error) {
rootCmd.AddCommand(NewDisconnectCmd(globalFlags))
rootCmd.AddCommand(NewUpgradeCmd())
rootCmd.AddCommand(get.NewGetCmd(globalFlags))
rootCmd.AddCommand(migrate.NewMigrateCmd(globalFlags))
rootCmd.AddCommand(cmdtelemetry.NewTelemetryCmd())
rootCmd.AddCommand(versionCmd)
rootCmd.AddCommand(NewInfoCmd())
Expand Down

0 comments on commit df2c75b

Please sign in to comment.