Skip to content

Commit 6594ffd

Browse files
committed
Issue1317 - Added error logging to webhook controller
1 parent 2de5dfc commit 6594ffd

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

pkg/build/webhook/controller.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"net/http"
66
"strings"
77

8+
"github.com/golang/glog"
9+
810
kapi "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
911
"github.com/openshift/origin/pkg/build/api"
1012
buildclient "github.com/openshift/origin/pkg/build/client"
@@ -54,23 +56,27 @@ func NewController(buildConfigGetter buildclient.BuildConfigGetter, buildCreator
5456
func (c *controller) ServeHTTP(w http.ResponseWriter, req *http.Request) {
5557
uv, err := parseURL(req)
5658
if err != nil {
59+
glog.V(4).Infof("Failed parsing request URL: %v", err)
5760
notFound(w, err.Error())
5861
return
5962
}
6063

6164
buildCfg, err := c.buildConfigGetter.Get(uv.namespace, uv.buildConfigName)
6265
if err != nil {
66+
glog.V(4).Infof("Failed getting BuildConfig: %v", err)
6367
badRequest(w, err.Error())
6468
return
6569
}
6670

6771
plugin, ok := c.plugins[uv.plugin]
6872
if !ok {
73+
glog.V(4).Infof("Plugin %s not found", uv.plugin)
6974
notFound(w, "Plugin ", uv.plugin, " not found")
7075
return
7176
}
7277
revision, proceed, err := plugin.Extract(buildCfg, uv.secret, uv.path, req)
7378
if err != nil {
79+
glog.V(4).Infof("Failed extracting information from webhook: %v", err)
7480
badRequest(w, err.Error())
7581
return
7682
}
@@ -79,10 +85,12 @@ func (c *controller) ServeHTTP(w http.ResponseWriter, req *http.Request) {
7985
}
8086
build, err := buildutil.GenerateBuildWithImageTag(buildCfg, revision, c.imageRepoGetter)
8187
if err != nil {
88+
glog.V(4).Infof("Failed generating new build: %v", err)
8289
badRequest(w, err.Error())
8390
return
8491
}
8592
if err := c.buildCreator.Create(uv.namespace, build); err != nil {
93+
glog.V(4).Infof("Failed creating new build: %v", err)
8694
badRequest(w, err.Error())
8795
}
8896
}

pkg/build/webhook/generic/generic.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"strings"
99

1010
"github.com/golang/glog"
11+
1112
"github.com/openshift/origin/pkg/build/api"
1213
"github.com/openshift/origin/pkg/build/webhook"
1314
)
@@ -40,11 +41,12 @@ func (p *WebHookPlugin) Extract(buildCfg *api.BuildConfig, secret, path string,
4041
return nil, false, err
4142
}
4243
if len(body) == 0 {
43-
return revision, true, nil
44+
return nil, true, nil
4445
}
4546
var data api.GenericWebHookEvent
4647
if err = json.Unmarshal(body, &data); err != nil {
47-
return nil, false, err
48+
glog.V(4).Infof("Error unmarshaling json %v, but continuing", err)
49+
return nil, true, nil
4850
}
4951
if !webhook.GitRefMatches(data.Git.Ref, buildCfg.Parameters.Source.Git.Ref) {
5052
glog.V(2).Infof("Skipping build for '%s'. Branch reference from '%s' does not match configuration", buildCfg, data)

pkg/build/webhook/generic/generic_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package generic
22

33
import (
44
"bytes"
5+
"io"
56
"io/ioutil"
67
"net/http"
78
"strings"
@@ -191,3 +192,37 @@ func TestExtractWithGitPayload(t *testing.T) {
191192
t.Error("Expected the 'revision' return value to not be nil")
192193
}
193194
}
195+
196+
type errJSON struct{}
197+
198+
func (*errJSON) Read(p []byte) (n int, err error) {
199+
p = []byte("{")
200+
return len(p), io.EOF
201+
}
202+
203+
func TestExtractWithUnmarshalError(t *testing.T) {
204+
req, _ := http.NewRequest("POST", "http://someurl.com", &errJSON{})
205+
req.Header.Add("User-Agent", "Some User Agent")
206+
req.Header.Add("Content-Type", "application/json")
207+
buildConfig := &api.BuildConfig{
208+
Triggers: []api.BuildTriggerPolicy{
209+
{
210+
Type: api.GenericWebHookBuildTriggerType,
211+
GenericWebHook: &api.WebHookTrigger{
212+
Secret: "secret100",
213+
},
214+
},
215+
},
216+
}
217+
plugin := New()
218+
revision, proceed, err := plugin.Extract(buildConfig, "secret100", "", req)
219+
if err != nil {
220+
t.Errorf("Expected to be able to trigger a build without a payload error: %s", err)
221+
}
222+
if !proceed {
223+
t.Error("Expected 'proceed' return value to be 'true'")
224+
}
225+
if revision != nil {
226+
t.Error("Expected the 'revision' return value to be nil")
227+
}
228+
}

0 commit comments

Comments
 (0)