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

Commit 5d9d39d

Browse files
author
aiordache
committed
Add port-forwarding on compose up
Signed-off-by: aiordache <[email protected]>
1 parent d8db079 commit 5d9d39d

File tree

2 files changed

+12
-14
lines changed

2 files changed

+12
-14
lines changed

kube/client/client.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,8 @@ func (kc KubeClient) WaitForPodState(ctx context.Context, opts WaitForStatusOpti
246246
return nil
247247
}
248248

249-
func (kc KubeClient) MapPorts(ctx context.Context, opts PortMappingOptions) error {
250-
249+
//MapPortsToLocalhost runs a port-forwarder daemon process
250+
func (kc KubeClient) MapPortsToLocalhost(ctx context.Context, opts PortMappingOptions) error {
251251
stopChannel := make(chan struct{}, 1)
252252
readyChannel := make(chan struct{})
253253

@@ -260,19 +260,20 @@ func (kc KubeClient) MapPorts(ctx context.Context, opts PortMappingOptions) erro
260260
return err
261261
}
262262
eg.Go(func() error {
263+
ports := []string{}
264+
for _, p := range servicePorts {
265+
ports = append(ports, fmt.Sprintf("%d:%d", p.PublishedPort, p.TargetPort))
266+
}
263267

264-
req := kc.client.RESTClient().Post().Resource("pods").Namespace(kc.namespace).Name(pod.Name).SubResource("portforward") //fmt.Sprintf("service/%s", serviceName)).SubResource("portforward")
268+
req := kc.client.CoreV1().RESTClient().Post().
269+
Resource("pods").
270+
Name(pod.Name).
271+
Namespace(kc.namespace).
272+
SubResource("portforward")
265273
transport, upgrader, err := spdy.RoundTripperFor(kc.config)
266274
if err != nil {
267275
return err
268276
}
269-
270-
ports := []string{}
271-
for _, p := range servicePorts {
272-
ports = append(ports, fmt.Sprintf("%d:%d", p.PublishedPort, p.TargetPort))
273-
}
274-
//println(req.URL().String())
275-
//os.Exit(0)
276277
dialer := spdy.NewDialer(upgrader, &http.Client{Transport: transport}, "POST", req.URL())
277278
fw, err := portforward.New(dialer, ports, stopChannel, readyChannel, os.Stdout, os.Stderr)
278279
if err != nil {

kube/compose.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,15 +122,12 @@ func (s *composeService) Up(ctx context.Context, project *types.Project, options
122122
w.Event(progress.NewEvent(pod, state, message))
123123
},
124124
})
125-
//return err
126-
127125
if err != nil {
128126
return err
129127
}
130128

131129
// check if there is a port mapping
132130
services := map[string]client.Ports{}
133-
134131
for _, s := range project.Services {
135132
if len(s.Ports) > 0 {
136133
services[s.Name] = client.Ports{}
@@ -144,7 +141,7 @@ func (s *composeService) Up(ctx context.Context, project *types.Project, options
144141
}
145142
}
146143
if len(services) > 0 {
147-
return s.client.MapPorts(ctx, client.PortMappingOptions{
144+
return s.client.MapPortsToLocalhost(ctx, client.PortMappingOptions{
148145
ProjectName: project.Name,
149146
Services: services,
150147
})

0 commit comments

Comments
 (0)