From 5da386745af1040681bbfe40bef6fff339fb58f5 Mon Sep 17 00:00:00 2001 From: gxwilkerson33 <125314298+gxwilkerson33@users.noreply.github.com> Date: Tue, 11 Apr 2023 21:17:53 -0500 Subject: [PATCH] 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. --- pkg/kubectl/cmd/convert/convert.go | 16 ++++++--- pkg/kubectl/cmd/convert/convert_test.go | 10 ++++++ .../cmd/convert/serviceandingress.yaml | 34 +++++++++++++++++++ 3 files changed, 56 insertions(+), 4 deletions(-) create mode 100644 test/fixtures/pkg/kubectl/cmd/convert/serviceandingress.yaml diff --git a/pkg/kubectl/cmd/convert/convert.go b/pkg/kubectl/cmd/convert/convert.go index 008aa6debd8..b2fa8d23e8f 100644 --- a/pkg/kubectl/cmd/convert/convert.go +++ b/pkg/kubectl/cmd/convert/convert.go @@ -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) diff --git a/pkg/kubectl/cmd/convert/convert_test.go b/pkg/kubectl/cmd/convert/convert_test.go index 7b500fca520..eed35c9a648 100644 --- a/pkg/kubectl/cmd/convert/convert_test.go +++ b/pkg/kubectl/cmd/convert/convert_test.go @@ -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 { diff --git a/test/fixtures/pkg/kubectl/cmd/convert/serviceandingress.yaml b/test/fixtures/pkg/kubectl/cmd/convert/serviceandingress.yaml new file mode 100644 index 00000000000..ef04da2899a --- /dev/null +++ b/test/fixtures/pkg/kubectl/cmd/convert/serviceandingress.yaml @@ -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 + \ No newline at end of file