Add port-forwarding on `compose up`

Signed-off-by: aiordache <anca.iordache@docker.com>
This commit is contained in:
aiordache 2021-04-26 11:47:35 +02:00
Родитель d8db079af3
Коммит 5d9d39dabd
2 изменённых файлов: 13 добавлений и 15 удалений

Просмотреть файл

@ -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 {

Просмотреть файл

@ -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,
})