Browse Source

Add port mapping to `compose ps`

Signed-off-by: aiordache <[email protected]>
aiordache 4 years ago
parent
commit
0d25709ada
4 changed files with 40 additions and 33 deletions
  1. 35 4
      kube/client/client.go
  2. 2 0
      kube/client/utils.go
  3. 1 27
      kube/compose.go
  4. 2 2
      kube/resources/kube.go

+ 35 - 4
kube/client/client.go

@@ -24,6 +24,7 @@ import (
 	"io"
 	"net/http"
 	"os"
+	"strings"
 	"time"
 
 	"github.com/docker/compose-cli/api/compose"
@@ -73,7 +74,7 @@ func NewKubeClient(config genericclioptions.RESTClientGetter) (*KubeClient, erro
 	}, nil
 }
 
-// GetContainers get containers for a given compose project
+// GetPod retrieves a service pod
 func (kc KubeClient) GetPod(ctx context.Context, projectName, serviceName string) (*corev1.Pod, error) {
 	pods, err := kc.client.CoreV1().Pods(kc.namespace).List(ctx, metav1.ListOptions{
 		LabelSelector: fmt.Sprintf("%s=%s", compose.ProjectTag, projectName),
@@ -160,9 +161,39 @@ func (kc KubeClient) GetContainers(ctx context.Context, projectName string, all
 	if err != nil {
 		return nil, err
 	}
+	services := map[string][]compose.PortPublisher{}
 	result := []compose.ContainerSummary{}
 	for _, pod := range pods.Items {
-		result = append(result, podToContainerSummary(pod))
+		summary := podToContainerSummary(pod)
+		serviceName := pod.GetObjectMeta().GetLabels()[compose.ServiceTag]
+		ports, ok := services[serviceName]
+		if !ok {
+			s, err := kc.client.CoreV1().Services(kc.namespace).Get(ctx, serviceName, metav1.GetOptions{})
+			if err != nil {
+				if !strings.Contains(err.Error(), "not found") {
+					return nil, err
+				}
+				result = append(result, summary)
+				continue
+			}
+			ports = []compose.PortPublisher{}
+			if s != nil {
+				if s.Spec.Type == corev1.ServiceTypeLoadBalancer {
+					if len(s.Status.LoadBalancer.Ingress) > 0 {
+						port := compose.PortPublisher{URL: s.Status.LoadBalancer.Ingress[0].IP}
+						if len(s.Spec.Ports) > 0 {
+							port.URL = fmt.Sprintf("%s:%d", port.URL, s.Spec.Ports[0].Port)
+							port.TargetPort = s.Spec.Ports[0].TargetPort.IntValue()
+							port.Protocol = string(s.Spec.Ports[0].Protocol)
+						}
+						ports = append(ports, port)
+					}
+				}
+			}
+			services[serviceName] = ports
+		}
+		summary.Publishers = ports
+		result = append(result, summary)
 	}
 
 	return result, nil
@@ -253,8 +284,8 @@ func (kc KubeClient) MapPortsToLocalhost(ctx context.Context, opts PortMappingOp
 
 	eg, ctx := errgroup.WithContext(ctx)
 	for serviceName, servicePorts := range opts.Services {
-		serviceName = serviceName
-		servicePorts = servicePorts
+		serviceName := serviceName
+		servicePorts := servicePorts
 		pod, err := kc.GetPod(ctx, opts.ProjectName, serviceName)
 		if err != nil {
 			return err

+ 2 - 0
kube/client/utils.go

@@ -99,8 +99,10 @@ type WaitForStatusOptions struct {
 	Log         LogFunc
 }
 
+// Ports holds published ports data
 type Ports []compose.PortPublisher
 
+// PortMappingOptions holds the port mapping for project services
 type PortMappingOptions struct {
 	ProjectName string
 	Services    map[string]Ports

+ 1 - 27
kube/compose.go

@@ -110,7 +110,7 @@ func (s *composeService) Up(ctx context.Context, project *types.Project, options
 
 	w.Event(progress.NewEvent(eventName, progress.Done, ""))
 
-	err = s.client.WaitForPodState(ctx, client.WaitForStatusOptions{
+	return s.client.WaitForPodState(ctx, client.WaitForStatusOptions{
 		ProjectName: project.Name,
 		Services:    project.ServiceNames(),
 		Status:      compose.RUNNING,
@@ -122,32 +122,6 @@ func (s *composeService) Up(ctx context.Context, project *types.Project, options
 			w.Event(progress.NewEvent(pod, state, message))
 		},
 	})
-	if err != nil {
-		return err
-	}
-
-	// check if there is a port mapping
-	services := map[string]client.Ports{}
-	for _, s := range project.Services {
-		if len(s.Ports) > 0 {
-			services[s.Name] = client.Ports{}
-			for _, p := range s.Ports {
-				services[s.Name] = append(services[s.Name], compose.PortPublisher{
-					TargetPort:    int(p.Target),
-					PublishedPort: int(p.Published),
-					Protocol:      p.Protocol,
-				})
-			}
-		}
-	}
-	if len(services) > 0 {
-		return s.client.MapPortsToLocalhost(ctx, client.PortMappingOptions{
-			ProjectName: project.Name,
-			Services:    services,
-		})
-	}
-	return nil
-
 }
 
 // Down executes the equivalent to a `compose down`

+ 2 - 2
kube/resources/kube.go

@@ -94,8 +94,8 @@ func mapToService(project *types.Project, service types.ServiceConfig) *core.Ser
 		}
 		ports = append(ports,
 			core.ServicePort{
-				Name:       fmt.Sprintf("%d-%s", p.Target, strings.ToLower(p.Protocol)),
-				Port:       int32(p.Target),
+				Name:       fmt.Sprintf("%d-%s", p.Published, strings.ToLower(p.Protocol)),
+				Port:       int32(p.Published),
 				TargetPort: intstr.FromInt(int(p.Target)),
 				Protocol:   toProtocol(p.Protocol),
 			})