Skip to content

Commit

Permalink
Limit the type for kubectl expose command
Browse files Browse the repository at this point in the history
  • Loading branch information
feihujiang committed Sep 15, 2015
1 parent 3d28164 commit 98689a9
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 15 deletions.
9 changes: 6 additions & 3 deletions docs/man/man1/kubectl-expose.1
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

.SH NAME
.PP
kubectl expose \- Take a replicated application and expose it as Kubernetes Service
kubectl expose \- Take a replication controller, service or pod and expose it as a new Kubernetes Service


.SH SYNOPSIS
Expand All @@ -13,10 +13,10 @@ kubectl expose \- Take a replicated application and expose it as Kubernetes Serv

.SH DESCRIPTION
.PP
Take a replicated application and expose it as Kubernetes Service.
Take a replication controller, service or pod and expose it as a new Kubernetes Service.

.PP
Looks up a replication controller or service by name and uses the selector for that resource as the
Looks up a replication controller, service or pod by name and uses the selector for that resource as the
selector for a new Service on the specified port. If no labels are specified, the new service will
re\-use the labels from the resource it exposes.

Expand Down Expand Up @@ -219,6 +219,9 @@ $ kubectl expose rc nginx \-\-port=80 \-\-target\-port=8000
# Create a service for a replication controller identified by type and name specified in "nginx\-controller.yaml", which serves on port 80 and connects to the containers on port 8000.
$ kubectl expose \-f nginx\-controller.yaml \-\-port=80 \-\-target\-port=8000

# Create a service for a pod valid\-pod, which serves on port 444 with the name "frontend"
$ kubectl expose pod valid\-pod \-\-port=444 \-\-name=frontend

# Create 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 \-\-name=nginx\-https

Expand Down
4 changes: 2 additions & 2 deletions docs/user-guide/kubectl/kubectl.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ kubectl
* [kubectl delete](kubectl_delete.md) - Delete resources by filenames, stdin, resources and names, or by resources and label selector.
* [kubectl describe](kubectl_describe.md) - Show details of a specific resource or group of resources
* [kubectl exec](kubectl_exec.md) - Execute a command in a container.
* [kubectl expose](kubectl_expose.md) - Take a replicated application and expose it as Kubernetes Service
* [kubectl expose](kubectl_expose.md) - Take a replication controller, service or pod and expose it as a new Kubernetes Service
* [kubectl get](kubectl_get.md) - Display one or many resources
* [kubectl label](kubectl_label.md) - Update the labels on a resource
* [kubectl logs](kubectl_logs.md) - Print the logs for a container in a pod.
Expand All @@ -100,7 +100,7 @@ kubectl
* [kubectl stop](kubectl_stop.md) - Deprecated: Gracefully shut down a resource by name or filename.
* [kubectl version](kubectl_version.md) - Print the client and server version information.

###### Auto generated by spf13/cobra at 2015-09-10 18:53:03.165115265 +0000 UTC
###### Auto generated by spf13/cobra at 2015-09-11 06:17:55.670147499 +0000 UTC

