Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add port forwarding for rkt with kvm stage1 #32126

Merged
merged 1 commit into from
Sep 22, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 26 additions & 6 deletions pkg/kubelet/rkt/rkt.go
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2101,7 +2101,6 @@ func (r *Runtime) ExecInContainer(containerID kubecontainer.ContainerID, cmd []s
// - should we support nsenter + socat in a container, running with elevated privs and --pid=host?
//
// TODO(yifan): Merge with the same function in dockertools.
// TODO(yifan): If the rkt is using lkvm as the stage1 image, then this function will fail.
func (r *Runtime) PortForward(pod *kubecontainer.Pod, port uint16, stream io.ReadWriteCloser) error {
glog.V(4).Infof("Rkt port forwarding in container.")

Expand All @@ -2122,20 +2121,41 @@ func (r *Runtime) PortForward(pod *kubecontainer.Pod, port uint16, stream io.Rea
}
return fmt.Errorf("more than one running rkt pod for the kubernetes pod [%s]", strings.Join(podlist, ", "))
}
listPod := listResp.Pods[0]

socatPath, lookupErr := exec.LookPath("socat")
if lookupErr != nil {
return fmt.Errorf("unable to do port forwarding: socat not found.")
}

args := []string{"-t", fmt.Sprintf("%d", listResp.Pods[0].Pid), "-n", socatPath, "-", fmt.Sprintf("TCP4:localhost:%d", port)}
// Check in config and in annotations if we're running kvm flavor
isKvm := strings.Contains(r.config.Stage1Image, "kvm")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This breaks in the reverse now; if you have stage1-image=kvm and then have the annotation for non-kvm, isKvm still remains true.

Copy link
Author

@jjlakis jjlakis Sep 13, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@euank I don't see any mistake, there's a loop below overriding isKvm if there's another image in annotations

for _, anno := range listPod.Annotations {
if anno.Key == k8sRktStage1NameAnno {
isKvm = strings.Contains(anno.Value, "kvm")
break
}
}

nsenterPath, lookupErr := exec.LookPath("nsenter")
if lookupErr != nil {
return fmt.Errorf("unable to do port forwarding: nsenter not found.")
var args []string
var fwCaller string
if isKvm {
podNetworks := listPod.GetNetworks()
if podNetworks == nil {
return fmt.Errorf("unable to get networks")
}
args = []string{"-", fmt.Sprintf("TCP4:%s:%d", podNetworks[0].Ipv4, port)}
fwCaller = socatPath
} else {
args = []string{"-t", fmt.Sprintf("%d", listPod.Pid), "-n", socatPath, "-", fmt.Sprintf("TCP4:localhost:%d", port)}
nsenterPath, lookupErr := exec.LookPath("nsenter")
if lookupErr != nil {
return fmt.Errorf("unable to do port forwarding: nsenter not found")
}
fwCaller = nsenterPath
}

command := exec.Command(nsenterPath, args...)
command := exec.Command(fwCaller, args...)
command.Stdout = stream

// If we use Stdin, command.Run() won't return until the goroutine that's copying
Expand Down