Skip to content

Commit

Permalink
Bump to K8S 1.18.15, Added commands to manage snapshots
Browse files Browse the repository at this point in the history
  • Loading branch information
Christophe VILA committed Jan 26, 2021
1 parent 88a15bb commit e64dade
Show file tree
Hide file tree
Showing 9 changed files with 158 additions and 89 deletions.
26 changes: 15 additions & 11 deletions cmd/gokube/cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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
Expand All @@ -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: <number>[<unit>], 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.")
Expand Down Expand Up @@ -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 <CTRL+C> within the next 10s it you need to check this or press <ENTER> now to continue...")
enter := make(chan bool, 1)
Expand All @@ -136,6 +139,7 @@ func confirmCommandExecution() {
case <-time.After(10 * time.Second):
fmt.Println()
}
time.Sleep(200 * time.Millisecond)
}

func resetVBLease() {
Expand Down Expand Up @@ -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()
Expand All @@ -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...")
Expand Down Expand Up @@ -309,4 +312,5 @@ func initRun(cmd *cobra.Command, args []string) {
exposeDashboard(30000)

fmt.Println("\ngokube has been installed.")
return nil
}
19 changes: 10 additions & 9 deletions cmd/gokube/cmd/pause.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
43 changes: 35 additions & 8 deletions cmd/gokube/cmd/reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 <CTRL+C> within the next 10s it you have to stop VM or press <ENTER> 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")
}
19 changes: 10 additions & 9 deletions cmd/gokube/cmd/resume.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
2 changes: 2 additions & 0 deletions cmd/gokube/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
42 changes: 33 additions & 9 deletions cmd/gokube/cmd/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 <CTRL+C> within the next 10s it you need to stop VM or press <ENTER> 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")
}
18 changes: 10 additions & 8 deletions cmd/gokube/cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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
}
42 changes: 24 additions & 18 deletions cmd/gokube/cmd/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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 <CTRL+C> within the next 10s it you need to perform some clean or press <ENTER> 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 <CTRL+C> within the next 10s it you need to perform some clean or press <ENTER> 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()
}
Loading

0 comments on commit e64dade

Please sign in to comment.