Introduce a new service generator that leaves service port unnamed
This commit is contained in:
@@ -177,7 +177,8 @@ func NewAPIFactory() (*cmdutil.Factory, *testFactory, runtime.Codec) {
|
||||
}
|
||||
generators := map[string]kubectl.Generator{
|
||||
"run/v1": kubectl.BasicReplicationController{},
|
||||
"service/v1": kubectl.ServiceGenerator{},
|
||||
"service/v1": kubectl.ServiceGeneratorV1{},
|
||||
"service/v2": kubectl.ServiceGeneratorV2{},
|
||||
}
|
||||
return &cmdutil.Factory{
|
||||
Object: func() (meta.RESTMapper, runtime.ObjectTyper) {
|
||||
|
@@ -56,7 +56,7 @@ func NewCmdExposeService(f *cmdutil.Factory, out io.Writer) *cobra.Command {
|
||||
},
|
||||
}
|
||||
cmdutil.AddPrinterFlags(cmd)
|
||||
cmd.Flags().String("generator", "service/v1", "The name of the API generator to use. Default is 'service/v1'.")
|
||||
cmd.Flags().String("generator", "service/v2", "The name of the API generator to use. There are 2 generators: 'service/v1' and 'service/v2'. The only difference between them is that service port in v1 is named 'default', while it is left unnamed in v2. Default is 'service/v2'.")
|
||||
cmd.Flags().String("protocol", "TCP", "The network protocol for the service to be created. Default is 'tcp'.")
|
||||
cmd.Flags().Int("port", -1, "The port that the service should serve on. Required.")
|
||||
cmd.MarkFlagRequired("port")
|
||||
|
@@ -97,7 +97,8 @@ func NewFactory(optionalClientConfig clientcmd.ClientConfig) *Factory {
|
||||
|
||||
generators := map[string]kubectl.Generator{
|
||||
"run/v1": kubectl.BasicReplicationController{},
|
||||
"service/v1": kubectl.ServiceGenerator{},
|
||||
"service/v1": kubectl.ServiceGeneratorV1{},
|
||||
"service/v2": kubectl.ServiceGeneratorV2{},
|
||||
}
|
||||
|
||||
clientConfig := optionalClientConfig
|
||||
|
@@ -25,9 +25,29 @@ import (
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
|
||||
)
|
||||
|
||||
type ServiceGenerator struct{}
|
||||
// The only difference between ServiceGeneratorV1 and V2 is that the service port is named "default" in V1, while it is left unnamed in V2.
|
||||
type ServiceGeneratorV1 struct{}
|
||||
|
||||
func (ServiceGenerator) ParamNames() []GeneratorParam {
|
||||
func (ServiceGeneratorV1) ParamNames() []GeneratorParam {
|
||||
return paramNames()
|
||||
}
|
||||
|
||||
func (ServiceGeneratorV1) Generate(params map[string]string) (runtime.Object, error) {
|
||||
params["port-name"] = "default"
|
||||
return generate(params)
|
||||
}
|
||||
|
||||
type ServiceGeneratorV2 struct{}
|
||||
|
||||
func (ServiceGeneratorV2) ParamNames() []GeneratorParam {
|
||||
return paramNames()
|
||||
}
|
||||
|
||||
func (ServiceGeneratorV2) Generate(params map[string]string) (runtime.Object, error) {
|
||||
return generate(params)
|
||||
}
|
||||
|
||||
func paramNames() []GeneratorParam {
|
||||
return []GeneratorParam{
|
||||
{"default-name", true},
|
||||
{"name", false},
|
||||
@@ -40,10 +60,11 @@ func (ServiceGenerator) ParamNames() []GeneratorParam {
|
||||
{"protocol", false},
|
||||
{"container-port", false}, // alias of target-port
|
||||
{"target-port", false},
|
||||
{"port-name", false},
|
||||
}
|
||||
}
|
||||
|
||||
func (ServiceGenerator) Generate(params map[string]string) (runtime.Object, error) {
|
||||
func generate(params map[string]string) (runtime.Object, error) {
|
||||
selectorString, found := params["selector"]
|
||||
if !found || len(selectorString) == 0 {
|
||||
return nil, fmt.Errorf("'selector' is a required parameter.")
|
||||
@@ -77,6 +98,11 @@ func (ServiceGenerator) Generate(params map[string]string) (runtime.Object, erro
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
servicePortName, found := params["port-name"]
|
||||
if !found {
|
||||
// Leave the port unnamed.
|
||||
servicePortName = ""
|
||||
}
|
||||
service := api.Service{
|
||||
ObjectMeta: api.ObjectMeta{
|
||||
Name: name,
|
||||
@@ -86,7 +112,7 @@ func (ServiceGenerator) Generate(params map[string]string) (runtime.Object, erro
|
||||
Selector: selector,
|
||||
Ports: []api.ServicePort{
|
||||
{
|
||||
Name: "default",
|
||||
Name: servicePortName,
|
||||
Port: port,
|
||||
Protocol: api.Protocol(params["protocol"]),
|
||||
},
|
||||
|
@@ -26,10 +26,12 @@ import (
|
||||
|
||||
func TestGenerateService(t *testing.T) {
|
||||
tests := []struct {
|
||||
params map[string]string
|
||||
expected api.Service
|
||||
generator Generator
|
||||
params map[string]string
|
||||
expected api.Service
|
||||
}{
|
||||
{
|
||||
generator: ServiceGeneratorV2{},
|
||||
params: map[string]string{
|
||||
"selector": "foo=bar,baz=blah",
|
||||
"name": "test",
|
||||
@@ -48,7 +50,6 @@ func TestGenerateService(t *testing.T) {
|
||||
},
|
||||
Ports: []api.ServicePort{
|
||||
{
|
||||
Name: "default",
|
||||
Port: 80,
|
||||
Protocol: "TCP",
|
||||
TargetPort: util.NewIntOrStringFromInt(1234),
|
||||
@@ -58,6 +59,8 @@ func TestGenerateService(t *testing.T) {
|
||||
},
|
||||
},
|
||||
{
|
||||
|
||||
generator: ServiceGeneratorV2{},
|
||||
params: map[string]string{
|
||||
"selector": "foo=bar,baz=blah",
|
||||
"name": "test",
|
||||
@@ -76,7 +79,6 @@ func TestGenerateService(t *testing.T) {
|
||||
},
|
||||
Ports: []api.ServicePort{
|
||||
{
|
||||
Name: "default",
|
||||
Port: 80,
|
||||
Protocol: "UDP",
|
||||
TargetPort: util.NewIntOrStringFromString("foobar"),
|
||||
@@ -86,6 +88,7 @@ func TestGenerateService(t *testing.T) {
|
||||
},
|
||||
},
|
||||
{
|
||||
generator: ServiceGeneratorV2{},
|
||||
params: map[string]string{
|
||||
"selector": "foo=bar,baz=blah",
|
||||
"labels": "key1=value1,key2=value2",
|
||||
@@ -109,7 +112,6 @@ func TestGenerateService(t *testing.T) {
|
||||
},
|
||||
Ports: []api.ServicePort{
|
||||
{
|
||||
Name: "default",
|
||||
Port: 80,
|
||||
Protocol: "TCP",
|
||||
TargetPort: util.NewIntOrStringFromInt(1234),
|
||||
@@ -119,6 +121,7 @@ func TestGenerateService(t *testing.T) {
|
||||
},
|
||||
},
|
||||
{
|
||||
generator: ServiceGeneratorV2{},
|
||||
params: map[string]string{
|
||||
"selector": "foo=bar,baz=blah",
|
||||
"name": "test",
|
||||
@@ -138,7 +141,6 @@ func TestGenerateService(t *testing.T) {
|
||||
},
|
||||
Ports: []api.ServicePort{
|
||||
{
|
||||
Name: "default",
|
||||
Port: 80,
|
||||
Protocol: "UDP",
|
||||
TargetPort: util.NewIntOrStringFromString("foobar"),
|
||||
@@ -149,6 +151,7 @@ func TestGenerateService(t *testing.T) {
|
||||
},
|
||||
},
|
||||
{
|
||||
generator: ServiceGeneratorV2{},
|
||||
params: map[string]string{
|
||||
"selector": "foo=bar,baz=blah",
|
||||
"name": "test",
|
||||
@@ -169,7 +172,6 @@ func TestGenerateService(t *testing.T) {
|
||||
},
|
||||
Ports: []api.ServicePort{
|
||||
{
|
||||
Name: "default",
|
||||
Port: 80,
|
||||
Protocol: "UDP",
|
||||
TargetPort: util.NewIntOrStringFromString("foobar"),
|
||||
@@ -181,6 +183,7 @@ func TestGenerateService(t *testing.T) {
|
||||
},
|
||||
},
|
||||
{
|
||||
generator: ServiceGeneratorV2{},
|
||||
params: map[string]string{
|
||||
"selector": "foo=bar,baz=blah",
|
||||
"name": "test",
|
||||
@@ -200,7 +203,6 @@ func TestGenerateService(t *testing.T) {
|
||||
},
|
||||
Ports: []api.ServicePort{
|
||||
{
|
||||
Name: "default",
|
||||
Port: 80,
|
||||
Protocol: "UDP",
|
||||
TargetPort: util.NewIntOrStringFromString("foobar"),
|
||||
@@ -211,6 +213,7 @@ func TestGenerateService(t *testing.T) {
|
||||
},
|
||||
},
|
||||
{
|
||||
generator: ServiceGeneratorV2{},
|
||||
params: map[string]string{
|
||||
"selector": "foo=bar,baz=blah",
|
||||
"name": "test",
|
||||
@@ -231,7 +234,6 @@ func TestGenerateService(t *testing.T) {
|
||||
},
|
||||
Ports: []api.ServicePort{
|
||||
{
|
||||
Name: "default",
|
||||
Port: 80,
|
||||
Protocol: "UDP",
|
||||
TargetPort: util.NewIntOrStringFromString("foobar"),
|
||||
@@ -241,10 +243,38 @@ func TestGenerateService(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
generator: ServiceGeneratorV1{},
|
||||
params: map[string]string{
|
||||
"selector": "foo=bar,baz=blah",
|
||||
"name": "test",
|
||||
"port": "80",
|
||||
"protocol": "TCP",
|
||||
"container-port": "1234",
|
||||
},
|
||||
expected: api.Service{
|
||||
ObjectMeta: api.ObjectMeta{
|
||||
Name: "test",
|
||||
},
|
||||
Spec: api.ServiceSpec{
|
||||
Selector: map[string]string{
|
||||
"foo": "bar",
|
||||
"baz": "blah",
|
||||
},
|
||||
Ports: []api.ServicePort{
|
||||
{
|
||||
Name: "default",
|
||||
Port: 80,
|
||||
Protocol: "TCP",
|
||||
TargetPort: util.NewIntOrStringFromInt(1234),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
generator := ServiceGenerator{}
|
||||
for _, test := range tests {
|
||||
obj, err := generator.Generate(test.params)
|
||||
obj, err := test.generator.Generate(test.params)
|
||||
if !reflect.DeepEqual(obj, &test.expected) {
|
||||
t.Errorf("expected:\n%#v\ngot\n%#v\n", &test.expected, obj)
|
||||
}
|
||||
|
Reference in New Issue
Block a user