Skip to content
This repository was archived by the owner on Nov 27, 2023. It is now read-only.

Commit 0d25709

Browse files
author
aiordache
committed
Add port mapping to compose ps
Signed-off-by: aiordache <[email protected]>
1 parent 5d9d39d commit 0d25709

File tree

4 files changed

+40
-33
lines changed

4 files changed

+40
-33
lines changed

kube/client/client.go

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"io"
2525
"net/http"
2626
"os"
27+
"strings"
2728
"time"
2829

2930
"github.com/docker/compose-cli/api/compose"
@@ -73,7 +74,7 @@ func NewKubeClient(config genericclioptions.RESTClientGetter) (*KubeClient, erro
7374
}, nil
7475
}
7576

76-
// GetContainers get containers for a given compose project
77+
// GetPod retrieves a service pod
7778
func (kc KubeClient) GetPod(ctx context.Context, projectName, serviceName string) (*corev1.Pod, error) {
7879
pods, err := kc.client.CoreV1().Pods(kc.namespace).List(ctx, metav1.ListOptions{
7980
LabelSelector: fmt.Sprintf("%s=%s", compose.ProjectTag, projectName),
@@ -160,9 +161,39 @@ func (kc KubeClient) GetContainers(ctx context.Context, projectName string, all
160161
if err != nil {
161162
return nil, err
162163
}
164+
services := map[string][]compose.PortPublisher{}
163165
result := []compose.ContainerSummary{}
164166
for _, pod := range pods.Items {
165-
result = append(result, podToContainerSummary(pod))
167+
summary := podToContainerSummary(pod)
168+
serviceName := pod.GetObjectMeta().GetLabels()[compose.ServiceTag]
169+
ports, ok := services[serviceName]
170+
if !ok {
171+
s, err := kc.client.CoreV1().Services(kc.namespace).Get(ctx, serviceName, metav1.GetOptions{})
172+
if err != nil {
173+
if !strings.Contains(err.Error(), "not found") {
174+
return nil, err
175+
}
176+
result = append(result, summary)
177+
continue
178+
}
179+
ports = []compose.PortPublisher{}
180+
if s != nil {
181+
if s.Spec.Type == corev1.ServiceTypeLoadBalancer {
182+
if len(s.Status.LoadBalancer.Ingress) > 0 {
183+
port := compose.PortPublisher{URL: s.Status.LoadBalancer.Ingress[0].IP}
184+
if len(s.Spec.Ports) > 0 {
185+
port.URL = fmt.Sprintf("%s:%d", port.URL, s.Spec.Ports[0].Port)
186+
port.TargetPort = s.Spec.Ports[0].TargetPort.IntValue()
187+
port.Protocol = string(s.Spec.Ports[0].Protocol)
188+
}
189+
ports = append(ports, port)
190+
}
191+
}
192+
}
193+
services[serviceName] = ports
194+
}
195+
summary.Publishers = ports
196+
result = append(result, summary)
166197
}
167198

168199
return result, nil
@@ -253,8 +284,8 @@ func (kc KubeClient) MapPortsToLocalhost(ctx context.Context, opts PortMappingOp
253284

254285
eg, ctx := errgroup.WithContext(ctx)
255286
for serviceName, servicePorts := range opts.Services {
256-
serviceName = serviceName
257-
servicePorts = servicePorts
287+
serviceName := serviceName
288+
servicePorts := servicePorts
258289
pod, err := kc.GetPod(ctx, opts.ProjectName, serviceName)
259290
if err != nil {
260291
return err

kube/client/utils.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,10 @@ type WaitForStatusOptions struct {
9999
Log LogFunc
100100
}
101101

102+
// Ports holds published ports data
102103
type Ports []compose.PortPublisher
103104

105+
// PortMappingOptions holds the port mapping for project services
104106
type PortMappingOptions struct {
105107
ProjectName string
106108
Services map[string]Ports

kube/compose.go

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ func (s *composeService) Up(ctx context.Context, project *types.Project, options
110110

111111
w.Event(progress.NewEvent(eventName, progress.Done, ""))
112112

113-
err = s.client.WaitForPodState(ctx, client.WaitForStatusOptions{
113+
return s.client.WaitForPodState(ctx, client.WaitForStatusOptions{
114114
ProjectName: project.Name,
115115
Services: project.ServiceNames(),
116116
Status: compose.RUNNING,
@@ -122,32 +122,6 @@ func (s *composeService) Up(ctx context.Context, project *types.Project, options
122122
w.Event(progress.NewEvent(pod, state, message))
123123
},
124124
})
125-
if err != nil {
126-
return err
127-
}
128-
129-
// check if there is a port mapping
130-
services := map[string]client.Ports{}
131-
for _, s := range project.Services {
132-
if len(s.Ports) > 0 {
133-
services[s.Name] = client.Ports{}
134-
for _, p := range s.Ports {
135-
services[s.Name] = append(services[s.Name], compose.PortPublisher{
136-
TargetPort: int(p.Target),
137-
PublishedPort: int(p.Published),
138-
Protocol: p.Protocol,
139-
})
140-
}
141-
}
142-
}
143-
if len(services) > 0 {
144-
return s.client.MapPortsToLocalhost(ctx, client.PortMappingOptions{
145-
ProjectName: project.Name,
146-
Services: services,
147-
})
148-
}
149-
return nil
150-
151125
}
152126

153127
// Down executes the equivalent to a `compose down`

kube/resources/kube.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ func mapToService(project *types.Project, service types.ServiceConfig) *core.Ser
9494
}
9595
ports = append(ports,
9696
core.ServicePort{
97-
Name: fmt.Sprintf("%d-%s", p.Target, strings.ToLower(p.Protocol)),
98-
Port: int32(p.Target),
97+
Name: fmt.Sprintf("%d-%s", p.Published, strings.ToLower(p.Protocol)),
98+
Port: int32(p.Published),
9999
TargetPort: intstr.FromInt(int(p.Target)),
100100
Protocol: toProtocol(p.Protocol),
101101
})

0 commit comments

Comments
 (0)