From 809288e7047f010cc46f1acb5f66a9a9296f5349 Mon Sep 17 00:00:00 2001 From: Jordan Liggitt Date: Sat, 26 Aug 2017 15:03:17 -0400 Subject: [PATCH 1/3] Allow -n namespaces/ Kubernetes-commit: 666e4be37bab925d0842935bccd3ef849d802608 --- tools/clientcmd/flag.go | 49 ++++++++++++++++++++++++++++++++++++ tools/clientcmd/overrides.go | 23 ++++++++++++++++- 2 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 tools/clientcmd/flag.go diff --git a/tools/clientcmd/flag.go b/tools/clientcmd/flag.go new file mode 100644 index 00000000..8d60d201 --- /dev/null +++ b/tools/clientcmd/flag.go @@ -0,0 +1,49 @@ +/* +Copyright 2017 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 clientcmd + +// transformingStringValue implements pflag.Value to store string values, +// allowing transforming them while being set +type transformingStringValue struct { + target *string + transformer func(string) (string, error) +} + +func newTransformingStringValue(val string, target *string, transformer func(string) (string, error)) *transformingStringValue { + *target = val + return &transformingStringValue{ + target: target, + transformer: transformer, + } +} + +func (t *transformingStringValue) Set(val string) error { + val, err := t.transformer(val) + if err != nil { + return err + } + *t.target = val + return nil +} + +func (t *transformingStringValue) Type() string { + return "string" +} + +func (t *transformingStringValue) String() string { + return string(*t.target) +} diff --git a/tools/clientcmd/overrides.go b/tools/clientcmd/overrides.go index db831440..bfca0328 100644 --- a/tools/clientcmd/overrides.go +++ b/tools/clientcmd/overrides.go @@ -18,6 +18,7 @@ package clientcmd import ( "strconv" + "strings" "github.com/spf13/pflag" @@ -101,6 +102,15 @@ func (f FlagInfo) BindStringFlag(flags *pflag.FlagSet, target *string) FlagInfo return f } +// BindTransformingStringFlag binds the flag based on the provided info. If LongName == "", nothing is registered +func (f FlagInfo) BindTransformingStringFlag(flags *pflag.FlagSet, target *string, transformer func(string) (string, error)) FlagInfo { + // you can't register a flag without a long name + if len(f.LongName) > 0 { + flags.VarP(newTransformingStringValue(f.Default, target, transformer), f.LongName, f.ShortName, f.Description) + } + return f +} + // BindStringSliceFlag binds the flag based on the provided info. If LongName == "", nothing is registered func (f FlagInfo) BindStringArrayFlag(flags *pflag.FlagSet, target *[]string) FlagInfo { // you can't register a flag without a long name @@ -222,5 +232,16 @@ func BindClusterFlags(clusterInfo *clientcmdapi.Cluster, flags *pflag.FlagSet, f func BindContextFlags(contextInfo *clientcmdapi.Context, flags *pflag.FlagSet, flagNames ContextOverrideFlags) { flagNames.ClusterName.BindStringFlag(flags, &contextInfo.Cluster) flagNames.AuthInfoName.BindStringFlag(flags, &contextInfo.AuthInfo) - flagNames.Namespace.BindStringFlag(flags, &contextInfo.Namespace) + flagNames.Namespace.BindTransformingStringFlag(flags, &contextInfo.Namespace, RemoveNamespacesPrefix) +} + +// RemoveNamespacesPrefix is a transformer that strips "ns/", "namespace/" and "namespaces/" prefixes case-insensitively +func RemoveNamespacesPrefix(value string) (string, error) { + for _, prefix := range []string{"namespaces/", "namespace/", "ns/"} { + if len(value) > len(prefix) && strings.EqualFold(value[0:len(prefix)], prefix) { + value = value[len(prefix):] + break + } + } + return value, nil } From 6316abbfad189c590ca697ee6cc5091d7655765d Mon Sep 17 00:00:00 2001 From: Edmund Rhudy Date: Wed, 30 Aug 2017 10:50:02 -0400 Subject: [PATCH 2/3] Add tests for stripping "namespaces/" from passed-in namespace Kubernetes-commit: 33dff7275da46788ab4126e269a497da9c1c8003 --- tools/clientcmd/overrides_test.go | 50 +++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 tools/clientcmd/overrides_test.go diff --git a/tools/clientcmd/overrides_test.go b/tools/clientcmd/overrides_test.go new file mode 100644 index 00000000..13a995cf --- /dev/null +++ b/tools/clientcmd/overrides_test.go @@ -0,0 +1,50 @@ +/* +Copyright 2017 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 clientcmd + +import ( + "testing" + + "github.com/spf13/pflag" +) + +func TestNamespacePrefixStrip(t *testing.T) { + testData := map[string]string{ + "namespaces/foo": "foo", + "NAMESPACES/foo": "foo", + "NameSpaces/foo": "foo", + "namespace/foo": "foo", + "NAMESPACE/foo": "foo", + "nameSpace/foo": "foo", + "ns/foo": "foo", + "NS/foo": "foo", + "namespaces/": "namespaces/", + "namespace/": "namespace/", + "ns/": "ns/", + } + + for before, after := range testData { + overrides := &ConfigOverrides{} + fs := &pflag.FlagSet{} + BindOverrideFlags(overrides, fs, RecommendedConfigOverrideFlags("")) + fs.Parse([]string{"--namespace", before}) + + if overrides.Context.Namespace != after { + t.Fatalf("Expected %s, got %s", after, overrides.Context.Namespace) + } + } +} From d779598b406767d372eaaaf5096ae5e94f558c7b Mon Sep 17 00:00:00 2001 From: Edmund Rhudy Date: Wed, 30 Aug 2017 17:22:59 -0400 Subject: [PATCH 3/3] Update Bazel configuration for flag.go and overrides_test.go Kubernetes-commit: 0d0591046c3ea7780600746846e64c2a7abbf1fe --- tools/clientcmd/BUILD | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tools/clientcmd/BUILD b/tools/clientcmd/BUILD index c91b688e..7767cd7d 100644 --- a/tools/clientcmd/BUILD +++ b/tools/clientcmd/BUILD @@ -12,12 +12,14 @@ go_test( "client_config_test.go", "loader_test.go", "merged_client_builder_test.go", + "overrides_test.go", "validation_test.go", ], library = ":go_default_library", deps = [ "//vendor/github.com/ghodss/yaml:go_default_library", "//vendor/github.com/imdario/mergo:go_default_library", + "//vendor/github.com/spf13/pflag:go_default_library", "//vendor/k8s.io/apimachinery/pkg/runtime:go_default_library", "//vendor/k8s.io/apimachinery/pkg/util/errors:go_default_library", "//vendor/k8s.io/client-go/rest:go_default_library", @@ -33,6 +35,7 @@ go_library( "client_config.go", "config.go", "doc.go", + "flag.go", "helpers.go", "loader.go", "merged_client_builder.go",