Limit the type for kubectl expose command

This commit is contained in:
feihujiang
2015-08-26 16:06:40 +08:00
parent 3d2816435b
commit 98689a99ec
9 changed files with 90 additions and 15 deletions

View File

@@ -86,6 +86,8 @@ type Factory struct {
DefaultNamespace func() (string, bool, error)
// Returns the generator for the provided generator name
Generator func(name string) (kubectl.Generator, bool)
// Check whether the kind of resources could be exposed
CanBeExposed func(kind string) error
}
// NewFactory creates a factory with the default Kubernetes resources defined
@@ -246,6 +248,12 @@ func NewFactory(optionalClientConfig clientcmd.ClientConfig) *Factory {
generator, ok := generators[name]
return generator, ok
},
CanBeExposed: func(kind string) error {
if kind != "ReplicationController" && kind != "Service" && kind != "Pod" {
return fmt.Errorf("invalid resource provided: %v, only a replication controller, service or pod is accepted", kind)
}
return nil
},
}
}

View File

@@ -165,6 +165,33 @@ func TestLabelsForObject(t *testing.T) {
}
}
func TestCanBeExposed(t *testing.T) {
factory := NewFactory(nil)
tests := []struct {
kind string
expectErr bool
}{
{
kind: "ReplicationController",
expectErr: false,
},
{
kind: "Node",
expectErr: true,
},
}
for _, test := range tests {
err := factory.CanBeExposed(test.kind)
if test.expectErr && err == nil {
t.Error("unexpected non-error")
}
if !test.expectErr && err != nil {
t.Errorf("unexpected error: %v", err)
}
}
}
func TestFlagUnderscoreRenaming(t *testing.T) {
factory := NewFactory(nil)