MCPcopy Index your code
hub / github.com/devspace-sh/devspace / openLocal

Method openLocal

cmd/open.go:285–360  ·  view source on GitHub ↗
(ctx devspacecontext.Context, domain string)

Source from the content-addressed store, hash-verified

283}
284
285func (cmd *OpenCmd) openLocal(ctx devspacecontext.Context, domain string) error {
286 _, servicePort, serviceLabels, err := cmd.getService(ctx.KubeClient(), ctx.KubeClient().Namespace(), domain, true)
287 if err != nil {
288 return errors.Errorf("Unable to get service: %v", err)
289 }
290
291 localPort := servicePort
292 if cmd.Port != 0 {
293 localPort = cmd.Port
294 } else {
295 if localPort < 1024 {
296 localPort = localPort + 8000
297 }
298
299 // Check if port is open
300 portOpen, _ := port.IsAvailable(fmt.Sprintf(":%d", localPort))
301 for i := 0; i < 10 && !portOpen; i++ {
302 localPort++
303 portOpen, _ = port.IsAvailable(fmt.Sprintf(":%d", localPort))
304 }
305 }
306
307 domain = "http://localhost:" + strconv.Itoa(localPort)
308 portMappings := []*latest.PortMapping{
309 {
310 Port: fmt.Sprintf("%d:%d", localPort, servicePort),
311 },
312 }
313
314 labelSelector := map[string]string{}
315 for key, value := range *serviceLabels {
316 labelSelector[key] = value
317 }
318
319 devPod := &latest.DevPod{
320 Name: "open",
321 LabelSelector: labelSelector,
322 Ports: portMappings,
323 }
324 fakeConfig := &latest.Config{
325 Dev: map[string]*latest.DevPod{
326 "open": devPod,
327 },
328 }
329
330 devSpaceConfig := config.Ensure(config.NewConfig(nil, nil, fakeConfig, nil, nil, nil, constants.DefaultConfigPath))
331 ctx = ctx.WithConfig(devSpaceConfig)
332 options := targetselector.NewEmptyOptions().
333 WithLabelSelector(labels.Set(labelSelector).String()).
334 WithWaitingStrategy(targetselector.NewUntilNewestRunningWaitingStrategy(time.Second))
335
336 // start port-forwarding for localhost access
337 ctx, t := ctx.WithNewTomb()
338 <-t.NotifyGo(func() error {
339 return portforwarding.StartPortForwarding(ctx, devPod, targetselector.NewTargetSelector(options), t)
340 })
341 if !t.Alive() {
342 return t.Err()

Callers 1

RunOpenMethod · 0.95

Calls 15

getServiceMethod · 0.95
IsAvailableFunction · 0.92
EnsureFunction · 0.92
NewConfigFunction · 0.92
NewEmptyOptionsFunction · 0.92
StartPortForwardingFunction · 0.92
NewTargetSelectorFunction · 0.92
WithWaitingStrategyMethod · 0.80
WithLabelSelectorMethod · 0.80
NotifyGoMethod · 0.80
AliveMethod · 0.80

Tested by

no test coverage detected