Kubectl convert - warn users with NotRegisteredError and Fail on all other errors (#117002)

* Convert file but warn user with impossible conversions

* Only continuing for NotRegisteredErrors. Using iostreams for warning user instead of stdError

* Formatting, correct tests to use valid DNS-1035.
This commit is contained in:
gxwilkerson33 2023-04-11 21:17:53 -05:00 committed by GitHub
parent 95d3492eb8
commit 5da386745a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 56 additions and 4 deletions

View File

@ -180,7 +180,7 @@ func (o *ConvertOptions) RunConvert() error {
internalEncoder := scheme.Codecs.LegacyCodec(scheme.Scheme.PrioritizedVersionsAllGroups()...)
internalVersionJSONEncoder := unstructured.NewJSONFallbackEncoder(internalEncoder)
objects, err := asVersionedObject(infos, !singleItemImplied, specifiedOutputVersion, internalVersionJSONEncoder)
objects, err := asVersionedObject(infos, !singleItemImplied, specifiedOutputVersion, internalVersionJSONEncoder, o.IOStreams)
if err != nil {
return err
}
@ -192,8 +192,8 @@ func (o *ConvertOptions) RunConvert() error {
// the objects as children, or if only a single Object is present, as that object. The provided
// version will be preferred as the conversion target, but the Object's mapping version will be
// used if that version is not present.
func asVersionedObject(infos []*resource.Info, forceList bool, specifiedOutputVersion schema.GroupVersion, encoder runtime.Encoder) (runtime.Object, error) {
objects, err := asVersionedObjects(infos, specifiedOutputVersion, encoder)
func asVersionedObject(infos []*resource.Info, forceList bool, specifiedOutputVersion schema.GroupVersion, encoder runtime.Encoder, iostream genericclioptions.IOStreams) (runtime.Object, error) {
objects, err := asVersionedObjects(infos, specifiedOutputVersion, encoder, iostream)
if err != nil {
return nil, err
}
@ -230,7 +230,7 @@ func asVersionedObject(infos []*resource.Info, forceList bool, specifiedOutputVe
// asVersionedObjects converts a list of infos into versioned objects. The provided
// version will be preferred as the conversion target, but the Object's mapping version will be
// used if that version is not present.
func asVersionedObjects(infos []*resource.Info, specifiedOutputVersion schema.GroupVersion, encoder runtime.Encoder) ([]runtime.Object, error) {
func asVersionedObjects(infos []*resource.Info, specifiedOutputVersion schema.GroupVersion, encoder runtime.Encoder, iostream genericclioptions.IOStreams) ([]runtime.Object, error) {
objects := []runtime.Object{}
for _, info := range infos {
if info.Object == nil {
@ -263,6 +263,14 @@ func asVersionedObjects(infos []*resource.Info, specifiedOutputVersion schema.Gr
converted, err := tryConvert(scheme.Scheme, info.Object, targetVersions...)
if err != nil {
// Dont fail on not registered error converting objects.
// Simply warn the user with the error returned from api-machinery and continue with the rest of the file
// fail on all other errors
if runtime.IsNotRegisteredError(err) {
fmt.Fprintln(iostream.ErrOut, err.Error())
continue
}
return nil, err
}
objects = append(objects, converted)

View File

@ -100,6 +100,16 @@ func TestConvertObject(t *testing.T) {
},
},
},
{
name: "converting multiple including service to neworking.k8s.io/v1",
file: "../../../../test/fixtures/pkg/kubectl/cmd/convert/serviceandingress.yaml",
outputVersion: "networking.k8s.io/v1",
fields: []checkField{
{
expected: "apiVersion: networking.k8s.io/v1",
},
},
},
}
for _, tc := range testcases {

View File

@ -0,0 +1,34 @@
apiVersion: v1
kind: Service
metadata:
name: test-cluster-ip
spec:
ports:
- name: port
port: 80
protocol: TCP
targetPort: 80
selector:
app: test-cluster-ip
type: ClusterIP
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: test-ingress
spec:
rules:
- host: test.com
http:
paths:
- backend:
serviceName: test1
servicePort: 80
path: /test1
pathType: ImplementationSpecific
- backend:
serviceName: webapi
servicePort: 80
path: /test2
pathType: ImplementationSpecific