From dafd60fd52cd19aa626af7e7f5c7b82c7e6e89ef Mon Sep 17 00:00:00 2001 From: Brian Goff Date: Sat, 2 Mar 2019 11:25:47 -0800 Subject: [PATCH] Support building an allow-list of providers (#527) * Add providers subcommand to verify providers Allows users to check what providers are available * Fix version output to add new line This command was totally broken until we moved around the call to `initConfig()`, this just fixes the output now that it works. * Flip boolean of provider include tags All providers are still included by default and fix tags using the old format. --- providers.go | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++ root.go | 4 ++-- version.go | 2 +- 3 files changed, 58 insertions(+), 3 deletions(-) create mode 100644 providers.go diff --git a/providers.go b/providers.go new file mode 100644 index 0000000..29fed9a --- /dev/null +++ b/providers.go @@ -0,0 +1,55 @@ +// Copyright © 2017 NAME HERE +// +// 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 cmd + +import ( + "fmt" + "os" + + "github.com/spf13/cobra" + "github.com/virtual-kubelet/virtual-kubelet/providers/register" +) + +// versionCmd represents the version command +var providersCmd = &cobra.Command{ + Use: "providers", + Short: "Show the list of supported providers", + Long: "Show the list of supported providers", + Args: cobra.MaximumNArgs(2), + Run: func(cmd *cobra.Command, args []string) { + switch len(args) { + case 0: + ls := register.List() + for _, p := range ls { + fmt.Fprintln(cmd.OutOrStdout(), p) + } + case 1: + if !register.Exists(args[0]) { + fmt.Fprintln(cmd.OutOrStderr(), "no such provider", args[0]) + + // TODO(@cpuuy83): would be nice to not short-circuit the exit here + // But at the momemt this seems to be the only way to exit non-zero and + // handle our own error output + os.Exit(1) + } + fmt.Fprintln(cmd.OutOrStdout(), args[0]) + } + return + }, +} + +func init() { + RootCmd.AddCommand(providersCmd) +} diff --git a/root.go b/root.go index 826963c..32b8cf6 100644 --- a/root.go +++ b/root.go @@ -90,6 +90,8 @@ var RootCmd = &cobra.Command{ backend implementation allowing users to create kubernetes nodes without running the kubelet. This allows users to schedule kubernetes workloads on nodes that aren't running Kubernetes.`, Run: func(cmd *cobra.Command, args []string) { + initConfig() + defer rootContextCancel() vk := vkubelet.New(vkubelet.Config{ @@ -167,8 +169,6 @@ func init() { log.L = logruslogger.FromLogrus(logrus.NewEntry(logrus.StandardLogger())) trace.T = opencensus.Adapter{} - cobra.OnInitialize(initConfig) - // read default node name from environment variable. // it can be overwritten by cli flags if specified. defaultNodeName := os.Getenv("DEFAULT_NODE_NAME") diff --git a/version.go b/version.go index 2206716..20545e1 100644 --- a/version.go +++ b/version.go @@ -27,7 +27,7 @@ var versionCmd = &cobra.Command{ Short: "Show the version of the program", Long: `Show the version of the program`, Run: func(cmd *cobra.Command, args []string) { - fmt.Printf("Version: %s, Built: %s", version.Version, version.BuildTime) + fmt.Printf("Version: %s, Built: %s\n", version.Version, version.BuildTime) }, }