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

Commit 7fd3c6f

Browse files
author
aiordache
committed
Port forwarding attempt
Signed-off-by: aiordache <[email protected]>
1 parent bb25812 commit 7fd3c6f

File tree

3 files changed

+79
-1
lines changed

3 files changed

+79
-1
lines changed

kube/client/client.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import (
2222
"context"
2323
"fmt"
2424
"io"
25+
"net/http"
26+
"os"
2527
"time"
2628

2729
"github.com/docker/compose-cli/api/compose"
@@ -31,12 +33,16 @@ import (
3133
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3234
"k8s.io/cli-runtime/pkg/genericclioptions"
3335
"k8s.io/client-go/kubernetes"
36+
"k8s.io/client-go/rest"
37+
"k8s.io/client-go/tools/portforward"
38+
"k8s.io/client-go/transport/spdy"
3439
)
3540

3641
// KubeClient API to access kube objects
3742
type KubeClient struct {
3843
client *kubernetes.Clientset
3944
namespace string
45+
config *rest.Config
4046
}
4147

4248
// NewKubeClient new kubernetes client
@@ -59,6 +65,7 @@ func NewKubeClient(config genericclioptions.RESTClientGetter) (*KubeClient, erro
5965
return &KubeClient{
6066
client: clientset,
6167
namespace: namespace,
68+
config: restConfig,
6269
}, nil
6370
}
6471

@@ -161,3 +168,37 @@ func (kc KubeClient) WaitForPodState(ctx context.Context, opts WaitForStatusOpti
161168
}
162169
return nil
163170
}
171+
172+
func (kc KubeClient) MapPorts(ctx context.Context, opts PortMappingOptions) error {
173+
174+
stopChannel := make(chan struct{}, 1)
175+
readyChannel := make(chan struct{})
176+
177+
eg, ctx := errgroup.WithContext(ctx)
178+
for serviceName, servicePorts := range opts.Services {
179+
serviceName = serviceName
180+
servicePorts = servicePorts
181+
eg.Go(func() error {
182+
183+
req := kc.client.RESTClient().Post().Resource("services").Namespace(kc.namespace).Name(serviceName).SubResource("portforward")
184+
transport, upgrader, err := spdy.RoundTripperFor(kc.config)
185+
if err != nil {
186+
return err
187+
}
188+
189+
ports := []string{}
190+
for _, p := range servicePorts {
191+
ports = append(ports, fmt.Sprintf("%d:%d", p.PublishedPort, p.TargetPort))
192+
}
193+
//println(req.URL().String())
194+
//os.Exit(0)
195+
dialer := spdy.NewDialer(upgrader, &http.Client{Transport: transport}, "POST", req.URL())
196+
fw, err := portforward.New(dialer, ports, stopChannel, readyChannel, os.Stdout, os.Stderr)
197+
if err != nil {
198+
return err
199+
}
200+
return fw.ForwardPorts()
201+
})
202+
}
203+
return eg.Wait()
204+
}

kube/client/utils.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,10 @@ type WaitForStatusOptions struct {
9898
Timeout *time.Duration
9999
Log LogFunc
100100
}
101+
102+
type Ports []compose.PortPublisher
103+
104+
type PortMappingOptions struct {
105+
ProjectName string
106+
Services map[string]Ports
107+
}

kube/compose.go

Lines changed: 31 additions & 1 deletion
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-
return s.client.WaitForPodState(ctx, client.WaitForStatusOptions{
113+
err = s.client.WaitForPodState(ctx, client.WaitForStatusOptions{
114114
ProjectName: project.Name,
115115
Services: project.ServiceNames(),
116116
Status: compose.RUNNING,
@@ -122,6 +122,36 @@ 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+
/*
127+
if err != nil {
128+
return err
129+
}
130+
131+
// check if there is a port mapping
132+
services := map[string]client.Ports{}
133+
134+
for _, s := range project.Services {
135+
if len(s.Ports) > 0 {
136+
services[s.Name] = client.Ports{}
137+
for _, p := range s.Ports {
138+
services[s.Name] = append(services[s.Name], compose.PortPublisher{
139+
TargetPort: int(p.Target),
140+
PublishedPort: int(p.Published),
141+
Protocol: p.Protocol,
142+
})
143+
}
144+
}
145+
}
146+
if len(services) > 0 {
147+
return s.client.MapPorts(ctx, client.PortMappingOptions{
148+
ProjectName: project.Name,
149+
Services: services,
150+
})
151+
}
152+
return nil
153+
*/
154+
125155
}
126156

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

0 commit comments

Comments
 (0)