Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UPSTREAM: 53464: use unstructured builder - oc export #16665

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 43 additions & 2 deletions pkg/oc/cli/cmd/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,17 @@ func RunExport(f *clientcmd.Factory, exporter Exporter, in io.Reader, out io.Wri
return err
}

mapper, typer := f.Object()
b := f.NewBuilder(true).
builder, err := f.NewUnstructuredBuilder(true)
if err != nil {
return err
}

mapper, typer, err := f.UnstructuredObject()
if err != nil {
return err
}

b := builder.
NamespaceParam(cmdNamespace).DefaultNamespace().AllNamespaces(allNamespaces).
FilenameParam(explicit, &resource.FilenameOptions{Recursive: false, Filenames: filenames}).
SelectorParam(selector).
Expand All @@ -132,12 +141,44 @@ func RunExport(f *clientcmd.Factory, exporter Exporter, in io.Reader, out io.Wri
newInfos := []*resource.Info{}
errs := []error{}
for _, info := range infos {
converted := false

// convert unstructured object to runtime.Object
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm ok with the approach, but only if we deprecate oc export and stop adding any new features to it.

@fabianofranz it's a pretty ugly hammer, but it behaves slightly better on unknown types.

data, err := runtime.Encode(kapi.Codecs.LegacyCodec(), info.Object)
if err != nil {
errs = append(errs, err)
continue
}
decoded, err := runtime.Decode(f.Decoder(true), data)
if err == nil {
// ignore error, if any, in order to allow resources
// not known by the client to still be exported
info.Object = decoded
converted = true
}

if err := exporter.Export(info.Object, exact); err != nil {
if err == ErrExportOmit {
continue
}
errs = append(errs, err)
}

// if an unstructured resource was successfully converted by the universal decoder,
// re-convert that object once again into its external version.
// If object cannot be converted to an external version, ignore error and proceed with
// internal version.
if converted {
if data, err = runtime.Encode(kapi.Codecs.LegacyCodec(outputVersion), info.Object); err == nil {
external, err := runtime.Decode(f.Decoder(false), data)
if err != nil {
errs = append(errs, fmt.Errorf("error: failed to convert resource to external version: %v", err))
continue
}
info.Object = external
}
}

newInfos = append(newInfos, info)
}
if len(errs) > 0 {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.