mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-03 17:30:00 +00:00
expose: Use separate parameters for default and custom name
This commit is contained in:
parent
6fa2777e26
commit
6c85040cef
@ -98,6 +98,9 @@ func RunExpose(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []str
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
if len(infos) > 1 {
|
||||||
|
return fmt.Errorf("multiple resources provided: %v", args)
|
||||||
|
}
|
||||||
info := infos[0]
|
info := infos[0]
|
||||||
|
|
||||||
// Get the input object
|
// Get the input object
|
||||||
@ -118,10 +121,7 @@ func RunExpose(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []str
|
|||||||
}
|
}
|
||||||
names := generator.ParamNames()
|
names := generator.ParamNames()
|
||||||
params := kubectl.MakeParams(cmd, names)
|
params := kubectl.MakeParams(cmd, names)
|
||||||
params["name"] = cmdutil.GetFlagString(cmd, "name")
|
params["default-name"] = info.Name
|
||||||
if len(params["name"]) == 0 {
|
|
||||||
params["name"] = info.Name
|
|
||||||
}
|
|
||||||
if s, found := params["selector"]; !found || len(s) == 0 || cmdutil.GetFlagInt(cmd, "port") < 1 {
|
if s, found := params["selector"]; !found || len(s) == 0 || cmdutil.GetFlagInt(cmd, "port") < 1 {
|
||||||
if len(s) == 0 {
|
if len(s) == 0 {
|
||||||
s, err := f.PodSelectorForObject(inputObject)
|
s, err := f.PodSelectorForObject(inputObject)
|
||||||
|
@ -31,14 +31,22 @@ func TestRunExposeService(t *testing.T) {
|
|||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
args []string
|
args []string
|
||||||
|
ns string
|
||||||
|
calls map[string]string
|
||||||
input runtime.Object
|
input runtime.Object
|
||||||
flags map[string]string
|
flags map[string]string
|
||||||
output runtime.Object
|
output runtime.Object
|
||||||
|
expected string
|
||||||
status int
|
status int
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "expose-service-from-service",
|
name: "expose-service-from-service",
|
||||||
args: []string{"service", "baz"},
|
args: []string{"service", "baz"},
|
||||||
|
ns: "test",
|
||||||
|
calls: map[string]string{
|
||||||
|
"GET": "/namespaces/test/services/baz",
|
||||||
|
"POST": "/namespaces/test/services",
|
||||||
|
},
|
||||||
input: &api.Service{
|
input: &api.Service{
|
||||||
ObjectMeta: api.ObjectMeta{Name: "baz", Namespace: "test", ResourceVersion: "12"},
|
ObjectMeta: api.ObjectMeta{Name: "baz", Namespace: "test", ResourceVersion: "12"},
|
||||||
TypeMeta: api.TypeMeta{Kind: "Service", APIVersion: "v1beta3"},
|
TypeMeta: api.TypeMeta{Kind: "Service", APIVersion: "v1beta3"},
|
||||||
@ -61,6 +69,41 @@ func TestRunExposeService(t *testing.T) {
|
|||||||
Selector: map[string]string{"func": "stream"},
|
Selector: map[string]string{"func": "stream"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
expected: "services/foo",
|
||||||
|
status: 200,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "no-name-passed-from-the-cli",
|
||||||
|
args: []string{"service", "mayor"},
|
||||||
|
ns: "default",
|
||||||
|
calls: map[string]string{
|
||||||
|
"GET": "/namespaces/default/services/mayor",
|
||||||
|
"POST": "/namespaces/default/services",
|
||||||
|
},
|
||||||
|
input: &api.Service{
|
||||||
|
ObjectMeta: api.ObjectMeta{Name: "mayor", Namespace: "default", ResourceVersion: "12"},
|
||||||
|
TypeMeta: api.TypeMeta{Kind: "Service", APIVersion: "v1beta3"},
|
||||||
|
Spec: api.ServiceSpec{
|
||||||
|
Selector: map[string]string{"run": "this"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
// No --name flag specified below. Service will use the rc's name passed via the 'default-name' parameter
|
||||||
|
flags: map[string]string{"selector": "run=this", "port": "80", "labels": "runas=amayor"},
|
||||||
|
output: &api.Service{
|
||||||
|
ObjectMeta: api.ObjectMeta{Name: "mayor", Namespace: "default", ResourceVersion: "12", Labels: map[string]string{"runas": "amayor"}},
|
||||||
|
TypeMeta: api.TypeMeta{Kind: "Service", APIVersion: "v1beta3"},
|
||||||
|
Spec: api.ServiceSpec{
|
||||||
|
Ports: []api.ServicePort{
|
||||||
|
{
|
||||||
|
Name: "default",
|
||||||
|
Protocol: api.Protocol("TCP"),
|
||||||
|
Port: 80,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Selector: map[string]string{"run": "this"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
expected: "services/mayor",
|
||||||
status: 200,
|
status: 200,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@ -72,9 +115,9 @@ func TestRunExposeService(t *testing.T) {
|
|||||||
Codec: codec,
|
Codec: codec,
|
||||||
Client: client.HTTPClientFunc(func(req *http.Request) (*http.Response, error) {
|
Client: client.HTTPClientFunc(func(req *http.Request) (*http.Response, error) {
|
||||||
switch p, m := req.URL.Path, req.Method; {
|
switch p, m := req.URL.Path, req.Method; {
|
||||||
case p == "/namespaces/test/services/baz" && m == "GET":
|
case p == test.calls[m] && m == "GET":
|
||||||
return &http.Response{StatusCode: test.status, Body: objBody(codec, test.input)}, nil
|
return &http.Response{StatusCode: test.status, Body: objBody(codec, test.input)}, nil
|
||||||
case p == "/namespaces/test/services" && m == "POST":
|
case p == test.calls[m] && m == "POST":
|
||||||
return &http.Response{StatusCode: test.status, Body: objBody(codec, test.output)}, nil
|
return &http.Response{StatusCode: test.status, Body: objBody(codec, test.output)}, nil
|
||||||
default:
|
default:
|
||||||
t.Fatalf("unexpected request: %#v\n%#v", req.URL, req)
|
t.Fatalf("unexpected request: %#v\n%#v", req.URL, req)
|
||||||
@ -82,7 +125,7 @@ func TestRunExposeService(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
}
|
}
|
||||||
tf.Namespace = "test"
|
tf.Namespace = test.ns
|
||||||
buf := bytes.NewBuffer([]byte{})
|
buf := bytes.NewBuffer([]byte{})
|
||||||
|
|
||||||
cmd := NewCmdExposeService(f, buf)
|
cmd := NewCmdExposeService(f, buf)
|
||||||
@ -93,7 +136,7 @@ func TestRunExposeService(t *testing.T) {
|
|||||||
cmd.Run(cmd, test.args)
|
cmd.Run(cmd, test.args)
|
||||||
|
|
||||||
out := buf.String()
|
out := buf.String()
|
||||||
if strings.Contains(out, "services/foo") {
|
if strings.Contains(out, test.expected) {
|
||||||
t.Errorf("%s: unexpected output: %s", test.name, out)
|
t.Errorf("%s: unexpected output: %s", test.name, out)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -29,7 +29,8 @@ type BasicReplicationController struct{}
|
|||||||
func (BasicReplicationController) ParamNames() []GeneratorParam {
|
func (BasicReplicationController) ParamNames() []GeneratorParam {
|
||||||
return []GeneratorParam{
|
return []GeneratorParam{
|
||||||
{"labels", false},
|
{"labels", false},
|
||||||
{"name", true},
|
{"default-name", true},
|
||||||
|
{"name", false},
|
||||||
{"replicas", true},
|
{"replicas", true},
|
||||||
{"image", true},
|
{"image", true},
|
||||||
{"port", false},
|
{"port", false},
|
||||||
@ -38,6 +39,13 @@ func (BasicReplicationController) ParamNames() []GeneratorParam {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (BasicReplicationController) Generate(params map[string]string) (runtime.Object, error) {
|
func (BasicReplicationController) Generate(params map[string]string) (runtime.Object, error) {
|
||||||
|
name, found := params["name"]
|
||||||
|
if !found || len(name) == 0 {
|
||||||
|
name, found = params["default-name"]
|
||||||
|
if !found || len(name) == 0 {
|
||||||
|
return nil, fmt.Errorf("'name' is a required parameter.")
|
||||||
|
}
|
||||||
|
}
|
||||||
// TODO: extract this flag to a central location.
|
// TODO: extract this flag to a central location.
|
||||||
labelString, found := params["labels"]
|
labelString, found := params["labels"]
|
||||||
var labels map[string]string
|
var labels map[string]string
|
||||||
@ -49,7 +57,7 @@ func (BasicReplicationController) Generate(params map[string]string) (runtime.Ob
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
labels = map[string]string{
|
labels = map[string]string{
|
||||||
"run": params["name"],
|
"run": name,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
count, err := strconv.Atoi(params["replicas"])
|
count, err := strconv.Atoi(params["replicas"])
|
||||||
@ -58,7 +66,7 @@ func (BasicReplicationController) Generate(params map[string]string) (runtime.Ob
|
|||||||
}
|
}
|
||||||
controller := api.ReplicationController{
|
controller := api.ReplicationController{
|
||||||
ObjectMeta: api.ObjectMeta{
|
ObjectMeta: api.ObjectMeta{
|
||||||
Name: params["name"],
|
Name: name,
|
||||||
Labels: labels,
|
Labels: labels,
|
||||||
},
|
},
|
||||||
Spec: api.ReplicationControllerSpec{
|
Spec: api.ReplicationControllerSpec{
|
||||||
@ -71,7 +79,7 @@ func (BasicReplicationController) Generate(params map[string]string) (runtime.Ob
|
|||||||
Spec: api.PodSpec{
|
Spec: api.PodSpec{
|
||||||
Containers: []api.Container{
|
Containers: []api.Container{
|
||||||
{
|
{
|
||||||
Name: params["name"],
|
Name: name,
|
||||||
Image: params["image"],
|
Image: params["image"],
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -29,7 +29,8 @@ type ServiceGenerator struct{}
|
|||||||
|
|
||||||
func (ServiceGenerator) ParamNames() []GeneratorParam {
|
func (ServiceGenerator) ParamNames() []GeneratorParam {
|
||||||
return []GeneratorParam{
|
return []GeneratorParam{
|
||||||
{"name", true},
|
{"default-name", true},
|
||||||
|
{"name", false},
|
||||||
{"selector", true},
|
{"selector", true},
|
||||||
{"port", true},
|
{"port", true},
|
||||||
{"labels", false},
|
{"labels", false},
|
||||||
@ -62,9 +63,12 @@ func (ServiceGenerator) Generate(params map[string]string) (runtime.Object, erro
|
|||||||
}
|
}
|
||||||
|
|
||||||
name, found := params["name"]
|
name, found := params["name"]
|
||||||
if !found {
|
if !found || len(name) == 0 {
|
||||||
|
name, found = params["default-name"]
|
||||||
|
if !found || len(name) == 0 {
|
||||||
return nil, fmt.Errorf("'name' is a required parameter.")
|
return nil, fmt.Errorf("'name' is a required parameter.")
|
||||||
}
|
}
|
||||||
|
}
|
||||||
portString, found := params["port"]
|
portString, found := params["port"]
|
||||||
if !found {
|
if !found {
|
||||||
return nil, fmt.Errorf("'port' is a required parameter.")
|
return nil, fmt.Errorf("'port' is a required parameter.")
|
||||||
|
Loading…
Reference in New Issue
Block a user