Add optional arguments to kubectl run ...

This commit is contained in:
Brendan Burns
2015-08-11 22:48:00 -07:00
parent a6148e79c3
commit 586931fe16
12 changed files with 305 additions and 57 deletions

View File

@@ -18,6 +18,7 @@ package kubectl
import (
"fmt"
"reflect"
"strconv"
"strings"
@@ -35,17 +36,24 @@ type GeneratorParam struct {
// Generator is an interface for things that can generate API objects from input parameters.
type Generator interface {
// Generate creates an API object given a set of parameters
Generate(params map[string]string) (runtime.Object, error)
Generate(params map[string]interface{}) (runtime.Object, error)
// ParamNames returns the list of parameters that this generator uses
ParamNames() []GeneratorParam
}
func IsZero(i interface{}) bool {
if i == nil {
return true
}
return reflect.DeepEqual(i, reflect.Zero(reflect.TypeOf(i)).Interface())
}
// ValidateParams ensures that all required params are present in the params map
func ValidateParams(paramSpec []GeneratorParam, params map[string]string) error {
func ValidateParams(paramSpec []GeneratorParam, params map[string]interface{}) error {
for ix := range paramSpec {
if paramSpec[ix].Required {
value, found := params[paramSpec[ix].Name]
if !found || len(value) == 0 {
if !found || IsZero(value) {
return fmt.Errorf("Parameter: %s is required", paramSpec[ix].Name)
}
}
@@ -54,8 +62,8 @@ func ValidateParams(paramSpec []GeneratorParam, params map[string]string) error
}
// MakeParams is a utility that creates generator parameters from a command line
func MakeParams(cmd *cobra.Command, params []GeneratorParam) map[string]string {
result := map[string]string{}
func MakeParams(cmd *cobra.Command, params []GeneratorParam) map[string]interface{} {
result := map[string]interface{}{}
for ix := range params {
f := cmd.Flags().Lookup(params[ix].Name)
if f != nil {
@@ -74,7 +82,11 @@ func MakeLabels(labels map[string]string) string {
}
// ParseLabels turns a string representation of a label set into a map[string]string
func ParseLabels(labelString string) (map[string]string, error) {
func ParseLabels(labelSpec interface{}) (map[string]string, error) {
labelString, isString := labelSpec.(string)
if !isString {
return nil, fmt.Errorf("expected string, found %v", labelSpec)
}
if len(labelString) == 0 {
return nil, fmt.Errorf("no label spec passed")
}