mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-01 15:58:37 +00:00
expose: Use resource builder
Plus: * Rename flag --service-name to --name * Some refactoring
This commit is contained in:
parent
a76bdd9710
commit
30958f892d
@ -615,6 +615,7 @@ _kubectl_expose()
|
|||||||
flags+=("-h")
|
flags+=("-h")
|
||||||
flags+=("--labels=")
|
flags+=("--labels=")
|
||||||
two_word_flags+=("-l")
|
two_word_flags+=("-l")
|
||||||
|
flags+=("--name=")
|
||||||
flags+=("--no-headers")
|
flags+=("--no-headers")
|
||||||
flags+=("--output=")
|
flags+=("--output=")
|
||||||
two_word_flags+=("-o")
|
two_word_flags+=("-o")
|
||||||
@ -624,7 +625,6 @@ _kubectl_expose()
|
|||||||
flags+=("--protocol=")
|
flags+=("--protocol=")
|
||||||
flags+=("--public-ip=")
|
flags+=("--public-ip=")
|
||||||
flags+=("--selector=")
|
flags+=("--selector=")
|
||||||
flags+=("--service-name=")
|
|
||||||
flags+=("--target-port=")
|
flags+=("--target-port=")
|
||||||
flags+=("--template=")
|
flags+=("--template=")
|
||||||
two_word_flags+=("-t")
|
two_word_flags+=("-t")
|
||||||
|
@ -12,7 +12,7 @@ selector for a new Service on the specified port. If no labels are specified, th
|
|||||||
re-use the labels from the resource it exposes.
|
re-use the labels from the resource it exposes.
|
||||||
|
|
||||||
```
|
```
|
||||||
kubectl expose RESOURCE NAME --port=port [--protocol=TCP|UDP] [--target-port=number-or-name] [--service-name=name] [--public-ip=ip] [--create-external-load-balancer=bool]
|
kubectl expose RESOURCE NAME --port=port [--protocol=TCP|UDP] [--target-port=number-or-name] [--name=name] [--public-ip=ip] [--create-external-load-balancer=bool]
|
||||||
```
|
```
|
||||||
|
|
||||||
### Examples
|
### Examples
|
||||||
@ -22,10 +22,10 @@ kubectl expose RESOURCE NAME --port=port [--protocol=TCP|UDP] [--target-port=num
|
|||||||
$ kubectl expose rc nginx --port=80 --target-port=8000
|
$ kubectl expose rc nginx --port=80 --target-port=8000
|
||||||
|
|
||||||
// Creates a second service based on the above service, exposing the container port 8443 as port 443 with the name "nginx-https"
|
// Creates a second service based on the above service, exposing the container port 8443 as port 443 with the name "nginx-https"
|
||||||
$ kubectl expose service nginx --port=443 --target-port=8443 --service-name=nginx-https
|
$ kubectl expose service nginx --port=443 --target-port=8443 --name=nginx-https
|
||||||
|
|
||||||
// Create a service for a replicated streaming application on port 4100 balancing UDP traffic and named 'video-stream'.
|
// Create a service for a replicated streaming application on port 4100 balancing UDP traffic and named 'video-stream'.
|
||||||
$ kubectl expose rc streamer --port=4100 --protocol=udp --service-name=video-stream
|
$ kubectl expose rc streamer --port=4100 --protocol=udp --name=video-stream
|
||||||
```
|
```
|
||||||
|
|
||||||
### Options
|
### Options
|
||||||
@ -37,6 +37,7 @@ $ kubectl expose rc streamer --port=4100 --protocol=udp --service-name=video-str
|
|||||||
--generator="service/v1": The name of the API generator to use. Default is 'service/v1'.
|
--generator="service/v1": The name of the API generator to use. Default is 'service/v1'.
|
||||||
-h, --help=false: help for expose
|
-h, --help=false: help for expose
|
||||||
-l, --labels="": Labels to apply to the service created by this call.
|
-l, --labels="": Labels to apply to the service created by this call.
|
||||||
|
--name="": The name for the newly created service.
|
||||||
--no-headers=false: When using the default output, don't print headers.
|
--no-headers=false: When using the default output, don't print headers.
|
||||||
-o, --output="": Output format. One of: json|yaml|template|templatefile.
|
-o, --output="": Output format. One of: json|yaml|template|templatefile.
|
||||||
--output-version="": Output the formatted object with the given version (default api-version).
|
--output-version="": Output the formatted object with the given version (default api-version).
|
||||||
@ -45,7 +46,6 @@ $ kubectl expose rc streamer --port=4100 --protocol=udp --service-name=video-str
|
|||||||
--protocol="TCP": The network protocol for the service to be created. Default is 'tcp'.
|
--protocol="TCP": The network protocol for the service to be created. Default is 'tcp'.
|
||||||
--public-ip="": Name of a public IP address to set for the service. The service will be assigned this IP in addition to its generated service IP.
|
--public-ip="": Name of a public IP address to set for the service. The service will be assigned this IP in addition to its generated service IP.
|
||||||
--selector="": A label selector to use for this service. If empty (the default) infer the selector from the replication controller.
|
--selector="": A label selector to use for this service. If empty (the default) infer the selector from the replication controller.
|
||||||
--service-name="": The name for the newly created service.
|
|
||||||
--target-port="": Name or number for the port on the container that the service should direct traffic to. Optional.
|
--target-port="": Name or number for the port on the container that the service should direct traffic to. Optional.
|
||||||
-t, --template="": Template string or path to template file to use when -o=template or -o=templatefile. The template format is golang templates [http://golang.org/pkg/text/template/#pkg-overview]
|
-t, --template="": Template string or path to template file to use when -o=template or -o=templatefile. The template format is golang templates [http://golang.org/pkg/text/template/#pkg-overview]
|
||||||
```
|
```
|
||||||
@ -82,6 +82,6 @@ $ kubectl expose rc streamer --port=4100 --protocol=udp --service-name=video-str
|
|||||||
### SEE ALSO
|
### SEE ALSO
|
||||||
* [kubectl](kubectl.md) - kubectl controls the Kubernetes cluster manager
|
* [kubectl](kubectl.md) - kubectl controls the Kubernetes cluster manager
|
||||||
|
|
||||||
###### Auto generated by spf13/cobra at 2015-05-15 00:05:04.553214179 +0000 UTC
|
###### Auto generated by spf13/cobra at 2015-05-19 14:12:47.467953048 +0000 UTC
|
||||||
|
|
||||||
[]()
|
[]()
|
||||||
|
@ -46,6 +46,10 @@ re\-use the labels from the resource it exposes.
|
|||||||
\fB\-l\fP, \fB\-\-labels\fP=""
|
\fB\-l\fP, \fB\-\-labels\fP=""
|
||||||
Labels to apply to the service created by this call.
|
Labels to apply to the service created by this call.
|
||||||
|
|
||||||
|
.PP
|
||||||
|
\fB\-\-name\fP=""
|
||||||
|
The name for the newly created service.
|
||||||
|
|
||||||
.PP
|
.PP
|
||||||
\fB\-\-no\-headers\fP=false
|
\fB\-\-no\-headers\fP=false
|
||||||
When using the default output, don't print headers.
|
When using the default output, don't print headers.
|
||||||
@ -78,10 +82,6 @@ re\-use the labels from the resource it exposes.
|
|||||||
\fB\-\-selector\fP=""
|
\fB\-\-selector\fP=""
|
||||||
A label selector to use for this service. If empty (the default) infer the selector from the replication controller.
|
A label selector to use for this service. If empty (the default) infer the selector from the replication controller.
|
||||||
|
|
||||||
.PP
|
|
||||||
\fB\-\-service\-name\fP=""
|
|
||||||
The name for the newly created service.
|
|
||||||
|
|
||||||
.PP
|
.PP
|
||||||
\fB\-\-target\-port\fP=""
|
\fB\-\-target\-port\fP=""
|
||||||
Name or number for the port on the container that the service should direct traffic to. Optional.
|
Name or number for the port on the container that the service should direct traffic to. Optional.
|
||||||
@ -199,10 +199,10 @@ re\-use the labels from the resource it exposes.
|
|||||||
$ kubectl expose rc nginx \-\-port=80 \-\-target\-port=8000
|
$ kubectl expose rc nginx \-\-port=80 \-\-target\-port=8000
|
||||||
|
|
||||||
// Creates a second service based on the above service, exposing the container port 8443 as port 443 with the name "nginx\-https"
|
// Creates a second service based on the above service, exposing the container port 8443 as port 443 with the name "nginx\-https"
|
||||||
$ kubectl expose service nginx \-\-port=443 \-\-target\-port=8443 \-\-service\-name=nginx\-https
|
$ kubectl expose service nginx \-\-port=443 \-\-target\-port=8443 \-\-name=nginx\-https
|
||||||
|
|
||||||
// Create a service for a replicated streaming application on port 4100 balancing UDP traffic and named 'video\-stream'.
|
// Create a service for a replicated streaming application on port 4100 balancing UDP traffic and named 'video\-stream'.
|
||||||
$ kubectl expose rc streamer \-\-port=4100 \-\-protocol=udp \-\-service\-name=video\-stream
|
$ kubectl expose rc streamer \-\-port=4100 \-\-protocol=udp \-\-name=video\-stream
|
||||||
|
|
||||||
.fi
|
.fi
|
||||||
.RE
|
.RE
|
||||||
|
@ -531,12 +531,12 @@ __EOF__
|
|||||||
# Post-condition: service exists
|
# Post-condition: service exists
|
||||||
kube::test::get_object_assert 'service frontend' "{{$port_field}}" '80'
|
kube::test::get_object_assert 'service frontend' "{{$port_field}}" '80'
|
||||||
# Command
|
# Command
|
||||||
kubectl expose service frontend --port=443 --service-name=frontend-2 "${kube_flags[@]}"
|
kubectl expose service frontend --port=443 --name=frontend-2 "${kube_flags[@]}"
|
||||||
# Post-condition: service exists
|
# Post-condition: service exists
|
||||||
kube::test::get_object_assert 'service frontend-2' "{{$port_field}}" '443'
|
kube::test::get_object_assert 'service frontend-2' "{{$port_field}}" '443'
|
||||||
# Command
|
# Command
|
||||||
kubectl create -f examples/limitrange/valid-pod.json "${kube_flags[@]}"
|
kubectl create -f examples/limitrange/valid-pod.json "${kube_flags[@]}"
|
||||||
kubectl expose pod valid-pod --port=444 --service-name=frontend-3 "${kube_flags[@]}"
|
kubectl expose pod valid-pod --port=444 --name=frontend-3 "${kube_flags[@]}"
|
||||||
# Post-condition: service exists
|
# Post-condition: service exists
|
||||||
kube::test::get_object_assert 'service frontend-3' "{{$port_field}}" '444'
|
kube::test::get_object_assert 'service frontend-3' "{{$port_field}}" '444'
|
||||||
# Cleanup services
|
# Cleanup services
|
||||||
|
@ -20,7 +20,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/meta"
|
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl"
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl"
|
||||||
cmdutil "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd/util"
|
cmdutil "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd/util"
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/resource"
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/resource"
|
||||||
@ -39,15 +38,15 @@ re-use the labels from the resource it exposes.`
|
|||||||
$ kubectl expose rc nginx --port=80 --target-port=8000
|
$ kubectl expose rc nginx --port=80 --target-port=8000
|
||||||
|
|
||||||
// Creates a second service based on the above service, exposing the container port 8443 as port 443 with the name "nginx-https"
|
// Creates a second service based on the above service, exposing the container port 8443 as port 443 with the name "nginx-https"
|
||||||
$ kubectl expose service nginx --port=443 --target-port=8443 --service-name=nginx-https
|
$ kubectl expose service nginx --port=443 --target-port=8443 --name=nginx-https
|
||||||
|
|
||||||
// Create a service for a replicated streaming application on port 4100 balancing UDP traffic and named 'video-stream'.
|
// Create a service for a replicated streaming application on port 4100 balancing UDP traffic and named 'video-stream'.
|
||||||
$ kubectl expose rc streamer --port=4100 --protocol=udp --service-name=video-stream`
|
$ kubectl expose rc streamer --port=4100 --protocol=udp --name=video-stream`
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewCmdExposeService(f *cmdutil.Factory, out io.Writer) *cobra.Command {
|
func NewCmdExposeService(f *cmdutil.Factory, out io.Writer) *cobra.Command {
|
||||||
cmd := &cobra.Command{
|
cmd := &cobra.Command{
|
||||||
Use: "expose RESOURCE NAME --port=port [--protocol=TCP|UDP] [--target-port=number-or-name] [--service-name=name] [--public-ip=ip] [--create-external-load-balancer=bool]",
|
Use: "expose RESOURCE NAME --port=port [--protocol=TCP|UDP] [--target-port=number-or-name] [--name=name] [--public-ip=ip] [--create-external-load-balancer=bool]",
|
||||||
Short: "Take a replicated application and expose it as Kubernetes Service",
|
Short: "Take a replicated application and expose it as Kubernetes Service",
|
||||||
Long: expose_long,
|
Long: expose_long,
|
||||||
Example: expose_example,
|
Example: expose_example,
|
||||||
@ -69,40 +68,43 @@ func NewCmdExposeService(f *cmdutil.Factory, out io.Writer) *cobra.Command {
|
|||||||
cmd.Flags().String("target-port", "", "Name or number for the port on the container that the service should direct traffic to. Optional.")
|
cmd.Flags().String("target-port", "", "Name or number for the port on the container that the service should direct traffic to. Optional.")
|
||||||
cmd.Flags().String("public-ip", "", "Name of a public IP address to set for the service. The service will be assigned this IP in addition to its generated service IP.")
|
cmd.Flags().String("public-ip", "", "Name of a public IP address to set for the service. The service will be assigned this IP in addition to its generated service IP.")
|
||||||
cmd.Flags().String("overrides", "", "An inline JSON override for the generated object. If this is non-empty, it is used to override the generated object. Requires that the object supply a valid apiVersion field.")
|
cmd.Flags().String("overrides", "", "An inline JSON override for the generated object. If this is non-empty, it is used to override the generated object. Requires that the object supply a valid apiVersion field.")
|
||||||
cmd.Flags().String("service-name", "", "The name for the newly created service.")
|
cmd.Flags().String("name", "", "The name for the newly created service.")
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
func RunExpose(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string) error {
|
func RunExpose(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string) error {
|
||||||
var name, res string
|
|
||||||
switch l := len(args); {
|
|
||||||
case l == 2:
|
|
||||||
res, name = args[0], args[1]
|
|
||||||
default:
|
|
||||||
return cmdutil.UsageError(cmd, "the type and name of a resource to expose are required arguments")
|
|
||||||
}
|
|
||||||
|
|
||||||
namespace, err := f.DefaultNamespace()
|
namespace, err := f.DefaultNamespace()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the input object
|
|
||||||
var mapping *meta.RESTMapping
|
|
||||||
mapper, typer := f.Object()
|
mapper, typer := f.Object()
|
||||||
v, k, err := mapper.VersionAndKindForResource(res)
|
r := resource.NewBuilder(mapper, typer, f.ClientMapperForCommand()).
|
||||||
|
ContinueOnError().
|
||||||
|
NamespaceParam(namespace).DefaultNamespace().
|
||||||
|
ResourceTypeOrNameArgs(false, args...).
|
||||||
|
Flatten().
|
||||||
|
Do()
|
||||||
|
err = r.Err()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
mapping, err = mapper.RESTMapping(k, v)
|
mapping, err := r.ResourceMapping()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
infos, err := r.Infos()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
info := infos[0]
|
||||||
|
|
||||||
|
// Get the input object
|
||||||
client, err := f.RESTClient(mapping)
|
client, err := f.RESTClient(mapping)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
inputObject, err := resource.NewHelper(client, mapping).Get(namespace, name)
|
inputObject, err := resource.NewHelper(client, mapping).Get(info.Namespace, info.Name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -115,10 +117,9 @@ 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)
|
||||||
if len(cmdutil.GetFlagString(cmd, "service-name")) == 0 {
|
params["name"] = cmdutil.GetFlagString(cmd, "name")
|
||||||
params["name"] = name
|
if len(params["name"]) == 0 {
|
||||||
} else {
|
params["name"] = info.Name
|
||||||
params["name"] = cmdutil.GetFlagString(cmd, "service-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 {
|
||||||
@ -140,13 +141,14 @@ 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(ports) == 0 {
|
switch len(ports) {
|
||||||
|
case 0:
|
||||||
return cmdutil.UsageError(cmd, "couldn't find a suitable port via --port flag or introspection")
|
return cmdutil.UsageError(cmd, "couldn't find a suitable port via --port flag or introspection")
|
||||||
}
|
case 1:
|
||||||
if len(ports) > 1 {
|
params["port"] = ports[0]
|
||||||
|
default:
|
||||||
return cmdutil.UsageError(cmd, "more than one port to choose from, please explicitly specify a port using the --port flag.")
|
return cmdutil.UsageError(cmd, "more than one port to choose from, please explicitly specify a port using the --port flag.")
|
||||||
}
|
}
|
||||||
params["port"] = ports[0]
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if cmdutil.GetFlagBool(cmd, "create-external-load-balancer") {
|
if cmdutil.GetFlagBool(cmd, "create-external-load-balancer") {
|
||||||
|
@ -46,7 +46,7 @@ func TestRunExposeService(t *testing.T) {
|
|||||||
Selector: map[string]string{"app": "go"},
|
Selector: map[string]string{"app": "go"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
flags: map[string]string{"selector": "func=stream", "protocol": "UDP", "port": "14", "service-name": "foo", "labels": "svc=test"},
|
flags: map[string]string{"selector": "func=stream", "protocol": "UDP", "port": "14", "name": "foo", "labels": "svc=test"},
|
||||||
output: &api.Service{
|
output: &api.Service{
|
||||||
ObjectMeta: api.ObjectMeta{Name: "foo", Namespace: "test", ResourceVersion: "12", Labels: map[string]string{"svc": "test"}},
|
ObjectMeta: api.ObjectMeta{Name: "foo", Namespace: "test", ResourceVersion: "12", Labels: map[string]string{"svc": "test"}},
|
||||||
TypeMeta: api.TypeMeta{Kind: "Service", APIVersion: "v1beta3"},
|
TypeMeta: api.TypeMeta{Kind: "Service", APIVersion: "v1beta3"},
|
||||||
|
Loading…
Reference in New Issue
Block a user