diff --git a/CHANGELOG.md b/CHANGELOG.md index f4d74d1..d64e0bc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # GoKube Release Notes +## Version 1.32.0 - 04/05/2024 +* Bump to minikube v1.32.0, K8S v1.28.3, helm v3.14.3 +* Added swap memory option MINIKUBE_SWAP. Disabled by default (0). + ## Version 1.31.0 - 10/07/2023 * Bump to minikube v1.31.2, K8S v1.27.4, helm v3.12.3 diff --git a/cmd/gokube/cmd/init.go b/cmd/gokube/cmd/init.go index ce1a98f..4374e26 100644 --- a/cmd/gokube/cmd/init.go +++ b/cmd/gokube/cmd/init.go @@ -34,10 +34,13 @@ import ( "github.com/gemalto/gokube/pkg/kubectl" "github.com/gemalto/gokube/pkg/minikube" "github.com/spf13/cobra" + "os/exec" ) var memory int16 var cpus int16 +var swap int16 +var enableSwap bool var disk string var checkIP string var ipCheckNeeded bool @@ -64,6 +67,11 @@ var initCmd = &cobra.Command{ func init() { defaultVMMemory, _ := strconv.Atoi(utils.GetValueFromEnv("MINIKUBE_MEMORY", strconv.Itoa(DEFAULT_MINIKUBE_MEMORY))) defaultVMCPUs, _ := strconv.Atoi(utils.GetValueFromEnv("MINIKUBE_CPUS", strconv.Itoa(DEFAULT_MINIKUBE_CPUS))) + defaultVMSwap, _ := strconv.Atoi(utils.GetValueFromEnv("MINIKUBE_SWAP", strconv.Itoa(DEFAULT_MINIKUBE_SWAP))) + enableSwap = false + if defaultVMSwap != 0 { + enableSwap = true + } defaultGokubeQuiet := false if len(utils.GetValueFromEnv("GOKUBE_QUIET", "")) > 0 { defaultGokubeQuiet = true @@ -75,6 +83,7 @@ func init() { initCmd.Flags().BoolVarP(&askForClean, "clean", "c", false, "Clean gokube (remove docker, minikube, kubectl and helm working directories)") 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().Int16VarP(&swap, "swap", "", int16(defaultVMSwap), "Amount of SWAP allocated to the minikube VM in MB") initCmd.Flags().StringVarP(&disk, "disk", "", utils.GetValueFromEnv("MINIKUBE_DISK", DEFAULT_MINIKUBE_DISK), "Disk size allocated to the minikube VM. Format: [], where unit = b, k, m or g") initCmd.Flags().StringVarP(&checkIP, "check-ip", "", utils.GetValueFromEnv("GOKUBE_CHECK_IP", DEFAULT_GOKUBE_CHECK_IP), "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.") @@ -289,6 +298,16 @@ func initRun(cmd *cobra.Command, args []string) error { return fmt.Errorf("cannot start minikube VM: %w", err) } + // Create & attach swap drive to minikube + if enableSwap { + fmt.Println("Creating & attaching swap drive to minikube VM...") + vboxManager := virtualbox.NewVBoxManager() + err = vboxManager.AddSwapDisk(swap) + if err != nil { + fmt.Printf("Warning: cannot create & attach swap drive to minikube VM: %s\n", err) + } + } + // Enable dashboard err = minikube.AddonsEnable("dashboard") if err != nil { @@ -331,6 +350,7 @@ func initRun(cmd *cobra.Command, args []string) error { if err != nil { return err } + } if askForUpgrade { @@ -347,6 +367,36 @@ func initRun(cmd *cobra.Command, args []string) error { return fmt.Errorf("cannot write gokube configuration: %w", err) } + // Format & enable swap drive in minikube VM + if enableSwap { + fmt.Println("Formatting & enabling swap drive in minikube VM...") + err = addSwapToMinikube() + if err != nil { + fmt.Printf("Warning: cannot format/enable swap drive in minikube VM: %s\n", err) + } + } + fmt.Printf("\ngokube init completed in %s\n", util.Duration(time.Since(startTime))) return nil } + +func addSwapToMinikube() error { + + // Add swap file commands + swapCmds := []string{ + "sudo mkswap /dev/sdb", + "sudo swapon /dev/sdb", + "echo '/dev/sdb none swap defaults 0 0' | sudo tee -a /etc/fstab", + } + + // Execute each command + for _, cmd := range swapCmds { + sshCmd := exec.Command("minikube", "ssh", cmd) + err := sshCmd.Run() + if err != nil { + return fmt.Errorf("error running command '%s': %w", cmd, err) + } + } + + return nil +} \ No newline at end of file diff --git a/cmd/gokube/cmd/root.go b/cmd/gokube/cmd/root.go index 578255e..cd39f70 100644 --- a/cmd/gokube/cmd/root.go +++ b/cmd/gokube/cmd/root.go @@ -34,20 +34,21 @@ import ( ) const ( - DEFAULT_KUBERNETES_VERSION = "v1.27.4" - DEFAULT_KUBECTL_VERSION = "v1.27.4" - DEFAULT_MINIKUBE_VERSION = "v1.31.2" + DEFAULT_KUBERNETES_VERSION = "v1.28.3" + DEFAULT_KUBECTL_VERSION = "v1.28.3" + DEFAULT_MINIKUBE_VERSION = "v1.32.0" DEFAULT_MINIKUBE_MEMORY = 12288 DEFAULT_MINIKUBE_CPUS = 6 + DEFAULT_MINIKUBE_SWAP = 0 DEFAULT_MINIKUBE_DISK = "20g" DEFAULT_MINIKUBE_DNS_DOMAIN = "cluster.local" DEFAULT_MINIKUBE_CONTAINER_RUNTIME = "docker" DEFAULT_DOCKER_VERSION = "20.10.14" - DEFAULT_HELM_VERSION = "v3.12.3" + DEFAULT_HELM_VERSION = "v3.14.3" DEFAULT_HELM_SPRAY_VERSION = "v4.0.10" DEFAULT_HELM_IMAGE_VERSION = "v1.0.7" - DEFAULT_HELM_PUSH_VERSION = "0.10.3" - DEFAULT_STERN_VERSION = "1.23.0" + DEFAULT_HELM_PUSH_VERSION = "0.10.4" + DEFAULT_STERN_VERSION = "1.28.0" DEFAULT_MINIAPPS_REPO = "https://thalesgroup.github.io/miniapps" DEFAULT_GOKUBE_CHECK_IP = "192.168.99.100" DEFAULT_GOKUBE_CIDR = "192.168.99.1/24" diff --git a/cmd/gokube/cmd/start.go b/cmd/gokube/cmd/start.go index 31f51ab..3d01909 100644 --- a/cmd/gokube/cmd/start.go +++ b/cmd/gokube/cmd/start.go @@ -22,6 +22,7 @@ import ( "github.com/gemalto/gokube/pkg/virtualbox" "github.com/spf13/cobra" "github.com/spf13/viper" + "os/exec" ) // startCmd represents the start command @@ -62,6 +63,16 @@ func start() error { if err != nil { return fmt.Errorf("cannot restart minikube VM: %w", err) } + + // Add swap to Minikube VM + if enableSwap { + fmt.Println("Enabling swap drive in minikube VM...") + err = addSwapToMinikubeDuringStart() + if err != nil { + fmt.Printf("Warning: cannot enable swap drive in minikube VM - start: %s\n", err) + } + } + return nil } @@ -84,5 +95,40 @@ func startRun(cmd *cobra.Command, args []string) error { return err } } - return start() + // Start minikube + err := start() + if err != nil { + return err + } + + // Add swap to Minikube VM + if enableSwap { + fmt.Println("Enabling swap drive in minikube VM...") + err = addSwapToMinikubeDuringStart() + if err != nil { + fmt.Printf("Warning: cannot enable swap drive in minikube VM - start: %s\n", err) + } + } + + return nil } + +func addSwapToMinikubeDuringStart() error { + + // Add swap file commands + swapCmds := []string{ + "sudo swapon /dev/sdb", + "echo '/dev/sdb none swap defaults 0 0' | sudo tee -a /etc/fstab", + } + + // Execute each command + for _, cmd := range swapCmds { + sshCmd := exec.Command("minikube", "ssh", cmd) + err := sshCmd.Run() + if err != nil { + return fmt.Errorf("error running command '%s': %w", cmd, err) + } + } + + return nil +} \ No newline at end of file diff --git a/cmd/gokube/cmd/version.go b/cmd/gokube/cmd/version.go index b0f05c0..ed7b0e2 100644 --- a/cmd/gokube/cmd/version.go +++ b/cmd/gokube/cmd/version.go @@ -28,7 +28,7 @@ import ( ) const ( - GOKUBE_VERSION = "1.31.0" + GOKUBE_VERSION = "1.32.0" ) var gokubeVersion string diff --git a/pkg/virtualbox/vbm.go b/pkg/virtualbox/vbm.go index b313ba2..cfa9910 100644 --- a/pkg/virtualbox/vbm.go +++ b/pkg/virtualbox/vbm.go @@ -12,6 +12,8 @@ import ( "strconv" "time" + "os" + "github.com/gemalto/gokube/pkg/utils" ) const ( @@ -56,6 +58,26 @@ func NewVBoxManager() *VBoxCmdManager { } } +// CreateDisk create a disk in VBox to be used as swap +func (v *VBoxCmdManager) CreateDisk(sizeInMB int16, filePath string) error { + size := fmt.Sprintf("--size=%d", sizeInMB) + command := []string{"createmedium", "disk", "--filename", filePath, size, "--format", "VDI"} + return v.vbm(command...) +} + +// AttachDisk attach the disk created for swap to the minikube VM +func (v *VBoxCmdManager) AttachDisk(vmName string, port int, device int, filePath string) error { + command := []string{ + "storageattach", vmName, + "--storagectl", "SATA", + "--port", fmt.Sprintf("%d", port), + "--device", fmt.Sprintf("%d", device), + "--type", "hdd", + "--medium", filePath, + } + return v.vbm(command...) +} + func (v *VBoxCmdManager) vbm(args ...string) error { _, _, err := v.vbmOutErr(args...) return err @@ -155,3 +177,22 @@ func parseKeyValues(stdOut string, regexp *regexp.Regexp, callback func(key, val return s.Err() } + +// AddSwapDisk prepare, create, and attach swap disk to minikube VM +func (v *VBoxCmdManager) AddSwapDisk(swapsize int16) error { + + // Create the disk + swapDiskPath := utils.GetUserHome() + string(os.PathSeparator) + ".minikube/machines/minikube/swapdisk.vdi" + err := v.CreateDisk(swapsize, swapDiskPath) + if err != nil { + return fmt.Errorf("cannot create swap disk: %w", err) + } + + // Attach the disk to the VM + err = v.AttachDisk("minikube", 2, 0, swapDiskPath) + if err != nil { + return fmt.Errorf("cannot attach swap disk to VM: %w", err) + } + + return nil +} \ No newline at end of file