expose.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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 expose
  14. import (
  15. "regexp"
  16. "strings"
  17. "github.com/spf13/cobra"
  18. "k8s.io/klog"
  19. "k8s.io/apimachinery/pkg/api/meta"
  20. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  21. "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
  22. "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured/unstructuredscheme"
  23. "k8s.io/apimachinery/pkg/runtime"
  24. "k8s.io/apimachinery/pkg/util/validation"
  25. "k8s.io/cli-runtime/pkg/genericclioptions"
  26. "k8s.io/cli-runtime/pkg/printers"
  27. "k8s.io/cli-runtime/pkg/resource"
  28. "k8s.io/client-go/dynamic"
  29. "k8s.io/kubernetes/pkg/kubectl"
  30. cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
  31. "k8s.io/kubernetes/pkg/kubectl/generate"
  32. generateversioned "k8s.io/kubernetes/pkg/kubectl/generate/versioned"
  33. "k8s.io/kubernetes/pkg/kubectl/polymorphichelpers"
  34. "k8s.io/kubernetes/pkg/kubectl/scheme"
  35. "k8s.io/kubernetes/pkg/kubectl/util/i18n"
  36. "k8s.io/kubernetes/pkg/kubectl/util/templates"
  37. )
  38. var (
  39. exposeResources = `pod (po), service (svc), replicationcontroller (rc), deployment (deploy), replicaset (rs)`
  40. exposeLong = templates.LongDesc(`
  41. Expose a resource as a new Kubernetes service.
  42. Looks up a deployment, service, replica set, replication controller or pod by name and uses the selector
  43. for that resource as the selector for a new service on the specified port. A deployment or replica set
  44. will be exposed as a service only if its selector is convertible to a selector that service supports,
  45. i.e. when the selector contains only the matchLabels component. Note that if no port is specified via
  46. --port and the exposed resource has multiple ports, all will be re-used by the new service. Also if no
  47. labels are specified, the new service will re-use the labels from the resource it exposes.
  48. Possible resources include (case insensitive):
  49. ` + exposeResources)
  50. exposeExample = templates.Examples(i18n.T(`
  51. # Create a service for a replicated nginx, which serves on port 80 and connects to the containers on port 8000.
  52. kubectl expose rc nginx --port=80 --target-port=8000
  53. # 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.
  54. kubectl expose -f nginx-controller.yaml --port=80 --target-port=8000
  55. # Create a service for a pod valid-pod, which serves on port 444 with the name "frontend"
  56. kubectl expose pod valid-pod --port=444 --name=frontend
  57. # Create a second service based on the above service, exposing the container port 8443 as port 443 with the name "nginx-https"
  58. kubectl expose service nginx --port=443 --target-port=8443 --name=nginx-https
  59. # Create a service for a replicated streaming application on port 4100 balancing UDP traffic and named 'video-stream'.
  60. kubectl expose rc streamer --port=4100 --protocol=UDP --name=video-stream
  61. # Create a service for a replicated nginx using replica set, which serves on port 80 and connects to the containers on port 8000.
  62. kubectl expose rs nginx --port=80 --target-port=8000
  63. # Create a service for an nginx deployment, which serves on port 80 and connects to the containers on port 8000.
  64. kubectl expose deployment nginx --port=80 --target-port=8000`))
  65. )
  66. type ExposeServiceOptions struct {
  67. FilenameOptions resource.FilenameOptions
  68. RecordFlags *genericclioptions.RecordFlags
  69. PrintFlags *genericclioptions.PrintFlags
  70. PrintObj printers.ResourcePrinterFunc
  71. DryRun bool
  72. EnforceNamespace bool
  73. Generators func(string) map[string]generate.Generator
  74. CanBeExposed polymorphichelpers.CanBeExposedFunc
  75. MapBasedSelectorForObject func(runtime.Object) (string, error)
  76. PortsForObject polymorphichelpers.PortsForObjectFunc
  77. ProtocolsForObject func(runtime.Object) (map[string]string, error)
  78. Namespace string
  79. Mapper meta.RESTMapper
  80. DynamicClient dynamic.Interface
  81. Builder *resource.Builder
  82. Recorder genericclioptions.Recorder
  83. genericclioptions.IOStreams
  84. }
  85. func NewExposeServiceOptions(ioStreams genericclioptions.IOStreams) *ExposeServiceOptions {
  86. return &ExposeServiceOptions{
  87. RecordFlags: genericclioptions.NewRecordFlags(),
  88. PrintFlags: genericclioptions.NewPrintFlags("exposed").WithTypeSetter(scheme.Scheme),
  89. Recorder: genericclioptions.NoopRecorder{},
  90. IOStreams: ioStreams,
  91. }
  92. }
  93. func NewCmdExposeService(f cmdutil.Factory, streams genericclioptions.IOStreams) *cobra.Command {
  94. o := NewExposeServiceOptions(streams)
  95. validArgs := []string{}
  96. resources := regexp.MustCompile(`\s*,`).Split(exposeResources, -1)
  97. for _, r := range resources {
  98. validArgs = append(validArgs, strings.Fields(r)[0])
  99. }
  100. cmd := &cobra.Command{
  101. Use: "expose (-f FILENAME | TYPE NAME) [--port=port] [--protocol=TCP|UDP|SCTP] [--target-port=number-or-name] [--name=name] [--external-ip=external-ip-of-service] [--type=type]",
  102. DisableFlagsInUseLine: true,
  103. Short: i18n.T("Take a replication controller, service, deployment or pod and expose it as a new Kubernetes Service"),
  104. Long: exposeLong,
  105. Example: exposeExample,
  106. Run: func(cmd *cobra.Command, args []string) {
  107. cmdutil.CheckErr(o.Complete(f, cmd))
  108. cmdutil.CheckErr(o.RunExpose(cmd, args))
  109. },
  110. ValidArgs: validArgs,
  111. }
  112. o.RecordFlags.AddFlags(cmd)
  113. o.PrintFlags.AddFlags(cmd)
  114. cmd.Flags().String("generator", "service/v2", i18n.T("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'."))
  115. cmd.Flags().String("protocol", "", i18n.T("The network protocol for the service to be created. Default is 'TCP'."))
  116. cmd.Flags().String("port", "", i18n.T("The port that the service should serve on. Copied from the resource being exposed, if unspecified"))
  117. cmd.Flags().String("type", "", i18n.T("Type for this service: ClusterIP, NodePort, LoadBalancer, or ExternalName. Default is 'ClusterIP'."))
  118. cmd.Flags().String("load-balancer-ip", "", i18n.T("IP to assign to the LoadBalancer. If empty, an ephemeral IP will be created and used (cloud-provider specific)."))
  119. cmd.Flags().String("selector", "", i18n.T("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.)"))
  120. cmd.Flags().StringP("labels", "l", "", "Labels to apply to the service created by this call.")
  121. cmd.Flags().String("container-port", "", i18n.T("Synonym for --target-port"))
  122. cmd.Flags().MarkDeprecated("container-port", "--container-port will be removed in the future, please use --target-port instead")
  123. cmd.Flags().String("target-port", "", i18n.T("Name or number for the port on the container that the service should direct traffic to. Optional."))
  124. cmd.Flags().String("external-ip", "", i18n.T("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."))
  125. cmd.Flags().String("overrides", "", i18n.T("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."))
  126. cmd.Flags().String("name", "", i18n.T("The name for the newly created object."))
  127. cmd.Flags().String("session-affinity", "", i18n.T("If non-empty, set the session affinity for the service to this; legal values: 'None', 'ClientIP'"))
  128. cmd.Flags().String("cluster-ip", "", i18n.T("ClusterIP to be assigned to the service. Leave empty to auto-allocate, or set to 'None' to create a headless service."))
  129. usage := "identifying the resource to expose a service"
  130. cmdutil.AddFilenameOptionFlags(cmd, &o.FilenameOptions, usage)
  131. cmdutil.AddDryRunFlag(cmd)
  132. cmdutil.AddApplyAnnotationFlags(cmd)
  133. return cmd
  134. }
  135. func (o *ExposeServiceOptions) Complete(f cmdutil.Factory, cmd *cobra.Command) error {
  136. o.DryRun = cmdutil.GetDryRunFlag(cmd)
  137. if o.DryRun {
  138. o.PrintFlags.Complete("%s (dry run)")
  139. }
  140. printer, err := o.PrintFlags.ToPrinter()
  141. if err != nil {
  142. return err
  143. }
  144. o.PrintObj = printer.PrintObj
  145. o.RecordFlags.Complete(cmd)
  146. o.Recorder, err = o.RecordFlags.ToRecorder()
  147. if err != nil {
  148. return err
  149. }
  150. o.DynamicClient, err = f.DynamicClient()
  151. if err != nil {
  152. return err
  153. }
  154. o.Generators = generateversioned.GeneratorFn
  155. o.Builder = f.NewBuilder()
  156. o.CanBeExposed = polymorphichelpers.CanBeExposedFn
  157. o.MapBasedSelectorForObject = polymorphichelpers.MapBasedSelectorForObjectFn
  158. o.ProtocolsForObject = polymorphichelpers.ProtocolsForObjectFn
  159. o.PortsForObject = polymorphichelpers.PortsForObjectFn
  160. o.Mapper, err = f.ToRESTMapper()
  161. if err != nil {
  162. return err
  163. }
  164. o.Namespace, o.EnforceNamespace, err = f.ToRawKubeConfigLoader().Namespace()
  165. if err != nil {
  166. return err
  167. }
  168. return err
  169. }
  170. func (o *ExposeServiceOptions) RunExpose(cmd *cobra.Command, args []string) error {
  171. r := o.Builder.
  172. WithScheme(scheme.Scheme, scheme.Scheme.PrioritizedVersionsAllGroups()...).
  173. ContinueOnError().
  174. NamespaceParam(o.Namespace).DefaultNamespace().
  175. FilenameParam(o.EnforceNamespace, &o.FilenameOptions).
  176. ResourceTypeOrNameArgs(false, args...).
  177. Flatten().
  178. Do()
  179. err := r.Err()
  180. if err != nil {
  181. return cmdutil.UsageErrorf(cmd, err.Error())
  182. }
  183. // Get the generator, setup and validate all required parameters
  184. generatorName := cmdutil.GetFlagString(cmd, "generator")
  185. generators := o.Generators("expose")
  186. generator, found := generators[generatorName]
  187. if !found {
  188. return cmdutil.UsageErrorf(cmd, "generator %q not found.", generatorName)
  189. }
  190. names := generator.ParamNames()
  191. err = r.Visit(func(info *resource.Info, err error) error {
  192. if err != nil {
  193. return err
  194. }
  195. mapping := info.ResourceMapping()
  196. if err := o.CanBeExposed(mapping.GroupVersionKind.GroupKind()); err != nil {
  197. return err
  198. }
  199. params := generate.MakeParams(cmd, names)
  200. name := info.Name
  201. if len(name) > validation.DNS1035LabelMaxLength {
  202. name = name[:validation.DNS1035LabelMaxLength]
  203. }
  204. params["default-name"] = name
  205. // For objects that need a pod selector, derive it from the exposed object in case a user
  206. // didn't explicitly specify one via --selector
  207. if s, found := params["selector"]; found && generate.IsZero(s) {
  208. s, err := o.MapBasedSelectorForObject(info.Object)
  209. if err != nil {
  210. return cmdutil.UsageErrorf(cmd, "couldn't retrieve selectors via --selector flag or introspection: %v", err)
  211. }
  212. params["selector"] = s
  213. }
  214. isHeadlessService := params["cluster-ip"] == "None"
  215. // For objects that need a port, derive it from the exposed object in case a user
  216. // didn't explicitly specify one via --port
  217. if port, found := params["port"]; found && generate.IsZero(port) {
  218. ports, err := o.PortsForObject(info.Object)
  219. if err != nil {
  220. return cmdutil.UsageErrorf(cmd, "couldn't find port via --port flag or introspection: %v", err)
  221. }
  222. switch len(ports) {
  223. case 0:
  224. if !isHeadlessService {
  225. return cmdutil.UsageErrorf(cmd, "couldn't find port via --port flag or introspection")
  226. }
  227. case 1:
  228. params["port"] = ports[0]
  229. default:
  230. params["ports"] = strings.Join(ports, ",")
  231. }
  232. }
  233. // Always try to derive protocols from the exposed object, may use
  234. // different protocols for different ports.
  235. if _, found := params["protocol"]; found {
  236. protocolsMap, err := o.ProtocolsForObject(info.Object)
  237. if err != nil {
  238. return cmdutil.UsageErrorf(cmd, "couldn't find protocol via introspection: %v", err)
  239. }
  240. if protocols := generate.MakeProtocols(protocolsMap); !generate.IsZero(protocols) {
  241. params["protocols"] = protocols
  242. }
  243. }
  244. if generate.IsZero(params["labels"]) {
  245. labels, err := meta.NewAccessor().Labels(info.Object)
  246. if err != nil {
  247. return err
  248. }
  249. params["labels"] = generate.MakeLabels(labels)
  250. }
  251. if err = generate.ValidateParams(names, params); err != nil {
  252. return err
  253. }
  254. // Check for invalid flags used against the present generator.
  255. if err := generate.EnsureFlagsValid(cmd, generators, generatorName); err != nil {
  256. return err
  257. }
  258. // Generate new object
  259. object, err := generator.Generate(params)
  260. if err != nil {
  261. return err
  262. }
  263. if inline := cmdutil.GetFlagString(cmd, "overrides"); len(inline) > 0 {
  264. codec := runtime.NewCodec(scheme.DefaultJSONEncoder(), scheme.Codecs.UniversalDecoder(scheme.Scheme.PrioritizedVersionsAllGroups()...))
  265. object, err = cmdutil.Merge(codec, object, inline)
  266. if err != nil {
  267. return err
  268. }
  269. }
  270. if err := o.Recorder.Record(object); err != nil {
  271. klog.V(4).Infof("error recording current command: %v", err)
  272. }
  273. if o.DryRun {
  274. return o.PrintObj(object, o.Out)
  275. }
  276. if err := kubectl.CreateOrUpdateAnnotation(cmdutil.GetFlagBool(cmd, cmdutil.ApplyAnnotationsFlag), object, scheme.DefaultJSONEncoder()); err != nil {
  277. return err
  278. }
  279. asUnstructured := &unstructured.Unstructured{}
  280. if err := scheme.Scheme.Convert(object, asUnstructured, nil); err != nil {
  281. return err
  282. }
  283. gvks, _, err := unstructuredscheme.NewUnstructuredObjectTyper().ObjectKinds(asUnstructured)
  284. if err != nil {
  285. return err
  286. }
  287. objMapping, err := o.Mapper.RESTMapping(gvks[0].GroupKind(), gvks[0].Version)
  288. if err != nil {
  289. return err
  290. }
  291. // Serialize the object with the annotation applied.
  292. actualObject, err := o.DynamicClient.Resource(objMapping.Resource).Namespace(o.Namespace).Create(asUnstructured, metav1.CreateOptions{})
  293. if err != nil {
  294. return err
  295. }
  296. return o.PrintObj(actualObject, o.Out)
  297. })
  298. if err != nil {
  299. return err
  300. }
  301. return nil
  302. }