@@ -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+
255272func applyPatch (data , patchBytes []byte ) ([]byte , error ) {
256273 patch , err := mergepatch .DecodePatch (patchBytes )
257274 if err != nil {
0 commit comments