Skip to content

Commit

Permalink
Merge pull request #991 from dgageot/print-kubectl-version
Browse files Browse the repository at this point in the history
Print kubectl client version
  • Loading branch information
dgageot authored Sep 21, 2018
2 parents af05c9b + 3158437 commit 7755b82
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pkg/skaffold/deploy/kubectl.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"strings"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/build"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/color"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/constants"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/deploy/kubectl"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/v1alpha3"
Expand Down Expand Up @@ -64,6 +65,8 @@ func (k *KubectlDeployer) Labels() map[string]string {
// Deploy templates the provided manifests with a simple `find and replace` and
// runs `kubectl apply` on those manifests
func (k *KubectlDeployer) Deploy(ctx context.Context, out io.Writer, builds []build.Artifact) ([]Artifact, error) {
color.Default.Fprintln(out, "kubectl client version:", k.kubectl.Version())

manifests, err := k.readManifests(ctx)
if err != nil {
return nil, errors.Wrap(err, "reading manifests")
Expand Down
3 changes: 3 additions & 0 deletions pkg/skaffold/deploy/kubectl/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"io"
"os/exec"
"sync"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/v1alpha3"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
Expand All @@ -33,6 +34,8 @@ type CLI struct {
KubeContext string
Flags v1alpha3.KubectlFlags

version ClientVersion
versionOnce sync.Once
previousApply ManifestList
}

Expand Down
75 changes: 75 additions & 0 deletions pkg/skaffold/deploy/kubectl/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
Copyright 2018 The Skaffold Authors
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 kubectl

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

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
"github.com/sirupsen/logrus"
)

const unknown = "unknown"

// Version is the client version of kubectl.
type Version struct {
Client ClientVersion `json:"clientVersion"`
}

// ClientVersion is the client version of kubectl.
type ClientVersion struct {
Major string `json:"major"`
Minor string `json:"minor"`
}

func (v ClientVersion) String() string {
if v.Major == unknown || v.Minor == unknown {
return unknown
}

return v.Major + "." + v.Minor
}

// Version returns the client version of kubectl.
func (c *CLI) Version() ClientVersion {
c.versionOnce.Do(func() {
version := Version{
Client: ClientVersion{
Major: unknown,
Minor: unknown,
},
}

buf, err := c.getVersion(context.Background())
if err != nil {
logrus.Warnln("unable to get kubectl client version", err)
} else if err := json.Unmarshal(buf, &version); err != nil {
logrus.Warnln("unable to parse client version", err)
}

c.version = version.Client
})

return c.version
}

func (c *CLI) getVersion(ctx context.Context) ([]byte, error) {
cmd := exec.CommandContext(ctx, "kubectl", "version", "--client", "-ojson")
return util.RunCmdOut(cmd)
}

0 comments on commit 7755b82

Please sign in to comment.