| 360 | } |
| 361 | |
| 362 | func (cmd *OpenCmd) getService(client kubectl.Client, namespace, host string, getEndpoints bool) (string, int, *map[string]string, error) { |
| 363 | // Let user select service |
| 364 | serviceNameList := []string{} |
| 365 | serviceLabels := map[string]map[string]string{} |
| 366 | |
| 367 | serviceList, err := client.KubeClient().CoreV1().Services(namespace).List(context.TODO(), metav1.ListOptions{}) |
| 368 | if err != nil { |
| 369 | return "", 0, nil, errors.Wrap(err, "list services") |
| 370 | } |
| 371 | |
| 372 | for _, service := range serviceList.Items { |
| 373 | // We skip tiller-deploy, because usually you don't want to create an ingress for tiller |
| 374 | if service.Name == "tiller-deploy" { |
| 375 | continue |
| 376 | } |
| 377 | |
| 378 | if service.Spec.Type == v1.ServiceTypeClusterIP || service.Spec.Type == v1.ServiceTypeLoadBalancer { |
| 379 | if service.Spec.ClusterIP == "None" { |
| 380 | continue |
| 381 | } |
| 382 | |
| 383 | for _, ports := range service.Spec.Ports { |
| 384 | port := ports.Port |
| 385 | if getEndpoints { |
| 386 | port = ports.TargetPort.IntVal |
| 387 | } |
| 388 | |
| 389 | serviceNameList = append(serviceNameList, service.Name+":"+strconv.Itoa(int(port))) |
| 390 | } |
| 391 | |
| 392 | if getEndpoints { |
| 393 | serviceLabels[service.Name] = service.Spec.Selector |
| 394 | } else { |
| 395 | serviceLabels[service.Name] = service.Labels |
| 396 | } |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | serviceName := "" |
| 401 | servicePort := "" |
| 402 | |
| 403 | if len(serviceNameList) == 0 { |
| 404 | return "", 0, nil, errors.Errorf(message.ServiceNotFound, namespace) |
| 405 | } else if len(serviceNameList) == 1 { |
| 406 | splitted := strings.Split(serviceNameList[0], ":") |
| 407 | |
| 408 | serviceName = splitted[0] |
| 409 | servicePort = splitted[1] |
| 410 | } else { |
| 411 | servicePickerQuestion := "Select the service you want to open:" |
| 412 | if host != "" { |
| 413 | servicePickerQuestion = fmt.Sprintf("Select the service you want to make available on '%s':", ansi.Color(host, "white+b")) |
| 414 | } |
| 415 | |
| 416 | // Ask user which service |
| 417 | service, err := cmd.log.Question(&survey.QuestionOptions{ |
| 418 | Question: servicePickerQuestion, |
| 419 | Options: serviceNameList, |