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

minikube version: add --components flag to list all included software #11843

Merged
merged 5 commits into from
Jul 1, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
46 changes: 44 additions & 2 deletions cmd/minikube/cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,23 @@ package cmd

import (
"encoding/json"
"os/exec"
"strings"

"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
"k8s.io/klog/v2"
"k8s.io/minikube/pkg/minikube/exit"
"k8s.io/minikube/pkg/minikube/mustload"
"k8s.io/minikube/pkg/minikube/out"
"k8s.io/minikube/pkg/minikube/reason"
"k8s.io/minikube/pkg/version"
)

var (
versionOutput string
shortVersion bool
versionOutput string
shortVersion bool
listComponentsVersions bool
)

var versionCmd = &cobra.Command{
Expand All @@ -43,13 +48,49 @@ var versionCmd = &cobra.Command{
"minikubeVersion": minikubeVersion,
"commit": gitCommitID,
}

if listComponentsVersions && !shortVersion {
co := mustload.Running(ClusterFlagValue())
runner := co.CP.Runner
versionCMDS := map[string]*exec.Cmd{
"docker": exec.Command("docker", "version", "--format={{.Client.Version}}"),
"containerd": exec.Command("containerd", "--version"),
"crio": exec.Command("crio", "version"),
"podman": exec.Command("sudo", "podman", "version"),
"crictl": exec.Command("sudo", "crictl", "version"),
"buildctl": exec.Command("buildctl", "--version"),
"ctr": exec.Command("sudo", "ctr", "version"),
"runc": exec.Command("runc", "--version"),
}
for k, v := range versionCMDS {
rr, err := runner.RunCmd(v)
if err != nil {
klog.Warningf("error getting %s's version: %v", k, err)
data[k] = "error"
} else {
data[k] = strings.TrimSpace(rr.Stdout.String())
}

}

}

switch versionOutput {
case "":
if !shortVersion {
out.Ln("minikube version: %v", minikubeVersion)
if gitCommitID != "" {
out.Ln("commit: %v", gitCommitID)
}
for k, v := range data {
// for backward compatibility we keep displaying the old way for these two
if k == "minikubeVersion" || k == "commit" {
continue
}
if v != "" {
out.Ln("\n%s:\n%s", k, v)
}
}
} else {
out.Ln("%v", minikubeVersion)
}
Expand All @@ -74,4 +115,5 @@ var versionCmd = &cobra.Command{
func init() {
versionCmd.Flags().StringVarP(&versionOutput, "output", "o", "", "One of 'yaml' or 'json'.")
versionCmd.Flags().BoolVar(&shortVersion, "short", false, "Print just the version number.")
versionCmd.Flags().BoolVar(&listComponentsVersions, "components", false, "list versions of all components included with minikube. (the cluster must be running)")
}
35 changes: 35 additions & 0 deletions test/integration/functional_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import (
"k8s.io/minikube/pkg/minikube/reason"
"k8s.io/minikube/pkg/util/retry"

"github.com/blang/semver/v4"
"github.com/elazarl/goproxy"
"github.com/hashicorp/go-retryablehttp"
"github.com/otiai10/copy"
Expand Down Expand Up @@ -154,6 +155,7 @@ func TestFunctional(t *testing.T) {
{"BuildImage", validateBuildImage},
{"ListImages", validateListImages},
{"NonActiveRuntimeDisabled", validateNotActiveRuntimeDisabled},
{"Version", validateVersionCmd},
}
for _, tc := range tests {
tc := tc
Expand Down Expand Up @@ -1879,3 +1881,36 @@ func startMinikubeWithProxy(ctx context.Context, t *testing.T, profile string, p
t.Errorf("start stderr=%s, want: *%s*", rr.Stderr.String(), want)
}
}

// validateVersionCmd asserts `minikube version` command works fine for both --short and --components
func validateVersionCmd(ctx context.Context, t *testing.T, profile string) {

t.Run("short", func(t *testing.T) {
MaybeParallel(t)
rr, err := Run(t, exec.CommandContext(ctx, Target(), "-p", profile, "version", "--short"))
if err != nil {
t.Errorf("failed to get version --short: %v", err)
}

_, err = semver.Make(strings.TrimSpace(strings.Trim(rr.Stdout.String(), "v")))
if err != nil {
t.Errorf("failed to get a valid semver for minikube version --short:%s %v", rr.Output(), err)
}
})

t.Run("components", func(t *testing.T) {
MaybeParallel(t)
rr, err := Run(t, exec.CommandContext(ctx, Target(), "-p", profile, "version", "-o=json", "--components"))
if err != nil {
t.Errorf("error version: %v", err)
}
got := rr.Stdout.String()
for _, c := range []string{"buildctl", "commit", "containerd", "crictl", "crio", "ctr", "docker", "minikubeVersion", "podman", "run"} {
if !strings.Contains(got, c) {
t.Errorf("expected to see %q in the minikube version --components but got:\n%s", c, got)
}

}
})

}