From 9eca9c882ba5fc4d8f1912723ab5a02a7dbf2ea6 Mon Sep 17 00:00:00 2001 From: Ole Markus With Date: Wed, 15 Sep 2021 17:10:24 +0200 Subject: [PATCH] Add flag to version showing the kops 'server' version --- cmd/kops/root.go | 11 ++++++ cmd/kops/version.go | 81 +++++++++++++++++++++++++++++++++++++--- docs/cli/kops_version.md | 5 ++- pkg/commands/version.go | 45 ---------------------- 4 files changed, 90 insertions(+), 52 deletions(-) delete mode 100644 pkg/commands/version.go diff --git a/cmd/kops/root.go b/cmd/kops/root.go index c413435651..732740cb17 100644 --- a/cmd/kops/root.go +++ b/cmd/kops/root.go @@ -231,6 +231,17 @@ func (c *RootCmd) clusterNameArgsNoKubeconfig(clusterName *string) func(cmd *cob } } +func (c *RootCmd) clusterNameArgsAllowNoCluster(clusterName *string) func(cmd *cobra.Command, args []string) error { + return func(cmd *cobra.Command, args []string) error { + if err := c.ProcessArgs(args); err != nil { + return err + } + + *clusterName = c.clusterName + return nil + } +} + // ProcessArgs will parse the positional args. It assumes one of these formats: // * // * (and --name not specified) diff --git a/cmd/kops/version.go b/cmd/kops/version.go index 1a911bac20..97d4cd7f3a 100644 --- a/cmd/kops/version.go +++ b/cmd/kops/version.go @@ -17,11 +17,15 @@ limitations under the License. package main import ( + "context" + "fmt" "io" "github.com/spf13/cobra" + "k8s.io/kops" "k8s.io/kops/cmd/kops/util" - "k8s.io/kops/pkg/commands" + "k8s.io/kops/pkg/apis/kops/registry" + "k8s.io/kops/util/pkg/vfs" "k8s.io/kubectl/pkg/util/i18n" "k8s.io/kubectl/pkg/util/templates" ) @@ -38,21 +42,88 @@ var ( // NewCmdVersion builds a cobra command for the kops version command func NewCmdVersion(f *util.Factory, out io.Writer) *cobra.Command { - options := &commands.VersionOptions{} + options := &VersionOptions{} cmd := &cobra.Command{ Use: "version", Short: versionShort, Long: versionLong, Example: versionExample, - Args: cobra.NoArgs, + Args: rootCommand.clusterNameArgsAllowNoCluster(&options.ClusterName), ValidArgsFunction: cobra.NoFileCompletions, RunE: func(cmd *cobra.Command, args []string) error { - return commands.RunVersion(f, out, options) + return RunVersion(f, out, options) }, } - cmd.Flags().BoolVar(&options.Short, "short", options.Short, "only print the main kOps version. Useful for scripting.") + cmd.Flags().BoolVar(&options.short, "short", options.short, "only print the main kOps version. Useful for scripting.") + cmd.Flags().BoolVar(&options.server, "server", options.server, "show the kOps version that made the last change to the state store.") return cmd } + +type VersionOptions struct { + short bool + server bool + ClusterName string +} + +// RunVersion implements the version command logic +func RunVersion(f *util.Factory, out io.Writer, options *VersionOptions) error { + if options.short { + s := kops.Version + _, err := fmt.Fprintf(out, "%s\n", s) + if err != nil { + return err + } + if options.server { + server := serverVersion(f, options) + + _, err := fmt.Fprintf(out, "%s\n", server) + return err + } + + return nil + } else { + client := kops.Version + if kops.GitVersion != "" { + client += " (git-" + kops.GitVersion + ")" + } + + { + _, err := fmt.Fprintf(out, "Client version: %s\n", client) + if err != nil { + return err + } + } + if options.server { + server := serverVersion(f, options) + + _, err := fmt.Fprintf(out, "Last applied server version: %s\n", server) + return err + } + return nil + } +} + +func serverVersion(f *util.Factory, options *VersionOptions) string { + if options.ClusterName == "" { + return "No cluster selected" + } + + ctx := context.Background() + cluster, err := GetCluster(ctx, f, options.ClusterName) + if err != nil { + return "could not fetch cluster" + } + configBase, err := vfs.Context.BuildVfsPath(cluster.Spec.ConfigBase) + if err != nil { + return "could not talk to vfs" + } + + kopsVersionUpdatedBytes, err := configBase.Join(registry.PathKopsVersionUpdated).ReadFile() + if err != nil { + return "could get cluster version" + } + return string(kopsVersionUpdatedBytes) +} diff --git a/docs/cli/kops_version.md b/docs/cli/kops_version.md index 1d829b7bc1..d1ff910b23 100644 --- a/docs/cli/kops_version.md +++ b/docs/cli/kops_version.md @@ -22,8 +22,9 @@ kops version [flags] ### Options ``` - -h, --help help for version - --short only print the main kOps version. Useful for scripting. + -h, --help help for version + --server show the kOps version that made the last change to the state store. + --short only print the main kOps version. Useful for scripting. ``` ### Options inherited from parent commands diff --git a/pkg/commands/version.go b/pkg/commands/version.go deleted file mode 100644 index 0174d10160..0000000000 --- a/pkg/commands/version.go +++ /dev/null @@ -1,45 +0,0 @@ -/* -Copyright 2019 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 -}