From e64dadecb91fd6f9d183cece99d05e8c48289004 Mon Sep 17 00:00:00 2001 From: Christophe VILA Date: Tue, 26 Jan 2021 18:31:15 +0100 Subject: [PATCH] Bump to K8S 1.18.15, Added commands to manage snapshots --- cmd/gokube/cmd/init.go | 26 +++++++++++++---------- cmd/gokube/cmd/pause.go | 19 +++++++++-------- cmd/gokube/cmd/reset.go | 43 +++++++++++++++++++++++++++++++------- cmd/gokube/cmd/resume.go | 19 +++++++++-------- cmd/gokube/cmd/root.go | 2 ++ cmd/gokube/cmd/snapshot.go | 42 +++++++++++++++++++++++++++++-------- cmd/gokube/cmd/start.go | 18 +++++++++------- cmd/gokube/cmd/stop.go | 42 +++++++++++++++++++++---------------- cmd/gokube/cmd/version.go | 36 ++++++++++++++++--------------- 9 files changed, 158 insertions(+), 89 deletions(-) diff --git a/cmd/gokube/cmd/init.go b/cmd/gokube/cmd/init.go index 7010d26..02227b5 100644 --- a/cmd/gokube/cmd/init.go +++ b/cmd/gokube/cmd/init.go @@ -48,10 +48,11 @@ var hostDNSResolver bool // initCmd represents the init command var initCmd = &cobra.Command{ - Use: "init", - Short: "Initializes gokube. This command downloads dependencies: minikube + helm + kubectl + docker + stern and creates the virtual machine (minikube)", - Long: "Initializes gokube. This command downloads dependencies: minikube + helm + kubectl + docker + stern and creates the virtual machine (minikube)", - Run: initRun, + Use: "init", + Short: "Initializes gokube. This command downloads dependencies: minikube + helm + kubectl + docker + stern and creates a minikube VM", + Long: "Initializes gokube. This command downloads dependencies: minikube + helm + kubectl + docker + stern and creates a minikube VM", + RunE: initRun, + SilenceUsage: true, } func init() { @@ -65,6 +66,8 @@ func init() { var defaultHelmSprayVersion = getValueFromEnv("HELM_SPRAY_VERSION", DEFAULT_HELM_SPRAY_VERSION) var defaultHelmImageUrl = getValueFromEnv("HELM_IMAGE_URL", DEFAULT_HELM_IMAGE_URL) var defaultHelmImageVersion = getValueFromEnv("HELM_IMAGE_VERSION", DEFAULT_HELM_IMAGE_VERSION) + defaultVMMemory, _ := strconv.Atoi(getValueFromEnv("MINIKUBE_MEMORY", strconv.Itoa(DEFAULT_MINIKUBE_MEMORY))) + defaultVMCPUs, _ := strconv.Atoi(getValueFromEnv("MINIKUBE_CPUS", strconv.Itoa(DEFAULT_MINIKUBE_CPUS))) defaultGokubeQuiet := false if len(getValueFromEnv("GOKUBE_QUIET", "")) > 0 { defaultGokubeQuiet = true @@ -82,8 +85,8 @@ func init() { initCmd.Flags().StringVarP(&sternVersion, "stern-version", "", DEFAULT_STERN_VERSION, "The stern version") initCmd.Flags().BoolVarP(&askForUpgrade, "upgrade", "u", false, "Upgrade gokube (download and setup docker, minikube, kubectl and helm)") initCmd.Flags().BoolVarP(&askForClean, "clean", "c", false, "Clean gokube (remove docker, minikube, kubectl and helm working directories)") - initCmd.Flags().Int16VarP(&memory, "memory", "", int16(8192), "Amount of RAM allocated to the minikube VM in MB") - initCmd.Flags().Int16VarP(&cpus, "cpus", "", int16(4), "Number of CPUs allocated to the minikube VM") + initCmd.Flags().Int16VarP(&memory, "memory", "", int16(defaultVMMemory), "Amount of RAM allocated to the minikube VM in MB") + initCmd.Flags().Int16VarP(&cpus, "cpus", "", int16(defaultVMCPUs), "Number of CPUs allocated to the minikube VM") initCmd.Flags().StringVarP(&disk, "disk", "", "20g", "Disk size allocated to the minikube VM. Format: [], where unit = b, k, m or g") initCmd.Flags().StringVarP(&checkIP, "check-ip", "", "192.168.99.100", "Checks if minikube VM allocated IP matches the provided one (0.0.0.0 means no check)") initCmd.Flags().StringVarP(&insecureRegistry, "insecure-registry", "", os.Getenv("INSECURE_REGISTRY"), "Insecure Docker registries to pass to the Docker daemon. The default service CIDR range will automatically be added.") @@ -126,7 +129,7 @@ func checkMinimumRequirements() { } } -func confirmCommandExecution() { +func confirmInitCommandExecution() { fmt.Println("WARNING: Your Virtualbox GUI shall not be open and no other VM shall be currently running") fmt.Print("Press within the next 10s it you need to check this or press now to continue...") enter := make(chan bool, 1) @@ -136,6 +139,7 @@ func confirmCommandExecution() { case <-time.After(10 * time.Second): fmt.Println() } + time.Sleep(200 * time.Millisecond) } func resetVBLease() { @@ -234,10 +238,9 @@ func clean() { docker.InitWorkingDirectory() } -func initRun(cmd *cobra.Command, args []string) { +func initRun(cmd *cobra.Command, args []string) error { if len(args) > 0 { - fmt.Println("usage: gokube init") - os.Exit(1) + return cmd.Usage() } checkMinimumRequirements() @@ -247,7 +250,7 @@ func initRun(cmd *cobra.Command, args []string) { // Warn user with pre-requisites if ipCheckNeeded && !quiet { - confirmCommandExecution() + confirmInitCommandExecution() } fmt.Println("Deleting previous minikube VM...") @@ -309,4 +312,5 @@ func initRun(cmd *cobra.Command, args []string) { exposeDashboard(30000) fmt.Println("\ngokube has been installed.") + return nil } diff --git a/cmd/gokube/cmd/pause.go b/cmd/gokube/cmd/pause.go index 3018340..51c65af 100644 --- a/cmd/gokube/cmd/pause.go +++ b/cmd/gokube/cmd/pause.go @@ -22,20 +22,21 @@ import ( // pauseCmd represents the pause command var pauseCmd = &cobra.Command{ - Use: "pause", - Short: "Pauses minikube. This command pauses minikube VM", - Long: "Pauses minikube. This command pauses minikube VM", - Run: pauseRun, + Use: "pause", + Short: "Pauses gokube. This command pauses the minikube VM", + Long: "Pauses gokube. This command pauses the minikube VM", + RunE: pauseRun, + SilenceUsage: true, } func init() { RootCmd.AddCommand(pauseCmd) } -func pauseRun(cmd *cobra.Command, args []string) { - fmt.Println("Pausing minikube VM...") - err := virtualbox.Pause() - if err != nil { - panic(err) +func pauseRun(cmd *cobra.Command, args []string) error { + if len(args) > 0 { + return cmd.Usage() } + fmt.Println("Pausing minikube VM...") + return virtualbox.Pause() } diff --git a/cmd/gokube/cmd/reset.go b/cmd/gokube/cmd/reset.go index 24c51c0..137d6c3 100644 --- a/cmd/gokube/cmd/reset.go +++ b/cmd/gokube/cmd/reset.go @@ -2,26 +2,53 @@ package cmd import ( "fmt" + "github.com/gemalto/gokube/pkg/gokube" + "github.com/gemalto/gokube/pkg/minikube" "github.com/gemalto/gokube/pkg/virtualbox" "github.com/spf13/cobra" + "time" ) // resetCmd represents the pause command var resetCmd = &cobra.Command{ - Use: "reset", - Short: "Reset minikube from previously taken snapshot.", - Long: "Reset minikube from previously taken snapshot.", - Run: resetRun, + Use: "reset", + Short: "Resets gokube. This command restores minikube VM from previously taken snapshot", + Long: "Resets gokube. This command restores minikube VM from previously taken snapshot", + RunE: resetRun, + SilenceUsage: true, } func init() { + defaultGokubeQuiet := false + if len(getValueFromEnv("GOKUBE_QUIET", "")) > 0 { + defaultGokubeQuiet = true + } RootCmd.AddCommand(resetCmd) + resetCmd.Flags().BoolVarP(&quiet, "quiet", "q", defaultGokubeQuiet, "Don't display warning message before resetting") } -func resetRun(cmd *cobra.Command, args []string) { - fmt.Println("Resetting minikube VM from snapshot...") - err := virtualbox.RestoreSnapshot("gokube") +func confirmResetCommandExecution() { + fmt.Println("WARNING: You cannot reset from a running VM") + fmt.Print("Press within the next 10s it you have to stop VM or press now to continue...") + enter := make(chan bool, 1) + go gokube.WaitEnter(enter) + select { + case <-enter: + case <-time.After(10 * time.Second): + fmt.Println() + } + time.Sleep(200 * time.Millisecond) +} + +func resetRun(cmd *cobra.Command, args []string) error { + if len(args) > 0 { + return cmd.Usage() + } + fmt.Println("Stopping minikube VM...") + err := minikube.Stop() if err != nil { - panic(err) + return err } + fmt.Println("Resetting minikube VM from snapshot...") + return virtualbox.RestoreSnapshot("gokube") } diff --git a/cmd/gokube/cmd/resume.go b/cmd/gokube/cmd/resume.go index d24ced2..ab50d54 100644 --- a/cmd/gokube/cmd/resume.go +++ b/cmd/gokube/cmd/resume.go @@ -22,20 +22,21 @@ import ( // resumeCmd represents the resume command var resumeCmd = &cobra.Command{ - Use: "resume", - Short: "Resumes minikube. This command resumes minikube VM", - Long: "Resumes minikube. This command resumes minikube VM", - Run: resumeRun, + Use: "resume", + Short: "Resumes gokube. This command resumes the minikube VM", + Long: "Resumes gokube. This command resumes the minikube VM", + RunE: resumeRun, + SilenceUsage: true, } func init() { RootCmd.AddCommand(resumeCmd) } -func resumeRun(cmd *cobra.Command, args []string) { - fmt.Println("Resuming minikube VM...") - err := virtualbox.Resume() - if err != nil { - panic(err) +func resumeRun(cmd *cobra.Command, args []string) error { + if len(args) > 0 { + return cmd.Usage() } + fmt.Println("Resuming minikube VM...") + return virtualbox.Resume() } diff --git a/cmd/gokube/cmd/root.go b/cmd/gokube/cmd/root.go index 327f95f..e1447dd 100644 --- a/cmd/gokube/cmd/root.go +++ b/cmd/gokube/cmd/root.go @@ -35,6 +35,8 @@ const ( DEFAULT_KUBECTL_VERSION = "v1.18.15" DEFAULT_MINIKUBE_VERSION = "v1.16.0" DEFAULT_MINIKUBE_URL = "https://storage.googleapis.com/minikube/releases/%s/minikube-windows-amd64.exe" + DEFAULT_MINIKUBE_MEMORY = 8192 + DEFAULT_MINIKUBE_CPUS = 4 DEFAULT_DOCKER_VERSION = "19.03.14" DEFAULT_HELM_VERSION = "v3.5.0" DEFAULT_HELM_SPRAY_VERSION = "v4.0.6" diff --git a/cmd/gokube/cmd/snapshot.go b/cmd/gokube/cmd/snapshot.go index e29e96f..564a5fc 100644 --- a/cmd/gokube/cmd/snapshot.go +++ b/cmd/gokube/cmd/snapshot.go @@ -2,26 +2,50 @@ package cmd import ( "fmt" + "github.com/gemalto/gokube/pkg/gokube" "github.com/gemalto/gokube/pkg/virtualbox" "github.com/spf13/cobra" + "time" ) // snapshotCmd represents the pause command var snapshotCmd = &cobra.Command{ - Use: "snapshot", - Short: "Snapshot minikube VM to set the reference for reset command.", - Long: "Snapshot minikube VM to set the reference for reset command.", - Run: snapshotRun, + Use: "snapshot", + Short: "Creates a gokube reference. This command takes a snapshot of the minikube VM (which will be the reference for reset command)", + Long: "Creates a gokube reference. This command takes a snapshot of the minikube VM (which will be the reference for reset command)", + RunE: snapshotRun, + SilenceUsage: true, } func init() { + defaultGokubeQuiet := false + if len(getValueFromEnv("GOKUBE_QUIET", "")) > 0 { + defaultGokubeQuiet = true + } RootCmd.AddCommand(snapshotCmd) + snapshotCmd.Flags().BoolVarP(&quiet, "quiet", "q", defaultGokubeQuiet, "Don't display warning message before snapshotting") } -func snapshotRun(cmd *cobra.Command, args []string) { - fmt.Println("Taking snapshot of minikube VM...") - err := virtualbox.TakeSnapshot("gokube") - if err != nil { - panic(err) +func confirmSnapshotCommandExecution() { + fmt.Println("WARNING: You should not snapshot a running VM as the process can be long and take more space on disk") + fmt.Print("Press within the next 10s it you need to stop VM or press now to continue...") + enter := make(chan bool, 1) + go gokube.WaitEnter(enter) + select { + case <-enter: + case <-time.After(10 * time.Second): + fmt.Println() } + time.Sleep(200 * time.Millisecond) +} + +func snapshotRun(cmd *cobra.Command, args []string) error { + if len(args) > 0 { + return cmd.Usage() + } + if !quiet { + confirmSnapshotCommandExecution() + } + fmt.Println("Taking snapshot of minikube VM...") + return virtualbox.TakeSnapshot("gokube") } diff --git a/cmd/gokube/cmd/start.go b/cmd/gokube/cmd/start.go index c39ea77..4b76bd9 100644 --- a/cmd/gokube/cmd/start.go +++ b/cmd/gokube/cmd/start.go @@ -24,10 +24,11 @@ import ( // startCmd represents the start command var startCmd = &cobra.Command{ - Use: "start", - Short: "Starts minikube. This command starts minikube", - Long: "Starts minikube. This command starts minikube", - Run: startRun, + Use: "start", + Short: "Starts gokube. This command starts minikube", + Long: "Starts gokube. This command starts minikube", + RunE: startRun, + SilenceUsage: true, } func init() { @@ -54,21 +55,22 @@ func init() { RootCmd.AddCommand(startCmd) } -func startRun(cmd *cobra.Command, args []string) { - +func startRun(cmd *cobra.Command, args []string) error { + if len(args) > 0 { + return cmd.Usage() + } if askForUpgrade { fmt.Println("Downloading gokube dependencies...") upgrade() fmt.Println("Installing helm plugins...") installHelmPlugins() } - gokube.ReadConfig() kubernetesVersionForStart := viper.GetString("kubernetes-version") if len(kubernetesVersionForStart) == 0 { kubernetesVersionForStart = getValueFromEnv("KUBERNETES_VERSION", DEFAULT_KUBERNETES_VERSION) } - fmt.Printf("Starting minikube VM with kubernetes %s...\n", kubernetesVersion) minikube.Restart(kubernetesVersion) + return nil } diff --git a/cmd/gokube/cmd/stop.go b/cmd/gokube/cmd/stop.go index d65a423..b58565a 100644 --- a/cmd/gokube/cmd/stop.go +++ b/cmd/gokube/cmd/stop.go @@ -24,10 +24,11 @@ import ( // stopCmd represents the stop command var stopCmd = &cobra.Command{ - Use: "stop", - Short: "Stops minikube. This command stops minikube", - Long: "Stops minikube. This command stops minikube", - Run: stopRun, + Use: "stop", + Short: "Stops gokube. This command stops minikube", + Long: "Stops gokube. This command stops minikube", + RunE: stopRun, + SilenceUsage: true, } func init() { @@ -39,21 +40,26 @@ func init() { stopCmd.Flags().BoolVarP(&quiet, "quiet", "q", defaultGokubeQuiet, "Don't display warning message before stopping") } -func stopRun(cmd *cobra.Command, args []string) { +func confirmStopCommandExecution() { + fmt.Println("WARNING: You should not stop a VM with a lot of running pods as the restart will be unstable") + fmt.Print("Press within the next 10s it you need to perform some clean or press now to continue...") + enter := make(chan bool, 1) + go gokube.WaitEnter(enter) + select { + case <-enter: + case <-time.After(10 * time.Second): + fmt.Println() + } + time.Sleep(200 * time.Millisecond) +} + +func stopRun(cmd *cobra.Command, args []string) error { + if len(args) > 0 { + return cmd.Usage() + } if !quiet { - fmt.Println("WARNING: You should not stop a VM with a lot of running pods as the restart will be unstable") - fmt.Print("Press within the next 10s it you need to perform some clean or press now to continue...") - enter := make(chan bool, 1) - go gokube.WaitEnter(enter) - select { - case <-enter: - case <-time.After(10 * time.Second): - fmt.Println() - } + confirmStopCommandExecution() } fmt.Println("Stopping minikube VM...") - err := minikube.Stop() - if err != nil { - panic(err) - } + return minikube.Stop() } diff --git a/cmd/gokube/cmd/version.go b/cmd/gokube/cmd/version.go index 2f82256..1379ace 100644 --- a/cmd/gokube/cmd/version.go +++ b/cmd/gokube/cmd/version.go @@ -24,7 +24,6 @@ import ( "github.com/gemalto/gokube/pkg/minikube" "github.com/spf13/cobra" "github.com/spf13/viper" - "os" "time" ) @@ -43,22 +42,11 @@ var ( // versionCmd represents the version command var versionCmd = &cobra.Command{ - Use: "version", - Short: "Shows version for gokube", - Long: `Shows version for gokube`, - Run: func(cmd *cobra.Command, args []string) { - if len(args) > 0 { - fmt.Fprintln(os.Stderr, "usage: gokube version") - os.Exit(1) - } - fmt.Println("gokube version: v" + GOKUBE_VERSION) - checkLatestVersion() - minikube.Version() - docker.Version() - kubectl.Version() - helm.Version() - helm.PluginsVersion() - }, + Use: "version", + Short: "Shows version for gokube", + Long: `Shows version for gokube`, + RunE: versionRun, + SilenceUsage: true, } func checkLatestVersion() { @@ -83,3 +71,17 @@ func init() { gokubeVersion = "0.0.0" } } + +func versionRun(cmd *cobra.Command, args []string) error { + if len(args) > 0 { + return cmd.Usage() + } + fmt.Println("gokube version: v" + GOKUBE_VERSION) + checkLatestVersion() + minikube.Version() + docker.Version() + kubectl.Version() + helm.Version() + helm.PluginsVersion() + return nil +}