Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --no-kubernetes flag to start minikube without kubernetes #12848

Merged
merged 20 commits into from
Nov 4, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion cmd/minikube/cmd/config/profile_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

"k8s.io/minikube/pkg/minikube/bootstrapper/bsutil/kverify"
"k8s.io/minikube/pkg/minikube/config"
"k8s.io/minikube/pkg/minikube/constants"
"k8s.io/minikube/pkg/minikube/driver"
"k8s.io/minikube/pkg/minikube/exit"
"k8s.io/minikube/pkg/minikube/machine"
Expand Down Expand Up @@ -164,7 +165,11 @@ func profilesToTableData(profiles []*config.Profile) [][]string {
exit.Error(reason.GuestCpConfig, "error getting primary control plane", err)
}

data = append(data, []string{p.Name, p.Config.Driver, p.Config.KubernetesConfig.ContainerRuntime, cp.IP, strconv.Itoa(cp.Port), p.Config.KubernetesConfig.KubernetesVersion, p.Status, strconv.Itoa(len(p.Config.Nodes))})
k8sVersion := p.Config.KubernetesConfig.KubernetesVersion
if k8sVersion == constants.NoKubernetesVersion { // for --no-kubernetes flag
k8sVersion = "N/A"
}
data = append(data, []string{p.Name, p.Config.Driver, p.Config.KubernetesConfig.ContainerRuntime, cp.IP, strconv.Itoa(cp.Port), k8sVersion, p.Status, strconv.Itoa(len(p.Config.Nodes))})
}
return data
}
Expand Down
31 changes: 23 additions & 8 deletions cmd/minikube/cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"strconv"
"strings"

"github.com/Delta456/box-cli-maker/v2"
"github.com/blang/semver/v4"
"github.com/docker/machine/libmachine/ssh"
"github.com/google/go-containerregistry/pkg/authn"
Expand Down Expand Up @@ -432,6 +433,17 @@ func displayEnviron(env []string) {
}

