From e256c15df3ae38791d83291283c00198fb830884 Mon Sep 17 00:00:00 2001 From: Clayton Coleman Date: Sat, 4 May 2019 16:54:02 -0400 Subject: [PATCH 1/2] Make metav1beta1 be equivalent to metav1 internally All except PartialObjectMetadataList are identical and should be made that way internally. This allows the internalversion to convert between them. --- .../pkg/apis/meta/internalversion/register.go | 18 +- .../apimachinery/pkg/apis/meta/v1/register.go | 15 +- .../apimachinery/pkg/apis/meta/v1/types.go | 2 +- .../pkg/apis/meta/v1beta1/deepcopy.go | 27 -- .../pkg/apis/meta/v1beta1/generated.pb.go | 314 ++---------------- .../pkg/apis/meta/v1beta1/generated.proto | 24 +- .../pkg/apis/meta/v1beta1/register.go | 4 +- .../pkg/apis/meta/v1beta1/types.go | 141 ++------ .../v1beta1/types_swagger_doc_generated.go | 67 +--- .../pkg/apis/meta/v1beta1/validation/BUILD | 1 + .../meta/v1beta1/validation/validation.go | 3 +- .../meta/v1beta1/zz_generated.deepcopy.go | 133 +------- 12 files changed, 88 insertions(+), 661 deletions(-) diff --git a/staging/src/k8s.io/apimachinery/pkg/apis/meta/internalversion/register.go b/staging/src/k8s.io/apimachinery/pkg/apis/meta/internalversion/register.go index 158655caad1..d0149810b3c 100644 --- a/staging/src/k8s.io/apimachinery/pkg/apis/meta/internalversion/register.go +++ b/staging/src/k8s.io/apimachinery/pkg/apis/meta/internalversion/register.go @@ -89,18 +89,12 @@ func addToGroupVersion(scheme *runtime.Scheme, groupVersion schema.GroupVersion) &metav1beta1.PartialObjectMetadata{}, &metav1beta1.PartialObjectMetadataList{}, ) - scheme.AddKnownTypes(metav1beta1.SchemeGroupVersion, - &metav1beta1.Table{}, - &metav1beta1.TableOptions{}, - &metav1beta1.PartialObjectMetadata{}, - &metav1beta1.PartialObjectMetadataList{}, - ) - scheme.AddKnownTypes(metav1.SchemeGroupVersion, - &metav1.Table{}, - &metav1.TableOptions{}, - &metav1.PartialObjectMetadata{}, - &metav1.PartialObjectMetadataList{}, - ) + if err := metav1beta1.AddMetaToScheme(scheme); err != nil { + return err + } + if err := metav1.AddMetaToScheme(scheme); err != nil { + return err + } // Allow delete options to be decoded across all version in this scheme (we may want to be more clever than this) scheme.AddUnversionedTypes(SchemeGroupVersion, &metav1.DeleteOptions{}, diff --git a/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/register.go b/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/register.go index 4610eed6462..24fc134150f 100644 --- a/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/register.go +++ b/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/register.go @@ -94,6 +94,15 @@ func init() { &PatchOptions{}, ) + if err := AddMetaToScheme(scheme); err != nil { + panic(err) + } + + // register manually. This usually goes through the SchemeBuilder, which we cannot use here. + utilruntime.Must(RegisterDefaults(scheme)) +} + +func AddMetaToScheme(scheme *runtime.Scheme) error { scheme.AddKnownTypes(SchemeGroupVersion, &Table{}, &TableOptions{}, @@ -101,6 +110,10 @@ func init() { &PartialObjectMetadataList{}, ) + return scheme.AddConversionFuncs( + Convert_Slice_string_To_v1_IncludeObjectPolicy, + ) + // register manually. This usually goes through the SchemeBuilder, which we cannot use here. - utilruntime.Must(RegisterDefaults(scheme)) + //scheme.AddGeneratedDeepCopyFuncs(GetGeneratedDeepCopyFuncs()...) } diff --git a/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/types.go b/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/types.go index d3547940024..a6fe80cc3c3 100644 --- a/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/types.go +++ b/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/types.go @@ -1150,8 +1150,8 @@ type Fields struct { // Table is a tabular representation of a set of API resources. The server transforms the // object into a set of preferred columns for quickly reviewing the objects. -// +protobuf=false // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object +// +protobuf=false type Table struct { TypeMeta `json:",inline"` // Standard list metadata. diff --git a/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1beta1/deepcopy.go b/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1beta1/deepcopy.go index 3b2bedd9233..2b7e8ca0bfb 100644 --- a/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1beta1/deepcopy.go +++ b/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1beta1/deepcopy.go @@ -15,30 +15,3 @@ limitations under the License. */ package v1beta1 - -import "k8s.io/apimachinery/pkg/runtime" - -func (in *TableRow) DeepCopy() *TableRow { - if in == nil { - return nil - } - - out := new(TableRow) - - if in.Cells != nil { - out.Cells = make([]interface{}, len(in.Cells)) - for i := range in.Cells { - out.Cells[i] = runtime.DeepCopyJSONValue(in.Cells[i]) - } - } - - if in.Conditions != nil { - out.Conditions = make([]TableRowCondition, len(in.Conditions)) - for i := range in.Conditions { - in.Conditions[i].DeepCopyInto(&out.Conditions[i]) - } - } - - in.Object.DeepCopyInto(&out.Object) - return out -} diff --git a/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1beta1/generated.pb.go b/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1beta1/generated.pb.go index 2c242fef71a..557dfa2c731 100644 --- a/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1beta1/generated.pb.go +++ b/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1beta1/generated.pb.go @@ -24,9 +24,7 @@ limitations under the License. k8s.io/kubernetes/vendor/k8s.io/apimachinery/pkg/apis/meta/v1beta1/generated.proto It has these top-level messages: - PartialObjectMetadata PartialObjectMetadataList - TableOptions */ package v1beta1 @@ -34,6 +32,8 @@ import proto "github.com/gogo/protobuf/proto" import fmt "fmt" import math "math" +import k8s_io_apimachinery_pkg_apis_meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1" + import strings "strings" import reflect "reflect" @@ -50,51 +50,15 @@ var _ = math.Inf // proto package needs to be updated. const _ = proto.GoGoProtoPackageIsVersion2 // please upgrade the proto package -func (m *PartialObjectMetadata) Reset() { *m = PartialObjectMetadata{} } -func (*PartialObjectMetadata) ProtoMessage() {} -func (*PartialObjectMetadata) Descriptor() ([]byte, []int) { return fileDescriptorGenerated, []int{0} } - func (m *PartialObjectMetadataList) Reset() { *m = PartialObjectMetadataList{} } func (*PartialObjectMetadataList) ProtoMessage() {} func (*PartialObjectMetadataList) Descriptor() ([]byte, []int) { - return fileDescriptorGenerated, []int{1} + return fileDescriptorGenerated, []int{0} } -func (m *TableOptions) Reset() { *m = TableOptions{} } -func (*TableOptions) ProtoMessage() {} -func (*TableOptions) Descriptor() ([]byte, []int) { return fileDescriptorGenerated, []int{2} } - func init() { - proto.RegisterType((*PartialObjectMetadata)(nil), "k8s.io.apimachinery.pkg.apis.meta.v1beta1.PartialObjectMetadata") proto.RegisterType((*PartialObjectMetadataList)(nil), "k8s.io.apimachinery.pkg.apis.meta.v1beta1.PartialObjectMetadataList") - proto.RegisterType((*TableOptions)(nil), "k8s.io.apimachinery.pkg.apis.meta.v1beta1.TableOptions") } -func (m *PartialObjectMetadata) Marshal() (dAtA []byte, err error) { - size := m.Size() - dAtA = make([]byte, size) - n, err := m.MarshalTo(dAtA) - if err != nil { - return nil, err - } - return dAtA[:n], nil -} - -func (m *PartialObjectMetadata) MarshalTo(dAtA []byte) (int, error) { - var i int - _ = i - var l int - _ = l - dAtA[i] = 0xa - i++ - i = encodeVarintGenerated(dAtA, i, uint64(m.ObjectMeta.Size())) - n1, err := m.ObjectMeta.MarshalTo(dAtA[i:]) - if err != nil { - return 0, err - } - i += n1 - return i, nil -} - func (m *PartialObjectMetadataList) Marshal() (dAtA []byte, err error) { size := m.Size() dAtA = make([]byte, size) @@ -125,33 +89,11 @@ func (m *PartialObjectMetadataList) MarshalTo(dAtA []byte) (int, error) { dAtA[i] = 0x12 i++ i = encodeVarintGenerated(dAtA, i, uint64(m.ListMeta.Size())) - n2, err := m.ListMeta.MarshalTo(dAtA[i:]) + n1, err := m.ListMeta.MarshalTo(dAtA[i:]) if err != nil { return 0, err } - i += n2 - return i, nil -} - -func (m *TableOptions) Marshal() (dAtA []byte, err error) { - size := m.Size() - dAtA = make([]byte, size) - n, err := m.MarshalTo(dAtA) - if err != nil { - return nil, err - } - return dAtA[:n], nil -} - -func (m *TableOptions) MarshalTo(dAtA []byte) (int, error) { - var i int - _ = i - var l int - _ = l - dAtA[i] = 0xa - i++ - i = encodeVarintGenerated(dAtA, i, uint64(len(m.IncludeObject))) - i += copy(dAtA[i:], m.IncludeObject) + i += n1 return i, nil } @@ -164,14 +106,6 @@ func encodeVarintGenerated(dAtA []byte, offset int, v uint64) int { dAtA[offset] = uint8(v) return offset + 1 } -func (m *PartialObjectMetadata) Size() (n int) { - var l int - _ = l - l = m.ObjectMeta.Size() - n += 1 + l + sovGenerated(uint64(l)) - return n -} - func (m *PartialObjectMetadataList) Size() (n int) { var l int _ = l @@ -186,14 +120,6 @@ func (m *PartialObjectMetadataList) Size() (n int) { return n } -func (m *TableOptions) Size() (n int) { - var l int - _ = l - l = len(m.IncludeObject) - n += 1 + l + sovGenerated(uint64(l)) - return n -} - func sovGenerated(x uint64) (n int) { for { n++ @@ -207,37 +133,17 @@ func sovGenerated(x uint64) (n int) { func sozGenerated(x uint64) (n int) { return sovGenerated(uint64((x << 1) ^ uint64((int64(x) >> 63)))) } -func (this *PartialObjectMetadata) String() string { - if this == nil { - return "nil" - } - s := strings.Join([]string{`&PartialObjectMetadata{`, - `ObjectMeta:` + strings.Replace(strings.Replace(this.ObjectMeta.String(), "ObjectMeta", "k8s_io_apimachinery_pkg_apis_meta_v1.ObjectMeta", 1), `&`, ``, 1) + `,`, - `}`, - }, "") - return s -} func (this *PartialObjectMetadataList) String() string { if this == nil { return "nil" } s := strings.Join([]string{`&PartialObjectMetadataList{`, - `Items:` + strings.Replace(fmt.Sprintf("%v", this.Items), "PartialObjectMetadata", "PartialObjectMetadata", 1) + `,`, + `Items:` + strings.Replace(fmt.Sprintf("%v", this.Items), "PartialObjectMetadata", "k8s_io_apimachinery_pkg_apis_meta_v1.PartialObjectMetadata", 1) + `,`, `ListMeta:` + strings.Replace(strings.Replace(this.ListMeta.String(), "ListMeta", "k8s_io_apimachinery_pkg_apis_meta_v1.ListMeta", 1), `&`, ``, 1) + `,`, `}`, }, "") return s } -func (this *TableOptions) String() string { - if this == nil { - return "nil" - } - s := strings.Join([]string{`&TableOptions{`, - `IncludeObject:` + fmt.Sprintf("%v", this.IncludeObject) + `,`, - `}`, - }, "") - return s -} func valueToStringGenerated(v interface{}) string { rv := reflect.ValueOf(v) if rv.IsNil() { @@ -246,86 +152,6 @@ func valueToStringGenerated(v interface{}) string { pv := reflect.Indirect(rv).Interface() return fmt.Sprintf("*%v", pv) } -func (m *PartialObjectMetadata) Unmarshal(dAtA []byte) error { - l := len(dAtA) - iNdEx := 0 - for iNdEx < l { - preIndex := iNdEx - var wire uint64 - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowGenerated - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - wire |= (uint64(b) & 0x7F) << shift - if b < 0x80 { - break - } - } - fieldNum := int32(wire >> 3) - wireType := int(wire & 0x7) - if wireType == 4 { - return fmt.Errorf("proto: PartialObjectMetadata: wiretype end group for non-group") - } - if fieldNum <= 0 { - return fmt.Errorf("proto: PartialObjectMetadata: illegal tag %d (wire type %d)", fieldNum, wire) - } - switch fieldNum { - case 1: - if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field ObjectMeta", wireType) - } - var msglen int - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowGenerated - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - msglen |= (int(b) & 0x7F) << shift - if b < 0x80 { - break - } - } - if msglen < 0 { - return ErrInvalidLengthGenerated - } - postIndex := iNdEx + msglen - if postIndex > l { - return io.ErrUnexpectedEOF - } - if err := m.ObjectMeta.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { - return err - } - iNdEx = postIndex - default: - iNdEx = preIndex - skippy, err := skipGenerated(dAtA[iNdEx:]) - if err != nil { - return err - } - if skippy < 0 { - return ErrInvalidLengthGenerated - } - if (iNdEx + skippy) > l { - return io.ErrUnexpectedEOF - } - iNdEx += skippy - } - } - - if iNdEx > l { - return io.ErrUnexpectedEOF - } - return nil -} func (m *PartialObjectMetadataList) Unmarshal(dAtA []byte) error { l := len(dAtA) iNdEx := 0 @@ -381,7 +207,7 @@ func (m *PartialObjectMetadataList) Unmarshal(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - m.Items = append(m.Items, &PartialObjectMetadata{}) + m.Items = append(m.Items, &k8s_io_apimachinery_pkg_apis_meta_v1.PartialObjectMetadata{}) if err := m.Items[len(m.Items)-1].Unmarshal(dAtA[iNdEx:postIndex]); err != nil { return err } @@ -437,85 +263,6 @@ func (m *PartialObjectMetadataList) Unmarshal(dAtA []byte) error { } return nil } -func (m *TableOptions) Unmarshal(dAtA []byte) error { - l := len(dAtA) - iNdEx := 0 - for iNdEx < l { - preIndex := iNdEx - var wire uint64 - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowGenerated - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - wire |= (uint64(b) & 0x7F) << shift - if b < 0x80 { - break - } - } - fieldNum := int32(wire >> 3) - wireType := int(wire & 0x7) - if wireType == 4 { - return fmt.Errorf("proto: TableOptions: wiretype end group for non-group") - } - if fieldNum <= 0 { - return fmt.Errorf("proto: TableOptions: illegal tag %d (wire type %d)", fieldNum, wire) - } - switch fieldNum { - case 1: - if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field IncludeObject", wireType) - } - var stringLen uint64 - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowGenerated - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - stringLen |= (uint64(b) & 0x7F) << shift - if b < 0x80 { - break - } - } - intStringLen := int(stringLen) - if intStringLen < 0 { - return ErrInvalidLengthGenerated - } - postIndex := iNdEx + intStringLen - if postIndex > l { - return io.ErrUnexpectedEOF - } - m.IncludeObject = IncludeObjectPolicy(dAtA[iNdEx:postIndex]) - iNdEx = postIndex - default: - iNdEx = preIndex - skippy, err := skipGenerated(dAtA[iNdEx:]) - if err != nil { - return err - } - if skippy < 0 { - return ErrInvalidLengthGenerated - } - if (iNdEx + skippy) > l { - return io.ErrUnexpectedEOF - } - iNdEx += skippy - } - } - - if iNdEx > l { - return io.ErrUnexpectedEOF - } - return nil -} func skipGenerated(dAtA []byte) (n int, err error) { l := len(dAtA) iNdEx := 0 @@ -626,31 +373,26 @@ func init() { } var fileDescriptorGenerated = []byte{ - // 402 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x8c, 0x91, 0x4f, 0x8b, 0xd3, 0x40, - 0x18, 0xc6, 0x33, 0xca, 0xe2, 0xee, 0xac, 0x0b, 0x12, 0x11, 0xd6, 0x1e, 0x26, 0xcb, 0x9e, 0x2a, - 0xd8, 0x19, 0x5b, 0x44, 0x3c, 0x4a, 0x6e, 0x05, 0xa5, 0x25, 0x78, 0x12, 0x0f, 0x4e, 0x92, 0xd7, - 0x74, 0xcc, 0x9f, 0x09, 0x99, 0x49, 0xa1, 0x37, 0x3f, 0x82, 0x1f, 0xab, 0xc7, 0x1e, 0x7b, 0x90, - 0x60, 0xe3, 0xb7, 0xf0, 0x24, 0xf9, 0xa3, 0x4d, 0x6b, 0x65, 0x73, 0x9b, 0xf7, 0x79, 0x79, 0x7e, - 0x79, 0x9e, 0x37, 0xd8, 0x09, 0x5f, 0x2b, 0x2a, 0x24, 0x0b, 0x73, 0x17, 0xb2, 0x04, 0x34, 0x28, - 0xb6, 0x84, 0xc4, 0x97, 0x19, 0x6b, 0x17, 0x3c, 0x15, 0x31, 0xf7, 0x16, 0x22, 0x81, 0x6c, 0xc5, - 0xd2, 0x30, 0xa8, 0x04, 0xc5, 0x62, 0xd0, 0x9c, 0x2d, 0xc7, 0x2e, 0x68, 0x3e, 0x66, 0x01, 0x24, - 0x90, 0x71, 0x0d, 0x3e, 0x4d, 0x33, 0xa9, 0xa5, 0xf9, 0xac, 0xb1, 0xd2, 0xae, 0x95, 0xa6, 0x61, - 0x50, 0x09, 0x8a, 0x56, 0x56, 0xda, 0x5a, 0x07, 0xa3, 0x40, 0xe8, 0x45, 0xee, 0x52, 0x4f, 0xc6, - 0x2c, 0x90, 0x81, 0x64, 0x35, 0xc1, 0xcd, 0x3f, 0xd7, 0x53, 0x3d, 0xd4, 0xaf, 0x86, 0x3c, 0x78, - 0xd9, 0x27, 0xd4, 0x71, 0x9e, 0xc1, 0x7f, 0xab, 0x64, 0x79, 0xa2, 0x45, 0x0c, 0xff, 0x18, 0x5e, - 0xdd, 0x65, 0x50, 0xde, 0x02, 0x62, 0x7e, 0xec, 0xbb, 0x5d, 0xe1, 0x27, 0x73, 0x9e, 0x69, 0xc1, - 0xa3, 0x99, 0xfb, 0x05, 0x3c, 0xfd, 0x0e, 0x34, 0xf7, 0xb9, 0xe6, 0xe6, 0x27, 0x7c, 0x1e, 0xb7, - 0xef, 0x6b, 0x74, 0x83, 0x86, 0x97, 0x93, 0x17, 0xb4, 0xcf, 0x91, 0xe8, 0x9e, 0x63, 0x9b, 0xeb, - 0xc2, 0x32, 0xca, 0xc2, 0xc2, 0x7b, 0xcd, 0xf9, 0x4b, 0xbd, 0xfd, 0x8e, 0xf0, 0xd3, 0x93, 0xdf, - 0x7e, 0x2b, 0x94, 0x36, 0x39, 0x3e, 0x13, 0x1a, 0x62, 0x75, 0x8d, 0x6e, 0xee, 0x0f, 0x2f, 0x27, - 0x6f, 0x68, 0xef, 0x3f, 0x44, 0x4f, 0x42, 0xed, 0x8b, 0xb2, 0xb0, 0xce, 0xa6, 0x15, 0xd2, 0x69, - 0xc8, 0xe6, 0xc7, 0x4e, 0xc5, 0x7b, 0x75, 0x45, 0xda, 0xaf, 0x62, 0x15, 0xb0, 0x2e, 0xf8, 0xa8, - 0x2d, 0x78, 0xfe, 0x47, 0xe9, 0xd4, 0x73, 0xf1, 0xc3, 0xf7, 0xdc, 0x8d, 0x60, 0x96, 0x6a, 0x21, - 0x13, 0x65, 0x3a, 0xf8, 0x4a, 0x24, 0x5e, 0x94, 0xfb, 0xd0, 0x04, 0xab, 0xaf, 0x7a, 0x61, 0x3f, - 0x6f, 0x11, 0x57, 0xd3, 0xee, 0xf2, 0x57, 0x61, 0x3d, 0x3e, 0x10, 0xe6, 0x32, 0x12, 0xde, 0xca, - 0x39, 0x44, 0xd8, 0xa3, 0xf5, 0x8e, 0x18, 0x9b, 0x1d, 0x31, 0xb6, 0x3b, 0x62, 0x7c, 0x2d, 0x09, - 0x5a, 0x97, 0x04, 0x6d, 0x4a, 0x82, 0xb6, 0x25, 0x41, 0x3f, 0x4a, 0x82, 0xbe, 0xfd, 0x24, 0xc6, - 0x87, 0x07, 0xed, 0x61, 0x7e, 0x07, 0x00, 0x00, 0xff, 0xff, 0xf8, 0x78, 0xd8, 0x63, 0x3a, 0x03, + // 322 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x8c, 0x91, 0xb1, 0x4e, 0xeb, 0x30, + 0x14, 0x86, 0xe3, 0x7b, 0x55, 0x51, 0xd2, 0x05, 0x75, 0x2a, 0x1d, 0xdc, 0x8a, 0xa9, 0x0c, 0xb5, + 0xd5, 0x0a, 0x21, 0x24, 0xb6, 0x6e, 0x48, 0x20, 0x50, 0x47, 0xd4, 0xc5, 0x49, 0x0f, 0xa9, 0x09, + 0x8e, 0x23, 0xfb, 0xa4, 0x12, 0x1b, 0x8f, 0xc0, 0x63, 0x75, 0xec, 0x46, 0xa7, 0x8a, 0x9a, 0x17, + 0x41, 0x49, 0x03, 0x42, 0x05, 0x44, 0xb6, 0x9c, 0xff, 0xe8, 0xfb, 0xf2, 0xdb, 0xf6, 0xc7, 0xf1, + 0x99, 0x65, 0x52, 0xf3, 0x38, 0x0b, 0xc0, 0x24, 0x80, 0x60, 0xf9, 0x1c, 0x92, 0xa9, 0x36, 0xbc, + 0x5c, 0x88, 0x54, 0x2a, 0x11, 0xce, 0x64, 0x02, 0xe6, 0x91, 0xa7, 0x71, 0x94, 0x07, 0x96, 0x2b, + 0x40, 0xc1, 0xe7, 0x83, 0x00, 0x50, 0x0c, 0x78, 0x04, 0x09, 0x18, 0x81, 0x30, 0x65, 0xa9, 0xd1, + 0xa8, 0x9b, 0xc7, 0x5b, 0x94, 0x7d, 0x45, 0x59, 0x1a, 0x47, 0x79, 0x60, 0x59, 0x8e, 0xb2, 0x12, + 0x6d, 0xf7, 0x23, 0x89, 0xb3, 0x2c, 0x60, 0xa1, 0x56, 0x3c, 0xd2, 0x91, 0xe6, 0x85, 0x21, 0xc8, + 0xee, 0x8a, 0xa9, 0x18, 0x8a, 0xaf, 0xad, 0xb9, 0x7d, 0x52, 0xa5, 0xd4, 0x6e, 0x9f, 0xf6, 0xaf, + 0x47, 0x31, 0x59, 0x82, 0x52, 0xc1, 0x37, 0xe0, 0xf4, 0x2f, 0xc0, 0x86, 0x33, 0x50, 0x62, 0x97, + 0x3b, 0x7a, 0x21, 0xfe, 0xe1, 0x8d, 0x30, 0x28, 0xc5, 0xc3, 0x75, 0x70, 0x0f, 0x21, 0x5e, 0x01, + 0x8a, 0xa9, 0x40, 0x71, 0x29, 0x2d, 0x36, 0x27, 0x7e, 0x4d, 0x22, 0x28, 0xdb, 0x22, 0xdd, 0xff, + 0xbd, 0xc6, 0xf0, 0x9c, 0x55, 0xb9, 0x26, 0xf6, 0xa3, 0x6f, 0xb4, 0xef, 0xd6, 0x9d, 0xda, 0x45, + 0x6e, 0x1b, 0x6f, 0xa5, 0xcd, 0x89, 0x5f, 0x57, 0xe5, 0xb6, 0xf5, 0xaf, 0x4b, 0x7a, 0x8d, 0x21, + 0xab, 0xf6, 0x83, 0xbc, 0x5b, 0xee, 0x1d, 0x1d, 0x2c, 0xd6, 0x1d, 0xcf, 0xad, 0x3b, 0xf5, 0x8f, + 0x64, 0xfc, 0x69, 0x1c, 0xf5, 0x17, 0x1b, 0xea, 0x2d, 0x37, 0xd4, 0x5b, 0x6d, 0xa8, 0xf7, 0xe4, + 0x28, 0x59, 0x38, 0x4a, 0x96, 0x8e, 0x92, 0x95, 0xa3, 0xe4, 0xd5, 0x51, 0xf2, 0xfc, 0x46, 0xbd, + 0xdb, 0xbd, 0xf2, 0x59, 0xdf, 0x03, 0x00, 0x00, 0xff, 0xff, 0xce, 0xfa, 0x86, 0x29, 0x56, 0x02, 0x00, 0x00, } diff --git a/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1beta1/generated.proto b/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1beta1/generated.proto index 5097ad350da..6339e719ad3 100644 --- a/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1beta1/generated.proto +++ b/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1beta1/generated.proto @@ -28,17 +28,7 @@ import "k8s.io/apimachinery/pkg/runtime/schema/generated.proto"; // Package-wide variables from generator "generated". option go_package = "v1beta1"; -// PartialObjectMetadata is a generic representation of any object with ObjectMeta. It allows clients -// to get access to a particular ObjectMeta schema without knowing the details of the version. -// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object -message PartialObjectMetadata { - // Standard object's metadata. - // More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#metadata - // +optional - optional k8s.io.apimachinery.pkg.apis.meta.v1.ObjectMeta metadata = 1; -} - -// PartialObjectMetadataList contains a list of objects containing only their metadata +// PartialObjectMetadataList contains a list of objects containing only their metadata. // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object message PartialObjectMetadataList { // Standard list metadata. @@ -47,16 +37,6 @@ message PartialObjectMetadataList { optional k8s.io.apimachinery.pkg.apis.meta.v1.ListMeta metadata = 2; // items contains each of the included items. - repeated PartialObjectMetadata items = 1; -} - -// TableOptions are used when a Table is requested by the caller. -// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object -message TableOptions { - // includeObject decides whether to include each object along with its columnar information. - // Specifying "None" will return no object, specifying "Object" will return the full object contents, and - // specifying "Metadata" (the default) will return the object's metadata in the PartialObjectMetadata kind - // in version v1beta1 of the meta.k8s.io API group. - optional string includeObject = 1; + repeated k8s.io.apimachinery.pkg.apis.meta.v1.PartialObjectMetadata items = 1; } diff --git a/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1beta1/register.go b/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1beta1/register.go index 6d348fe14f4..108a0764e72 100644 --- a/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1beta1/register.go +++ b/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1beta1/register.go @@ -39,12 +39,12 @@ var scheme = runtime.NewScheme() var ParameterCodec = runtime.NewParameterCodec(scheme) func init() { - if err := AddToScheme(scheme); err != nil { + if err := AddMetaToScheme(scheme); err != nil { panic(err) } } -func AddToScheme(scheme *runtime.Scheme) error { +func AddMetaToScheme(scheme *runtime.Scheme) error { scheme.AddKnownTypes(SchemeGroupVersion, &Table{}, &TableOptions{}, diff --git a/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1beta1/types.go b/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1beta1/types.go index 2750f65f829..8b7f6bd4f54 100644 --- a/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1beta1/types.go +++ b/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1beta1/types.go @@ -19,143 +19,46 @@ package v1beta1 import ( v1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/runtime" ) -// TODO: Table does not generate to protobuf because of the interface{} - fix protobuf -// generation to support a meta type that can accept any valid JSON. - // Table is a tabular representation of a set of API resources. The server transforms the // object into a set of preferred columns for quickly reviewing the objects. -// +protobuf=false // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object -type Table struct { - v1.TypeMeta `json:",inline"` - // Standard list metadata. - // More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#types-kinds - // +optional - v1.ListMeta `json:"metadata,omitempty"` - - // columnDefinitions describes each column in the returned items array. The number of cells per row - // will always match the number of column definitions. - ColumnDefinitions []TableColumnDefinition `json:"columnDefinitions"` - // rows is the list of items in the table. - Rows []TableRow `json:"rows"` -} +// +protobuf=false +type Table = v1.Table // TableColumnDefinition contains information about a column returned in the Table. // +protobuf=false -type TableColumnDefinition struct { - // name is a human readable name for the column. - Name string `json:"name"` - // type is an OpenAPI type definition for this column. - // See https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#data-types for more. - Type string `json:"type"` - // format is an optional OpenAPI type definition for this column. The 'name' format is applied - // to the primary identifier column to assist in clients identifying column is the resource name. - // See https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#data-types for more. - Format string `json:"format"` - // description is a human readable description of this column. - Description string `json:"description"` - // priority is an integer defining the relative importance of this column compared to others. Lower - // numbers are considered higher priority. Columns that may be omitted in limited space scenarios - // should be given a higher priority. - Priority int32 `json:"priority"` -} +type TableColumnDefinition = v1.TableColumnDefinition // TableRow is an individual row in a table. // +protobuf=false -type TableRow struct { - // cells will be as wide as headers and may contain strings, numbers (float64 or int64), booleans, simple - // maps, or lists, or null. See the type field of the column definition for a more detailed description. - Cells []interface{} `json:"cells"` - // conditions describe additional status of a row that are relevant for a human user. - // +optional - Conditions []TableRowCondition `json:"conditions,omitempty"` - // This field contains the requested additional information about each object based on the includeObject - // policy when requesting the Table. If "None", this field is empty, if "Object" this will be the - // default serialization of the object for the current API version, and if "Metadata" (the default) will - // contain the object metadata. Check the returned kind and apiVersion of the object before parsing. - // +optional - Object runtime.RawExtension `json:"object,omitempty"` -} +type TableRow = v1.TableRow // TableRowCondition allows a row to be marked with additional information. // +protobuf=false -type TableRowCondition struct { - // Type of row condition. - Type RowConditionType `json:"type"` - // Status of the condition, one of True, False, Unknown. - Status ConditionStatus `json:"status"` - // (brief) machine readable reason for the condition's last transition. - // +optional - Reason string `json:"reason,omitempty"` - // Human readable message indicating details about last transition. - // +optional - Message string `json:"message,omitempty"` -} +type TableRowCondition = v1.TableRowCondition -type RowConditionType string +type RowConditionType = v1.RowConditionType -// These are valid conditions of a row. This list is not exhaustive and new conditions may be -// included by other resources. -const ( - // RowCompleted means the underlying resource has reached completion and may be given less - // visual priority than other resources. - RowCompleted RowConditionType = "Completed" -) +type ConditionStatus = v1.ConditionStatus -type ConditionStatus string - -// These are valid condition statuses. "ConditionTrue" means a resource is in the condition. -// "ConditionFalse" means a resource is not in the condition. "ConditionUnknown" means kubernetes -// can't decide if a resource is in the condition or not. In the future, we could add other -// intermediate conditions, e.g. ConditionDegraded. -const ( - ConditionTrue ConditionStatus = "True" - ConditionFalse ConditionStatus = "False" - ConditionUnknown ConditionStatus = "Unknown" -) - -// IncludeObjectPolicy controls which portion of the object is returned with a Table. -type IncludeObjectPolicy string - -const ( - // IncludeNone returns no object. - IncludeNone IncludeObjectPolicy = "None" - // IncludeMetadata serializes the object containing only its metadata field. - IncludeMetadata IncludeObjectPolicy = "Metadata" - // IncludeObject contains the full object. - IncludeObject IncludeObjectPolicy = "Object" -) +type IncludeObjectPolicy = v1.IncludeObjectPolicy // TableOptions are used when a Table is requested by the caller. // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object -type TableOptions struct { - v1.TypeMeta `json:",inline"` - - // NoHeaders is only exposed for internal callers. - NoHeaders bool `json:"-"` - - // includeObject decides whether to include each object along with its columnar information. - // Specifying "None" will return no object, specifying "Object" will return the full object contents, and - // specifying "Metadata" (the default) will return the object's metadata in the PartialObjectMetadata kind - // in version v1beta1 of the meta.k8s.io API group. - IncludeObject IncludeObjectPolicy `json:"includeObject,omitempty" protobuf:"bytes,1,opt,name=includeObject,casttype=IncludeObjectPolicy"` -} +type TableOptions = v1.TableOptions // PartialObjectMetadata is a generic representation of any object with ObjectMeta. It allows clients // to get access to a particular ObjectMeta schema without knowing the details of the version. // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object -type PartialObjectMetadata struct { - v1.TypeMeta `json:",inline"` - // Standard object's metadata. - // More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#metadata - // +optional - v1.ObjectMeta `json:"metadata,omitempty" protobuf:"bytes,1,opt,name=metadata"` -} +type PartialObjectMetadata = v1.PartialObjectMetadata -// PartialObjectMetadataList contains a list of objects containing only their metadata +// IMPORTANT: PartialObjectMetadataList has different protobuf field ids in v1beta1 than +// v1 because ListMeta was accidentally omitted prior to 1.15. Therefore this type must +// remain independent of v1.PartialObjectMetadataList to preserve mappings. + +// PartialObjectMetadataList contains a list of objects containing only their metadata. // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object type PartialObjectMetadataList struct { v1.TypeMeta `json:",inline"` @@ -165,5 +68,17 @@ type PartialObjectMetadataList struct { v1.ListMeta `json:"metadata,omitempty" protobuf:"bytes,2,opt,name=metadata"` // items contains each of the included items. - Items []*PartialObjectMetadata `json:"items" protobuf:"bytes,1,rep,name=items"` + Items []*v1.PartialObjectMetadata `json:"items" protobuf:"bytes,1,rep,name=items"` } + +const ( + RowCompleted = v1.RowCompleted + + ConditionTrue = v1.ConditionTrue + ConditionFalse = v1.ConditionFalse + ConditionUnknown = v1.ConditionUnknown + + IncludeNone = v1.IncludeNone + IncludeMetadata = v1.IncludeMetadata + IncludeObject = v1.IncludeObject +) diff --git a/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1beta1/types_swagger_doc_generated.go b/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1beta1/types_swagger_doc_generated.go index d5bb86e84d1..26d13f5d91c 100644 --- a/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1beta1/types_swagger_doc_generated.go +++ b/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1beta1/types_swagger_doc_generated.go @@ -27,17 +27,8 @@ package v1beta1 // Those methods can be generated by using hack/update-generated-swagger-docs.sh // AUTO-GENERATED FUNCTIONS START HERE. DO NOT EDIT. -var map_PartialObjectMetadata = map[string]string{ - "": "PartialObjectMetadata is a generic representation of any object with ObjectMeta. It allows clients to get access to a particular ObjectMeta schema without knowing the details of the version.", - "metadata": "Standard object's metadata. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#metadata", -} - -func (PartialObjectMetadata) SwaggerDoc() map[string]string { - return map_PartialObjectMetadata -} - var map_PartialObjectMetadataList = map[string]string{ - "": "PartialObjectMetadataList contains a list of objects containing only their metadata", + "": "PartialObjectMetadataList contains a list of objects containing only their metadata.", "metadata": "Standard list metadata. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#types-kinds", "items": "items contains each of the included items.", } @@ -46,60 +37,4 @@ func (PartialObjectMetadataList) SwaggerDoc() map[string]string { return map_PartialObjectMetadataList } -var map_Table = map[string]string{ - "": "Table is a tabular representation of a set of API resources. The server transforms the object into a set of preferred columns for quickly reviewing the objects.", - "metadata": "Standard list metadata. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#types-kinds", - "columnDefinitions": "columnDefinitions describes each column in the returned items array. The number of cells per row will always match the number of column definitions.", - "rows": "rows is the list of items in the table.", -} - -func (Table) SwaggerDoc() map[string]string { - return map_Table -} - -var map_TableColumnDefinition = map[string]string{ - "": "TableColumnDefinition contains information about a column returned in the Table.", - "name": "name is a human readable name for the column.", - "type": "type is an OpenAPI type definition for this column. See https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#data-types for more.", - "format": "format is an optional OpenAPI type definition for this column. The 'name' format is applied to the primary identifier column to assist in clients identifying column is the resource name. See https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#data-types for more.", - "description": "description is a human readable description of this column.", - "priority": "priority is an integer defining the relative importance of this column compared to others. Lower numbers are considered higher priority. Columns that may be omitted in limited space scenarios should be given a higher priority.", -} - -func (TableColumnDefinition) SwaggerDoc() map[string]string { - return map_TableColumnDefinition -} - -var map_TableOptions = map[string]string{ - "": "TableOptions are used when a Table is requested by the caller.", - "includeObject": "includeObject decides whether to include each object along with its columnar information. Specifying \"None\" will return no object, specifying \"Object\" will return the full object contents, and specifying \"Metadata\" (the default) will return the object's metadata in the PartialObjectMetadata kind in version v1beta1 of the meta.k8s.io API group.", -} - -func (TableOptions) SwaggerDoc() map[string]string { - return map_TableOptions -} - -var map_TableRow = map[string]string{ - "": "TableRow is an individual row in a table.", - "cells": "cells will be as wide as headers and may contain strings, numbers (float64 or int64), booleans, simple maps, or lists, or null. See the type field of the column definition for a more detailed description.", - "conditions": "conditions describe additional status of a row that are relevant for a human user.", - "object": "This field contains the requested additional information about each object based on the includeObject policy when requesting the Table. If \"None\", this field is empty, if \"Object\" this will be the default serialization of the object for the current API version, and if \"Metadata\" (the default) will contain the object metadata. Check the returned kind and apiVersion of the object before parsing.", -} - -func (TableRow) SwaggerDoc() map[string]string { - return map_TableRow -} - -var map_TableRowCondition = map[string]string{ - "": "TableRowCondition allows a row to be marked with additional information.", - "type": "Type of row condition.", - "status": "Status of the condition, one of True, False, Unknown.", - "reason": "(brief) machine readable reason for the condition's last transition.", - "message": "Human readable message indicating details about last transition.", -} - -func (TableRowCondition) SwaggerDoc() map[string]string { - return map_TableRowCondition -} - // AUTO-GENERATED FUNCTIONS END HERE diff --git a/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1beta1/validation/BUILD b/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1beta1/validation/BUILD index f2515b972f0..cbf11f06dec 100644 --- a/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1beta1/validation/BUILD +++ b/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1beta1/validation/BUILD @@ -7,6 +7,7 @@ go_library( importpath = "k8s.io/apimachinery/pkg/apis/meta/v1beta1/validation", visibility = ["//visibility:public"], deps = [ + "//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library", "//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1beta1:go_default_library", "//staging/src/k8s.io/apimachinery/pkg/util/validation/field:go_default_library", ], diff --git a/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1beta1/validation/validation.go b/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1beta1/validation/validation.go index 28256349018..70fc01a34eb 100644 --- a/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1beta1/validation/validation.go +++ b/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1beta1/validation/validation.go @@ -17,6 +17,7 @@ limitations under the License. package validation import ( + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1beta1 "k8s.io/apimachinery/pkg/apis/meta/v1beta1" "k8s.io/apimachinery/pkg/util/validation/field" ) @@ -25,7 +26,7 @@ import ( func ValidateTableOptions(opts *metav1beta1.TableOptions) field.ErrorList { var allErrs field.ErrorList switch opts.IncludeObject { - case metav1beta1.IncludeMetadata, metav1beta1.IncludeNone, metav1beta1.IncludeObject, "": + case metav1.IncludeMetadata, metav1.IncludeNone, metav1.IncludeObject, "": default: allErrs = append(allErrs, field.Invalid(field.NewPath("includeObject"), opts.IncludeObject, "must be 'Metadata', 'Object', 'None', or empty")) } diff --git a/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1beta1/zz_generated.deepcopy.go b/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1beta1/zz_generated.deepcopy.go index 74c370149f2..9c21e91f13f 100644 --- a/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1beta1/zz_generated.deepcopy.go +++ b/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1beta1/zz_generated.deepcopy.go @@ -21,35 +21,10 @@ limitations under the License. package v1beta1 import ( + v1 "k8s.io/apimachinery/pkg/apis/meta/v1" runtime "k8s.io/apimachinery/pkg/runtime" ) -// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *PartialObjectMetadata) DeepCopyInto(out *PartialObjectMetadata) { - *out = *in - out.TypeMeta = in.TypeMeta - in.ObjectMeta.DeepCopyInto(&out.ObjectMeta) - return -} - -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new PartialObjectMetadata. -func (in *PartialObjectMetadata) DeepCopy() *PartialObjectMetadata { - if in == nil { - return nil - } - out := new(PartialObjectMetadata) - in.DeepCopyInto(out) - return out -} - -// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. -func (in *PartialObjectMetadata) DeepCopyObject() runtime.Object { - if c := in.DeepCopy(); c != nil { - return c - } - return nil -} - // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *PartialObjectMetadataList) DeepCopyInto(out *PartialObjectMetadataList) { *out = *in @@ -57,11 +32,11 @@ func (in *PartialObjectMetadataList) DeepCopyInto(out *PartialObjectMetadataList out.ListMeta = in.ListMeta if in.Items != nil { in, out := &in.Items, &out.Items - *out = make([]*PartialObjectMetadata, len(*in)) + *out = make([]*v1.PartialObjectMetadata, len(*in)) for i := range *in { if (*in)[i] != nil { in, out := &(*in)[i], &(*out)[i] - *out = new(PartialObjectMetadata) + *out = new(v1.PartialObjectMetadata) (*in).DeepCopyInto(*out) } } @@ -86,105 +61,3 @@ func (in *PartialObjectMetadataList) DeepCopyObject() runtime.Object { } return nil } - -// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *Table) DeepCopyInto(out *Table) { - *out = *in - out.TypeMeta = in.TypeMeta - out.ListMeta = in.ListMeta - if in.ColumnDefinitions != nil { - in, out := &in.ColumnDefinitions, &out.ColumnDefinitions - *out = make([]TableColumnDefinition, len(*in)) - copy(*out, *in) - } - if in.Rows != nil { - in, out := &in.Rows, &out.Rows - *out = make([]TableRow, len(*in)) - for i := range *in { - (*in)[i].DeepCopyInto(&(*out)[i]) - } - } - return -} - -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Table. -func (in *Table) DeepCopy() *Table { - if in == nil { - return nil - } - out := new(Table) - in.DeepCopyInto(out) - return out -} - -// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. -func (in *Table) DeepCopyObject() runtime.Object { - if c := in.DeepCopy(); c != nil { - return c - } - return nil -} - -// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *TableColumnDefinition) DeepCopyInto(out *TableColumnDefinition) { - *out = *in - return -} - -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new TableColumnDefinition. -func (in *TableColumnDefinition) DeepCopy() *TableColumnDefinition { - if in == nil { - return nil - } - out := new(TableColumnDefinition) - in.DeepCopyInto(out) - return out -} - -// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *TableOptions) DeepCopyInto(out *TableOptions) { - *out = *in - out.TypeMeta = in.TypeMeta - return -} - -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new TableOptions. -func (in *TableOptions) DeepCopy() *TableOptions { - if in == nil { - return nil - } - out := new(TableOptions) - in.DeepCopyInto(out) - return out -} - -// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. -func (in *TableOptions) DeepCopyObject() runtime.Object { - if c := in.DeepCopy(); c != nil { - return c - } - return nil -} - -// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *TableRow) DeepCopyInto(out *TableRow) { - clone := in.DeepCopy() - *out = *clone - return -} - -// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *TableRowCondition) DeepCopyInto(out *TableRowCondition) { - *out = *in - return -} - -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new TableRowCondition. -func (in *TableRowCondition) DeepCopy() *TableRowCondition { - if in == nil { - return nil - } - out := new(TableRowCondition) - in.DeepCopyInto(out) - return out -} From 33a3e325f754d179b25558dee116fca1c67d353a Mon Sep 17 00:00:00 2001 From: Clayton Coleman Date: Sat, 4 May 2019 16:55:49 -0400 Subject: [PATCH 2/2] API server should offer metav1 Table/Partial transforms Now that internal types are equivalent, allow the apiserver to serve metav1 and metav1beta1 depending on the client. Test that in the apiserver integration test and ensure we get the appropriate responses. Register the metav1 type in the appropriate external locations. --- pkg/kubectl/scheme/install.go | 3 +- pkg/printers/internalversion/printers.go | 12 +- .../test/integration/table_test.go | 4 +- .../k8s.io/apimachinery/pkg/api/meta/BUILD | 1 - .../k8s.io/apimachinery/pkg/api/meta/meta.go | 7 +- .../apimachinery/pkg/apis/meta/v1/register.go | 3 - .../apiserver/pkg/endpoints/apiserver_test.go | 165 +++++-- .../pkg/endpoints/handlers/response.go | 107 +++-- .../apiserver/pkg/endpoints/handlers/rest.go | 2 +- test/integration/apiserver/apiserver_test.go | 401 ++++++++++++++++-- 10 files changed, 594 insertions(+), 111 deletions(-) diff --git a/pkg/kubectl/scheme/install.go b/pkg/kubectl/scheme/install.go index fd25c730e67..76a0578dc19 100644 --- a/pkg/kubectl/scheme/install.go +++ b/pkg/kubectl/scheme/install.go @@ -57,7 +57,8 @@ import ( func init() { // Register external types for Scheme metav1.AddToGroupVersion(Scheme, schema.GroupVersion{Version: "v1"}) - utilruntime.Must(metav1beta1.AddToScheme(Scheme)) + utilruntime.Must(metav1beta1.AddMetaToScheme(Scheme)) + utilruntime.Must(metav1.AddMetaToScheme(Scheme)) utilruntime.Must(scheme.AddToScheme(Scheme)) utilruntime.Must(Scheme.SetVersionPriority(corev1.SchemeGroupVersion)) diff --git a/pkg/printers/internalversion/printers.go b/pkg/printers/internalversion/printers.go index dbb81ca8d6f..cc50fb9547c 100644 --- a/pkg/printers/internalversion/printers.go +++ b/pkg/printers/internalversion/printers.go @@ -539,12 +539,12 @@ func translateTimestampUntil(timestamp metav1.Time) string { } var ( - podSuccessConditions = []metav1beta1.TableRowCondition{{Type: metav1beta1.RowCompleted, Status: metav1beta1.ConditionTrue, Reason: string(api.PodSucceeded), Message: "The pod has completed successfully."}} - podFailedConditions = []metav1beta1.TableRowCondition{{Type: metav1beta1.RowCompleted, Status: metav1beta1.ConditionTrue, Reason: string(api.PodFailed), Message: "The pod failed."}} + podSuccessConditions = []metav1.TableRowCondition{{Type: metav1.RowCompleted, Status: metav1.ConditionTrue, Reason: string(api.PodSucceeded), Message: "The pod has completed successfully."}} + podFailedConditions = []metav1.TableRowCondition{{Type: metav1.RowCompleted, Status: metav1.ConditionTrue, Reason: string(api.PodFailed), Message: "The pod failed."}} ) -func printPodList(podList *api.PodList, options printers.PrintOptions) ([]metav1beta1.TableRow, error) { - rows := make([]metav1beta1.TableRow, 0, len(podList.Items)) +func printPodList(podList *api.PodList, options printers.PrintOptions) ([]metav1.TableRow, error) { + rows := make([]metav1.TableRow, 0, len(podList.Items)) for i := range podList.Items { r, err := printPod(&podList.Items[i], options) if err != nil { @@ -555,7 +555,7 @@ func printPodList(podList *api.PodList, options printers.PrintOptions) ([]metav1 return rows, nil } -func printPod(pod *api.Pod, options printers.PrintOptions) ([]metav1beta1.TableRow, error) { +func printPod(pod *api.Pod, options printers.PrintOptions) ([]metav1.TableRow, error) { restarts := 0 totalContainers := len(pod.Spec.Containers) readyContainers := 0 @@ -565,7 +565,7 @@ func printPod(pod *api.Pod, options printers.PrintOptions) ([]metav1beta1.TableR reason = pod.Status.Reason } - row := metav1beta1.TableRow{ + row := metav1.TableRow{ Object: runtime.RawExtension{Object: pod}, } diff --git a/staging/src/k8s.io/apiextensions-apiserver/test/integration/table_test.go b/staging/src/k8s.io/apiextensions-apiserver/test/integration/table_test.go index 14645504ac7..f2aa2c6a773 100644 --- a/staging/src/k8s.io/apiextensions-apiserver/test/integration/table_test.go +++ b/staging/src/k8s.io/apiextensions-apiserver/test/integration/table_test.go @@ -148,8 +148,10 @@ func TestTableGet(t *testing.T) { codecs := serializer.NewCodecFactory(scheme) parameterCodec := runtime.NewParameterCodec(scheme) metav1.AddToGroupVersion(scheme, gv) - scheme.AddKnownTypes(gv, &metav1beta1.Table{}, &metav1beta1.TableOptions{}) + scheme.AddKnownTypes(gv, &metav1beta1.TableOptions{}) + scheme.AddKnownTypes(gv, &metav1.TableOptions{}) scheme.AddKnownTypes(metav1beta1.SchemeGroupVersion, &metav1beta1.Table{}, &metav1beta1.TableOptions{}) + scheme.AddKnownTypes(metav1.SchemeGroupVersion, &metav1.Table{}, &metav1.TableOptions{}) crConfig := *config crConfig.GroupVersion = &gv diff --git a/staging/src/k8s.io/apimachinery/pkg/api/meta/BUILD b/staging/src/k8s.io/apimachinery/pkg/api/meta/BUILD index 4ced3ce5663..a424f4839d5 100644 --- a/staging/src/k8s.io/apimachinery/pkg/api/meta/BUILD +++ b/staging/src/k8s.io/apimachinery/pkg/api/meta/BUILD @@ -42,7 +42,6 @@ go_library( importpath = "k8s.io/apimachinery/pkg/api/meta", deps = [ "//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library", - "//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1beta1:go_default_library", "//staging/src/k8s.io/apimachinery/pkg/conversion:go_default_library", "//staging/src/k8s.io/apimachinery/pkg/runtime:go_default_library", "//staging/src/k8s.io/apimachinery/pkg/runtime/schema:go_default_library", diff --git a/staging/src/k8s.io/apimachinery/pkg/api/meta/meta.go b/staging/src/k8s.io/apimachinery/pkg/api/meta/meta.go index b50337e13f4..086bce04b0a 100644 --- a/staging/src/k8s.io/apimachinery/pkg/api/meta/meta.go +++ b/staging/src/k8s.io/apimachinery/pkg/api/meta/meta.go @@ -21,7 +21,6 @@ import ( "reflect" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - metav1beta1 "k8s.io/apimachinery/pkg/apis/meta/v1beta1" "k8s.io/apimachinery/pkg/conversion" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime/schema" @@ -114,12 +113,12 @@ func Accessor(obj interface{}) (metav1.Object, error) { // AsPartialObjectMetadata takes the metav1 interface and returns a partial object. // TODO: consider making this solely a conversion action. -func AsPartialObjectMetadata(m metav1.Object) *metav1beta1.PartialObjectMetadata { +func AsPartialObjectMetadata(m metav1.Object) *metav1.PartialObjectMetadata { switch t := m.(type) { case *metav1.ObjectMeta: - return &metav1beta1.PartialObjectMetadata{ObjectMeta: *t} + return &metav1.PartialObjectMetadata{ObjectMeta: *t} default: - return &metav1beta1.PartialObjectMetadata{ + return &metav1.PartialObjectMetadata{ ObjectMeta: metav1.ObjectMeta{ Name: m.GetName(), GenerateName: m.GetGenerateName(), diff --git a/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/register.go b/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/register.go index 24fc134150f..368efe1efd9 100644 --- a/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/register.go +++ b/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/register.go @@ -113,7 +113,4 @@ func AddMetaToScheme(scheme *runtime.Scheme) error { return scheme.AddConversionFuncs( Convert_Slice_string_To_v1_IncludeObjectPolicy, ) - - // register manually. This usually goes through the SchemeBuilder, which we cannot use here. - //scheme.AddGeneratedDeepCopyFuncs(GetGeneratedDeepCopyFuncs()...) } diff --git a/staging/src/k8s.io/apiserver/pkg/endpoints/apiserver_test.go b/staging/src/k8s.io/apiserver/pkg/endpoints/apiserver_test.go index 61583fab34b..594576ba983 100644 --- a/staging/src/k8s.io/apiserver/pkg/endpoints/apiserver_test.go +++ b/staging/src/k8s.io/apiserver/pkg/endpoints/apiserver_test.go @@ -1749,14 +1749,14 @@ func TestGetPretty(t *testing.T) { pretty bool }{ {accept: runtime.ContentTypeJSON}, - {accept: runtime.ContentTypeJSON + ";pretty=0"}, + {accept: "application/json;pretty=0"}, {accept: runtime.ContentTypeJSON, userAgent: "kubectl"}, {accept: runtime.ContentTypeJSON, params: url.Values{"pretty": {"0"}}}, {pretty: true, accept: runtime.ContentTypeJSON, userAgent: "curl"}, {pretty: true, accept: runtime.ContentTypeJSON, userAgent: "Mozilla/5.0"}, {pretty: true, accept: runtime.ContentTypeJSON, userAgent: "Wget"}, - {pretty: true, accept: runtime.ContentTypeJSON + ";pretty=1"}, + {pretty: true, accept: "application/json;pretty=1"}, {pretty: true, accept: runtime.ContentTypeJSON, params: url.Values{"pretty": {"1"}}}, {pretty: true, accept: runtime.ContentTypeJSON, params: url.Values{"pretty": {"true"}}}, } @@ -1818,14 +1818,28 @@ func TestGetTable(t *testing.T) { if err != nil { t.Fatal(err) } - partial := meta.AsPartialObjectMetadata(m) - partial.GetObjectKind().SetGroupVersionKind(metav1beta1.SchemeGroupVersion.WithKind("PartialObjectMetadata")) - encodedBody, err := runtime.Encode(metainternalversion.Codecs.LegacyCodec(metav1beta1.SchemeGroupVersion), partial) - if err != nil { - t.Fatal(err) + var encodedV1Beta1Body []byte + { + partial := meta.AsPartialObjectMetadata(m) + partial.GetObjectKind().SetGroupVersionKind(metav1beta1.SchemeGroupVersion.WithKind("PartialObjectMetadata")) + encodedBody, err := runtime.Encode(metainternalversion.Codecs.LegacyCodec(metav1beta1.SchemeGroupVersion), partial) + if err != nil { + t.Fatal(err) + } + // the codec includes a trailing newline that is not present during decode + encodedV1Beta1Body = bytes.TrimSpace(encodedBody) + } + var encodedV1Body []byte + { + partial := meta.AsPartialObjectMetadata(m) + partial.GetObjectKind().SetGroupVersionKind(metav1.SchemeGroupVersion.WithKind("PartialObjectMetadata")) + encodedBody, err := runtime.Encode(metainternalversion.Codecs.LegacyCodec(metav1.SchemeGroupVersion), partial) + if err != nil { + t.Fatal(err) + } + // the codec includes a trailing newline that is not present during decode + encodedV1Body = bytes.TrimSpace(encodedBody) } - // the codec includes a trailing newline that is not present during decode - encodedBody = bytes.TrimSpace(encodedBody) metaDoc := metav1.ObjectMeta{}.SwaggerDoc() @@ -1838,16 +1852,36 @@ func TestGetTable(t *testing.T) { item bool }{ { - accept: runtime.ContentTypeJSON + ";as=Table;v=v1;g=meta.k8s.io", + accept: "application/json;as=Table;v=v1alpha1;g=meta.k8s.io", statusCode: http.StatusNotAcceptable, }, { accept: runtime.ContentTypeProtobuf + ";as=Table;v=v1beta1;g=meta.k8s.io", statusCode: http.StatusNotAcceptable, }, + { + accept: runtime.ContentTypeProtobuf + ";as=Table;v=v1;g=meta.k8s.io", + statusCode: http.StatusNotAcceptable, + }, + { item: true, - accept: runtime.ContentTypeJSON + ";as=Table;v=v1beta1;g=meta.k8s.io", + accept: "application/json;as=Table;v=v1;g=meta.k8s.io", + expected: &metav1.Table{ + TypeMeta: metav1.TypeMeta{Kind: "Table", APIVersion: "meta.k8s.io/v1"}, + ListMeta: metav1.ListMeta{ResourceVersion: "10", SelfLink: "/blah"}, + ColumnDefinitions: []metav1.TableColumnDefinition{ + {Name: "Name", Type: "string", Format: "name", Description: metaDoc["name"]}, + {Name: "Created At", Type: "date", Description: metaDoc["creationTimestamp"]}, + }, + Rows: []metav1.TableRow{ + {Cells: []interface{}{"foo1", now.Time.UTC().Format(time.RFC3339)}, Object: runtime.RawExtension{Raw: encodedV1Body}}, + }, + }, + }, + { + item: true, + accept: "application/json;as=Table;v=v1beta1;g=meta.k8s.io", expected: &metav1beta1.Table{ TypeMeta: metav1.TypeMeta{Kind: "Table", APIVersion: "meta.k8s.io/v1beta1"}, ListMeta: metav1.ListMeta{ResourceVersion: "10", SelfLink: "/blah"}, @@ -1856,7 +1890,7 @@ func TestGetTable(t *testing.T) { {Name: "Created At", Type: "date", Description: metaDoc["creationTimestamp"]}, }, Rows: []metav1beta1.TableRow{ - {Cells: []interface{}{"foo1", now.Time.UTC().Format(time.RFC3339)}, Object: runtime.RawExtension{Raw: encodedBody}}, + {Cells: []interface{}{"foo1", now.Time.UTC().Format(time.RFC3339)}, Object: runtime.RawExtension{Raw: encodedV1Beta1Body}}, }, }, }, @@ -1864,7 +1898,7 @@ func TestGetTable(t *testing.T) { item: true, accept: strings.Join([]string{ runtime.ContentTypeProtobuf + ";as=Table;v=v1beta1;g=meta.k8s.io", - runtime.ContentTypeJSON + ";as=Table;v=v1beta1;g=meta.k8s.io", + "application/json;as=Table;v=v1beta1;g=meta.k8s.io", }, ","), expected: &metav1beta1.Table{ TypeMeta: metav1.TypeMeta{Kind: "Table", APIVersion: "meta.k8s.io/v1beta1"}, @@ -1874,13 +1908,13 @@ func TestGetTable(t *testing.T) { {Name: "Created At", Type: "date", Description: metaDoc["creationTimestamp"]}, }, Rows: []metav1beta1.TableRow{ - {Cells: []interface{}{"foo1", now.Time.UTC().Format(time.RFC3339)}, Object: runtime.RawExtension{Raw: encodedBody}}, + {Cells: []interface{}{"foo1", now.Time.UTC().Format(time.RFC3339)}, Object: runtime.RawExtension{Raw: encodedV1Beta1Body}}, }, }, }, { item: true, - accept: runtime.ContentTypeJSON + ";as=Table;v=v1beta1;g=meta.k8s.io", + accept: "application/json;as=Table;v=v1beta1;g=meta.k8s.io", params: url.Values{"includeObject": []string{"Metadata"}}, expected: &metav1beta1.Table{ TypeMeta: metav1.TypeMeta{Kind: "Table", APIVersion: "meta.k8s.io/v1beta1"}, @@ -1890,12 +1924,12 @@ func TestGetTable(t *testing.T) { {Name: "Created At", Type: "date", Description: metaDoc["creationTimestamp"]}, }, Rows: []metav1beta1.TableRow{ - {Cells: []interface{}{"foo1", now.Time.UTC().Format(time.RFC3339)}, Object: runtime.RawExtension{Raw: encodedBody}}, + {Cells: []interface{}{"foo1", now.Time.UTC().Format(time.RFC3339)}, Object: runtime.RawExtension{Raw: encodedV1Beta1Body}}, }, }, }, { - accept: runtime.ContentTypeJSON + ";as=Table;v=v1beta1;g=meta.k8s.io", + accept: "application/json;as=Table;v=v1beta1;g=meta.k8s.io", params: url.Values{"includeObject": []string{"Metadata"}}, expected: &metav1beta1.Table{ TypeMeta: metav1.TypeMeta{Kind: "Table", APIVersion: "meta.k8s.io/v1beta1"}, @@ -1905,7 +1939,7 @@ func TestGetTable(t *testing.T) { {Name: "Created At", Type: "date", Description: metaDoc["creationTimestamp"]}, }, Rows: []metav1beta1.TableRow{ - {Cells: []interface{}{"foo1", now.Time.UTC().Format(time.RFC3339)}, Object: runtime.RawExtension{Raw: encodedBody}}, + {Cells: []interface{}{"foo1", now.Time.UTC().Format(time.RFC3339)}, Object: runtime.RawExtension{Raw: encodedV1Beta1Body}}, }, }, }, @@ -1996,6 +2030,13 @@ func TestWatchTable(t *testing.T) { // the codec includes a trailing newline that is not present during decode encodedBody = bytes.TrimSpace(encodedBody) + encodedBodyV1, err := runtime.Encode(metainternalversion.Codecs.LegacyCodec(metav1.SchemeGroupVersion), partial) + if err != nil { + t.Fatal(err) + } + // the codec includes a trailing newline that is not present during decode + encodedBodyV1 = bytes.TrimSpace(encodedBodyV1) + metaDoc := metav1.ObjectMeta{}.SwaggerDoc() s := metainternalversion.Codecs.SupportedMediaTypes()[0].Serializer @@ -2011,11 +2052,11 @@ func TestWatchTable(t *testing.T) { item bool }{ { - accept: runtime.ContentTypeJSON + ";as=Table;v=v1;g=meta.k8s.io", + accept: "application/json;as=Table;v=v1alpha1;g=meta.k8s.io", statusCode: http.StatusNotAcceptable, }, { - accept: runtime.ContentTypeJSON + ";as=Table;v=v1beta1;g=meta.k8s.io", + accept: "application/json;as=Table;v=v1beta1;g=meta.k8s.io", send: func(w *watch.FakeWatcher) { w.Add(&obj) }, @@ -2039,7 +2080,7 @@ func TestWatchTable(t *testing.T) { }, }, { - accept: runtime.ContentTypeJSON + ";as=Table;v=v1beta1;g=meta.k8s.io", + accept: "application/json;as=Table;v=v1beta1;g=meta.k8s.io", send: func(w *watch.FakeWatcher) { w.Add(&obj) w.Modify(&obj) @@ -2075,6 +2116,43 @@ func TestWatchTable(t *testing.T) { }, }, }, + { + accept: "application/json;as=Table;v=v1;g=meta.k8s.io", + send: func(w *watch.FakeWatcher) { + w.Add(&obj) + w.Modify(&obj) + }, + expected: []*metav1.WatchEvent{ + { + Type: "ADDED", + Object: runtime.RawExtension{ + Raw: []byte(strings.TrimSpace(runtime.EncodeOrDie(s, &metav1.Table{ + TypeMeta: metav1.TypeMeta{Kind: "Table", APIVersion: "meta.k8s.io/v1"}, + ListMeta: metav1.ListMeta{ResourceVersion: "10", SelfLink: "/blah"}, + ColumnDefinitions: []metav1beta1.TableColumnDefinition{ + {Name: "Name", Type: "string", Format: "name", Description: metaDoc["name"]}, + {Name: "Created At", Type: "date", Description: metaDoc["creationTimestamp"]}, + }, + Rows: []metav1.TableRow{ + {Cells: []interface{}{"foo1", time.Unix(1, 0).UTC().Format(time.RFC3339)}, Object: runtime.RawExtension{Raw: encodedBodyV1}}, + }, + }))), + }, + }, + { + Type: "MODIFIED", + Object: runtime.RawExtension{ + Raw: []byte(strings.TrimSpace(runtime.EncodeOrDie(s, &metav1.Table{ + TypeMeta: metav1.TypeMeta{Kind: "Table", APIVersion: "meta.k8s.io/v1"}, + ListMeta: metav1.ListMeta{ResourceVersion: "10", SelfLink: "/blah"}, + Rows: []metav1.TableRow{ + {Cells: []interface{}{"foo1", time.Unix(1, 0).UTC().Format(time.RFC3339)}, Object: runtime.RawExtension{Raw: encodedBodyV1}}, + }, + }))), + }, + }, + }, + }, } for i, test := range tests { t.Run(fmt.Sprintf("%d", i), func(t *testing.T) { @@ -2122,6 +2200,7 @@ func TestWatchTable(t *testing.T) { if err != nil { t.Fatal(err) } + defer resp.Body.Close() if test.statusCode != 0 { if resp.StatusCode != test.statusCode { t.Fatalf("%d: unexpected response: %#v", i, resp) @@ -2228,46 +2307,72 @@ func TestGetPartialObjectMetadata(t *testing.T) { statusCode int }{ { - accept: runtime.ContentTypeJSON + ";as=PartialObjectMetadata;v=v1;g=meta.k8s.io", + accept: "application/json;as=PartialObjectMetadata;v=v1alpha1;g=meta.k8s.io", statusCode: http.StatusNotAcceptable, }, { - accept: runtime.ContentTypeJSON + ";as=PartialObjectMetadata;v=v1;g=meta.k8s.io, application/json", + accept: "application/json;as=PartialObjectMetadata;v=v1alpha1;g=meta.k8s.io, application/json", expectKind: schema.GroupVersionKind{Kind: "Simple", Group: testGroupVersion.Group, Version: testGroupVersion.Version}, }, { - accept: runtime.ContentTypeJSON + ";as=PartialObjectMetadata;v=v1beta1;g=meta.k8s.io, application/json", + accept: "application/json;as=PartialObjectMetadata;v=v1beta1;g=meta.k8s.io, application/json", expectKind: schema.GroupVersionKind{Kind: "PartialObjectMetadata", Group: "meta.k8s.io", Version: "v1beta1"}, }, { list: true, - accept: runtime.ContentTypeJSON + ";as=PartialObjectMetadata;v=v1beta1;g=meta.k8s.io", + accept: "application/json;as=PartialObjectMetadata;v=v1beta1;g=meta.k8s.io", statusCode: http.StatusNotAcceptable, }, + + // verify preferred version overrides supported version + { + accept: "application/json;as=PartialObjectMetadata;v=v1beta1;g=meta.k8s.io, application/json;as=PartialObjectMetadata;v=v1;g=meta.k8s.io, application/json", + expectKind: schema.GroupVersionKind{Kind: "PartialObjectMetadata", Group: "meta.k8s.io", Version: "v1beta1"}, + }, + { + accept: "application/json;as=PartialObjectMetadata;v=v1;g=meta.k8s.io, application/json;as=PartialObjectMetadata;v=v1beta1;g=meta.k8s.io, application/json", + expectKind: schema.GroupVersionKind{Kind: "PartialObjectMetadata", Group: "meta.k8s.io", Version: "v1"}, + }, + { + accept: "application/json;as=PartialObjectMetadata;v=v1beta1;g=meta.k8s.io, application/json;as=PartialObjectMetadata;v=v1;g=meta.k8s.io", + expectKind: schema.GroupVersionKind{Kind: "PartialObjectMetadata", Group: "meta.k8s.io", Version: "v1beta1"}, + }, + { + accept: "application/json;as=PartialObjectMetadata;v=v1;g=meta.k8s.io, application/json;as=PartialObjectMetadata;v=v1beta1;g=meta.k8s.io", + expectKind: schema.GroupVersionKind{Kind: "PartialObjectMetadata", Group: "meta.k8s.io", Version: "v1"}, + }, + { list: true, - accept: runtime.ContentTypeJSON + ";as=PartialObjectMetadata;v=v1;g=meta.k8s.io, application/json", + accept: "application/json;as=PartialObjectMetadata;v=v1alpha1;g=meta.k8s.io, application/json", expectKind: schema.GroupVersionKind{Kind: "SimpleList", Group: testGroupVersion.Group, Version: testGroupVersion.Version}, }, { list: true, - accept: runtime.ContentTypeJSON + ";as=PartialObjectMetadataList;v=v1beta1;g=meta.k8s.io, application/json", + accept: "application/json;as=PartialObjectMetadataList;v=v1beta1;g=meta.k8s.io, application/json", expectKind: schema.GroupVersionKind{Kind: "PartialObjectMetadataList", Group: "meta.k8s.io", Version: "v1beta1"}, }, { - accept: runtime.ContentTypeJSON + ";as=PartialObjectMetadataList;v=v1beta1;g=meta.k8s.io", + accept: "application/json;as=PartialObjectMetadataList;v=v1beta1;g=meta.k8s.io", statusCode: http.StatusNotAcceptable, }, { - accept: runtime.ContentTypeJSON + ";as=PartialObjectMetadata;v=v1beta1;g=meta.k8s.io", + accept: "application/json;as=PartialObjectMetadata;v=v1beta1;g=meta.k8s.io", expected: &metav1beta1.PartialObjectMetadata{ ObjectMeta: metav1.ObjectMeta{Name: "foo1", Namespace: "ns1", CreationTimestamp: now, UID: types.UID("abcdef0123")}, }, expectKind: schema.GroupVersionKind{Kind: "PartialObjectMetadata", Group: "meta.k8s.io", Version: "v1beta1"}, }, + { + accept: "application/json;as=PartialObjectMetadata;v=v1;g=meta.k8s.io", + expected: &metav1.PartialObjectMetadata{ + ObjectMeta: metav1.ObjectMeta{Name: "foo1", Namespace: "ns1", CreationTimestamp: now, UID: types.UID("abcdef0123")}, + }, + expectKind: schema.GroupVersionKind{Kind: "PartialObjectMetadata", Group: "meta.k8s.io", Version: "v1"}, + }, { list: true, - accept: runtime.ContentTypeJSON + ";as=PartialObjectMetadataList;v=v1beta1;g=meta.k8s.io", + accept: "application/json;as=PartialObjectMetadataList;v=v1beta1;g=meta.k8s.io", expected: &metav1beta1.PartialObjectMetadataList{ ListMeta: metav1.ListMeta{ ResourceVersion: "10", diff --git a/staging/src/k8s.io/apiserver/pkg/endpoints/handlers/response.go b/staging/src/k8s.io/apiserver/pkg/endpoints/handlers/response.go index 214dbc1e7d2..0fe8a71c728 100644 --- a/staging/src/k8s.io/apiserver/pkg/endpoints/handlers/response.go +++ b/staging/src/k8s.io/apiserver/pkg/endpoints/handlers/response.go @@ -49,18 +49,18 @@ func transformObject(ctx context.Context, obj runtime.Object, opts interface{}, case target == nil: return obj, nil - case target.Kind == "PartialObjectMetadata" && target.GroupVersion() == metav1beta1.SchemeGroupVersion: - return asV1Beta1PartialObjectMetadata(obj) + case target.Kind == "PartialObjectMetadata": + return asPartialObjectMetadata(obj, target.GroupVersion()) - case target.Kind == "PartialObjectMetadataList" && target.GroupVersion() == metav1beta1.SchemeGroupVersion: - return asV1Beta1PartialObjectMetadataList(obj) + case target.Kind == "PartialObjectMetadataList": + return asPartialObjectMetadataList(obj, target.GroupVersion()) - case target.Kind == "Table" && target.GroupVersion() == metav1beta1.SchemeGroupVersion: + case target.Kind == "Table": options, ok := opts.(*metav1beta1.TableOptions) if !ok { return nil, fmt.Errorf("unexpected TableOptions, got %T", opts) } - return asV1Beta1Table(ctx, obj, options, scope) + return asTable(ctx, obj, options, scope, target.GroupVersion()) default: accepted, _ := negotiation.MediaTypesForSerializer(metainternalversion.Codecs) @@ -74,7 +74,7 @@ func transformObject(ctx context.Context, obj runtime.Object, opts interface{}, func optionsForTransform(mediaType negotiation.MediaTypeOptions, req *http.Request) (interface{}, error) { switch target := mediaType.Convert; { case target == nil: - case target.Kind == "Table" && target.GroupVersion() == metav1beta1.SchemeGroupVersion: + case target.Kind == "Table" && (target.GroupVersion() == metav1beta1.SchemeGroupVersion || target.GroupVersion() == metav1.SchemeGroupVersion): opts := &metav1beta1.TableOptions{} if err := metav1beta1.ParameterCodec.DecodeParameters(req.URL.Query(), metav1beta1.SchemeGroupVersion, opts); err != nil { return nil, err @@ -95,9 +95,8 @@ func optionsForTransform(mediaType negotiation.MediaTypeOptions, req *http.Reque func targetEncodingForTransform(scope *RequestScope, mediaType negotiation.MediaTypeOptions, req *http.Request) (schema.GroupVersionKind, runtime.NegotiatedSerializer, bool) { switch target := mediaType.Convert; { case target == nil: - case target.Kind == "PartialObjectMetadata" && target.GroupVersion() == metav1beta1.SchemeGroupVersion, - target.Kind == "PartialObjectMetadataList" && target.GroupVersion() == metav1beta1.SchemeGroupVersion, - target.Kind == "Table" && target.GroupVersion() == metav1beta1.SchemeGroupVersion: + case (target.Kind == "PartialObjectMetadata" || target.Kind == "PartialObjectMetadataList" || target.Kind == "Table") && + (target.GroupVersion() == metav1beta1.SchemeGroupVersion || target.GroupVersion() == metav1.SchemeGroupVersion): return *target, metainternalversion.Codecs, true } return scope.Kind, scope.Serializer, false @@ -142,31 +141,39 @@ func (e errNotAcceptable) Status() metav1.Status { } } -func asV1Beta1Table(ctx context.Context, result runtime.Object, opts *metav1beta1.TableOptions, scope *RequestScope) (runtime.Object, error) { - table, err := scope.TableConvertor.ConvertToTable(ctx, result, opts) +func asTable(ctx context.Context, result runtime.Object, opts *metav1beta1.TableOptions, scope *RequestScope, groupVersion schema.GroupVersion) (runtime.Object, error) { + switch groupVersion { + case metav1beta1.SchemeGroupVersion, metav1.SchemeGroupVersion: + default: + return nil, newNotAcceptableError(fmt.Sprintf("no Table exists in group version %s", groupVersion)) + } + + obj, err := scope.TableConvertor.ConvertToTable(ctx, result, opts) if err != nil { return nil, err } + table := (*metav1.Table)(obj) + for i := range table.Rows { item := &table.Rows[i] switch opts.IncludeObject { - case metav1beta1.IncludeObject: + case metav1.IncludeObject: item.Object.Object, err = scope.Convertor.ConvertToVersion(item.Object.Object, scope.Kind.GroupVersion()) if err != nil { return nil, err } // TODO: rely on defaulting for the value here? - case metav1beta1.IncludeMetadata, "": + case metav1.IncludeMetadata, "": m, err := meta.Accessor(item.Object.Object) if err != nil { return nil, err } // TODO: turn this into an internal type and do conversion in order to get object kind automatically set? partial := meta.AsPartialObjectMetadata(m) - partial.GetObjectKind().SetGroupVersionKind(metav1beta1.SchemeGroupVersion.WithKind("PartialObjectMetadata")) + partial.GetObjectKind().SetGroupVersionKind(groupVersion.WithKind("PartialObjectMetadata")) item.Object.Object = partial - case metav1beta1.IncludeNone: + case metav1.IncludeNone: item.Object.Object = nil default: err = errors.NewBadRequest(fmt.Sprintf("unrecognized includeObject value: %q", opts.IncludeObject)) @@ -177,42 +184,74 @@ func asV1Beta1Table(ctx context.Context, result runtime.Object, opts *metav1beta return table, nil } -func asV1Beta1PartialObjectMetadata(result runtime.Object) (runtime.Object, error) { +func asPartialObjectMetadata(result runtime.Object, groupVersion schema.GroupVersion) (runtime.Object, error) { if meta.IsListType(result) { err := newNotAcceptableError(fmt.Sprintf("you requested PartialObjectMetadata, but the requested object is a list (%T)", result)) return nil, err } + switch groupVersion { + case metav1beta1.SchemeGroupVersion, metav1.SchemeGroupVersion: + default: + return nil, newNotAcceptableError(fmt.Sprintf("no PartialObjectMetadataList exists in group version %s", groupVersion)) + } m, err := meta.Accessor(result) if err != nil { return nil, err } partial := meta.AsPartialObjectMetadata(m) - partial.GetObjectKind().SetGroupVersionKind(metav1beta1.SchemeGroupVersion.WithKind("PartialObjectMetadata")) + partial.GetObjectKind().SetGroupVersionKind(groupVersion.WithKind("PartialObjectMetadata")) return partial, nil } -func asV1Beta1PartialObjectMetadataList(result runtime.Object) (runtime.Object, error) { - if !meta.IsListType(result) { +func asPartialObjectMetadataList(result runtime.Object, groupVersion schema.GroupVersion) (runtime.Object, error) { + li, ok := result.(metav1.ListInterface) + if !ok { return nil, newNotAcceptableError(fmt.Sprintf("you requested PartialObjectMetadataList, but the requested object is not a list (%T)", result)) } - list := &metav1beta1.PartialObjectMetadataList{} - if li, ok := result.(metav1.ListInterface); ok { + + gvk := groupVersion.WithKind("PartialObjectMetadata") + switch { + case groupVersion == metav1beta1.SchemeGroupVersion: + list := &metav1beta1.PartialObjectMetadataList{} + err := meta.EachListItem(result, func(obj runtime.Object) error { + m, err := meta.Accessor(obj) + if err != nil { + return err + } + partial := meta.AsPartialObjectMetadata(m) + partial.GetObjectKind().SetGroupVersionKind(gvk) + list.Items = append(list.Items, partial) + return nil + }) + if err != nil { + return nil, err + } list.SelfLink = li.GetSelfLink() list.ResourceVersion = li.GetResourceVersion() list.Continue = li.GetContinue() - } - err := meta.EachListItem(result, func(obj runtime.Object) error { - m, err := meta.Accessor(obj) + return list, nil + + case groupVersion == metav1.SchemeGroupVersion: + list := &metav1.PartialObjectMetadataList{} + err := meta.EachListItem(result, func(obj runtime.Object) error { + m, err := meta.Accessor(obj) + if err != nil { + return err + } + partial := meta.AsPartialObjectMetadata(m) + partial.GetObjectKind().SetGroupVersionKind(gvk) + list.Items = append(list.Items, partial) + return nil + }) if err != nil { - return err + return nil, err } - partial := meta.AsPartialObjectMetadata(m) - partial.GetObjectKind().SetGroupVersionKind(metav1beta1.SchemeGroupVersion.WithKind("PartialObjectMetadata")) - list.Items = append(list.Items, partial) - return nil - }) - if err != nil { - return nil, err + list.SelfLink = li.GetSelfLink() + list.ResourceVersion = li.GetResourceVersion() + list.Continue = li.GetContinue() + return list, nil + + default: + return nil, newNotAcceptableError(fmt.Sprintf("no PartialObjectMetadataList exists in group version %s", groupVersion)) } - return list, nil } diff --git a/staging/src/k8s.io/apiserver/pkg/endpoints/handlers/rest.go b/staging/src/k8s.io/apiserver/pkg/endpoints/handlers/rest.go index 6adbf84955d..4db0c067627 100644 --- a/staging/src/k8s.io/apiserver/pkg/endpoints/handlers/rest.go +++ b/staging/src/k8s.io/apiserver/pkg/endpoints/handlers/rest.go @@ -78,7 +78,7 @@ func (scope *RequestScope) err(err error, w http.ResponseWriter, req *http.Reque func (scope *RequestScope) AllowsConversion(gvk schema.GroupVersionKind, mimeType, mimeSubType string) bool { // TODO: this is temporary, replace with an abstraction calculated at endpoint installation time - if gvk.GroupVersion() == metav1beta1.SchemeGroupVersion { + if gvk.GroupVersion() == metav1beta1.SchemeGroupVersion || gvk.GroupVersion() == metav1.SchemeGroupVersion { switch gvk.Kind { case "Table": return scope.TableConvertor != nil && diff --git a/test/integration/apiserver/apiserver_test.go b/test/integration/apiserver/apiserver_test.go index 22ae8ca317c..d62a725e69d 100644 --- a/test/integration/apiserver/apiserver_test.go +++ b/test/integration/apiserver/apiserver_test.go @@ -548,7 +548,7 @@ func TestAPICRDProtobuf(t *testing.T) { } } -func TestTransformOnWatch(t *testing.T) { +func TestTransform(t *testing.T) { tearDown, config, _, err := fixtures.StartDefaultServer(t) if err != nil { t.Fatal(err) @@ -592,23 +592,23 @@ func TestTransformOnWatch(t *testing.T) { testcases := []struct { name string accept string - includeObject metav1beta1.IncludeObjectPolicy + includeObject metav1.IncludeObjectPolicy object func(*testing.T) (metav1.Object, string, string) wantErr func(*testing.T, error) wantBody func(*testing.T, io.Reader) }{ { - name: "verify columns on cluster scoped resources", + name: "v1beta1 verify columns on cluster scoped resources", accept: "application/json;as=Table;g=meta.k8s.io;v=v1beta1", object: func(t *testing.T) (metav1.Object, string, string) { return &metav1.ObjectMeta{Name: "default", Namespace: ""}, "", "namespaces" }, wantBody: func(t *testing.T, w io.Reader) { - expectTableWatchEvents(t, 1, 3, metav1beta1.IncludeMetadata, json.NewDecoder(w)) + expectTableWatchEvents(t, 1, 3, metav1.IncludeMetadata, json.NewDecoder(w)) }, }, { - name: "verify columns on CRDs in json", + name: "v1beta1 verify columns on CRDs in json", accept: "application/json;as=Table;g=meta.k8s.io;v=v1beta1", object: func(t *testing.T) (metav1.Object, string, string) { cr, err := crclient.Create(&unstructured.Unstructured{Object: map[string]interface{}{"apiVersion": "cr.bar.com/v1", "kind": "Foo", "metadata": map[string]interface{}{"name": "test-1"}}}, metav1.CreateOptions{}) @@ -621,11 +621,11 @@ func TestTransformOnWatch(t *testing.T) { return cr, crdGVR.Group, "foos" }, wantBody: func(t *testing.T, w io.Reader) { - expectTableWatchEvents(t, 2, 2, metav1beta1.IncludeMetadata, json.NewDecoder(w)) + expectTableWatchEvents(t, 2, 2, metav1.IncludeMetadata, json.NewDecoder(w)) }, }, { - name: "verify columns on CRDs in json;stream=watch", + name: "v1beta1 verify columns on CRDs in json;stream=watch", accept: "application/json;stream=watch;as=Table;g=meta.k8s.io;v=v1beta1", object: func(t *testing.T) (metav1.Object, string, string) { cr, err := crclient.Create(&unstructured.Unstructured{Object: map[string]interface{}{"apiVersion": "cr.bar.com/v1", "kind": "Foo", "metadata": map[string]interface{}{"name": "test-2"}}}, metav1.CreateOptions{}) @@ -638,11 +638,11 @@ func TestTransformOnWatch(t *testing.T) { return cr, crdGVR.Group, "foos" }, wantBody: func(t *testing.T, w io.Reader) { - expectTableWatchEvents(t, 2, 2, metav1beta1.IncludeMetadata, json.NewDecoder(w)) + expectTableWatchEvents(t, 2, 2, metav1.IncludeMetadata, json.NewDecoder(w)) }, }, { - name: "verify columns on CRDs in yaml", + name: "v1beta1 verify columns on CRDs in yaml", accept: "application/yaml;as=Table;g=meta.k8s.io;v=v1beta1", object: func(t *testing.T) (metav1.Object, string, string) { 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{}) @@ -665,7 +665,7 @@ func TestTransformOnWatch(t *testing.T) { }, }, { - name: "verify columns on services", + name: "v1beta1 verify columns on services", accept: "application/json;as=Table;g=meta.k8s.io;v=v1beta1", object: func(t *testing.T) (metav1.Object, string, string) { ns := "default" @@ -679,13 +679,13 @@ func TestTransformOnWatch(t *testing.T) { return svc, "", "services" }, wantBody: func(t *testing.T, w io.Reader) { - expectTableWatchEvents(t, 2, 7, metav1beta1.IncludeMetadata, json.NewDecoder(w)) + expectTableWatchEvents(t, 2, 7, metav1.IncludeMetadata, json.NewDecoder(w)) }, }, { - name: "verify columns on services with no object", + name: "v1beta1 verify columns on services with no object", accept: "application/json;as=Table;g=meta.k8s.io;v=v1beta1", - includeObject: metav1beta1.IncludeNone, + includeObject: metav1.IncludeNone, object: func(t *testing.T) (metav1.Object, string, string) { ns := "default" obj, err := clientset.CoreV1().Services(ns).Create(&v1.Service{ObjectMeta: metav1.ObjectMeta{Name: "test-2"}, Spec: v1.ServiceSpec{Ports: []v1.ServicePort{{Port: 1000}}}}) @@ -698,13 +698,13 @@ func TestTransformOnWatch(t *testing.T) { return obj, "", "services" }, wantBody: func(t *testing.T, w io.Reader) { - expectTableWatchEvents(t, 2, 7, metav1beta1.IncludeNone, json.NewDecoder(w)) + expectTableWatchEvents(t, 2, 7, metav1.IncludeNone, json.NewDecoder(w)) }, }, { - name: "verify columns on services with full object", + name: "v1beta1 verify columns on services with full object", accept: "application/json;as=Table;g=meta.k8s.io;v=v1beta1", - includeObject: metav1beta1.IncludeObject, + includeObject: metav1.IncludeObject, object: func(t *testing.T) (metav1.Object, string, string) { ns := "default" obj, err := clientset.CoreV1().Services(ns).Create(&v1.Service{ObjectMeta: metav1.ObjectMeta{Name: "test-3"}, Spec: v1.ServiceSpec{Ports: []v1.ServicePort{{Port: 1000}}}}) @@ -717,7 +717,7 @@ func TestTransformOnWatch(t *testing.T) { return obj, "", "services" }, wantBody: func(t *testing.T, w io.Reader) { - objects := expectTableWatchEvents(t, 2, 7, metav1beta1.IncludeObject, json.NewDecoder(w)) + objects := expectTableWatchEvents(t, 2, 7, metav1.IncludeObject, json.NewDecoder(w)) var svc v1.Service if err := json.Unmarshal(objects[1], &svc); err != nil { t.Fatal(err) @@ -728,7 +728,7 @@ func TestTransformOnWatch(t *testing.T) { }, }, { - name: "verify partial metadata object on config maps", + name: "v1beta1 verify partial metadata object on config maps", accept: "application/json;as=PartialObjectMetadata;g=meta.k8s.io;v=v1beta1", object: func(t *testing.T) (metav1.Object, string, string) { ns := "default" @@ -746,7 +746,7 @@ func TestTransformOnWatch(t *testing.T) { }, }, { - name: "verify partial metadata object on config maps in protobuf", + name: "v1beta1 verify partial metadata object on config maps in protobuf", accept: "application/vnd.kubernetes.protobuf;as=PartialObjectMetadata;g=meta.k8s.io;v=v1beta1", object: func(t *testing.T) (metav1.Object, string, string) { ns := "default" @@ -764,7 +764,7 @@ func TestTransformOnWatch(t *testing.T) { }, }, { - name: "verify error on unsupported mimetype protobuf for table conversion", + name: "v1beta1 verify error on unsupported mimetype protobuf for table conversion", accept: "application/vnd.kubernetes.protobuf;as=Table;g=meta.k8s.io;v=v1beta1", object: func(t *testing.T) (metav1.Object, string, string) { return &metav1.ObjectMeta{Name: "kubernetes", Namespace: "default"}, "", "services" @@ -781,7 +781,7 @@ func TestTransformOnWatch(t *testing.T) { }, { name: "verify error on invalid mimetype - bad version", - accept: "application/json;as=PartialObjectMetadata;g=meta.k8s.io;v=v1", + accept: "application/json;as=PartialObjectMetadata;g=meta.k8s.io;v=v1alpha1", object: func(t *testing.T) (metav1.Object, string, string) { return &metav1.ObjectMeta{Name: "kubernetes", Namespace: "default"}, "", "services" }, @@ -792,7 +792,7 @@ func TestTransformOnWatch(t *testing.T) { }, }, { - name: "verify error on invalid mimetype - bad group", + name: "v1beta1 verify error on invalid mimetype - bad group", accept: "application/json;as=PartialObjectMetadata;g=k8s.io;v=v1beta1", object: func(t *testing.T) (metav1.Object, string, string) { return &metav1.ObjectMeta{Name: "kubernetes", Namespace: "default"}, "", "services" @@ -804,7 +804,7 @@ func TestTransformOnWatch(t *testing.T) { }, }, { - name: "verify error on invalid mimetype - bad kind", + name: "v1beta1 verify error on invalid mimetype - bad kind", accept: "application/json;as=PartialObject;g=meta.k8s.io;v=v1beta1", object: func(t *testing.T) (metav1.Object, string, string) { return &metav1.ObjectMeta{Name: "kubernetes", Namespace: "default"}, "", "services" @@ -816,7 +816,7 @@ func TestTransformOnWatch(t *testing.T) { }, }, { - name: "verify error on invalid mimetype - missing kind", + name: "v1beta1 verify error on invalid mimetype - missing kind", accept: "application/json;g=meta.k8s.io;v=v1beta1", object: func(t *testing.T) (metav1.Object, string, string) { return &metav1.ObjectMeta{Name: "kubernetes", Namespace: "default"}, "", "services" @@ -828,9 +828,241 @@ func TestTransformOnWatch(t *testing.T) { }, }, { - name: "verify error on invalid transform parameter", + name: "v1beta1 verify error on invalid transform parameter", accept: "application/json;as=Table;g=meta.k8s.io;v=v1beta1", - includeObject: metav1beta1.IncludeObjectPolicy("unrecognized"), + includeObject: metav1.IncludeObjectPolicy("unrecognized"), + object: func(t *testing.T) (metav1.Object, string, string) { + return &metav1.ObjectMeta{Name: "kubernetes", Namespace: "default"}, "", "services" + }, + wantErr: func(t *testing.T, err error) { + if !apierrors.IsBadRequest(err) || !strings.Contains(err.Error(), `Invalid value: "unrecognized": must be 'Metadata', 'Object', 'None', or empty`) { + t.Fatal(err) + } + }, + }, + + { + name: "v1 verify columns on cluster scoped resources", + accept: "application/json;as=Table;g=meta.k8s.io;v=v1", + object: func(t *testing.T) (metav1.Object, string, string) { + return &metav1.ObjectMeta{Name: "default", Namespace: ""}, "", "namespaces" + }, + wantBody: func(t *testing.T, w io.Reader) { + expectTableV1WatchEvents(t, 1, 3, metav1.IncludeMetadata, json.NewDecoder(w)) + }, + }, + { + name: "v1 verify columns on CRDs in json", + accept: "application/json;as=Table;g=meta.k8s.io;v=v1", + object: func(t *testing.T) (metav1.Object, string, string) { + cr, err := crclient.Create(&unstructured.Unstructured{Object: map[string]interface{}{"apiVersion": "cr.bar.com/v1", "kind": "Foo", "metadata": map[string]interface{}{"name": "test-4"}}}, metav1.CreateOptions{}) + if err != nil { + t.Fatalf("unable to create cr: %v", err) + } + if _, err := crclient.Patch("test-4", types.MergePatchType, []byte(`{"metadata":{"annotations":{"test":"1"}}}`), metav1.PatchOptions{}); err != nil { + t.Fatalf("unable to patch cr: %v", err) + } + return cr, crdGVR.Group, "foos" + }, + wantBody: func(t *testing.T, w io.Reader) { + expectTableV1WatchEvents(t, 2, 2, metav1.IncludeMetadata, json.NewDecoder(w)) + }, + }, + { + name: "v1 verify columns on CRDs in json;stream=watch", + accept: "application/json;stream=watch;as=Table;g=meta.k8s.io;v=v1", + object: func(t *testing.T) (metav1.Object, string, string) { + cr, err := crclient.Create(&unstructured.Unstructured{Object: map[string]interface{}{"apiVersion": "cr.bar.com/v1", "kind": "Foo", "metadata": map[string]interface{}{"name": "test-5"}}}, metav1.CreateOptions{}) + if err != nil { + t.Fatalf("unable to create cr: %v", err) + } + if _, err := crclient.Patch("test-5", types.MergePatchType, []byte(`{"metadata":{"annotations":{"test":"1"}}}`), metav1.PatchOptions{}); err != nil { + t.Fatalf("unable to patch cr: %v", err) + } + return cr, crdGVR.Group, "foos" + }, + wantBody: func(t *testing.T, w io.Reader) { + expectTableV1WatchEvents(t, 2, 2, metav1.IncludeMetadata, json.NewDecoder(w)) + }, + }, + { + name: "v1 verify columns on CRDs in yaml", + accept: "application/yaml;as=Table;g=meta.k8s.io;v=v1", + object: func(t *testing.T) (metav1.Object, string, string) { + cr, err := crclient.Create(&unstructured.Unstructured{Object: map[string]interface{}{"apiVersion": "cr.bar.com/v1", "kind": "Foo", "metadata": map[string]interface{}{"name": "test-6"}}}, metav1.CreateOptions{}) + if err != nil { + t.Fatalf("unable to create cr: %v", err) + } + if _, err := crclient.Patch("test-6", types.MergePatchType, []byte(`{"metadata":{"annotations":{"test":"1"}}}`), metav1.PatchOptions{}); err != nil { + t.Fatalf("unable to patch cr: %v", err) + } + return cr, crdGVR.Group, "foos" + }, + wantErr: func(t *testing.T, err error) { + if !apierrors.IsNotAcceptable(err) { + t.Fatal(err) + } + // TODO: this should be a more specific error + if err.Error() != "only the following media types are accepted: application/json;stream=watch" { + t.Fatal(err) + } + }, + }, + { + name: "v1 verify columns on services", + accept: "application/json;as=Table;g=meta.k8s.io;v=v1", + object: func(t *testing.T) (metav1.Object, string, string) { + ns := "default" + svc, err := clientset.CoreV1().Services(ns).Create(&v1.Service{ObjectMeta: metav1.ObjectMeta{Name: "test-4"}, Spec: v1.ServiceSpec{Ports: []v1.ServicePort{{Port: 1000}}}}) + if err != nil { + t.Fatalf("unable to create service: %v", err) + } + if _, err := clientset.CoreV1().Services(ns).Patch(svc.Name, types.MergePatchType, []byte(`{"metadata":{"annotations":{"test":"1"}}}`)); err != nil { + t.Fatalf("unable to update service: %v", err) + } + return svc, "", "services" + }, + wantBody: func(t *testing.T, w io.Reader) { + expectTableV1WatchEvents(t, 2, 7, metav1.IncludeMetadata, json.NewDecoder(w)) + }, + }, + { + name: "v1 verify columns on services with no object", + accept: "application/json;as=Table;g=meta.k8s.io;v=v1", + includeObject: metav1.IncludeNone, + object: func(t *testing.T) (metav1.Object, string, string) { + ns := "default" + obj, err := clientset.CoreV1().Services(ns).Create(&v1.Service{ObjectMeta: metav1.ObjectMeta{Name: "test-5"}, Spec: v1.ServiceSpec{Ports: []v1.ServicePort{{Port: 1000}}}}) + if err != nil { + t.Fatalf("unable to create object: %v", err) + } + if _, err := clientset.CoreV1().Services(ns).Patch(obj.Name, types.MergePatchType, []byte(`{"metadata":{"annotations":{"test":"1"}}}`)); err != nil { + t.Fatalf("unable to update object: %v", err) + } + return obj, "", "services" + }, + wantBody: func(t *testing.T, w io.Reader) { + expectTableV1WatchEvents(t, 2, 7, metav1.IncludeNone, json.NewDecoder(w)) + }, + }, + { + name: "v1 verify columns on services with full object", + accept: "application/json;as=Table;g=meta.k8s.io;v=v1", + includeObject: metav1.IncludeObject, + object: func(t *testing.T) (metav1.Object, string, string) { + ns := "default" + obj, err := clientset.CoreV1().Services(ns).Create(&v1.Service{ObjectMeta: metav1.ObjectMeta{Name: "test-6"}, Spec: v1.ServiceSpec{Ports: []v1.ServicePort{{Port: 1000}}}}) + if err != nil { + t.Fatalf("unable to create object: %v", err) + } + if _, err := clientset.CoreV1().Services(ns).Patch(obj.Name, types.MergePatchType, []byte(`{"metadata":{"annotations":{"test":"1"}}}`)); err != nil { + t.Fatalf("unable to update object: %v", err) + } + return obj, "", "services" + }, + wantBody: func(t *testing.T, w io.Reader) { + objects := expectTableV1WatchEvents(t, 2, 7, metav1.IncludeObject, json.NewDecoder(w)) + var svc v1.Service + if err := json.Unmarshal(objects[1], &svc); err != nil { + t.Fatal(err) + } + if svc.Annotations["test"] != "1" || svc.Spec.Ports[0].Port != 1000 { + t.Fatalf("unexpected object: %#v", svc) + } + }, + }, + { + name: "v1 verify partial metadata object on config maps", + accept: "application/json;as=PartialObjectMetadata;g=meta.k8s.io;v=v1", + object: func(t *testing.T) (metav1.Object, string, string) { + ns := "default" + obj, err := clientset.CoreV1().ConfigMaps(ns).Create(&v1.ConfigMap{ObjectMeta: metav1.ObjectMeta{Name: "test-3", Annotations: map[string]string{"test": "0"}}}) + if err != nil { + t.Fatalf("unable to create object: %v", err) + } + if _, err := clientset.CoreV1().ConfigMaps(ns).Patch(obj.Name, types.MergePatchType, []byte(`{"metadata":{"annotations":{"test":"1"}}}`)); err != nil { + t.Fatalf("unable to update object: %v", err) + } + return obj, "", "configmaps" + }, + wantBody: func(t *testing.T, w io.Reader) { + expectPartialObjectMetaV1Events(t, json.NewDecoder(w), "0", "1") + }, + }, + { + name: "v1 verify partial metadata object on config maps in protobuf", + accept: "application/vnd.kubernetes.protobuf;as=PartialObjectMetadata;g=meta.k8s.io;v=v1", + object: func(t *testing.T) (metav1.Object, string, string) { + ns := "default" + obj, err := clientset.CoreV1().ConfigMaps(ns).Create(&v1.ConfigMap{ObjectMeta: metav1.ObjectMeta{Name: "test-4", Annotations: map[string]string{"test": "0"}}}) + if err != nil { + t.Fatalf("unable to create object: %v", err) + } + if _, err := clientset.CoreV1().ConfigMaps(ns).Patch(obj.Name, types.MergePatchType, []byte(`{"metadata":{"annotations":{"test":"1"}}}`)); err != nil { + t.Fatalf("unable to update object: %v", err) + } + return obj, "", "configmaps" + }, + wantBody: func(t *testing.T, w io.Reader) { + expectPartialObjectMetaV1EventsProtobuf(t, w, "0", "1") + }, + }, + { + name: "v1 verify error on unsupported mimetype protobuf for table conversion", + accept: "application/vnd.kubernetes.protobuf;as=Table;g=meta.k8s.io;v=v1", + object: func(t *testing.T) (metav1.Object, string, string) { + return &metav1.ObjectMeta{Name: "kubernetes", Namespace: "default"}, "", "services" + }, + wantErr: func(t *testing.T, err error) { + if !apierrors.IsNotAcceptable(err) { + t.Fatal(err) + } + // TODO: this should be a more specific error + if err.Error() != "only the following media types are accepted: application/json, application/yaml, application/vnd.kubernetes.protobuf" { + t.Fatal(err) + } + }, + }, + { + name: "v1 verify error on invalid mimetype - bad group", + accept: "application/json;as=PartialObjectMetadata;g=k8s.io;v=v1", + object: func(t *testing.T) (metav1.Object, string, string) { + return &metav1.ObjectMeta{Name: "kubernetes", Namespace: "default"}, "", "services" + }, + wantErr: func(t *testing.T, err error) { + if !apierrors.IsNotAcceptable(err) { + t.Fatal(err) + } + }, + }, + { + name: "v1 verify error on invalid mimetype - bad kind", + accept: "application/json;as=PartialObject;g=meta.k8s.io;v=v1", + object: func(t *testing.T) (metav1.Object, string, string) { + return &metav1.ObjectMeta{Name: "kubernetes", Namespace: "default"}, "", "services" + }, + wantErr: func(t *testing.T, err error) { + if !apierrors.IsNotAcceptable(err) { + t.Fatal(err) + } + }, + }, + { + name: "v1 verify error on invalid mimetype - missing kind", + accept: "application/json;g=meta.k8s.io;v=v1", + object: func(t *testing.T) (metav1.Object, string, string) { + return &metav1.ObjectMeta{Name: "kubernetes", Namespace: "default"}, "", "services" + }, + wantErr: func(t *testing.T, err error) { + if !apierrors.IsNotAcceptable(err) { + t.Fatal(err) + } + }, + }, + { + name: "v1 verify error on invalid transform parameter", + accept: "application/json;as=Table;g=meta.k8s.io;v=v1", + includeObject: metav1.IncludeObjectPolicy("unrecognized"), object: func(t *testing.T) (metav1.Object, string, string) { return &metav1.ObjectMeta{Name: "kubernetes", Namespace: "default"}, "", "services" }, @@ -892,7 +1124,7 @@ func TestTransformOnWatch(t *testing.T) { } } -func expectTableWatchEvents(t *testing.T, count, columns int, policy metav1beta1.IncludeObjectPolicy, d *json.Decoder) [][]byte { +func expectTableWatchEvents(t *testing.T, count, columns int, policy metav1.IncludeObjectPolicy, d *json.Decoder) [][]byte { t.Helper() var objects [][]byte @@ -923,7 +1155,7 @@ func expectTableWatchEvents(t *testing.T, count, columns int, policy metav1beta1 t.Fatalf("Invalid row width: %#v", row.Cells) } switch policy { - case metav1beta1.IncludeMetadata: + case metav1.IncludeMetadata: var meta metav1beta1.PartialObjectMetadata if err := json.Unmarshal(row.Object.Raw, &meta); err != nil { t.Fatalf("expected partial object: %v", err) @@ -932,11 +1164,11 @@ func expectTableWatchEvents(t *testing.T, count, columns int, policy metav1beta1 if meta.TypeMeta != partialObj { t.Fatalf("expected partial object: %#v", meta) } - case metav1beta1.IncludeNone: + case metav1.IncludeNone: if len(row.Object.Raw) != 0 { t.Fatalf("Expected no object: %s", string(row.Object.Raw)) } - case metav1beta1.IncludeObject: + case metav1.IncludeObject: if len(row.Object.Raw) == 0 { t.Fatalf("Expected object: %s", string(row.Object.Raw)) } @@ -1000,3 +1232,112 @@ func expectPartialObjectMetaEventsProtobuf(t *testing.T, r io.Reader, values ... } } } + +func expectTableV1WatchEvents(t *testing.T, count, columns int, policy metav1.IncludeObjectPolicy, d *json.Decoder) [][]byte { + t.Helper() + + var objects [][]byte + + for i := 0; i < count; i++ { + var evt metav1.WatchEvent + if err := d.Decode(&evt); err != nil { + t.Fatal(err) + } + var table metav1.Table + if err := json.Unmarshal(evt.Object.Raw, &table); err != nil { + t.Fatal(err) + } + if i == 0 { + if len(table.ColumnDefinitions) != columns { + t.Fatalf("Got unexpected columns on first watch event: %d vs %#v", columns, table.ColumnDefinitions) + } + } else { + if len(table.ColumnDefinitions) != 0 { + t.Fatalf("Expected no columns on second watch event: %#v", table.ColumnDefinitions) + } + } + if len(table.Rows) != 1 { + t.Fatalf("Invalid rows: %#v", table.Rows) + } + row := table.Rows[0] + if len(row.Cells) != columns { + t.Fatalf("Invalid row width: %#v", row.Cells) + } + switch policy { + case metav1.IncludeMetadata: + var meta metav1.PartialObjectMetadata + if err := json.Unmarshal(row.Object.Raw, &meta); err != nil { + t.Fatalf("expected partial object: %v", err) + } + partialObj := metav1.TypeMeta{Kind: "PartialObjectMetadata", APIVersion: "meta.k8s.io/v1"} + if meta.TypeMeta != partialObj { + t.Fatalf("expected partial object: %#v", meta) + } + case metav1.IncludeNone: + if len(row.Object.Raw) != 0 { + t.Fatalf("Expected no object: %s", string(row.Object.Raw)) + } + case metav1.IncludeObject: + if len(row.Object.Raw) == 0 { + t.Fatalf("Expected object: %s", string(row.Object.Raw)) + } + objects = append(objects, row.Object.Raw) + } + } + return objects +} + +func expectPartialObjectMetaV1Events(t *testing.T, d *json.Decoder, values ...string) { + t.Helper() + + for i, value := range values { + var evt metav1.WatchEvent + if err := d.Decode(&evt); err != nil { + t.Fatal(err) + } + var meta metav1.PartialObjectMetadata + if err := json.Unmarshal(evt.Object.Raw, &meta); err != nil { + t.Fatal(err) + } + typeMeta := metav1.TypeMeta{Kind: "PartialObjectMetadata", APIVersion: "meta.k8s.io/v1"} + if meta.TypeMeta != typeMeta { + t.Fatalf("expected partial object: %#v", meta) + } + if meta.Annotations["test"] != value { + t.Fatalf("expected event %d to have value %q instead of %q", i+1, value, meta.Annotations["test"]) + } + } +} + +func expectPartialObjectMetaV1EventsProtobuf(t *testing.T, r io.Reader, values ...string) { + scheme := runtime.NewScheme() + metav1.AddToGroupVersion(scheme, schema.GroupVersion{Version: "v1"}) + rs := protobuf.NewRawSerializer(scheme, scheme) + d := streaming.NewDecoder( + protobuf.LengthDelimitedFramer.NewFrameReader(ioutil.NopCloser(r)), + rs, + ) + ds := metainternalversion.Codecs.UniversalDeserializer() + + for i, value := range values { + var evt metav1.WatchEvent + if _, _, err := d.Decode(nil, &evt); err != nil { + t.Fatal(err) + } + obj, gvk, err := ds.Decode(evt.Object.Raw, nil, nil) + if err != nil { + t.Fatal(err) + } + meta, ok := obj.(*metav1.PartialObjectMetadata) + if !ok { + t.Fatalf("unexpected watch object %T", obj) + } + expected := &schema.GroupVersionKind{Kind: "PartialObjectMetadata", Version: "v1", Group: "meta.k8s.io"} + if !reflect.DeepEqual(expected, gvk) { + t.Fatalf("expected partial object: %#v", meta) + } + if meta.Annotations["test"] != value { + t.Fatalf("expected event %d to have value %q instead of %q", i+1, value, meta.Annotations["test"]) + } + } +}