<!-- BEGIN MUNGE: GENERATED_ANALYTICS -->
[![Analytics](https://kubernetes-site.appspot.com/UA-36037335-10/GitHub/docs/user-guide/kubectl/kubectl.md?pixel)]()
Expand Down
11 changes: 7 additions & 4 deletions docs/user-guide/kubectl/kubectl_expose.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ Documentation for other releases can be found at

## kubectl expose

Take a replicated application and expose it as Kubernetes Service
Take a replication controller, service or pod and expose it as a new Kubernetes Service

### Synopsis


Take a replicated application and expose it as Kubernetes Service.
Take a replication controller, service or pod and expose it as a new Kubernetes Service.

Looks up a replication controller or service by name and uses the selector for that resource as the
Looks up a replication controller, service or pod by name and uses the selector for that resource as the
selector for a new Service on the specified port. If no labels are specified, the new service will
re-use the labels from the resource it exposes.

Expand All @@ -57,6 +57,9 @@ $ kubectl expose rc nginx --port=80 --target-port=8000
# Create a service for a replication controller identified by type and name specified in "nginx-controller.yaml", which serves on port 80 and connects to the containers on port 8000.
$ kubectl expose -f nginx-controller.yaml --port=80 --target-port=8000
# Create a service for a pod valid-pod, which serves on port 444 with the name "frontend"
$ kubectl expose pod valid-pod --port=444 --name=frontend
# Create 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 --name=nginx-https
Expand Down Expand Up @@ -122,7 +125,7 @@ $ kubectl expose rc streamer --port=4100 --protocol=udp --name=video-stream

* [kubectl](kubectl.md) - kubectl controls the Kubernetes cluster manager

###### Auto generated by spf13/cobra at 2015-09-11 03:36:48.458259032 +0000 UTC
###### Auto generated by spf13/cobra at 2015-09-14 01:48:52.452547937 +0000 UTC

<!-- BEGIN MUNGE: GENERATED_ANALYTICS -->
[![Analytics](https://kubernetes-site.appspot.com/UA-36037335-10/GitHub/docs/user-guide/kubectl/kubectl_expose.md?pixel)]()
Expand Down
18 changes: 18 additions & 0 deletions hack/lib/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,21 @@ kube::test::describe_resource_assert() {
echo -n ${reset}
return 0
}

kube::test::if_has_string() {
local message=$1
local match=$2

if [[ $(echo "$message" | grep "$match") ]]; then
echo "Successful"
echo "message:$message"
echo "has:$match"
return 0
else
echo "FAIL!"
echo "message:$message"
echo "has not:$match"
caller
return 1
fi
}
7 changes: 7 additions & 0 deletions hack/test-cmd.sh
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,13 @@ __EOF__
kubectl delete pod valid-pod "${kube_flags[@]}"
kubectl delete service frontend{,-2,-3,-4,-5} "${kube_flags[@]}"

### Expose negative invalid resource test
# Pre-condition: don't need
# Command
output_message=$(! kubectl expose nodes 127.0.0.1 2>&1 "${kube_flags[@]}")
# Post-condition: the error message has "invalid resource" string
kube::test::if_has_string "${output_message}" 'invalid resource'

### Delete replication controller with id
# Pre-condition: frontend replication controller is running
kube::test::get_object_assert rc "{{range.items}}{{$id_field}}:{{end}}" 'frontend:'
Expand Down
8 changes: 6 additions & 2 deletions pkg/kubectl/cmd/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,15 +227,19 @@ func NewAPIFactory() (*cmdutil.Factory, *testFactory, runtime.Codec) {
ClientConfig: func() (*client.Config, error) {
return t.ClientConfig, t.Err
},
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
},
Generator: func(name string) (kubectl.Generator, bool) {
generator, ok := generators[name]
return generator, ok
},
}

rf := cmdutil.NewFactory(nil)
f.PodSelectorForObject = rf.PodSelectorForObject

return f, t, testapi.Default.Codec()
}

Expand Down
13 changes: 9 additions & 4 deletions pkg/kubectl/cmd/expose.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ type ExposeOptions struct {
}

const (
expose_long = `Take a replicated application and expose it as Kubernetes Service.
expose_long = `Take a replication controller, service or pod and expose it as a new Kubernetes Service.
Looks up a replication controller or service by name and uses the selector for that resource as the
Looks up a replication controller, service or pod by name and uses the selector for that resource as the
selector for a new Service on the specified port. If no labels are specified, the new service will
re-use the labels from the resource it exposes.`

Expand All @@ -46,6 +46,9 @@ $ kubectl expose rc nginx --port=80 --target-port=8000
# Create a service for a replication controller identified by type and name specified in "nginx-controller.yaml", which serves on port 80 and connects to the containers on port 8000.
$ kubectl expose -f nginx-controller.yaml --port=80 --target-port=8000
# Create a service for a pod valid-pod, which serves on port 444 with the name "frontend"
$ kubectl expose pod valid-pod --port=444 --name=frontend
# Create 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 --name=nginx-https
Expand All @@ -58,7 +61,7 @@ func NewCmdExposeService(f *cmdutil.Factory, out io.Writer) *cobra.Command {

cmd := &cobra.Command{
Use: "expose (-f FILENAME | TYPE NAME) [--port=port] [--protocol=TCP|UDP] [--target-port=number-or-name] [--name=name] [----external-ip=external-ip-of-service] [--type=type]",
Short: "Take a replicated application and expose it as Kubernetes Service",
Short: "Take a replication controller, service or pod and expose it as a new Kubernetes Service",
Long: expose_long,
Example: expose_example,
Run: func(cmd *cobra.Command, args []string) {
Expand Down Expand Up @@ -113,7 +116,9 @@ func RunExpose(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []str
}
info := infos[0]
mapping := info.ResourceMapping()

if err := f.CanBeExposed(mapping.Kind); err != nil {
return err
}
// Get the input object
inputObject, err := r.Object()
if err != nil {
Expand Down
8 changes: 8 additions & 0 deletions pkg/kubectl/cmd/util/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
},
}
}

Expand Down
27 changes: 27 additions & 0 deletions pkg/kubectl/cmd/util/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 98689a9

Please sign in to comment.