From 2f491df2d6815e8ee97fe5c3adc3d89c489877ca Mon Sep 17 00:00:00 2001 From: Justin SB Date: Thu, 20 Dec 2018 10:33:06 -0500 Subject: [PATCH] kops version: Add --short flag, use it to get version in scripts We upload to a location that includes the version, but we need to specify the version in KOPS_BASE_URL. We expose an option to make `kops version` more amenable to this scripting. --- cmd/kops/root.go | 1 + cmd/kops/version.go | 33 +++++++------------- docs/development/adding_a_feature.md | 6 ++-- docs/development/testing.md | 5 ++-- hack/dev-build.sh | 4 +-- pkg/commands/BUILD.bazel | 2 ++ pkg/commands/version.go | 45 ++++++++++++++++++++++++++++ 7 files changed, 68 insertions(+), 28 deletions(-) create mode 100644 pkg/commands/version.go diff --git a/cmd/kops/root.go b/cmd/kops/root.go index 87e02b923f121..d2605d0047d09 100644 --- a/cmd/kops/root.go +++ b/cmd/kops/root.go @@ -147,6 +147,7 @@ func NewCmdRoot(f *util.Factory, out io.Writer) *cobra.Command { cmd.AddCommand(NewCmdSet(f, out)) cmd.AddCommand(NewCmdToolbox(f, out)) cmd.AddCommand(NewCmdValidate(f, out)) + cmd.AddCommand(NewCmdVersion(f, out)) return cmd } diff --git a/cmd/kops/version.go b/cmd/kops/version.go index 68d9f875719f6..e0250d9219439 100644 --- a/cmd/kops/version.go +++ b/cmd/kops/version.go @@ -17,10 +17,11 @@ limitations under the License. package main import ( - "fmt" + "io" "github.com/spf13/cobra" - "k8s.io/kops" + "k8s.io/kops/cmd/kops/util" + "k8s.io/kops/pkg/commands" "k8s.io/kubernetes/pkg/kubectl/cmd/templates" "k8s.io/kubernetes/pkg/kubectl/util/i18n" ) @@ -35,37 +36,25 @@ var ( versionShort = i18n.T(`Print the kops version information.`) ) -type VersionCmd struct { - cobraCommand *cobra.Command -} +// NewCmdVersion builds a cobra command for the kops version command +func NewCmdVersion(f *util.Factory, out io.Writer) *cobra.Command { + options := &commands.VersionOptions{} -var versionCmd = VersionCmd{ - cobraCommand: &cobra.Command{ + cmd := &cobra.Command{ Use: "version", Short: versionShort, Long: versionLong, Example: versionExample, - }, -} - -func init() { - cmd := versionCmd.cobraCommand - rootCommand.cobraCommand.AddCommand(cmd) + } cmd.Run = func(cmd *cobra.Command, args []string) { - err := versionCmd.Run() + err := commands.RunVersion(f, out, options) if err != nil { exitWithError(err) } } -} -func (c *VersionCmd) Run() error { - s := "Version " + kops.Version - if kops.GitVersion != "" { - s += " (git-" + kops.GitVersion + ")" - } - fmt.Println(s) + cmd.Flags().BoolVar(&options.Short, "short", options.Short, "only print the main kops version, useful for scripting") - return nil + return cmd } diff --git a/docs/development/adding_a_feature.md b/docs/development/adding_a_feature.md index 368cc927d75bc..774b4f256d1ce 100644 --- a/docs/development/adding_a_feature.md +++ b/docs/development/adding_a_feature.md @@ -186,9 +186,11 @@ and then push nodeup using: ``` export S3_BUCKET_NAME= -make kops-install upload S3_BUCKET=s3://${S3_BUCKET_NAME} VERSION=dev +KOPS_VERSION=`bazel run //cmd/kops version -- --short` -export KOPS_BASE_URL=https://${S3_BUCKET_NAME}.s3.amazonaws.com/kops/dev/ +make kops-install upload S3_BUCKET=s3://${S3_BUCKET_NAME} + +export KOPS_BASE_URL=https://${S3_BUCKET_NAME}.s3.amazonaws.com/kops/${KOPS_VERSION}/ kops create cluster --zones us-east-1b ... diff --git a/docs/development/testing.md b/docs/development/testing.md index 9a773e36bbd39..700a8152f49cc 100644 --- a/docs/development/testing.md +++ b/docs/development/testing.md @@ -74,9 +74,10 @@ can do the following: ``` # cd to your kops repo export S3_BUCKET_NAME= -make kops-install upload S3_BUCKET=s3://${S3_BUCKET_NAME} VERSION=dev +KOPS_VERSION=`bazel run //cmd/kops version -- --short` +make kops-install upload S3_BUCKET=s3://${S3_BUCKET_NAME} -export KOPS_BASE_URL=https://${S3_BUCKET_NAME}.s3.amazonaws.com/kops/dev/ +export KOPS_BASE_URL=https://${S3_BUCKET_NAME}.s3.amazonaws.com/kops/${KOPS_VERSION}/ kops create cluster --zones us-east-1b ``` diff --git a/hack/dev-build.sh b/hack/dev-build.sh index 450ea835ce6c3..e9f154091f33e 100755 --- a/hack/dev-build.sh +++ b/hack/dev-build.sh @@ -100,8 +100,8 @@ make && S3_BUCKET=s3://${NODEUP_BUCKET} make upload # removing make test since it relies on the files in the bucket # && make test -KOPS_CHANNEL=$(kops version | awk '{ print $2 }' |sed 's/\+/%2B/') -KOPS_BASE_URL="http://${NODEUP_BUCKET}.s3.amazonaws.com/kops/${KOPS_CHANNEL}/" +KOPS_VERSION=$(kops version --short) +KOPS_BASE_URL="http://${NODEUP_BUCKET}.s3.amazonaws.com/kops/${KOPS_VERSION}/" echo "KOPS_BASE_URL=${KOPS_BASE_URL}" echo "NODEUP_URL=${KOPS_BASE_URL}linux/amd64/nodeup" diff --git a/pkg/commands/BUILD.bazel b/pkg/commands/BUILD.bazel index 359feab1d1162..3854f8449d485 100644 --- a/pkg/commands/BUILD.bazel +++ b/pkg/commands/BUILD.bazel @@ -6,10 +6,12 @@ go_library( "helpers_readwrite.go", "set_cluster.go", "status_discovery.go", + "version.go", ], importpath = "k8s.io/kops/pkg/commands", visibility = ["//visibility:public"], deps = [ + "//:go_default_library", "//cmd/kops/util:go_default_library", "//pkg/apis/kops:go_default_library", "//pkg/apis/kops/validation:go_default_library", diff --git a/pkg/commands/version.go b/pkg/commands/version.go new file mode 100644 index 0000000000000..2652d2b1cf4b6 --- /dev/null +++ b/pkg/commands/version.go @@ -0,0 +1,45 @@ +/* +Copyright 2018 The Kubernetes 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 commands + +import ( + "fmt" + "io" + + "k8s.io/kops" + "k8s.io/kops/cmd/kops/util" +) + +type VersionOptions struct { + Short bool +} + +// RunVersion implements the version command logic +func RunVersion(f *util.Factory, out io.Writer, options *VersionOptions) error { + var s string + if options.Short { + s = kops.Version + } else { + s = "Version " + kops.Version + if kops.GitVersion != "" { + s += " (git-" + kops.GitVersion + ")" + } + } + + _, err := fmt.Fprintf(out, "%s\n", s) + return err +}