Skip to content

Commit 64ba0bf

Browse files
authored
Merge pull request kubernetes#87892 from wojtek-t/manual_fake_kubectl_conversions
Register conversions for kubectl testing types
2 parents 70807e8 + ea140f5 commit 64ba0bf

File tree

2 files changed

+112
-2
lines changed

2 files changed

+112
-2
lines changed

staging/src/k8s.io/kubectl/pkg/cmd/testing/BUILD

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@ go_library(
1616
"//staging/src/k8s.io/apimachinery/pkg/api/meta:go_default_library",
1717
"//staging/src/k8s.io/apimachinery/pkg/api/meta/testrestmapper:go_default_library",
1818
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
19+
"//staging/src/k8s.io/apimachinery/pkg/conversion:go_default_library",
1920
"//staging/src/k8s.io/apimachinery/pkg/runtime:go_default_library",
2021
"//staging/src/k8s.io/apimachinery/pkg/runtime/schema:go_default_library",
2122
"//staging/src/k8s.io/apimachinery/pkg/runtime/serializer:go_default_library",
2223
"//staging/src/k8s.io/apimachinery/pkg/types:go_default_library",
24+
"//staging/src/k8s.io/apimachinery/pkg/util/runtime:go_default_library",
2325
"//staging/src/k8s.io/cli-runtime/pkg/genericclioptions:go_default_library",
2426
"//staging/src/k8s.io/cli-runtime/pkg/resource:go_default_library",
2527
"//staging/src/k8s.io/client-go/discovery:go_default_library",

staging/src/k8s.io/kubectl/pkg/cmd/testing/fake.go

Lines changed: 110 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@ import (
2828
"k8s.io/apimachinery/pkg/api/meta"
2929
"k8s.io/apimachinery/pkg/api/meta/testrestmapper"
3030
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
31+
"k8s.io/apimachinery/pkg/conversion"
3132
"k8s.io/apimachinery/pkg/runtime"
3233
"k8s.io/apimachinery/pkg/runtime/schema"
3334
"k8s.io/apimachinery/pkg/runtime/serializer"
35+
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
3436
"k8s.io/cli-runtime/pkg/genericclioptions"
3537
"k8s.io/cli-runtime/pkg/resource"
3638
"k8s.io/client-go/discovery"
@@ -128,6 +130,34 @@ func NewInternalType(kind, apiversion, name string) *InternalType {
128130
return &item
129131
}
130132

133+
func convertInternalTypeToExternalType(in *InternalType, out *ExternalType, s conversion.Scope) error {
134+
out.Kind = in.Kind
135+
out.APIVersion = in.APIVersion
136+
out.Name = in.Name
137+
return nil
138+
}
139+
140+
func convertInternalTypeToExternalType2(in *InternalType, out *ExternalType2, s conversion.Scope) error {
141+
out.Kind = in.Kind
142+
out.APIVersion = in.APIVersion
143+
out.Name = in.Name
144+
return nil
145+
}
146+
147+
func convertExternalTypeToInternalType(in *ExternalType, out *InternalType, s conversion.Scope) error {
148+
out.Kind = in.Kind
149+
out.APIVersion = in.APIVersion
150+
out.Name = in.Name
151+
return nil
152+
}
153+
154+
func convertExternalType2ToInternalType(in *ExternalType2, out *InternalType, s conversion.Scope) error {
155+
out.Kind = in.Kind
156+
out.APIVersion = in.APIVersion
157+
out.Name = in.Name
158+
return nil
159+
}
160+
131161
// InternalNamespacedType schema for internal namespaced types
132162
// +k8s:deepcopy-gen=true
133163
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
@@ -209,6 +239,38 @@ func NewInternalNamespacedType(kind, apiversion, name, namespace string) *Intern
209239
return &item
210240
}
211241

242+
func convertInternalNamespacedTypeToExternalNamespacedType(in *InternalNamespacedType, out *ExternalNamespacedType, s conversion.Scope) error {
243+
out.Kind = in.Kind
244+
out.APIVersion = in.APIVersion
245+
out.Name = in.Name
246+
out.Namespace = in.Namespace
247+
return nil
248+
}
249+
250+
func convertInternalNamespacedTypeToExternalNamespacedType2(in *InternalNamespacedType, out *ExternalNamespacedType2, s conversion.Scope) error {
251+
out.Kind = in.Kind
252+
out.APIVersion = in.APIVersion
253+
out.Name = in.Name
254+
out.Namespace = in.Namespace
255+
return nil
256+
}
257+
258+
func convertExternalNamespacedTypeToInternalNamespacedType(in *ExternalNamespacedType, out *InternalNamespacedType, s conversion.Scope) error {
259+
out.Kind = in.Kind
260+
out.APIVersion = in.APIVersion
261+
out.Name = in.Name
262+
out.Namespace = in.Namespace
263+
return nil
264+
}
265+
266+
func convertExternalNamespacedType2ToInternalNamespacedType(in *ExternalNamespacedType2, out *InternalNamespacedType, s conversion.Scope) error {
267+
out.Kind = in.Kind
268+
out.APIVersion = in.APIVersion
269+
out.Name = in.Name
270+
out.Namespace = in.Namespace
271+
return nil
272+
}
273+
212274
var errInvalidVersion = errors.New("not a version")
213275

214276
// ValidVersion of API
@@ -230,18 +292,64 @@ func NewExternalScheme() (*runtime.Scheme, meta.RESTMapper, runtime.Codec) {
230292
return scheme, mapper, codec
231293
}
232294

295+
func registerConversions(s *runtime.Scheme) error {
296+
if err := s.AddConversionFunc((*InternalType)(nil), (*ExternalType)(nil), func(a, b interface{}, scope conversion.Scope) error {
297+
return convertInternalTypeToExternalType(a.(*InternalType), b.(*ExternalType), scope)
298+
}); err != nil {
299+
return err
300+
}
301+
if err := s.AddConversionFunc((*InternalType)(nil), (*ExternalType2)(nil), func(a, b interface{}, scope conversion.Scope) error {
302+
return convertInternalTypeToExternalType2(a.(*InternalType), b.(*ExternalType2), scope)
303+
}); err != nil {
304+
return err
305+
}
306+
if err := s.AddConversionFunc((*ExternalType)(nil), (*InternalType)(nil), func(a, b interface{}, scope conversion.Scope) error {
307+
return convertExternalTypeToInternalType(a.(*ExternalType), b.(*InternalType), scope)
308+
}); err != nil {
309+
return err
310+
}
311+
if err := s.AddConversionFunc((*ExternalType2)(nil), (*InternalType)(nil), func(a, b interface{}, scope conversion.Scope) error {
312+
return convertExternalType2ToInternalType(a.(*ExternalType2), b.(*InternalType), scope)
313+
}); err != nil {
314+
return err
315+
}
316+
if err := s.AddConversionFunc((*InternalNamespacedType)(nil), (*ExternalNamespacedType)(nil), func(a, b interface{}, scope conversion.Scope) error {
317+
return convertInternalNamespacedTypeToExternalNamespacedType(a.(*InternalNamespacedType), b.(*ExternalNamespacedType), scope)
318+
}); err != nil {
319+
return err
320+
}
321+
if err := s.AddConversionFunc((*InternalNamespacedType)(nil), (*ExternalNamespacedType2)(nil), func(a, b interface{}, scope conversion.Scope) error {
322+
return convertInternalNamespacedTypeToExternalNamespacedType2(a.(*InternalNamespacedType), b.(*ExternalNamespacedType2), scope)
323+
}); err != nil {
324+
return err
325+
}
326+
if err := s.AddConversionFunc((*ExternalNamespacedType)(nil), (*InternalNamespacedType)(nil), func(a, b interface{}, scope conversion.Scope) error {
327+
return convertExternalNamespacedTypeToInternalNamespacedType(a.(*ExternalNamespacedType), b.(*InternalNamespacedType), scope)
328+
}); err != nil {
329+
return err
330+
}
331+
if err := s.AddConversionFunc((*ExternalNamespacedType2)(nil), (*InternalNamespacedType)(nil), func(a, b interface{}, scope conversion.Scope) error {
332+
return convertExternalNamespacedType2ToInternalNamespacedType(a.(*ExternalNamespacedType2), b.(*InternalNamespacedType), scope)
333+
}); err != nil {
334+
return err
335+
}
336+
return nil
337+
}
338+
233339
// AddToScheme adds required objects into scheme
234340
func AddToScheme(scheme *runtime.Scheme) (meta.RESTMapper, runtime.Codec) {
235341
scheme.AddKnownTypeWithName(InternalGV.WithKind("Type"), &InternalType{})
236342
scheme.AddKnownTypeWithName(UnlikelyGV.WithKind("Type"), &ExternalType{})
237-
//This tests that kubectl will not confuse the external scheme with the internal scheme, even when they accidentally have versions of the same name.
343+
// This tests that kubectl will not confuse the external scheme with the internal scheme, even when they accidentally have versions of the same name.
238344
scheme.AddKnownTypeWithName(ValidVersionGV.WithKind("Type"), &ExternalType2{})
239345

240346
scheme.AddKnownTypeWithName(InternalGV.WithKind("NamespacedType"), &InternalNamespacedType{})
241347
scheme.AddKnownTypeWithName(UnlikelyGV.WithKind("NamespacedType"), &ExternalNamespacedType{})
242-
//This tests that kubectl will not confuse the external scheme with the internal scheme, even when they accidentally have versions of the same name.
348+
// This tests that kubectl will not confuse the external scheme with the internal scheme, even when they accidentally have versions of the same name.
243349
scheme.AddKnownTypeWithName(ValidVersionGV.WithKind("NamespacedType"), &ExternalNamespacedType2{})
244350

351+
utilruntime.Must(registerConversions(scheme))
352+
245353
codecs := serializer.NewCodecFactory(scheme)
246354
codec := codecs.LegacyCodec(UnlikelyGV)
247355
mapper := meta.NewDefaultRESTMapper([]schema.GroupVersion{UnlikelyGV, ValidVersionGV})

0 commit comments

Comments
 (0)