Skip to content

Commit

Permalink
convert unstructured objs before exporting
Browse files Browse the repository at this point in the history
  • Loading branch information
juanvallejo committed Oct 16, 2017
1 parent f48d397 commit dd1c7af
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions pkg/oc/cli/cmd/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,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
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

0 comments on commit dd1c7af

Please sign in to comment.