@@ -517,6 +517,9 @@ func TestMetadataClient(t *testing.T) {
517
517
Plural : "foos" ,
518
518
Kind : "Foo" ,
519
519
},
520
+ Subresources : & apiextensionsv1beta1.CustomResourceSubresources {
521
+ Status : & apiextensionsv1beta1.CustomResourceSubresourceStatus {},
522
+ },
520
523
},
521
524
}
522
525
fooCRD , err = fixtures .CreateNewCustomResourceDefinition (fooCRD , apiExtensionClient , dynamicClient )
@@ -835,6 +838,7 @@ func TestAPICRDProtobuf(t *testing.T) {
835
838
Plural : "foos" ,
836
839
Kind : "Foo" ,
837
840
},
841
+ Subresources : & apiextensionsv1beta1.CustomResourceSubresources {Status : & apiextensionsv1beta1.CustomResourceSubresourceStatus {}},
838
842
},
839
843
}
840
844
fooCRD , err = fixtures .CreateNewCustomResourceDefinition (fooCRD , apiExtensionClient , dynamicClient )
@@ -845,11 +849,12 @@ func TestAPICRDProtobuf(t *testing.T) {
845
849
crclient := dynamicClient .Resource (crdGVR ).Namespace (testNamespace )
846
850
847
851
testcases := []struct {
848
- name string
849
- accept string
850
- object func (* testing.T ) (metav1.Object , string , string )
851
- wantErr func (* testing.T , error )
852
- wantBody func (* testing.T , io.Reader )
852
+ name string
853
+ accept string
854
+ subresource string
855
+ object func (* testing.T ) (metav1.Object , string , string )
856
+ wantErr func (* testing.T , error )
857
+ wantBody func (* testing.T , io.Reader )
853
858
}{
854
859
{
855
860
name : "server returns 406 when asking for protobuf for CRDs, which dynamic client does not support" ,
@@ -908,6 +913,65 @@ func TestAPICRDProtobuf(t *testing.T) {
908
913
}
909
914
},
910
915
},
916
+ {
917
+ name : "server returns 406 when asking for protobuf for CRDs status, which dynamic client does not support" ,
918
+ accept : "application/vnd.kubernetes.protobuf" ,
919
+ subresource : "status" ,
920
+ object : func (t * testing.T ) (metav1.Object , string , string ) {
921
+ cr , err := crclient .Create (& unstructured.Unstructured {Object : map [string ]interface {}{"apiVersion" : "cr.bar.com/v1" , "kind" : "Foo" , "metadata" : map [string ]interface {}{"name" : "test-3" }}}, metav1.CreateOptions {})
922
+ if err != nil {
923
+ t .Fatalf ("unable to create cr: %v" , err )
924
+ }
925
+ if _ , err := crclient .Patch ("test-3" , types .MergePatchType , []byte (`{"metadata":{"annotations":{"test":"3"}}}` ), metav1.PatchOptions {}); err != nil {
926
+ t .Fatalf ("unable to patch cr: %v" , err )
927
+ }
928
+ return cr , crdGVR .Group , "foos"
929
+ },
930
+ wantErr : func (t * testing.T , err error ) {
931
+ if ! apierrors .IsNotAcceptable (err ) {
932
+ t .Fatal (err )
933
+ }
934
+ status := err .(apierrors.APIStatus ).Status ()
935
+ data , _ := json .MarshalIndent (status , "" , " " )
936
+ // because the dynamic client only has a json serializer, the client processing of the error cannot
937
+ // turn the response into something meaningful, so we verify that fallback handling works correctly
938
+ if ! apierrors .IsUnexpectedServerError (err ) {
939
+ t .Fatal (string (data ))
940
+ }
941
+ if status .Message != "the server was unable to respond with a content type that the client supports (get foos.cr.bar.com test-3)" {
942
+ t .Fatal (string (data ))
943
+ }
944
+ },
945
+ },
946
+ {
947
+ name : "server returns JSON when asking for protobuf and json for CRDs status" ,
948
+ accept : "application/vnd.kubernetes.protobuf,application/json" ,
949
+ subresource : "status" ,
950
+ object : func (t * testing.T ) (metav1.Object , string , string ) {
951
+ cr , err := crclient .Create (& unstructured.Unstructured {Object : map [string ]interface {}{"apiVersion" : "cr.bar.com/v1" , "kind" : "Foo" , "spec" : map [string ]interface {}{"field" : 1 }, "metadata" : map [string ]interface {}{"name" : "test-4" }}}, metav1.CreateOptions {})
952
+ if err != nil {
953
+ t .Fatalf ("unable to create cr: %v" , err )
954
+ }
955
+ if _ , err := crclient .Patch ("test-4" , types .MergePatchType , []byte (`{"metadata":{"annotations":{"test":"4"}}}` ), metav1.PatchOptions {}); err != nil {
956
+ t .Fatalf ("unable to patch cr: %v" , err )
957
+ }
958
+ return cr , crdGVR .Group , "foos"
959
+ },
960
+ wantBody : func (t * testing.T , w io.Reader ) {
961
+ obj := & unstructured.Unstructured {}
962
+ if err := json .NewDecoder (w ).Decode (obj ); err != nil {
963
+ t .Fatal (err )
964
+ }
965
+ v , ok , err := unstructured .NestedInt64 (obj .UnstructuredContent (), "spec" , "field" )
966
+ if ! ok || err != nil {
967
+ data , _ := json .MarshalIndent (obj .UnstructuredContent (), "" , " " )
968
+ t .Fatalf ("err=%v ok=%t json=%s" , err , ok , string (data ))
969
+ }
970
+ if v != 1 {
971
+ t .Fatalf ("unexpected body: %#v" , obj .UnstructuredContent ())
972
+ }
973
+ },
974
+ },
911
975
}
912
976
913
977
for i := range testcases {
@@ -934,7 +998,7 @@ func TestAPICRDProtobuf(t *testing.T) {
934
998
}
935
999
936
1000
w , err := client .Get ().
937
- Resource (resource ).NamespaceIfScoped (obj .GetNamespace (), len (obj .GetNamespace ()) > 0 ).Name (obj .GetName ()).
1001
+ Resource (resource ).NamespaceIfScoped (obj .GetNamespace (), len (obj .GetNamespace ()) > 0 ).Name (obj .GetName ()).SubResource ( tc . subresource ).
938
1002
SetHeader ("Accept" , tc .accept ).
939
1003
Stream ()
940
1004
if (tc .wantErr != nil ) != (err != nil ) {
0 commit comments