Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
2f7793df
Commit
2f7793df
authored
Dec 13, 2017
by
David Eads
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
allow convert to default on a per object basis
parent
38e33513
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
37 additions
and
22 deletions
+37
-22
convert.go
pkg/kubectl/cmd/convert.go
+37
-22
No files found.
pkg/kubectl/cmd/convert.go
View file @
2f7793df
...
...
@@ -99,19 +99,15 @@ type ConvertOptions struct {
out
io
.
Writer
printer
printers
.
ResourcePrinter
o
utputVersion
schema
.
GroupVersion
specifiedO
utputVersion
schema
.
GroupVersion
}
// outputVersion returns the preferred output version for generic content (JSON, YAML, or templates)
// defaultVersion is never mutated. Nil simply allows clean passing in common usage from client.Config
func
outputVersion
(
cmd
*
cobra
.
Command
,
defaultVersion
*
schema
.
GroupVersion
)
(
schema
.
GroupVersion
,
error
)
{
func
outputVersion
(
cmd
*
cobra
.
Command
)
(
schema
.
GroupVersion
,
error
)
{
outputVersionString
:=
cmdutil
.
GetFlagString
(
cmd
,
"output-version"
)
if
len
(
outputVersionString
)
==
0
{
if
defaultVersion
==
nil
{
return
schema
.
GroupVersion
{},
nil
}
return
*
defaultVersion
,
nil
return
schema
.
GroupVersion
{},
nil
}
return
schema
.
ParseGroupVersion
(
outputVersionString
)
...
...
@@ -119,13 +115,10 @@ func outputVersion(cmd *cobra.Command, defaultVersion *schema.GroupVersion) (sch
// Complete collects information required to run Convert command from command line.
func
(
o
*
ConvertOptions
)
Complete
(
f
cmdutil
.
Factory
,
out
io
.
Writer
,
cmd
*
cobra
.
Command
)
(
err
error
)
{
o
.
outputVersion
,
err
=
outputVersion
(
cmd
,
&
scheme
.
Registry
.
EnabledVersionsForGroup
(
api
.
GroupName
)[
0
]
)
o
.
specifiedOutputVersion
,
err
=
outputVersion
(
cmd
)
if
err
!=
nil
{
return
err
}
if
!
scheme
.
Registry
.
IsEnabledVersion
(
o
.
outputVersion
)
{
return
cmdutil
.
UsageErrorf
(
cmd
,
"'%s' is not a registered version."
,
o
.
outputVersion
)
}
// build the builder
o
.
builder
=
f
.
NewBuilder
()
.
...
...
@@ -184,7 +177,7 @@ func (o *ConvertOptions) RunConvert() error {
return
fmt
.
Errorf
(
"no objects passed to convert"
)
}
objects
,
err
:=
asVersionedObject
(
infos
,
!
singleItemImplied
,
o
.
o
utputVersion
,
o
.
encoder
)
objects
,
err
:=
asVersionedObject
(
infos
,
!
singleItemImplied
,
o
.
specifiedO
utputVersion
,
o
.
encoder
)
if
err
!=
nil
{
return
err
}
...
...
@@ -194,7 +187,7 @@ func (o *ConvertOptions) RunConvert() error {
if
err
!=
nil
{
return
err
}
filteredObj
,
err
:=
objectListToVersionedObject
(
items
,
o
.
o
utputVersion
)
filteredObj
,
err
:=
objectListToVersionedObject
(
items
,
o
.
specifiedO
utputVersion
)
if
err
!=
nil
{
return
err
}
...
...
@@ -206,9 +199,14 @@ func (o *ConvertOptions) RunConvert() error {
// objectListToVersionedObject receives a list of api objects and a group version
// and squashes the list's items into a single versioned runtime.Object.
func
objectListToVersionedObject
(
objects
[]
runtime
.
Object
,
v
ersion
schema
.
GroupVersion
)
(
runtime
.
Object
,
error
)
{
func
objectListToVersionedObject
(
objects
[]
runtime
.
Object
,
specifiedOutputV
ersion
schema
.
GroupVersion
)
(
runtime
.
Object
,
error
)
{
objectList
:=
&
api
.
List
{
Items
:
objects
}
converted
,
err
:=
tryConvert
(
scheme
.
Scheme
,
objectList
,
version
,
scheme
.
Registry
.
GroupOrDie
(
api
.
GroupName
)
.
GroupVersion
)
targetVersions
:=
[]
schema
.
GroupVersion
{}
if
!
specifiedOutputVersion
.
Empty
()
{
targetVersions
=
append
(
targetVersions
,
specifiedOutputVersion
)
}
targetVersions
=
append
(
targetVersions
,
scheme
.
Registry
.
GroupOrDie
(
api
.
GroupName
)
.
GroupVersion
)
converted
,
err
:=
tryConvert
(
scheme
.
Scheme
,
objectList
,
targetVersions
...
)
if
err
!=
nil
{
return
nil
,
err
}
...
...
@@ -219,8 +217,8 @@ func objectListToVersionedObject(objects []runtime.Object, version schema.GroupV
// 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
,
v
ersion
schema
.
GroupVersion
,
encoder
runtime
.
Encoder
)
(
runtime
.
Object
,
error
)
{
objects
,
err
:=
asVersionedObjects
(
infos
,
v
ersion
,
encoder
)
func
asVersionedObject
(
infos
[]
*
resource
.
Info
,
forceList
bool
,
specifiedOutputV
ersion
schema
.
GroupVersion
,
encoder
runtime
.
Encoder
)
(
runtime
.
Object
,
error
)
{
objects
,
err
:=
asVersionedObjects
(
infos
,
specifiedOutputV
ersion
,
encoder
)
if
err
!=
nil
{
return
nil
,
err
}
...
...
@@ -230,7 +228,13 @@ func asVersionedObject(infos []*resource.Info, forceList bool, version schema.Gr
object
=
objects
[
0
]
}
else
{
object
=
&
api
.
List
{
Items
:
objects
}
converted
,
err
:=
tryConvert
(
scheme
.
Scheme
,
object
,
version
,
scheme
.
Registry
.
GroupOrDie
(
api
.
GroupName
)
.
GroupVersion
)
targetVersions
:=
[]
schema
.
GroupVersion
{}
if
!
specifiedOutputVersion
.
Empty
()
{
targetVersions
=
append
(
targetVersions
,
specifiedOutputVersion
)
}
targetVersions
=
append
(
targetVersions
,
scheme
.
Registry
.
GroupOrDie
(
api
.
GroupName
)
.
GroupVersion
)
converted
,
err
:=
tryConvert
(
scheme
.
Scheme
,
object
,
targetVersions
...
)
if
err
!=
nil
{
return
nil
,
err
}
...
...
@@ -238,7 +242,7 @@ func asVersionedObject(infos []*resource.Info, forceList bool, version schema.Gr
}
actualVersion
:=
object
.
GetObjectKind
()
.
GroupVersionKind
()
if
actualVersion
.
Version
!=
v
ersion
.
Version
{
if
actualVersion
.
Version
!=
specifiedOutputV
ersion
.
Version
{
defaultVersionInfo
:=
""
if
len
(
actualVersion
.
Version
)
>
0
{
defaultVersionInfo
=
fmt
.
Sprintf
(
"Defaulting to %q"
,
actualVersion
.
Version
)
...
...
@@ -251,16 +255,17 @@ func asVersionedObject(infos []*resource.Info, forceList bool, version schema.Gr
// 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
,
v
ersion
schema
.
GroupVersion
,
encoder
runtime
.
Encoder
)
([]
runtime
.
Object
,
error
)
{
func
asVersionedObjects
(
infos
[]
*
resource
.
Info
,
specifiedOutputV
ersion
schema
.
GroupVersion
,
encoder
runtime
.
Encoder
)
([]
runtime
.
Object
,
error
)
{
objects
:=
[]
runtime
.
Object
{}
for
_
,
info
:=
range
infos
{
if
info
.
Object
==
nil
{
continue
}
targetVersions
:=
[]
schema
.
GroupVersion
{}
// objects that are not part of api.Scheme must be converted to JSON
// TODO: convert to map[string]interface{}, attach to runtime.Unknown?
if
!
v
ersion
.
Empty
()
{
if
!
specifiedOutputV
ersion
.
Empty
()
{
if
_
,
_
,
err
:=
scheme
.
Scheme
.
ObjectKinds
(
info
.
Object
);
runtime
.
IsNotRegisteredError
(
err
)
{
// TODO: ideally this would encode to version, but we don't expose multiple codecs here.
data
,
err
:=
runtime
.
Encode
(
encoder
,
info
.
Object
)
...
...
@@ -271,9 +276,19 @@ func asVersionedObjects(infos []*resource.Info, version schema.GroupVersion, enc
objects
=
append
(
objects
,
&
runtime
.
Unknown
{
Raw
:
data
})
continue
}
targetVersions
=
append
(
targetVersions
,
specifiedOutputVersion
)
}
else
{
gvks
,
_
,
err
:=
scheme
.
Scheme
.
ObjectKinds
(
info
.
Object
)
if
err
==
nil
{
for
_
,
gvk
:=
range
gvks
{
for
_
,
version
:=
range
scheme
.
Registry
.
EnabledVersionsForGroup
(
gvk
.
Group
)
{
targetVersions
=
append
(
targetVersions
,
version
)
}
}
}
}
converted
,
err
:=
tryConvert
(
info
.
Mapping
.
ObjectConvertor
,
info
.
Object
,
version
,
info
.
Mapping
.
GroupVersionKind
.
GroupVersion
()
)
converted
,
err
:=
tryConvert
(
info
.
Mapping
.
ObjectConvertor
,
info
.
Object
,
targetVersions
...
)
if
err
!=
nil
{
return
nil
,
err
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment