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

Warn if incompatible kubectl version is in use #5596

Merged
merged 2 commits into from
Oct 17, 2019

Conversation

nanikjava
Copy link
Contributor

Fixes: #3329

Modification is done inside start.go where additional checking on the
version returned via the kubectl CLI is checked.

Running 'kubectl version --output=json' will return both client
and server information.

Fixes: kubernetes#3329

Modification is done inside start.go where additional checking on the
version returned via the kubectl CLI is checked.

Running 'kubectl version --output=json' will return both client
and server information.
@k8s-ci-robot k8s-ci-robot added do-not-merge/invalid-commit-message Indicates that a PR should not merge because it has an invalid commit message. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels Oct 11, 2019
@minikube-bot
Copy link
Collaborator

Can one of the admins verify this patch?

@k8s-ci-robot
Copy link
Contributor

Hi @nanikjava. Thanks for your PR.

I'm waiting for a kubernetes member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@k8s-ci-robot k8s-ci-robot added needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Oct 11, 2019
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: nanikjava
To complete the pull request process, please assign medyagh
You can assign the PR to them by writing /assign @medyagh in a comment when ready.

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@codecov-io
Copy link

codecov-io commented Oct 11, 2019

Codecov Report

Merging #5596 into master will increase coverage by 0.25%.
The diff coverage is 0%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #5596      +/-   ##
==========================================
+ Coverage   36.89%   37.15%   +0.25%     
==========================================
  Files         102      103       +1     
  Lines        7369     7545     +176     
==========================================
+ Hits         2719     2803      +84     
- Misses       4297     4369      +72     
- Partials      353      373      +20
Impacted Files Coverage Δ
cmd/minikube/cmd/start.go 22% <0%> (-0.64%) ⬇️
pkg/minikube/config/profile.go 71.59% <0%> (-1.67%) ⬇️
pkg/minikube/assets/addons.go 36.66% <0%> (ø) ⬆️
pkg/minikube/cluster/machine.go 50% <0%> (ø)
pkg/minikube/cluster/cluster.go 38.05% <0%> (+0.58%) ⬆️
cmd/minikube/cmd/delete.go 30% <0%> (+30%) ⬆️

out.T(out.Tip, "For best results, install kubectl: https://kubernetes.io/docs/tasks/tools/install-kubectl/")
}
}

if kcs.KeepContext {
Copy link
Contributor

@tstromberg tstromberg Oct 17, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move this connection output to the top of showKubectlConnectInfo.

This will allow both the version mismatch error to come at the end, which is more visible, and will allow you to exit early if kubectl does not exist, which will allow for more readable code as the version checking won't have to be indented.

out.T(out.Tip, "For best results, install kubectl: https://kubernetes.io/docs/tasks/tools/install-kubectl/")
} else {
glog.Infof("Kubectl found at the following location %s. Let's execute and get the result", path)
output, err = exec.Command(path, "version", "--output=json").Output()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may show the wrong server version, as it queries the active context kubectl is configured for, which may not be minikube.

My suggestion is to use kubectl version --short --output=json, and add k8sVersion as an argument to the function.

if err != nil {
out.T(out.Tip, "For best results, install kubectl: https://kubernetes.io/docs/tasks/tools/install-kubectl/")
} else {
glog.Infof("Kubectl found at the following location %s. Let's execute and get the result", path)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While good for initial debugging, I think this log message can be removed.

output, err = exec.Command(path, "version", "--output=json").Output()

// ...once we get something back do some version checking
if err == nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mention this later, but try to structure functions so that exceptional conditions are handled first, and exited early. So for istance:

if err != nil {
.. return ..
}
.. do the rest with one level less indentation

clientjsonErr := json.Unmarshal(output, &clientVersion)

// ....... and {server} json
serverjsonErr := json.Unmarshal(output, &serverVersion)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to unmarshal the same output twice: the server version is already in clientVersion.SVersion

serverjsonErr := json.Unmarshal(output, &serverVersion)

if (clientjsonErr != nil) || (serverjsonErr != nil) {
glog.Infof("There was an error processing the json output")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here is another opportunity to return early.

serverMinor, _ := strconv.Atoi(serverVersion.SVersion.Minor)
clientMinor, _ := strconv.Atoi(serverVersion.CVersion.Minor)

if clientMinor < serverMinor {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not quite correct yet. https://kubernetes.io/docs/setup/release/version-skew-policy/#kubectl dictates that you are allowed up to one version difference. One possibility is:

if math.Abs(float64(clientMinor - serverMinor)) > 1 {

clientMinor, _ := strconv.Atoi(serverVersion.CVersion.Minor)

if clientMinor < serverMinor {
out.T(out.Tip, "The version of kubectl {{.kubectl}} installed is incompatible with your Kubernetes {{.kubernetes}} deployment. Please upgrade/downgrade as necessary, or use minikube kubectlto connect to your cluster",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be helpful to include the path. How about something like:

{{.path}} is version {{.version}}, and is incompatible with your specified Kubernetes version. You will need to update {{.path}} or use 'minikube kubectl' to connect with this cluster

* When nil is returned from function just return back
* Restructure the output so that it will make more sense
for the user
@k8s-ci-robot
Copy link
Contributor

Keywords which can automatically close issues and at(@) mentions are not allowed in commit messages.

The list of commits with invalid commit messages:

  • 9bf9041 Warn if incompatible kubectl version is in use

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. I understand the commands that are listed here.

Copy link
Contributor

@tstromberg tstromberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@tstromberg tstromberg merged commit a84b3a9 into kubernetes:master Oct 17, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. do-not-merge/invalid-commit-message Indicates that a PR should not merge because it has an invalid commit message. needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Warn if incompatible kubectl version is in use
5 participants