MCPcopy
hub / github.com/kubernetes/kubectl / RunExpose

Method RunExpose

pkg/cmd/expose/expose.go:296–428  ·  view source on GitHub ↗

RunExpose retrieves the Kubernetes Object from the API server and expose it to a Kubernetes Service

(cmd *cobra.Command, args []string)

Source from the content-addressed store, hash-verified

294// RunExpose retrieves the Kubernetes Object from the API server and expose it to a
295// Kubernetes Service
296func (o *ExposeServiceOptions) RunExpose(cmd *cobra.Command, args []string) error {
297 r := o.Builder.
298 WithScheme(scheme.Scheme, scheme.Scheme.PrioritizedVersionsAllGroups()...).
299 ContinueOnError().
300 NamespaceParam(o.Namespace).DefaultNamespace().
301 FilenameParam(o.EnforceNamespace, &o.FilenameOptions).
302 ResourceTypeOrNameArgs(false, args...).
303 Flatten().
304 Do()
305 err := r.Err()
306 if err != nil {
307 return err
308 }
309
310 err = r.Visit(func(info *resource.Info, err error) error {
311 if err != nil {
312 return err
313 }
314
315 mapping := info.ResourceMapping()
316 if err := o.CanBeExposed(mapping.GroupVersionKind.GroupKind()); err != nil {
317 return err
318 }
319
320 name := info.Name
321 if len(name) > validation.DNS1035LabelMaxLength {
322 name = name[:validation.DNS1035LabelMaxLength]
323 }
324 o.DefaultName = name
325
326 // For objects that need a pod selector, derive it from the exposed object in case a user
327 // didn't explicitly specify one via --selector
328 if len(o.Selector) == 0 {
329 s, err := o.MapBasedSelectorForObject(info.Object)
330 if err != nil {
331 return fmt.Errorf("couldn't retrieve selectors via --selector flag or introspection: %v", err)
332 }
333 o.Selector = s
334 }
335
336 isHeadlessService := o.ClusterIP == "None"
337
338 // For objects that need a port, derive it from the exposed object in case a user
339 // didn't explicitly specify one via --port
340 if len(o.Port) == 0 {
341 ports, err := o.PortsForObject(info.Object)
342 if err != nil {
343 return fmt.Errorf("couldn't find port via --port flag or introspection: %v", err)
344 }
345 switch len(ports) {
346 case 0:
347 if !isHeadlessService {
348 return fmt.Errorf("couldn't find port via --port flag or introspection")
349 }
350 case 1:
351 o.Port = ports[0]
352 default:
353 o.Ports = strings.Join(ports, ",")

Callers 1

NewCmdExposeServiceFunction · 0.80

Calls 12

createServiceMethod · 0.95
MakeLabelsFunction · 0.92
CreateOrUpdateAnnotationFunction · 0.92
DefaultJSONEncoderFunction · 0.92
makeProtocolsFunction · 0.85
NewOverriderMethod · 0.80
RESTMappingMethod · 0.80
ApplyMethod · 0.65
ClientForMappingMethod · 0.65
VisitMethod · 0.45
JoinMethod · 0.45
PrintObjMethod · 0.45

Tested by

no test coverage detected