Skip to content

Commit 190efe7

Browse files
authored
Merge pull request #11 from pusher/fix-template-input
Check status exists before trying to remove it
2 parents d59f7af + 466f6ac commit 190efe7

File tree

2 files changed

+42
-6
lines changed

2 files changed

+42
-6
lines changed

pkg/quack/quack.go

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func (ah *AdmissionHook) Admit(req *admissionv1beta1.AdmissionRequest) *admissio
106106

107107
templateInput, err := getTemplateInput(req.Object.Raw)
108108
if err != nil {
109-
return errorResponse(resp, "")
109+
return errorResponse(resp, "Error creating template input: %v", err)
110110
}
111111
// Run Templating
112112
glog.V(6).Infof("Input for %s: %s", requestName, templateInput)
@@ -194,12 +194,18 @@ func getTemplateInput(data []byte) ([]byte, error) {
194194
}
195195

196196
// We should not modify the status of objects
197-
patch := []byte(fmt.Sprintf(`[
198-
{"op": "remove", "path": "/status"}
199-
]`))
200-
data, err = applyPatch(data, patch)
197+
hasStatus, err := requestHasStatus(data)
201198
if err != nil {
202-
return nil, fmt.Errorf("error removing status: %v", err)
199+
return nil, fmt.Errorf("error reading object status: %v", err)
200+
}
201+
if hasStatus {
202+
patch := []byte(fmt.Sprintf(`[
203+
{"op": "remove", "path": "/status"}
204+
]`))
205+
data, err = applyPatch(data, patch)
206+
if err != nil {
207+
return nil, fmt.Errorf("error removing status: %v", err)
208+
}
203209
}
204210

205211
for annotation := range objectMeta.Annotations {
@@ -252,6 +258,17 @@ func getObjectMeta(raw []byte) (metav1.ObjectMeta, error) {
252258
return requestMeta.ObjectMeta, nil
253259
}
254260

261+
func requestHasStatus(raw []byte) (bool, error) {
262+
requestStatus := struct {
263+
Status map[string]interface{} `json:"status"`
264+
}{}
265+
err := json.Unmarshal(raw, &requestStatus)
266+
if err != nil {
267+
return false, fmt.Errorf("failed to unmarshal input: %v", err)
268+
}
269+
return requestStatus.Status != nil, nil
270+
}
271+
255272
func applyPatch(data, patchBytes []byte) ([]byte, error) {
256273
patch, err := mergepatch.DecodePatch(patchBytes)
257274
if err != nil {

pkg/quack/quack_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,3 +342,22 @@ func TestGetDelims(t *testing.T) {
342342
assert.Equal(t, delimiters{}, withEmptyDelimeters, "Object with empty delimiter should return empty delimiters")
343343
assert.NotNil(t, emptyErr, "Object with empty left delimiter should return error")
344344
}
345+
346+
func TestRequestHasStatus(t *testing.T) {
347+
withStatus := `{
348+
"status": {
349+
"foo": "bar",
350+
"baz": 3
351+
}
352+
}`
353+
hasStatus, err := requestHasStatus([]byte(withStatus))
354+
assert.Equal(t, nil, err, "Error should not have occurred")
355+
assert.Equal(t, true, hasStatus, "Expected object with status to return true")
356+
357+
withoutStatus := `{
358+
"foo": "bar"
359+
}`
360+
hasStatus, err = requestHasStatus([]byte(withoutStatus))
361+
assert.Equal(t, nil, err, "Error should not have occurred")
362+
assert.Equal(t, false, hasStatus, "Expected object without status to return false")
363+
}

0 commit comments

Comments
 (0)