Skip to content

Commit

Permalink
kops version: Add --short flag, use it to get version in scripts
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
justinsb committed Dec 21, 2018
1 parent ede358c commit 2f491df
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 28 deletions.
1 change: 1 addition & 0 deletions cmd/kops/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
33 changes: 11 additions & 22 deletions cmd/kops/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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
}
6 changes: 4 additions & 2 deletions docs/development/adding_a_feature.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,11 @@ and then push nodeup using:

```
export S3_BUCKET_NAME=<yourbucketname>
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 <clustername> --zones us-east-1b
...
Expand Down
5 changes: 3 additions & 2 deletions docs/development/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,10 @@ can do the following:
```
# cd to your kops repo
export S3_BUCKET_NAME=<yourbucketname>
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 <clustername> --zones us-east-1b
```
Expand Down
4 changes: 2 additions & 2 deletions hack/dev-build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 2 additions & 0 deletions pkg/commands/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
45 changes: 45 additions & 0 deletions pkg/commands/version.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 2f491df

Please sign in to comment.