@@ -17,6 +17,8 @@ limitations under the License.
17
17
package admission
18
18
19
19
import (
20
+ "fmt"
21
+
20
22
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
21
23
"k8s.io/apimachinery/pkg/runtime"
22
24
"k8s.io/apimachinery/pkg/runtime/serializer"
@@ -36,11 +38,17 @@ func NewDecoder(scheme *runtime.Scheme) (*Decoder, error) {
36
38
37
39
// Decode decodes the inlined object in the AdmissionRequest into the passed-in runtime.Object.
38
40
// If you want decode the OldObject in the AdmissionRequest, use DecodeRaw.
41
+ // It errors out if req.Object.Raw is empty i.e. containing 0 raw bytes.
39
42
func (d * Decoder ) Decode (req Request , into runtime.Object ) error {
43
+ // we error out if rawObj is an empty object.
44
+ if len (req .Object .Raw ) == 0 {
45
+ return fmt .Errorf ("there is no content to decode" )
46
+ }
40
47
return d .DecodeRaw (req .Object , into )
41
48
}
42
49
43
50
// DecodeRaw decodes a RawExtension object into the passed-in runtime.Object.
51
+ // It errors out if rawObj is empty i.e. containing 0 raw bytes.
44
52
func (d * Decoder ) DecodeRaw (rawObj runtime.RawExtension , into runtime.Object ) error {
45
53
// NB(directxman12): there's a bug/weird interaction between decoders and
46
54
// the API server where the API server doesn't send a GVK on the embedded
@@ -49,6 +57,11 @@ func (d *Decoder) DecodeRaw(rawObj runtime.RawExtension, into runtime.Object) er
49
57
// and call unstructured's special Unmarshal implementation, which calls
50
58
// back into that same decoder :-/
51
59
// See kubernetes/kubernetes#74373.
60
+
61
+ // we error out if rawObj is an empty object.
62
+ if len (rawObj .Raw ) == 0 {
63
+ return fmt .Errorf ("there is no content to decode" )
64
+ }
52
65
if unstructuredInto , isUnstructured := into .(* unstructured.Unstructured ); isUnstructured {
53
66
// unmarshal into unstructured's underlying object to avoid calling the decoder
54
67
if err := json .Unmarshal (rawObj .Raw , & unstructuredInto .Object ); err != nil {
0 commit comments