From 5d9d39dabd8e4b49a34a7f76027c81cb57a866c8 Mon Sep 17 00:00:00 2001 From: aiordache Date: Mon, 26 Apr 2021 11:47:35 +0200 Subject: [PATCH] Add port-forwarding on `compose up` Signed-off-by: aiordache --- kube/client/client.go | 23 ++++++++++++----------- kube/compose.go | 5 +---- 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/kube/client/client.go b/kube/client/client.go index 40f7ee73..24b115b6 100644 --- a/kube/client/client.go +++ b/kube/client/client.go @@ -246,8 +246,8 @@ func (kc KubeClient) WaitForPodState(ctx context.Context, opts WaitForStatusOpti return nil } -func (kc KubeClient) MapPorts(ctx context.Context, opts PortMappingOptions) error { - +//MapPortsToLocalhost runs a port-forwarder daemon process +func (kc KubeClient) MapPortsToLocalhost(ctx context.Context, opts PortMappingOptions) error { stopChannel := make(chan struct{}, 1) readyChannel := make(chan struct{}) @@ -260,19 +260,20 @@ func (kc KubeClient) MapPorts(ctx context.Context, opts PortMappingOptions) erro return err } eg.Go(func() error { - - req := kc.client.RESTClient().Post().Resource("pods").Namespace(kc.namespace).Name(pod.Name).SubResource("portforward") //fmt.Sprintf("service/%s", serviceName)).SubResource("portforward") - transport, upgrader, err := spdy.RoundTripperFor(kc.config) - if err != nil { - return err - } - ports := []string{} for _, p := range servicePorts { ports = append(ports, fmt.Sprintf("%d:%d", p.PublishedPort, p.TargetPort)) } - //println(req.URL().String()) - //os.Exit(0) + + req := kc.client.CoreV1().RESTClient().Post(). + Resource("pods"). + Name(pod.Name). + Namespace(kc.namespace). + SubResource("portforward") + transport, upgrader, err := spdy.RoundTripperFor(kc.config) + if err != nil { + return err + } dialer := spdy.NewDialer(upgrader, &http.Client{Transport: transport}, "POST", req.URL()) fw, err := portforward.New(dialer, ports, stopChannel, readyChannel, os.Stdout, os.Stderr) if err != nil { diff --git a/kube/compose.go b/kube/compose.go index 61fe57e3..a4a12430 100644 --- a/kube/compose.go +++ b/kube/compose.go @@ -122,15 +122,12 @@ func (s *composeService) Up(ctx context.Context, project *types.Project, options w.Event(progress.NewEvent(pod, state, message)) }, }) - //return err - 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{} @@ -144,7 +141,7 @@ func (s *composeService) Up(ctx context.Context, project *types.Project, options } } if len(services) > 0 { - return s.client.MapPorts(ctx, client.PortMappingOptions{ + return s.client.MapPortsToLocalhost(ctx, client.PortMappingOptions{ ProjectName: project.Name, Services: services, })