/
container_portforward.go
52 lines (42 loc) · 1.47 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
package server
import (
"fmt"
"io"
"time"
"github.com/cri-o/cri-o/oci"
"github.com/sirupsen/logrus"
"golang.org/x/net/context"
pb "k8s.io/kubernetes/pkg/kubelet/apis/cri/runtime/v1alpha2"
)
// PortForward prepares a streaming endpoint to forward ports from a PodSandbox.
func (s *Server) PortForward(ctx context.Context, req *pb.PortForwardRequest) (resp *pb.PortForwardResponse, err error) {
const operation = "port_forward"
defer func() {
recordOperation(operation, time.Now())
recordError(operation, err)
}()
logrus.Debugf("PortForwardRequest %+v", req)
resp, err = s.getPortForward(req)
if err != nil {
return nil, fmt.Errorf("unable to prepare portforward endpoint")
}
return resp, nil
}
func (ss streamService) PortForward(podSandboxID string, port int32, stream io.ReadWriteCloser) error {
sandboxID, err := ss.runtimeServer.PodIDIndex().Get(podSandboxID)
if err != nil {
return fmt.Errorf("PodSandbox with ID starting with %s not found: %v", podSandboxID, err)
}
c := ss.runtimeServer.GetSandboxContainer(sandboxID)
if c == nil {
return fmt.Errorf("could not find container for sandbox %q", podSandboxID)
}
if err := ss.runtimeServer.Runtime().UpdateContainerStatus(c); err != nil {
return err
}
cState := c.State()
if !(cState.Status == oci.ContainerStateRunning || cState.Status == oci.ContainerStateCreated) {
return fmt.Errorf("container is not created or running")
}
return ss.runtimeServer.Runtime().PortForwardContainer(c, port, stream)
}