func showKubectlInfo(kcs *kubeconfig.Settings, k8sVersion string, machineName string) error {
if k8sVersion == constants.NoKubernetesVersion {
register.Reg.SetStep(register.Done)
out.Step(style.Ready, "Done! minikube is ready without Kubernetes!")
out.BoxedWithConfig(box.Config{Py: 1, Px: 4, Type: "Round", Color: "Green"}, style.Tip, "Things to try without Kubernetes ...", `- "minikube ssh" to SSH into minikube's node.
- "minikube docker-env" to point your docker-cli to the docker inside minikube.
- "minikube image" to build images without docker`)
medyagh marked this conversation as resolved.
Show resolved Hide resolved
return nil
}

// here are some ideas to do without Kubernetes:
medyagh marked this conversation as resolved.
Show resolved Hide resolved

// To be shown at the end, regardless of exit path
defer func() {
register.Reg.SetStep(register.Done)
Expand Down Expand Up @@ -1463,16 +1475,14 @@ func autoSetDriverOptions(cmd *cobra.Command, drvName string) (err error) {
// validateKubernetesVersion ensures that the requested version is reasonable
func validateKubernetesVersion(old *config.ClusterConfig) {
nvs, _ := semver.Make(strings.TrimPrefix(getKubernetesVersion(old), version.VersionPrefix))
oldestVersion := semver.MustParse(strings.TrimPrefix(constants.OldestKubernetesVersion, version.VersionPrefix))
defaultVersion := semver.MustParse(strings.TrimPrefix(constants.DefaultKubernetesVersion, version.VersionPrefix))
sharifelgamal marked this conversation as resolved.
Show resolved Hide resolved
zeroVersion := semver.MustParse(strings.TrimPrefix(constants.NoKubernetesVersion, version.VersionPrefix))

oldestVersion, err := semver.Make(strings.TrimPrefix(constants.OldestKubernetesVersion, version.VersionPrefix))
if err != nil {
exit.Message(reason.InternalSemverParse, "Unable to parse oldest Kubernetes version from constants: {{.error}}", out.V{"error": err})
}
defaultVersion, err := semver.Make(strings.TrimPrefix(constants.DefaultKubernetesVersion, version.VersionPrefix))
if err != nil {
exit.Message(reason.InternalSemverParse, "Unable to parse default Kubernetes version from constants: {{.error}}", out.V{"error": err})
if nvs.Equals(zeroVersion) {
klog.Infof("No Kuberentes version set for minikube, setting Kubernetes version to %s", constants.NoKubernetesVersion)
return
}

if nvs.LT(oldestVersion) {
out.WarningT("Specified Kubernetes version {{.specified}} is less than the oldest supported version: {{.oldest}}", out.V{"specified": nvs, "oldest": constants.OldestKubernetesVersion})
if !viper.GetBool(force) {
Expand Down Expand Up @@ -1519,6 +1529,11 @@ func isBaseImageApplicable(drv string) bool {
}

func getKubernetesVersion(old *config.ClusterConfig) string {
if viper.GetBool(noKubernetes) {
klog.Infof("No Kubernetes flag is set, setting Kubernetes version to %s", constants.NoKubernetesVersion)
viper.Set(kubernetesVersion, constants.NoKubernetesVersion)
}

paramVersion := viper.GetString(kubernetesVersion)

// try to load the old version first if the user didn't specify anything
Expand Down
2 changes: 2 additions & 0 deletions cmd/minikube/cmd/start_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ const (
nfsSharesRoot = "nfs-shares-root"
nfsShare = "nfs-share"
kubernetesVersion = "kubernetes-version"
noKubernetes = "no-kubernetes"
hostOnlyCIDR = "host-only-cidr"
containerRuntime = "container-runtime"
criSocket = "cri-socket"
Expand Down Expand Up @@ -164,6 +165,7 @@ func initMinikubeFlags() {
startCmd.Flags().Bool(installAddons, true, "If set, install addons. Defaults to true.")
startCmd.Flags().IntP(nodes, "n", 1, "The number of nodes to spin up. Defaults to 1.")
startCmd.Flags().Bool(preload, true, "If set, download tarball of preloaded images if available to improve start time. Defaults to true.")
startCmd.Flags().Bool(noKubernetes, false, "If set, minikube VM/container will start without starting or configuring Kubernetes. (only works on new clusters)")
startCmd.Flags().Bool(deleteOnFailure, false, "If set, delete the current cluster if start fails and try again. Defaults to false.")
startCmd.Flags().Bool(forceSystemd, false, "If set, force the container runtime to use systemd as cgroup manager. Defaults to false.")
startCmd.Flags().StringP(network, "", "", "network to run minikube with. Now it is used by docker/podman and KVM drivers. If left empty, minikube will create a new network.")
Expand Down
3 changes: 3 additions & 0 deletions pkg/minikube/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ const (
NewestKubernetesVersion = "v1.22.4-rc.0"
// OldestKubernetesVersion is the oldest Kubernetes version to test against
OldestKubernetesVersion = "v1.14.0"
// NoKubernetesVersion is the version used when users does NOT want to install kubernetes
NoKubernetesVersion = "v0.0.0"

// DefaultClusterName is the default nane for the k8s cluster
DefaultClusterName = "minikube"
// DockerDaemonPort is the port Docker daemon listening inside a minikube node (vm or container).
Expand Down
18 changes: 13 additions & 5 deletions pkg/minikube/node/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ type Starter struct {

// Start spins up a guest and starts the Kubernetes node.
func Start(starter Starter, apiServer bool) (*kubeconfig.Settings, error) {
var kcs *kubeconfig.Settings
if starter.Node.KubernetesVersion == constants.NoKubernetesVersion { // do not bootstrap cluster if --no-kubernetes
return kcs, config.Write(viper.GetString(config.ProfileName), starter.Cfg)
}
// wait for preloaded tarball to finish downloading before configuring runtimes
waitCacheRequiredImages(&cacheGroup)

Expand Down Expand Up @@ -115,7 +119,6 @@ func Start(starter Starter, apiServer bool) (*kubeconfig.Settings, error) {
}

var bs bootstrapper.Bootstrapper
var kcs *kubeconfig.Settings
if apiServer {
// Must be written before bootstrap, otherwise health checks may flake due to stale IP
kcs = setupKubeconfig(starter.Host, starter.Cfg, starter.Node, starter.Cfg.Name)
Expand Down Expand Up @@ -286,11 +289,16 @@ func joinCluster(starter Starter, cpBs bootstrapper.Bootstrapper, bs bootstrappe
func Provision(cc *config.ClusterConfig, n *config.Node, apiServer bool, delOnFail bool) (command.Runner, bool, libmachine.API, *host.Host, error) {
register.Reg.SetStep(register.StartingNode)
name := config.MachineName(*cc, *n)
if apiServer {
out.Step(style.ThumbsUp, "Starting control plane node {{.name}} in cluster {{.cluster}}", out.V{"name": name, "cluster": cc.Name})
} else {
out.Step(style.ThumbsUp, "Starting node {{.name}} in cluster {{.cluster}}", out.V{"name": name, "cluster": cc.Name})

startingPhrase := "Starting control plane node"
if !apiServer {
startingPhrase = "Starting worker node"
}
if cc.KubernetesConfig.KubernetesVersion == constants.NoKubernetesVersion {
startingPhrase = "Starting minikube without Kubernetes"
}

out.Step(style.ThumbsUp, "{{.nodeName}} {{.name}} in cluster {{.cluster}}", out.V{"nodeName": startingPhrase, "name": name, "cluster": cc.Name})
medyagh marked this conversation as resolved.
Show resolved Hide resolved

if driver.IsKIC(cc.Driver) {
beginDownloadKicBaseImage(&kicGroup, cc, viper.GetBool("download-only"))
Expand Down
4 changes: 2 additions & 2 deletions pkg/minikube/out/out.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,13 @@ func BoxedErr(format string, a ...V) {
}

// BoxedWithConfig writes a templated message in a box with customized style config to stdout
func BoxedWithConfig(cfg box.Config, st style.Enum, title string, format string, a ...V) {
func BoxedWithConfig(cfg box.Config, st style.Enum, title string, text string, a ...V) {
if st != style.None {
title = Sprintf(st, title)
}
// need to make sure no newlines are in the title otherwise box-cli-maker panics
title = strings.ReplaceAll(title, "\n", "")
boxedCommon(String, cfg, title, format, a...)
boxedCommon(String, cfg, title, text, a...)
}

// Sprintf is used for returning the string (doesn't write anything)
Expand Down
135 changes: 135 additions & 0 deletions test/integration/no_kubernetes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
//go:build integration
// +build integration

/*
Copyright 2021 The Kubernetes Authors All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package integration

import (
"context"
"os/exec"
"strings"
"testing"
)

// TestNoKubernetes tests starting minikube without Kubernetes,
// for usecases where user only needs to use the container runtime (docker,containerd,crio) inside minikube
medyagh marked this conversation as resolved.
Show resolved Hide resolved
func TestNoKubernetes(t *testing.T) {
MaybeParallel(t)

type validateFunc func(context.Context, *testing.T, string)
profile := UniqueProfileName("NoKubernetes")
ctx, cancel := context.WithTimeout(context.Background(), Minutes(5))
defer Cleanup(t, profile, cancel)

// Serial tests
t.Run("serial", func(t *testing.T) {
tests := []struct {
name string
validator validateFunc
}{
{"Start", validateStartNoK8S},
{"VerifyK8sNotRunning", validateK8SNotRunning},
{"ProfileList", validateProfileListNoK8S},
{"Stop", validateStopNoK8S},
{"StartNoArgs", validateStartNorArgs},
medyagh marked this conversation as resolved.
Show resolved Hide resolved
{"VerifyK8sNotRunningSecond", validateK8SNotRunning},
}

for _, tc := range tests {
tc := tc

if ctx.Err() == context.DeadlineExceeded {
t.Fatalf("Unable to run more tests (deadline exceeded)")
}

t.Run(tc.name, func(t *testing.T) {
tc.validator(ctx, t, profile)
if t.Failed() && *postMortemLogs {
PostMortemLogs(t, profile)
}
})
}
})
}

// validateStartNoK8S starts a minikube cluster without kuberentes started/configured
medyagh marked this conversation as resolved.
Show resolved Hide resolved
func validateStartNoK8S(ctx context.Context, t *testing.T, profile string) {
defer PostMortemLogs(t, profile)

args := append([]string{"start", "-p", profile, "--no-kubernetes"}, StartArgs()...)
rr, err := Run(t, exec.CommandContext(ctx, Target(), args...))
if err != nil {
t.Fatalf("failed to start minikube with args: %q : %v", rr.Command(), err)
}
}

// validateK8SNotRunning validates that there is no kubernetes running inside minikube
func validateK8SNotRunning(ctx context.Context, t *testing.T, profile string) {
defer PostMortemLogs(t, profile)

args := []string{"ssh", "-p", profile, "sudo systemctl is-active --quiet service kubelet"}
rr, err := Run(t, exec.CommandContext(ctx, Target(), args...))
if err == nil {
t.Fatalf("Expected Kubelet not to be running and but it is running : %q : %v", rr.Command(), err)
}
}

// validateStopNoK8S validates that minikube is stopped after a --no-kubernetes start
func validateStopNoK8S(ctx context.Context, t *testing.T, profile string) {
defer PostMortemLogs(t, profile)

args := []string{"stop", "-p", profile}
rr, err := Run(t, exec.CommandContext(ctx, Target(), args...))
if err != nil {
t.Fatalf("Failed to stop minikbue %q : %v", rr.Command(), err)
medyagh marked this conversation as resolved.
Show resolved Hide resolved
}
}

// validateProfileListNoK8S validates that profile list works with --no-kubernetes
func validateProfileListNoK8S(ctx context.Context, t *testing.T, profile string) {
defer PostMortemLogs(t, profile)

args := []string{"profile", "list"}
rr, err := Run(t, exec.CommandContext(ctx, Target(), args...))
if err != nil {
t.Fatalf("Profile list failed : %q : %v", rr.Command(), err)
}

if !strings.Contains(rr.Output(), "N/A") {
t.Fatalf("expected N/A in the profile list for kubernets version but got : %q : %v", rr.Command(), rr.Output())
medyagh marked this conversation as resolved.
Show resolved Hide resolved
}

args = []string{"profile", "list", "--output=json"}
rr, err = Run(t, exec.CommandContext(ctx, Target(), args...))
if err != nil {
t.Fatalf("Profile list --output=json failed : %q : %v", rr.Command(), err)
}

}

// validateStartNorArgs valides that minikube start with no args works
medyagh marked this conversation as resolved.
Show resolved Hide resolved
func validateStartNorArgs(ctx context.Context, t *testing.T, profile string) {
medyagh marked this conversation as resolved.
Show resolved Hide resolved
defer PostMortemLogs(t, profile)

args := append([]string{"start", "-p", profile, "--no-kubernetes"}, StartArgs()...)
rr, err := Run(t, exec.CommandContext(ctx, Target(), args...))
if err != nil {
t.Fatalf("failed to start minikube with args: %q : %v", rr.Command(), err)
}

}