-
Notifications
You must be signed in to change notification settings - Fork 1k
/
container_portforward.go
66 lines (54 loc) · 1.92 KB
/
container_portforward.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package server
import (
"errors"
"fmt"
"io"
"github.com/containers/storage/pkg/pools"
"github.com/cri-o/cri-o/internal/log"
"golang.org/x/net/context"
types "k8s.io/cri-api/pkg/apis/runtime/v1"
)
// PortForward prepares a streaming endpoint to forward ports from a PodSandbox.
func (s *Server) PortForward(ctx context.Context, req *types.PortForwardRequest) (*types.PortForwardResponse, error) {
resp, err := s.getPortForward(req)
if err != nil {
return nil, errors.New("unable to prepare portforward endpoint")
}
return resp, nil
}
func (s StreamService) PortForward(ctx context.Context, podSandboxID string, port int32, stream io.ReadWriteCloser) error {
ctx, span := log.StartSpan(ctx)
defer span.End()
// if we error in this function before Copying all of the content out of the stream,
// this stream will eventually get full, which causes leakages and can eventually brick CRI-O
// ref https://bugzilla.redhat.com/show_bug.cgi?id=1798193
emptyStreamOnError := true
defer func() {
if emptyStreamOnError && stream != nil {
go func() {
_, copyError := pools.Copy(io.Discard, stream)
log.Errorf(ctx, "Error closing port forward stream after other error: %v", copyError)
}()
}
}()
sandboxID, err := s.runtimeServer.PodIDIndex().Get(podSandboxID)
if err != nil {
return fmt.Errorf("PodSandbox with ID starting with %s not found: %w", podSandboxID, err)
}
sb := s.runtimeServer.GetSandbox(sandboxID)
if sb == nil {
return fmt.Errorf("could not find sandbox %s", podSandboxID)
}
if !sb.Ready(true) {
return fmt.Errorf("sandbox %s is not running", podSandboxID)
}
netNsPath := sb.NetNsPath()
if netNsPath == "" {
return fmt.Errorf(
"network namespace path of sandbox %s is empty", sb.ID(),
)
}
// defer responsibility of emptying stream to PortForwardContainer
emptyStreamOnError = false
return s.runtimeServer.Runtime().PortForwardContainer(ctx, sb.InfraContainer(), netNsPath, port, stream)
}