2015-01-17 01:52:27 +00:00
/ *
2016-06-03 00:25:58 +00:00
Copyright 2014 The Kubernetes Authors .
2015-01-17 01:52:27 +00:00
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"
2016-04-03 10:44:35 +00:00
"regexp"
2015-10-12 09:12:36 +00:00
"strings"
2015-01-17 01:52:27 +00:00
2016-05-20 17:49:56 +00:00
"github.com/renstrom/dedent"
2015-08-11 08:38:27 +00:00
"github.com/spf13/cobra"
2015-08-05 22:03:47 +00:00
"k8s.io/kubernetes/pkg/kubectl"
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
"k8s.io/kubernetes/pkg/kubectl/resource"
2015-12-21 05:37:49 +00:00
"k8s.io/kubernetes/pkg/runtime"
2015-09-29 11:39:10 +00:00
"k8s.io/kubernetes/pkg/util/validation"
2015-01-17 01:52:27 +00:00
)
2016-05-20 17:49:56 +00:00
var (
expose_resources = dedent . Dedent ( `
pod ( po ) , service ( svc ) , replicationcontroller ( rc ) ,
2016-08-31 01:50:22 +00:00
deployment ( deploy ) , replicaset ( rs )
2016-05-20 17:49:56 +00:00
` )
2016-04-03 10:44:35 +00:00
2016-05-20 17:49:56 +00:00
expose_long = dedent . Dedent ( `
Expose a resource as a new Kubernetes service .
2015-03-11 17:22:08 +00:00
2016-05-20 17:49:56 +00:00
Looks up a deployment , service , replica set , replication controller or pod by name and uses the selector
for that resource as the selector for a new service on the specified port . A deployment or replica set
will be exposed as a service only if its selector is convertible to a selector that service supports ,
i . e . when the selector contains only the matchLabels component . Note that if no port is specified via
2016-07-24 17:01:38 +00:00
-- port and the exposed resource has multiple ports , all will be re - used by the new service . Also if no
2016-05-20 17:49:56 +00:00
labels are specified , the new service will re - use the labels from the resource it exposes .
2016-04-03 10:44:35 +00:00
2016-05-20 17:49:56 +00:00
Possible resources include ( case insensitive ) : ` ) + expose_resources
2015-01-17 01:52:27 +00:00
2016-05-20 17:49:56 +00:00
expose_example = dedent . Dedent ( `
# Create a service for a replicated nginx , which serves on port 80 and connects to the containers on port 8000.
kubectl expose rc nginx -- port = 80 -- target - port = 8000
2015-01-17 01:52:27 +00:00
2016-05-20 17:49:56 +00:00
# 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
2015-08-11 08:38:27 +00:00
2016-05-20 17:49:56 +00:00
# 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
2015-08-26 08:06:40 +00:00
2016-05-20 17:49:56 +00:00
# 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
2015-03-26 02:35:26 +00:00
2016-05-20 17:49:56 +00:00
# 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 -- name = video - stream
2016-02-09 21:18:45 +00:00
2016-05-20 17:49:56 +00:00
# Create a service for a replicated nginx using replica set , which serves on port 80 and connects to the containers on port 8000.
kubectl expose rs nginx -- port = 80 -- target - port = 8000
2016-03-18 23:07:41 +00:00
2016-05-20 17:49:56 +00:00
# Create a service for an nginx deployment , which serves on port 80 and connects to the containers on port 8000.
kubectl expose deployment nginx -- port = 80 -- target - port = 8000 ` )
2015-02-20 21:28:43 +00:00
)
2015-02-03 17:59:21 +00:00
2016-10-13 00:18:39 +00:00
func NewCmdExposeService ( f cmdutil . Factory , out io . Writer ) * cobra . Command {
2016-08-17 18:28:07 +00:00
options := & resource . FilenameOptions { }
2015-08-14 18:46:43 +00:00
2016-04-03 10:44:35 +00:00
validArgs , argAliases := [ ] string { } , [ ] string { }
resources := regexp . MustCompile ( ` \s*, ` ) . Split ( expose_resources , - 1 )
for _ , r := range resources {
validArgs = append ( validArgs , strings . Fields ( r ) [ 0 ] )
argAliases = kubectl . ResourceAliases ( validArgs )
}
2015-02-20 21:28:43 +00:00
cmd := & cobra . Command {
2016-01-11 07:29:57 +00:00
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]" ,
2016-03-22 18:43:28 +00:00
Short : "Take a replication controller, service, deployment or pod and expose it as a new Kubernetes Service" ,
2015-02-20 21:28:43 +00:00
Long : expose_long ,
Example : expose_example ,
2015-01-17 01:52:27 +00:00
Run : func ( cmd * cobra . Command , args [ ] string ) {
2015-08-14 18:46:43 +00:00
err := RunExpose ( f , out , cmd , args , options )
2015-04-07 18:21:25 +00:00
cmdutil . CheckErr ( err )
2015-01-17 01:52:27 +00:00
} ,
2016-04-03 10:44:35 +00:00
ValidArgs : validArgs ,
ArgAliases : argAliases ,
2015-01-17 01:52:27 +00:00
}
2015-04-07 18:21:25 +00:00
cmdutil . AddPrinterFlags ( cmd )
2015-07-16 21:18:17 +00:00
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'." )
2016-05-12 04:07:07 +00:00
cmd . Flags ( ) . String ( "protocol" , "" , "The network protocol for the service to be created. Default is 'TCP'." )
2015-10-03 23:06:32 +00:00
cmd . Flags ( ) . String ( "port" , "" , "The port that the service should serve on. Copied from the resource being exposed, if unspecified" )
2015-08-24 17:30:24 +00:00
cmd . Flags ( ) . String ( "type" , "" , "Type for this service: ClusterIP, NodePort, or LoadBalancer. Default is 'ClusterIP'." )
// TODO: remove create-external-load-balancer in code on or after Aug 25, 2016.
2015-05-22 21:49:26 +00:00
cmd . Flags ( ) . Bool ( "create-external-load-balancer" , false , "If true, create an external load balancer for this service (trumped by --type). Implementation is cloud provider dependent. Default is 'false'." )
2015-08-24 17:30:24 +00:00
cmd . Flags ( ) . MarkDeprecated ( "create-external-load-balancer" , "use --type=\"LoadBalancer\" instead" )
2016-08-02 16:51:51 +00:00
cmd . Flags ( ) . String ( "load-balancer-ip" , "" , "IP to assign to the Load Balancer. If empty, an ephemeral IP will be created and used (cloud-provider specific)." )
2016-02-09 21:18:45 +00:00
cmd . Flags ( ) . String ( "selector" , "" , "A label selector to use for this service. Only equality-based selector requirements are supported. If empty (the default) infer the selector from the replication controller or replica set." )
2015-03-16 21:18:17 +00:00
cmd . Flags ( ) . StringP ( "labels" , "l" , "" , "Labels to apply to the service created by this call." )
2015-03-26 20:56:36 +00:00
cmd . Flags ( ) . String ( "container-port" , "" , "Synonym for --target-port" )
2016-05-04 00:36:42 +00:00
cmd . Flags ( ) . MarkDeprecated ( "container-port" , "--container-port will be removed in the future, please use --target-port instead" )
2015-03-26 20:56:36 +00:00
cmd . Flags ( ) . String ( "target-port" , "" , "Name or number for the port on the container that the service should direct traffic to. Optional." )
2016-01-11 07:29:57 +00:00
cmd . Flags ( ) . String ( "external-ip" , "" , "Additional external IP address (not managed by Kubernetes) to accept for the service. If this IP is routed to a node, the service can be accessed by this IP in addition to its generated service IP." )
2015-02-03 17:59:21 +00:00
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." )
2015-06-02 10:54:09 +00:00
cmd . Flags ( ) . String ( "name" , "" , "The name for the newly created object." )
2015-07-31 17:34:52 +00:00
cmd . Flags ( ) . String ( "session-affinity" , "" , "If non-empty, set the session affinity for the service to this; legal values: 'None', 'ClientIP'" )
2016-06-29 20:41:08 +00:00
cmd . Flags ( ) . String ( "cluster-ip" , "" , "ClusterIP to be assigned to the service. Leave empty to auto-allocate, or set to 'None' to create a headless service." )
2015-08-11 08:38:27 +00:00
2016-08-17 18:28:07 +00:00
usage := "identifying the resource to expose a service"
cmdutil . AddFilenameOptionFlags ( cmd , options , usage )
2016-05-11 00:26:39 +00:00
cmdutil . AddDryRunFlag ( cmd )
2015-11-04 21:47:08 +00:00
cmdutil . AddApplyAnnotationFlags ( cmd )
2016-01-22 18:33:23 +00:00
cmdutil . AddRecordFlag ( cmd )
2015-01-17 01:52:27 +00:00
return cmd
}
2015-03-09 22:08:16 +00:00
2016-10-13 00:18:39 +00:00
func RunExpose ( f cmdutil . Factory , out io . Writer , cmd * cobra . Command , args [ ] string , options * resource . FilenameOptions ) error {
2015-08-11 08:38:27 +00:00
namespace , enforceNamespace , err := f . DefaultNamespace ( )
2015-03-09 22:08:16 +00:00
if err != nil {
return err
}
2016-09-16 19:50:34 +00:00
mapper , typer := f . Object ( )
2015-12-21 05:37:49 +00:00
r := resource . NewBuilder ( mapper , typer , resource . ClientMapperFunc ( f . ClientForMapping ) , f . Decoder ( true ) ) .
2015-05-13 12:14:44 +00:00
ContinueOnError ( ) .
NamespaceParam ( namespace ) . DefaultNamespace ( ) .
2016-08-17 18:28:07 +00:00
FilenameParam ( enforceNamespace , options ) .
2015-05-13 12:14:44 +00:00
ResourceTypeOrNameArgs ( false , args ... ) .
Flatten ( ) .
Do ( )
2016-04-14 22:00:40 +00:00
err = r . Err ( )
2015-05-07 15:30:28 +00:00
if err != nil {
2016-09-07 21:20:04 +00:00
return cmdutil . UsageError ( cmd , err . Error ( ) )
2015-05-07 15:30:28 +00:00
}
2015-03-09 22:08:16 +00:00
2015-05-07 15:30:28 +00:00
// Get the generator, setup and validate all required parameters
generatorName := cmdutil . GetFlagString ( cmd , "generator" )
2015-11-19 18:14:10 +00:00
generators := f . Generators ( "expose" )
generator , found := generators [ generatorName ]
2015-03-09 22:08:16 +00:00
if ! found {
2015-06-02 10:54:09 +00:00
return cmdutil . UsageError ( cmd , fmt . Sprintf ( "generator %q not found." , generatorName ) )
2015-03-09 22:08:16 +00:00
}
names := generator . ParamNames ( )
2015-09-09 18:55:56 +00:00
2016-04-14 22:00:40 +00:00
err = r . Visit ( func ( info * resource . Info , err error ) error {
2015-09-09 18:55:56 +00:00
if err != nil {
2016-04-14 22:00:40 +00:00
return err
2015-04-01 21:46:33 +00:00
}
2015-09-09 18:55:56 +00:00
2016-04-14 22:00:40 +00:00
mapping := info . ResourceMapping ( )
if err := f . CanBeExposed ( mapping . GroupVersionKind . GroupKind ( ) ) ; err != nil {
return err
2015-05-04 12:17:30 +00:00
}
2016-04-14 22:00:40 +00:00
params := kubectl . MakeParams ( cmd , names )
name := info . Name
2016-07-24 17:01:38 +00:00
if len ( name ) > validation . DNS1035LabelMaxLength {
name = name [ : validation . DNS1035LabelMaxLength ]
2015-03-09 22:08:16 +00:00
}
2016-04-14 22:00:40 +00:00
params [ "default-name" ] = name
// For objects that need a pod selector, derive it from the exposed object in case a user
// didn't explicitly specify one via --selector
if s , found := params [ "selector" ] ; found && kubectl . IsZero ( s ) {
s , err := f . MapBasedSelectorForObject ( info . Object )
if err != nil {
return cmdutil . UsageError ( cmd , fmt . Sprintf ( "couldn't retrieve selectors via --selector flag or introspection: %s" , err ) )
}
params [ "selector" ] = s
}
// For objects that need a port, derive it from the exposed object in case a user
// didn't explicitly specify one via --port
if port , found := params [ "port" ] ; found && kubectl . IsZero ( port ) {
ports , err := f . PortsForObject ( info . Object )
if err != nil {
return cmdutil . UsageError ( cmd , fmt . Sprintf ( "couldn't find port via --port flag or introspection: %s" , err ) )
}
switch len ( ports ) {
case 0 :
return cmdutil . UsageError ( cmd , "couldn't find port via --port flag or introspection" )
case 1 :
params [ "port" ] = ports [ 0 ]
default :
params [ "ports" ] = strings . Join ( ports , "," )
}
}
2016-05-12 04:07:07 +00:00
// Always try to derive protocols from the exposed object, may use
// different protocols for different ports.
if _ , found := params [ "protocol" ] ; found {
protocolsMap , err := f . ProtocolsForObject ( info . Object )
if err != nil {
return cmdutil . UsageError ( cmd , fmt . Sprintf ( "couldn't find protocol via introspection: %s" , err ) )
}
if protocols := kubectl . MakeProtocols ( protocolsMap ) ; ! kubectl . IsZero ( protocols ) {
params [ "protocols" ] = protocols
}
}
2016-04-14 22:00:40 +00:00
if kubectl . IsZero ( params [ "labels" ] ) {
labels , err := f . LabelsForObject ( info . Object )
if err != nil {
return err
}
params [ "labels" ] = kubectl . MakeLabels ( labels )
}
if err = kubectl . ValidateParams ( names , params ) ; err != nil {
return err
}
// Check for invalid flags used against the present generator.
if err := kubectl . EnsureFlagsValid ( cmd , generators , generatorName ) ; err != nil {
return err
}
// Generate new object
object , err := generator . Generate ( params )
2015-05-13 08:52:25 +00:00
if err != nil {
return err
}
2015-03-09 22:08:16 +00:00
2016-04-14 22:00:40 +00:00
if inline := cmdutil . GetFlagString ( cmd , "overrides" ) ; len ( inline ) > 0 {
codec := runtime . NewCodec ( f . JSONEncoder ( ) , f . Decoder ( true ) )
object , err = cmdutil . Merge ( codec , object , inline , mapping . GroupVersionKind . Kind )
if err != nil {
return err
}
}
2015-03-09 22:08:16 +00:00
2016-04-14 22:00:40 +00:00
resourceMapper := & resource . Mapper {
ObjectTyper : typer ,
RESTMapper : mapper ,
ClientMapper : resource . ClientMapperFunc ( f . ClientForMapping ) ,
Decoder : f . Decoder ( true ) ,
}
info , err = resourceMapper . InfoForObject ( object , nil )
2015-03-09 22:08:16 +00:00
if err != nil {
return err
}
2016-04-14 22:00:40 +00:00
if cmdutil . ShouldRecord ( cmd , info ) {
if err := cmdutil . RecordChangeCause ( object , f . Command ( ) ) ; err != nil {
return err
}
}
info . Refresh ( object , true )
2016-05-11 00:26:39 +00:00
if cmdutil . GetDryRunFlag ( cmd ) {
2016-08-23 18:11:39 +00:00
if len ( cmdutil . GetFlagString ( cmd , "output" ) ) > 0 {
return f . PrintObject ( cmd , mapper , object , out )
}
cmdutil . PrintSuccess ( mapper , false , out , info . Mapping . Resource , info . Name , true , "exposed" )
return nil
2016-04-14 22:00:40 +00:00
}
if err := kubectl . CreateOrUpdateAnnotation ( cmdutil . GetFlagBool ( cmd , cmdutil . ApplyAnnotationsFlag ) , info , f . JSONEncoder ( ) ) ; err != nil {
return err
}
2015-03-09 22:08:16 +00:00
2016-04-14 22:00:40 +00:00
// Serialize the object with the annotation applied.
object , err = resource . NewHelper ( info . Client , info . Mapping ) . Create ( namespace , false , object )
if err != nil {
2016-01-22 18:33:23 +00:00
return err
}
2015-09-10 21:32:57 +00:00
2016-04-14 22:00:40 +00:00
if len ( cmdutil . GetFlagString ( cmd , "output" ) ) > 0 {
return f . PrintObject ( cmd , mapper , object , out )
}
2016-08-23 18:11:39 +00:00
cmdutil . PrintSuccess ( mapper , false , out , info . Mapping . Resource , info . Name , false , "exposed" )
2016-04-14 22:00:40 +00:00
return nil
} )
2015-10-02 23:52:42 +00:00
if err != nil {
return err
}
2015-08-26 08:37:16 +00:00
return nil
2015-03-09 22:08:16 +00:00
}