@@ -28,9 +28,11 @@ import (
28
28
"k8s.io/apimachinery/pkg/api/meta"
29
29
"k8s.io/apimachinery/pkg/api/meta/testrestmapper"
30
30
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
31
+ "k8s.io/apimachinery/pkg/conversion"
31
32
"k8s.io/apimachinery/pkg/runtime"
32
33
"k8s.io/apimachinery/pkg/runtime/schema"
33
34
"k8s.io/apimachinery/pkg/runtime/serializer"
35
+ utilruntime "k8s.io/apimachinery/pkg/util/runtime"
34
36
"k8s.io/cli-runtime/pkg/genericclioptions"
35
37
"k8s.io/cli-runtime/pkg/resource"
36
38
"k8s.io/client-go/discovery"
@@ -128,6 +130,34 @@ func NewInternalType(kind, apiversion, name string) *InternalType {
128
130
return & item
129
131
}
130
132
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
+
131
161
// InternalNamespacedType schema for internal namespaced types
132
162
// +k8s:deepcopy-gen=true
133
163
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
@@ -209,6 +239,38 @@ func NewInternalNamespacedType(kind, apiversion, name, namespace string) *Intern
209
239
return & item
210
240
}
211
241
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
+
212
274
var errInvalidVersion = errors .New ("not a version" )
213
275
214
276
// ValidVersion of API
@@ -230,18 +292,64 @@ func NewExternalScheme() (*runtime.Scheme, meta.RESTMapper, runtime.Codec) {
230
292
return scheme , mapper , codec
231
293
}
232
294
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
+
233
339
// AddToScheme adds required objects into scheme
234
340
func AddToScheme (scheme * runtime.Scheme ) (meta.RESTMapper , runtime.Codec ) {
235
341
scheme .AddKnownTypeWithName (InternalGV .WithKind ("Type" ), & InternalType {})
236
342
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.
238
344
scheme .AddKnownTypeWithName (ValidVersionGV .WithKind ("Type" ), & ExternalType2 {})
239
345
240
346
scheme .AddKnownTypeWithName (InternalGV .WithKind ("NamespacedType" ), & InternalNamespacedType {})
241
347
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.
243
349
scheme .AddKnownTypeWithName (ValidVersionGV .WithKind ("NamespacedType" ), & ExternalNamespacedType2 {})
244
350
351
+ utilruntime .Must (registerConversions (scheme ))
352
+
245
353
codecs := serializer .NewCodecFactory (scheme )
246
354
codec := codecs .LegacyCodec (UnlikelyGV )
247
355
mapper := meta .NewDefaultRESTMapper ([]schema.GroupVersion {UnlikelyGV , ValidVersionGV })
0 commit comments