(ctx devspacecontext.Context, domain string)
| 283 | } |
| 284 | |
| 285 | func (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() |
no test coverage detected