Skip to content

Commit

Permalink
Merge pull request kubernetes#3647 from deads2k/deads-make-factory-fl…
Browse files Browse the repository at this point in the history
…ag-binding-optional

make kubectl factory flag binding optional
  • Loading branch information
smarterclayton committed Jan 21, 2015
2 parents 903de0c + 500bb3a commit 3c15427
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 4 deletions.
2 changes: 1 addition & 1 deletion cmd/gendocs/gen_kubectl_docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func main() {
// Set environment variables used by kubectl so the output is consistent,
// regardless of where we run.
os.Setenv("HOME", "/home/username")
kubectl := cmd.NewFactory().NewKubectlCommand(out)
kubectl := cmd.NewFactory(nil).NewKubectlCommand(out)
fmt.Fprintf(out, "## %s\n\n", kubectl.Name())
fmt.Fprintf(out, "%s\n\n", kubectl.Short)
fmt.Fprintln(out, "### Commands\n")
Expand Down
2 changes: 1 addition & 1 deletion cmd/kubectl/kubectl.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
)

func main() {
cmd := cmd.NewFactory().NewKubectlCommand(os.Stdout)
cmd := cmd.NewFactory(nil).NewKubectlCommand(os.Stdout)
if err := cmd.Execute(); err != nil {
os.Exit(1)
}
Expand Down
11 changes: 9 additions & 2 deletions pkg/kubectl/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,18 @@ type Factory struct {
}

// NewFactory creates a factory with the default Kubernetes resources defined
func NewFactory() *Factory {
// if optionalClientConfig is nil, then flags will be bound to a new clientcmd.ClientConfig.
// if optionalClientConfig is not nil, then this factory will make use of it.
func NewFactory(optionalClientConfig clientcmd.ClientConfig) *Factory {
mapper := kubectl.ShortcutExpander{latest.RESTMapper}

flags := pflag.NewFlagSet("", pflag.ContinueOnError)
clientConfig := DefaultClientConfig(flags)

clientConfig := optionalClientConfig
if optionalClientConfig == nil {
clientConfig = DefaultClientConfig(flags)
}

clients := &clientCache{
clients: make(map[string]*client.Client),
loader: clientConfig,
Expand Down
41 changes: 41 additions & 0 deletions pkg/kubectl/cmd/factory_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
Copyright 2014 Google Inc. All rights reserved.
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 (
"testing"

"github.com/GoogleCloudPlatform/kubernetes/pkg/client/clientcmd"
clientcmdapi "github.com/GoogleCloudPlatform/kubernetes/pkg/client/clientcmd/api"
)

func TestNewFactoryDefaultFlagBindings(t *testing.T) {
factory := NewFactory(nil)

if !factory.flags.HasFlags() {
t.Errorf("Expected flags, but didn't get any")
}
}

func TestNewFactoryNoFlagBindings(t *testing.T) {
clientConfig := clientcmd.NewDefaultClientConfig(*clientcmdapi.NewConfig(), &clientcmd.ConfigOverrides{})
factory := NewFactory(clientConfig)

if factory.flags.HasFlags() {
t.Errorf("Expected zero flags, but got %v", factory.flags)
}
}

0 comments on commit 3c15427

Please sign in to comment.