mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-10-08 14:29:45 +00:00
Limit the type for kubectl expose command
This commit is contained in:
@@ -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
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -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)
|
||||
|
||||
|
Reference in New Issue
Block a user