Skip to content
This repository was archived by the owner on Jul 25, 2019. It is now read-only.

Return better errors from SetUpPod/TearDownPod #295

Merged
merged 1 commit into from
Apr 15, 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
46 changes: 39 additions & 7 deletions plugins/osdn/ovs/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package ovs
import (
"fmt"
"net"
"os/exec"
"strconv"
"strings"

"github.com/golang/glog"

Expand All @@ -14,7 +16,6 @@ import (
"k8s.io/kubernetes/pkg/api/resource"
kubeletTypes "k8s.io/kubernetes/pkg/kubelet/container"
knetwork "k8s.io/kubernetes/pkg/kubelet/network"
utilexec "k8s.io/kubernetes/pkg/util/exec"
)

type ovsPlugin struct {
Expand Down Expand Up @@ -175,6 +176,22 @@ func wantsMacvlan(pod *kapi.Pod) (bool, error) {
return false, fmt.Errorf("Pod has 'pod.network.openshift.io/assign-macvlan' annotation but is not privileged")
}

func isScriptError(err error) bool {
_, ok := err.(*exec.ExitError)
return ok
}

// Get the last command (which is prefixed with "+" because of "set -x") and its output
func getScriptError(output []byte) string {
lines := strings.Split(string(output), "\n")
for n := len(lines) - 1; n >= 0; n-- {
if strings.HasPrefix(lines[n], "+") {
return strings.Join(lines[n:], "\n")
}
}
return string(output)
}

func (plugin *ovsPlugin) SetUpPod(namespace string, name string, id kubeletTypes.DockerID) error {
err := plugin.WaitForPodNetworkReady()
if err != nil {
Expand Down Expand Up @@ -210,16 +227,26 @@ func (plugin *ovsPlugin) SetUpPod(namespace string, name string, id kubeletTypes
return err
}

out, err := utilexec.New().Command(plugin.getExecutable(), setUpCmd, string(id), vnidstr, ingressStr, egressStr, fmt.Sprintf("%t", macvlan)).CombinedOutput()
out, err := exec.Command(plugin.getExecutable(), setUpCmd, string(id), vnidstr, ingressStr, egressStr, fmt.Sprintf("%t", macvlan)).CombinedOutput()
glog.V(5).Infof("SetUpPod network plugin output: %s, %v", string(out), err)
return err

if isScriptError(err) {
return fmt.Errorf("Error running network setup script: %s", getScriptError(out))

Choose a reason for hiding this comment

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

Can you also include pod name and namespace in the error msg? I think that will be useful.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

kubernetes adds that when it logs the error:

message := fmt.Sprintf("Failed to setup network for pod %q using network plugins %q: %v; Skipping pod", format.Pod(pod), dm.networkPlugin.Name(), err)

where format.Pod(pod) outputs fmt.Sprintf("%s_%s(%s)", pod.Name, pod.Namespace, pod.UID)

} else {
return err
}
}

func (plugin *ovsPlugin) TearDownPod(namespace string, name string, id kubeletTypes.DockerID) error {
// The script's teardown functionality doesn't need the VNID
out, err := utilexec.New().Command(plugin.getExecutable(), tearDownCmd, string(id), "-1", "-1", "-1").CombinedOutput()
out, err := exec.Command(plugin.getExecutable(), tearDownCmd, string(id), "-1", "-1", "-1").CombinedOutput()
glog.V(5).Infof("TearDownPod network plugin output: %s, %v", string(out), err)
return err

if isScriptError(err) {
return fmt.Errorf("Error running network teardown script: %s", getScriptError(out))
} else {
return err
}
}

func (plugin *ovsPlugin) Status(namespace string, name string, id kubeletTypes.DockerID) (*knetwork.PodNetworkStatus, error) {
Expand All @@ -232,9 +259,14 @@ func (plugin *ovsPlugin) UpdatePod(namespace string, name string, id kubeletType
return err
}

out, err := utilexec.New().Command(plugin.getExecutable(), updateCmd, string(id), vnidstr).CombinedOutput()
out, err := exec.Command(plugin.getExecutable(), updateCmd, string(id), vnidstr).CombinedOutput()
glog.V(5).Infof("UpdatePod network plugin output: %s, %v", string(out), err)
return err

if isScriptError(err) {
return fmt.Errorf("Error running network update script: %s", getScriptError(out))
} else {
return err
}
}

func (plugin *ovsPlugin) Event(name string, details map[string]interface{}) {
Expand Down