@@ -31,6 +31,7 @@ import (
31
31
apierrors "k8s.io/apimachinery/pkg/api/errors"
32
32
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
33
33
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
34
+ testapigroupv1 "k8s.io/apimachinery/pkg/apis/testapigroup/v1"
34
35
"k8s.io/apimachinery/pkg/runtime"
35
36
"k8s.io/apimachinery/pkg/runtime/schema"
36
37
"k8s.io/apimachinery/pkg/runtime/serializer"
@@ -43,6 +44,7 @@ import (
43
44
examplev1 "k8s.io/apiserver/pkg/apis/example/v1"
44
45
"k8s.io/apiserver/pkg/endpoints/request"
45
46
"k8s.io/apiserver/pkg/registry/rest"
47
+ clientgoscheme "k8s.io/client-go/kubernetes/scheme"
46
48
utiltrace "k8s.io/utils/trace"
47
49
)
48
50
@@ -950,3 +952,51 @@ func (f mutateObjectUpdateFunc) Handles(operation admission.Operation) bool {
950
952
func (f mutateObjectUpdateFunc ) Admit (a admission.Attributes , o admission.ObjectInterfaces ) (err error ) {
951
953
return f (a .GetObject (), a .GetOldObject ())
952
954
}
955
+
956
+ func TestTransformDecodeErrorEnsuresBadRequestError (t * testing.T ) {
957
+ testCases := []struct {
958
+ name string
959
+ typer runtime.ObjectTyper
960
+ decodedGVK * schema.GroupVersionKind
961
+ decodeIntoObject runtime.Object
962
+ baseErr error
963
+ expectedErr error
964
+ }{
965
+ {
966
+ name : "decoding normal objects fails and returns a bad-request error" ,
967
+ typer : clientgoscheme .Scheme ,
968
+ decodedGVK : & schema.GroupVersionKind {
969
+ Group : testapigroupv1 .GroupName ,
970
+ Version : "v1" ,
971
+ Kind : "Carp" ,
972
+ },
973
+ decodeIntoObject : & testapigroupv1.Carp {}, // which client-go's scheme doesn't recognize
974
+ baseErr : fmt .Errorf ("plain error" ),
975
+ },
976
+ {
977
+ name : "decoding objects with unknown GVK fails and returns a bad-request error" ,
978
+ typer : alwaysErrorTyper {},
979
+ decodedGVK : nil ,
980
+ decodeIntoObject : & testapigroupv1.Carp {}, // which client-go's scheme doesn't recognize
981
+ baseErr : nil ,
982
+ },
983
+ }
984
+ for _ , testCase := range testCases {
985
+ err := transformDecodeError (testCase .typer , testCase .baseErr , testCase .decodeIntoObject , testCase .decodedGVK , []byte (`` ))
986
+ if apiStatus , ok := err .(apierrors.APIStatus ); ! ok || apiStatus .Status ().Code != http .StatusBadRequest {
987
+ t .Errorf ("expected bad request error but got: %v" , err )
988
+ }
989
+ }
990
+ }
991
+
992
+ var _ runtime.ObjectTyper = alwaysErrorTyper {}
993
+
994
+ type alwaysErrorTyper struct {}
995
+
996
+ func (alwaysErrorTyper ) ObjectKinds (runtime.Object ) ([]schema.GroupVersionKind , bool , error ) {
997
+ return nil , false , fmt .Errorf ("always error" )
998
+ }
999
+
1000
+ func (alwaysErrorTyper ) Recognizes (gvk schema.GroupVersionKind ) bool {
1001
+ return false
1002
+ }
0 commit comments