-
Notifications
You must be signed in to change notification settings - Fork 38.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a service generator and a command to easily expose services. #3592
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -395,6 +395,7 @@ Additional help topics: | |
kubectl resize Set a new size for a resizable resource (currently only Replication Controllers) | ||
kubectl run-container Run a particular image on the cluster. | ||
kubectl stop Gracefully shutdown a resource | ||
kubectl expose Take a replicated application and expose it as Kubernetes Service | ||
|
||
Use "kubectl help [command]" for more information about that command. | ||
``` | ||
|
@@ -899,7 +900,7 @@ Examples: | |
|
||
Usage: | ||
``` | ||
kubectl run-container <name> --image=<image> [--replicas=replicas] [--dry-run=<bool>] [--overrides=<inline-json>] [flags] | ||
kubectl run-container <name> --image=<image> [--port=<port>] [--replicas=replicas] [--dry-run=<bool>] [--overrides=<inline-json>] [flags] | ||
|
||
Available Flags: | ||
--alsologtostderr=false: log to standard error as well as files | ||
|
@@ -911,12 +912,12 @@ Usage: | |
--cluster="": The name of the kubeconfig cluster to use | ||
--context="": The name of the kubeconfig context to use | ||
--dry-run=false: If true, only print the object that would be sent, don't actually do anything | ||
--generator="run-container/v1": The name of the api generator that you want to use. Default 'run-container-controller-v1' | ||
--generator="run-container/v1": The name of the api generator that you want to use. Default 'run-container-controller/v1' | ||
-h, --help=false: help for run-container | ||
--image="": The image for the container you wish to run. | ||
--insecure-skip-tls-verify=false: If true, the server's certificate will not be checked for validity. This will make your HTTPS connections insecure. | ||
--kubeconfig="": Path to the kubeconfig file to use for CLI requests. | ||
-l, --labels="": Labels to apply to the pod(s) created by this call to run. | ||
-l, --labels="": Labels to apply to the pod(s) created by this call to run-container. | ||
--log_backtrace_at=:0: when logging hits line file:N, emit a stack trace | ||
--log_dir=: If non-empty, write log files in this directory | ||
--log_flush_frequency=5s: Maximum number of seconds between log flushes | ||
|
@@ -928,6 +929,7 @@ Usage: | |
-o, --output="": Output format: json|yaml|template|templatefile | ||
--output-version="": Output the formatted object with the given version (default api-version) | ||
--overrides="": An inline JSON override for the generated object. If this is non-empty, it is parsed used to override the generated object. Requires that the object supply a valid apiVersion field. | ||
--port=-1: The port that this container exposes. | ||
-r, --replicas=1: Number of replicas to create for this container. Default 1 | ||
-s, --server="": The address of the Kubernetes API server | ||
--stderrthreshold=2: logs at or above this threshold go to stderr | ||
|
@@ -983,3 +985,64 @@ Usage: | |
|
||
``` | ||
|
||
#### expose | ||
Take a replicated application and expose it as Kubernetes Service. | ||
|
||
Looks up a ReplicationController named <name>, and uses the selector for that replication controller | ||
as the selector for a new Service which services on <port> | ||
|
||
Examples: | ||
$ kubectl expose nginx --port=80 --container-port=8000 | ||
<creates a service for a replicated nginx, which serves on port 80 and connects to the containers on port 8000> | ||
|
||
$ kubectl expose streamer --port=4100 --protocol=udp --service-name=video-stream | ||
<create a service for a replicated streaming application on port 4100 balancing UDP traffic and is named 'video-stream'> | ||
|
||
|
||
Usage: | ||
``` | ||
kubectl expose <name> --port=<port> [--protocol=TCP|UDP] [--container-port=<number-or-name>] [--service-name=<name>] [--public-ip=<ip>] [--create-external-load-balancer] [flags] | ||
|
||
Available Flags: | ||
--alsologtostderr=false: log to standard error as well as files | ||
--api-version="": The API version to use when talking to the server | ||
-a, --auth-path="": Path to the auth info file. If missing, prompt the user. Only used if using https. | ||
--certificate-authority="": Path to a cert. file for the certificate authority. | ||
--client-certificate="": Path to a client key file for TLS. | ||
--client-key="": Path to a client key file for TLS. | ||
--cluster="": The name of the kubeconfig cluster to use | ||
--container-port="": Name or number for the port on the container that the service should direct traffic to. Optional. | ||
--context="": The name of the kubeconfig context to use | ||
--create-external-load-balancer=false: If true, create an external load balancer for this service. Implementation is cloud provider dependent. Default false | ||
--dry-run=false: If true, only print the object that would be sent, don't actually do anything | ||
--generator="service/v1": The name of the api generator that you want to use. Default 'service/v1' | ||
-h, --help=false: help for expose | ||
--insecure-skip-tls-verify=false: If true, the server's certificate will not be checked for validity. This will make your HTTPS connections insecure. | ||
--kubeconfig="": Path to the kubeconfig file to use for CLI requests. | ||
--log_backtrace_at=:0: when logging hits line file:N, emit a stack trace | ||
--log_dir=: If non-empty, write log files in this directory | ||
--log_flush_frequency=5s: Maximum number of seconds between log flushes | ||
--logtostderr=true: log to standard error instead of files | ||
--match-server-version=false: Require server version to match client version | ||
--namespace="": If present, the namespace scope for this CLI request. | ||
--no-headers=false: When using the default output, don't print headers | ||
--ns-path="": Path to the namespace info file that holds the namespace context to use for CLI requests. | ||
-o, --output="": Output format: json|yaml|template|templatefile | ||
--output-version="": Output the formatted object with the given version (default api-version) | ||
--overrides="": An inline JSON override for the generated object. If this is non-empty, it is parsed used to override the generated object. Requires that the object supply a valid apiVersion field. | ||
--port=-1: The port that the service should serve on. Required. | ||
--protocol="TCP": The network protocol for the service you want to be created. Default 'tcp' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. --public-ip isn't in the list of flags There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done. |
||
--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 | ||
-s, --server="": The address of the Kubernetes API server | ||
--service-name="": The name for the newly created service. | ||
--stderrthreshold=2: logs at or above this threshold go to stderr | ||
-t, --template="": Template string or path to template file to use when -o=template or -o=templatefile. | ||
--token="": Bearer token for authentication to the API server. | ||
--user="": The name of the kubeconfig user to use | ||
--v=0: log level for V logs | ||
--validate=false: If true, use a schema to validate the input before sending it | ||
--vmodule=: comma-separated list of pattern=N settings for file-filtered logging | ||
|
||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/* | ||
Copyright 2014 Google Inc. All rights reserved. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
|
||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api" | ||
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func (f *Factory) NewCmdExposeService(out io.Writer) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "expose <name> --port=<port> [--protocol=TCP|UDP] [--container-port=<number-or-name>] [--service-name=<name>] [--public-ip=<ip>] [--create-external-load-balancer]", | ||
Short: "Take a replicated application and expose it as Kubernetes Service", | ||
Long: `Take a replicated application and expose it as Kubernetes Service. | ||
|
||
Looks up a ReplicationController named <name>, and uses the selector for that replication controller | ||
as the selector for a new Service which services on <port> | ||
|
||
Examples: | ||
$ kubectl expose nginx --port=80 --container-port=8000 | ||
<creates a service for a replicated nginx, which serves on port 80 and connects to the containers on port 8000> | ||
|
||
$ kubectl expose streamer --port=4100 --protocol=udp --service-name=video-stream | ||
<create a service for a replicated streaming application on port 4100 balancing UDP traffic and is named 'video-stream'> | ||
`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
if len(args) != 1 { | ||
usageError(cmd, "<name> is required for expose") | ||
} | ||
|
||
namespace, err := f.DefaultNamespace(cmd) | ||
checkErr(err) | ||
client, err := f.Client(cmd) | ||
checkErr(err) | ||
|
||
rc, err := client.ReplicationControllers(namespace).Get(args[0]) | ||
checkErr(err) | ||
|
||
generatorName := GetFlagString(cmd, "generator") | ||
generator, found := kubectl.Generators[generatorName] | ||
if !found { | ||
usageError(cmd, fmt.Sprintf("Generator: %s not found.", generator)) | ||
} | ||
if GetFlagInt(cmd, "port") < 1 { | ||
usageError(cmd, "--port is required and must be a positive integer.") | ||
} | ||
names := generator.ParamNames() | ||
params := kubectl.MakeParams(cmd, names) | ||
if len(GetFlagString(cmd, "service-name")) == 0 { | ||
params["name"] = args[0] | ||
} else { | ||
params["name"] = GetFlagString(cmd, "service-name") | ||
} | ||
if _, found := params["selector"]; !found { | ||
params["selector"] = kubectl.MakeLabels(rc.Spec.Selector) | ||
} | ||
if GetFlagBool(cmd, "create-external-load-balancer") { | ||
params["create-external-load-balancer"] = "true" | ||
} | ||
|
||
err = kubectl.ValidateParams(names, params) | ||
checkErr(err) | ||
|
||
service, err := generator.Generate(params) | ||
checkErr(err) | ||
|
||
inline := GetFlagString(cmd, "overrides") | ||
if len(inline) > 0 { | ||
Merge(service, inline, "Service") | ||
} | ||
|
||
// TODO: extract this flag to a central location, when such a location exists. | ||
if !GetFlagBool(cmd, "dry-run") { | ||
service, err = client.Services(namespace).Create(service.(*api.Service)) | ||
checkErr(err) | ||
} | ||
|
||
err = PrintObject(cmd, service, f, out) | ||
checkErr(err) | ||
}, | ||
} | ||
AddPrinterFlags(cmd) | ||
cmd.Flags().String("generator", "service/v1", "The name of the api generator that you want to use. Default 'service/v1'") | ||
cmd.Flags().String("protocol", "TCP", "The network protocol for the service you want to be created. Default 'tcp'") | ||
cmd.Flags().Int("port", -1, "The port that the service should serve on. Required.") | ||
cmd.Flags().Bool("create-external-load-balancer", false, "If true, create an external load balancer for this service. Implementation is cloud provider dependent. Default false") | ||
cmd.Flags().String("selector", "", "A label selector to use for this service. If empty (the default) infer the selector from the replication controller") | ||
cmd.Flags().Bool("dry-run", false, "If true, only print the object that would be sent, don't actually do anything") | ||
cmd.Flags().String("container-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("overrides", "", "An inline JSON override for the generated object. If this is non-empty, it is parsed 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.") | ||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you intend to remove this? It's included in the other help dumps.
If we keep it, it should say
run-container
rather than justrun
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
restored.