expose.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*
  2. Copyright 2014 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package cmd
  14. import (
  15. "fmt"
  16. "io"
  17. "regexp"
  18. "strings"
  19. "github.com/renstrom/dedent"
  20. "github.com/spf13/cobra"
  21. "k8s.io/kubernetes/pkg/kubectl"
  22. cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
  23. "k8s.io/kubernetes/pkg/kubectl/resource"
  24. "k8s.io/kubernetes/pkg/runtime"
  25. "k8s.io/kubernetes/pkg/util/validation"
  26. )
  27. // ExposeOptions is the start of the data required to perform the operation. As new fields are added, add them here instead of
  28. // referencing the cmd.Flags()
  29. type ExposeOptions struct {
  30. Filenames []string
  31. Recursive bool
  32. }
  33. var (
  34. expose_resources = dedent.Dedent(`
  35. pod (po), service (svc), replicationcontroller (rc),
  36. deployment, replicaset (rs)
  37. `)
  38. expose_long = dedent.Dedent(`
  39. Expose a resource as a new Kubernetes service.
  40. Looks up a deployment, service, replica set, replication controller or pod by name and uses the selector
  41. for that resource as the selector for a new service on the specified port. A deployment or replica set
  42. will be exposed as a service only if its selector is convertible to a selector that service supports,
  43. i.e. when the selector contains only the matchLabels component. Note that if no port is specified via
  44. --port and the exposed resource has multiple ports, all will be re-used by the new service. Also if no
  45. labels are specified, the new service will re-use the labels from the resource it exposes.
  46. Possible resources include (case insensitive): `) + expose_resources
  47. expose_example = dedent.Dedent(`
  48. # Create a service for a replicated nginx, which serves on port 80 and connects to the containers on port 8000.
  49. kubectl expose rc nginx --port=80 --target-port=8000
  50. # 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.
  51. kubectl expose -f nginx-controller.yaml --port=80 --target-port=8000
  52. # Create a service for a pod valid-pod, which serves on port 444 with the name "frontend"
  53. kubectl expose pod valid-pod --port=444 --name=frontend
  54. # Create a second service based on the above service, exposing the container port 8443 as port 443 with the name "nginx-https"
  55. kubectl expose service nginx --port=443 --target-port=8443 --name=nginx-https
  56. # Create a service for a replicated streaming application on port 4100 balancing UDP traffic and named 'video-stream'.
  57. kubectl expose rc streamer --port=4100 --protocol=udp --name=video-stream
  58. # Create a service for a replicated nginx using replica set, which serves on port 80 and connects to the containers on port 8000.
  59. kubectl expose rs nginx --port=80 --target-port=8000
  60. # Create a service for an nginx deployment, which serves on port 80 and connects to the containers on port 8000.
  61. kubectl expose deployment nginx --port=80 --target-port=8000`)
  62. )
  63. func NewCmdExposeService(f *cmdutil.Factory, out io.Writer) *cobra.Command {
  64. options := &ExposeOptions{}
  65. validArgs, argAliases := []string{}, []string{}
  66. resources := regexp.MustCompile(`\s*,`).Split(expose_resources, -1)
  67. for _, r := range resources {
  68. validArgs = append(validArgs, strings.Fields(r)[0])
  69. argAliases = kubectl.ResourceAliases(validArgs)
  70. }
  71. cmd := &cobra.Command{
  72. 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]",
  73. Short: "Take a replication controller, service, deployment or pod and expose it as a new Kubernetes Service",
  74. Long: expose_long,
  75. Example: expose_example,
  76. Run: func(cmd *cobra.Command, args []string) {
  77. err := RunExpose(f, out, cmd, args, options)
  78. cmdutil.CheckErr(err)
  79. },
  80. ValidArgs: validArgs,
  81. ArgAliases: argAliases,
  82. }
  83. cmdutil.AddPrinterFlags(cmd)
  84. 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'.")
  85. cmd.Flags().String("protocol", "", "The network protocol for the service to be created. Default is 'TCP'.")
  86. cmd.Flags().String("port", "", "The port that the service should serve on. Copied from the resource being exposed, if unspecified")
  87. cmd.Flags().String("type", "", "Type for this service: ClusterIP, NodePort, or LoadBalancer. Default is 'ClusterIP'.")
  88. // TODO: remove create-external-load-balancer in code on or after Aug 25, 2016.
  89. 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'.")
  90. cmd.Flags().MarkDeprecated("create-external-load-balancer", "use --type=\"LoadBalancer\" instead")
  91. 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).")
  92. 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.")
  93. cmd.Flags().StringP("labels", "l", "", "Labels to apply to the service created by this call.")
  94. cmd.Flags().String("container-port", "", "Synonym for --target-port")
  95. cmd.Flags().MarkDeprecated("container-port", "--container-port will be removed in the future, please use --target-port instead")
  96. cmd.Flags().String("target-port", "", "Name or number for the port on the container that the service should direct traffic to. Optional.")
  97. 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.")
  98. 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.")
  99. cmd.Flags().String("name", "", "The name for the newly created object.")
  100. cmd.Flags().String("session-affinity", "", "If non-empty, set the session affinity for the service to this; legal values: 'None', 'ClientIP'")
  101. 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.")
  102. usage := "Filename, directory, or URL to a file identifying the resource to expose a service"
  103. kubectl.AddJsonFilenameFlag(cmd, &options.Filenames, usage)
  104. cmdutil.AddDryRunFlag(cmd)
  105. cmdutil.AddRecursiveFlag(cmd, &options.Recursive)
  106. cmdutil.AddApplyAnnotationFlags(cmd)
  107. cmdutil.AddRecordFlag(cmd)
  108. return cmd
  109. }
  110. func RunExpose(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string, options *ExposeOptions) error {
  111. namespace, enforceNamespace, err := f.DefaultNamespace()
  112. if err != nil {
  113. return err
  114. }
  115. mapper, typer := f.Object(false)
  116. r := resource.NewBuilder(mapper, typer, resource.ClientMapperFunc(f.ClientForMapping), f.Decoder(true)).
  117. ContinueOnError().
  118. NamespaceParam(namespace).DefaultNamespace().
  119. FilenameParam(enforceNamespace, options.Recursive, options.Filenames...).
  120. ResourceTypeOrNameArgs(false, args...).
  121. Flatten().
  122. Do()
  123. err = r.Err()
  124. if err != nil {
  125. return err
  126. }
  127. // Get the generator, setup and validate all required parameters
  128. generatorName := cmdutil.GetFlagString(cmd, "generator")
  129. generators := f.Generators("expose")
  130. generator, found := generators[generatorName]
  131. if !found {
  132. return cmdutil.UsageError(cmd, fmt.Sprintf("generator %q not found.", generatorName))
  133. }
  134. names := generator.ParamNames()
  135. err = r.Visit(func(info *resource.Info, err error) error {
  136. if err != nil {
  137. return err
  138. }
  139. mapping := info.ResourceMapping()
  140. if err := f.CanBeExposed(mapping.GroupVersionKind.GroupKind()); err != nil {
  141. return err
  142. }
  143. params := kubectl.MakeParams(cmd, names)
  144. name := info.Name
  145. if len(name) > validation.DNS1035LabelMaxLength {
  146. name = name[:validation.DNS1035LabelMaxLength]
  147. }
  148. params["default-name"] = name
  149. // For objects that need a pod selector, derive it from the exposed object in case a user
  150. // didn't explicitly specify one via --selector
  151. if s, found := params["selector"]; found && kubectl.IsZero(s) {
  152. s, err := f.MapBasedSelectorForObject(info.Object)
  153. if err != nil {
  154. return cmdutil.UsageError(cmd, fmt.Sprintf("couldn't retrieve selectors via --selector flag or introspection: %s", err))
  155. }
  156. params["selector"] = s
  157. }
  158. // For objects that need a port, derive it from the exposed object in case a user
  159. // didn't explicitly specify one via --port
  160. if port, found := params["port"]; found && kubectl.IsZero(port) {
  161. ports, err := f.PortsForObject(info.Object)
  162. if err != nil {
  163. return cmdutil.UsageError(cmd, fmt.Sprintf("couldn't find port via --port flag or introspection: %s", err))
  164. }
  165. switch len(ports) {
  166. case 0:
  167. return cmdutil.UsageError(cmd, "couldn't find port via --port flag or introspection")
  168. case 1:
  169. params["port"] = ports[0]
  170. default:
  171. params["ports"] = strings.Join(ports, ",")
  172. }
  173. }
  174. // Always try to derive protocols from the exposed object, may use
  175. // different protocols for different ports.
  176. if _, found := params["protocol"]; found {
  177. protocolsMap, err := f.ProtocolsForObject(info.Object)
  178. if err != nil {
  179. return cmdutil.UsageError(cmd, fmt.Sprintf("couldn't find protocol via introspection: %s", err))
  180. }
  181. if protocols := kubectl.MakeProtocols(protocolsMap); !kubectl.IsZero(protocols) {
  182. params["protocols"] = protocols
  183. }
  184. }
  185. if kubectl.IsZero(params["labels"]) {
  186. labels, err := f.LabelsForObject(info.Object)
  187. if err != nil {
  188. return err
  189. }
  190. params["labels"] = kubectl.MakeLabels(labels)
  191. }
  192. if err = kubectl.ValidateParams(names, params); err != nil {
  193. return err
  194. }
  195. // Check for invalid flags used against the present generator.
  196. if err := kubectl.EnsureFlagsValid(cmd, generators, generatorName); err != nil {
  197. return err
  198. }
  199. // Generate new object
  200. object, err := generator.Generate(params)
  201. if err != nil {
  202. return err
  203. }
  204. if inline := cmdutil.GetFlagString(cmd, "overrides"); len(inline) > 0 {
  205. codec := runtime.NewCodec(f.JSONEncoder(), f.Decoder(true))
  206. object, err = cmdutil.Merge(codec, object, inline, mapping.GroupVersionKind.Kind)
  207. if err != nil {
  208. return err
  209. }
  210. }
  211. resourceMapper := &resource.Mapper{
  212. ObjectTyper: typer,
  213. RESTMapper: mapper,
  214. ClientMapper: resource.ClientMapperFunc(f.ClientForMapping),
  215. Decoder: f.Decoder(true),
  216. }
  217. info, err = resourceMapper.InfoForObject(object, nil)
  218. if err != nil {
  219. return err
  220. }
  221. if cmdutil.ShouldRecord(cmd, info) {
  222. if err := cmdutil.RecordChangeCause(object, f.Command()); err != nil {
  223. return err
  224. }
  225. }
  226. info.Refresh(object, true)
  227. if cmdutil.GetDryRunFlag(cmd) {
  228. return f.PrintObject(cmd, mapper, object, out)
  229. }
  230. if err := kubectl.CreateOrUpdateAnnotation(cmdutil.GetFlagBool(cmd, cmdutil.ApplyAnnotationsFlag), info, f.JSONEncoder()); err != nil {
  231. return err
  232. }
  233. // Serialize the object with the annotation applied.
  234. object, err = resource.NewHelper(info.Client, info.Mapping).Create(namespace, false, object)
  235. if err != nil {
  236. return err
  237. }
  238. if len(cmdutil.GetFlagString(cmd, "output")) > 0 {
  239. return f.PrintObject(cmd, mapper, object, out)
  240. }
  241. cmdutil.PrintSuccess(mapper, false, out, info.Mapping.Resource, info.Name, "exposed")
  242. return nil
  243. })
  244. if err != nil {
  245. return err
  246. }
  247. return nil
  248. }