Skip to content

Commit 855a002

Browse files
committed
Merge pull request #295 from danwinship/better-setuppod-errors
Return better errors from SetUpPod/TearDownPod
2 parents 5106c5a + 1cb07c7 commit 855a002

File tree

1 file changed

+39
-7
lines changed

1 file changed

+39
-7
lines changed

plugins/osdn/ovs/plugin.go

+39-7
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ package ovs
33
import (
44
"fmt"
55
"net"
6+
"os/exec"
67
"strconv"
8+
"strings"
79

810
"github.com/golang/glog"
911

@@ -14,7 +16,6 @@ import (
1416
"k8s.io/kubernetes/pkg/api/resource"
1517
kubeletTypes "k8s.io/kubernetes/pkg/kubelet/container"
1618
knetwork "k8s.io/kubernetes/pkg/kubelet/network"
17-
utilexec "k8s.io/kubernetes/pkg/util/exec"
1819
)
1920

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

179+
func isScriptError(err error) bool {
180+
_, ok := err.(*exec.ExitError)
181+
return ok
182+
}
183+
184+
// Get the last command (which is prefixed with "+" because of "set -x") and its output
185+
func getScriptError(output []byte) string {
186+
lines := strings.Split(string(output), "\n")
187+
for n := len(lines) - 1; n >= 0; n-- {
188+
if strings.HasPrefix(lines[n], "+") {
189+
return strings.Join(lines[n:], "\n")
190+
}
191+
}
192+
return string(output)
193+
}
194+
178195
func (plugin *ovsPlugin) SetUpPod(namespace string, name string, id kubeletTypes.DockerID) error {
179196
err := plugin.WaitForPodNetworkReady()
180197
if err != nil {
@@ -210,16 +227,26 @@ func (plugin *ovsPlugin) SetUpPod(namespace string, name string, id kubeletTypes
210227
return err
211228
}
212229

213-
out, err := utilexec.New().Command(plugin.getExecutable(), setUpCmd, string(id), vnidstr, ingressStr, egressStr, fmt.Sprintf("%t", macvlan)).CombinedOutput()
230+
out, err := exec.Command(plugin.getExecutable(), setUpCmd, string(id), vnidstr, ingressStr, egressStr, fmt.Sprintf("%t", macvlan)).CombinedOutput()
214231
glog.V(5).Infof("SetUpPod network plugin output: %s, %v", string(out), err)
215-
return err
232+
233+
if isScriptError(err) {
234+
return fmt.Errorf("Error running network setup script: %s", getScriptError(out))
235+
} else {
236+
return err
237+
}
216238
}
217239

218240
func (plugin *ovsPlugin) TearDownPod(namespace string, name string, id kubeletTypes.DockerID) error {
219241
// The script's teardown functionality doesn't need the VNID
220-
out, err := utilexec.New().Command(plugin.getExecutable(), tearDownCmd, string(id), "-1", "-1", "-1").CombinedOutput()
242+
out, err := exec.Command(plugin.getExecutable(), tearDownCmd, string(id), "-1", "-1", "-1").CombinedOutput()
221243
glog.V(5).Infof("TearDownPod network plugin output: %s, %v", string(out), err)
222-
return err
244+
245+
if isScriptError(err) {
246+
return fmt.Errorf("Error running network teardown script: %s", getScriptError(out))
247+
} else {
248+
return err
249+
}
223250
}
224251

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

235-
out, err := utilexec.New().Command(plugin.getExecutable(), updateCmd, string(id), vnidstr).CombinedOutput()
262+
out, err := exec.Command(plugin.getExecutable(), updateCmd, string(id), vnidstr).CombinedOutput()
236263
glog.V(5).Infof("UpdatePod network plugin output: %s, %v", string(out), err)
237-
return err
264+
265+
if isScriptError(err) {
266+
return fmt.Errorf("Error running network update script: %s", getScriptError(out))
267+
} else {
268+
return err
269+
}
238270
}
239271

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

0 commit comments

Comments
 (0)