2015-07-16 09:20:53 +00:00
/ *
2016-06-03 00:25:58 +00:00
Copyright 2014 The Kubernetes Authors .
2015-07-16 09:20:53 +00:00
Licensed under the Apache License , Version 2.0 ( the "License" ) ;
you may not use this file except in compliance with the License .
You may obtain a copy of the License at
http : //www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing , software
distributed under the License is distributed on an "AS IS" BASIS ,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND , either express or implied .
See the License for the specific language governing permissions and
limitations under the License .
* /
package cmd
import (
"fmt"
2018-06-27 18:14:44 +00:00
"github.com/golang/glog"
"github.com/spf13/cobra"
2017-01-11 14:09:48 +00:00
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
2017-11-01 07:18:48 +00:00
scheme "k8s.io/kubernetes/pkg/api/legacyscheme"
2017-11-08 22:34:54 +00:00
api "k8s.io/kubernetes/pkg/apis/core"
2016-10-07 22:24:42 +00:00
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
2015-07-16 09:20:53 +00:00
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
2018-04-19 00:02:37 +00:00
"k8s.io/kubernetes/pkg/kubectl/genericclioptions"
2018-05-02 19:15:47 +00:00
"k8s.io/kubernetes/pkg/kubectl/genericclioptions/printers"
2018-05-10 12:20:27 +00:00
"k8s.io/kubernetes/pkg/kubectl/genericclioptions/resource"
2017-07-07 04:04:11 +00:00
"k8s.io/kubernetes/pkg/kubectl/util/i18n"
2018-06-27 18:14:44 +00:00
"k8s.io/kubernetes/pkg/kubectl/validation"
2015-07-16 09:20:53 +00:00
)
2016-05-20 17:49:56 +00:00
var (
2017-03-15 03:49:10 +00:00
convert_long = templates . LongDesc ( i18n . T ( `
2016-05-20 17:49:56 +00:00
Convert config files between different API versions . Both YAML
and JSON formats are accepted .
The command takes filename , directory , or URL as input , and convert it into format
of version specified by -- output - version flag . If target version is not specified or
not supported , convert to latest version .
2015-07-16 09:20:53 +00:00
2016-05-20 17:49:56 +00:00
The default output will be printed to stdout in YAML format . One can use - o option
2017-03-15 03:49:10 +00:00
to change to output destination . ` ) )
2015-07-16 09:20:53 +00:00
2017-03-15 03:49:10 +00:00
convert_example = templates . Examples ( i18n . T ( `
2016-05-20 17:49:56 +00:00
# Convert ' pod . yaml ' to latest version and print to stdout .
kubectl convert - f pod . yaml
2015-07-16 09:20:53 +00:00
2016-05-20 17:49:56 +00:00
# Convert the live state of the resource specified by ' pod . yaml ' to the latest version
2017-10-17 08:50:43 +00:00
# and print to stdout in JSON format .
2016-05-20 17:49:56 +00:00
kubectl convert - f pod . yaml -- local - o json
2015-07-16 09:20:53 +00:00
2016-05-20 17:49:56 +00:00
# Convert all files under current directory to latest version and create them all .
2017-03-15 03:49:10 +00:00
kubectl convert - f . | kubectl create - f - ` ) )
2015-07-16 09:20:53 +00:00
)
2018-06-27 18:14:44 +00:00
// ConvertOptions have the data required to perform the convert operation
type ConvertOptions struct {
PrintFlags * genericclioptions . PrintFlags
Printer printers . ResourcePrinter
OutputVersion string
Namespace string
builder func ( ) * resource . Builder
local bool
validator func ( ) ( validation . Schema , error )
resource . FilenameOptions
genericclioptions . IOStreams
}
func NewConvertOptions ( ioStreams genericclioptions . IOStreams ) * ConvertOptions {
return & ConvertOptions {
PrintFlags : genericclioptions . NewPrintFlags ( "converted" ) . WithTypeSetter ( scheme . Scheme ) . WithDefaultOutput ( "yaml" ) ,
local : true ,
IOStreams : ioStreams ,
}
}
2015-07-16 09:20:53 +00:00
// NewCmdConvert creates a command object for the generic "convert" action, which
// translates the config file into a given version.
2018-04-19 00:02:37 +00:00
func NewCmdConvert ( f cmdutil . Factory , ioStreams genericclioptions . IOStreams ) * cobra . Command {
2018-06-27 18:14:44 +00:00
o := NewConvertOptions ( ioStreams )
2015-07-16 09:20:53 +00:00
cmd := & cobra . Command {
2017-10-11 06:26:02 +00:00
Use : "convert -f FILENAME" ,
DisableFlagsInUseLine : true ,
2017-01-25 01:00:32 +00:00
Short : i18n . T ( "Convert config files between different API versions" ) ,
2015-07-16 09:20:53 +00:00
Long : convert_long ,
Example : convert_example ,
Run : func ( cmd * cobra . Command , args [ ] string ) {
2018-06-27 18:14:44 +00:00
cmdutil . CheckErr ( o . Complete ( f , cmd ) )
cmdutil . CheckErr ( o . RunConvert ( ) )
2015-07-16 09:20:53 +00:00
} ,
}
2018-06-27 18:14:44 +00:00
cmd . Flags ( ) . BoolVar ( & o . local , "local" , o . local , "If true, convert will NOT try to contact api-server but run locally." )
2018-08-09 04:35:18 +00:00
cmd . Flags ( ) . StringVar ( & o . OutputVersion , "output-version" , o . OutputVersion , i18n . T ( "Output the formatted object with the given group version (for ex: 'extensions/v1beta1')." ) )
2018-06-27 18:14:44 +00:00
o . PrintFlags . AddFlags ( cmd )
2018-04-19 00:02:37 +00:00
2015-07-16 09:20:53 +00:00
cmdutil . AddValidateFlags ( cmd )
2018-06-27 18:14:44 +00:00
cmdutil . AddFilenameOptionFlags ( cmd , & o . FilenameOptions , "to need to get converted." )
cmd . MarkFlagRequired ( "filename" )
2015-07-16 09:20:53 +00:00
return cmd
}
2018-06-27 18:14:44 +00:00
// Complete collects information required to run Convert command from command line.
func ( o * ConvertOptions ) Complete ( f cmdutil . Factory , cmd * cobra . Command ) ( err error ) {
o . builder = f . NewBuilder
2015-07-16 09:20:53 +00:00
2018-06-27 18:14:44 +00:00
o . Namespace , _ , err = f . ToRawKubeConfigLoader ( ) . Namespace ( )
if err != nil {
return err
2018-04-19 00:02:37 +00:00
}
2018-02-24 07:55:55 +00:00
2018-06-27 18:14:44 +00:00
o . validator = func ( ) ( validation . Schema , error ) {
return f . Validator ( cmdutil . GetFlagBool ( cmd , "validate" ) )
2017-02-16 18:23:59 +00:00
}
2018-06-27 18:14:44 +00:00
// build the printer
o . Printer , err = o . PrintFlags . ToPrinter ( )
2015-11-16 16:42:09 +00:00
if err != nil {
2015-12-01 16:52:11 +00:00
return err
2015-11-16 16:42:09 +00:00
}
2018-06-27 18:14:44 +00:00
return nil
}
2015-07-16 09:20:53 +00:00
2018-06-27 18:14:44 +00:00
// RunConvert implements the generic Convert command
func ( o * ConvertOptions ) RunConvert ( ) error {
b := o . builder ( ) .
2018-04-27 15:38:34 +00:00
WithScheme ( scheme . Scheme ) .
2017-11-15 06:10:30 +00:00
LocalParam ( o . local )
2017-11-14 03:43:58 +00:00
if ! o . local {
2018-06-27 18:14:44 +00:00
schema , err := o . validator ( )
2015-07-16 09:20:53 +00:00
if err != nil {
return err
}
2018-06-27 18:14:44 +00:00
b . Schema ( schema )
2015-07-16 09:20:53 +00:00
}
2017-08-02 20:23:07 +00:00
2018-06-27 18:14:44 +00:00
r := b . NamespaceParam ( o . Namespace ) .
2015-07-16 09:20:53 +00:00
ContinueOnError ( ) .
2016-08-17 18:28:07 +00:00
FilenameParam ( false , & o . FilenameOptions ) .
2018-06-27 18:14:44 +00:00
Flatten ( ) .
Do ( )
2015-07-16 09:20:53 +00:00
2016-04-14 22:00:40 +00:00
err := r . Err ( )
2015-07-16 09:20:53 +00:00
if err != nil {
return err
}
2017-01-09 19:21:24 +00:00
singleItemImplied := false
infos , err := r . IntoSingleItemImplied ( & singleItemImplied ) . Infos ( )
2016-11-10 19:52:42 +00:00
if err != nil {
return err
}
if len ( infos ) == 0 {
return fmt . Errorf ( "no objects passed to convert" )
}
2018-06-27 18:14:44 +00:00
var specifiedOutputVersion schema . GroupVersion
if len ( o . OutputVersion ) > 0 {
specifiedOutputVersion , err = schema . ParseGroupVersion ( o . OutputVersion )
2018-06-26 15:50:37 +00:00
if err != nil {
return err
}
2015-07-16 09:20:53 +00:00
}
2016-11-10 19:52:42 +00:00
2018-06-27 18:14:44 +00:00
objects , err := asVersionedObject ( infos , ! singleItemImplied , specifiedOutputVersion , cmdutil . InternalVersionJSONEncoder ( ) )
2017-10-13 02:32:41 +00:00
if err != nil {
2018-06-27 18:14:44 +00:00
return err
2017-10-13 02:32:41 +00:00
}
2018-06-27 18:14:44 +00:00
return o . Printer . PrintObj ( objects , o . Out )
2017-10-13 02:32:41 +00:00
}
2017-10-17 08:50:43 +00:00
// asVersionedObject converts a list of infos into a single object - either a List containing
2017-10-13 02:32:41 +00:00
// 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.
2017-12-13 13:48:14 +00:00
func asVersionedObject ( infos [ ] * resource . Info , forceList bool , specifiedOutputVersion schema . GroupVersion , encoder runtime . Encoder ) ( runtime . Object , error ) {
objects , err := asVersionedObjects ( infos , specifiedOutputVersion , encoder )
2017-10-13 02:32:41 +00:00
if err != nil {
return nil , err
}
var object runtime . Object
if len ( objects ) == 1 && ! forceList {
object = objects [ 0 ]
} else {
object = & api . List { Items : objects }
2017-12-13 13:48:14 +00:00
targetVersions := [ ] schema . GroupVersion { }
if ! specifiedOutputVersion . Empty ( ) {
targetVersions = append ( targetVersions , specifiedOutputVersion )
}
2018-05-07 12:32:20 +00:00
targetVersions = append ( targetVersions , schema . GroupVersion { Group : "" , Version : "v1" } )
2017-12-13 13:48:14 +00:00
converted , err := tryConvert ( scheme . Scheme , object , targetVersions ... )
2017-10-13 02:32:41 +00:00
if err != nil {
return nil , err
}
object = converted
}
actualVersion := object . GetObjectKind ( ) . GroupVersionKind ( )
2017-12-13 13:48:14 +00:00
if actualVersion . Version != specifiedOutputVersion . Version {
2017-10-13 02:32:41 +00:00
defaultVersionInfo := ""
if len ( actualVersion . Version ) > 0 {
defaultVersionInfo = fmt . Sprintf ( "Defaulting to %q" , actualVersion . Version )
}
glog . V ( 1 ) . Infof ( "info: the output version specified is invalid. %s\n" , defaultVersionInfo )
}
return object , nil
}
2017-10-17 08:50:43 +00:00
// asVersionedObjects converts a list of infos into versioned objects. The provided
2017-10-13 02:32:41 +00:00
// version will be preferred as the conversion target, but the Object's mapping version will be
// used if that version is not present.
2017-12-13 13:48:14 +00:00
func asVersionedObjects ( infos [ ] * resource . Info , specifiedOutputVersion schema . GroupVersion , encoder runtime . Encoder ) ( [ ] runtime . Object , error ) {
2017-10-13 02:32:41 +00:00
objects := [ ] runtime . Object { }
for _ , info := range infos {
if info . Object == nil {
continue
}
2017-12-13 13:48:14 +00:00
targetVersions := [ ] schema . GroupVersion { }
2017-10-13 02:32:41 +00:00
// objects that are not part of api.Scheme must be converted to JSON
// TODO: convert to map[string]interface{}, attach to runtime.Unknown?
2017-12-13 13:48:14 +00:00
if ! specifiedOutputVersion . Empty ( ) {
2017-10-28 01:31:42 +00:00
if _ , _ , err := scheme . Scheme . ObjectKinds ( info . Object ) ; runtime . IsNotRegisteredError ( err ) {
2017-10-13 02:32:41 +00:00
// TODO: ideally this would encode to version, but we don't expose multiple codecs here.
data , err := runtime . Encode ( encoder , info . Object )
if err != nil {
return nil , err
}
// TODO: Set ContentEncoding and ContentType.
objects = append ( objects , & runtime . Unknown { Raw : data } )
continue
}
2017-12-13 13:48:14 +00:00
targetVersions = append ( targetVersions , specifiedOutputVersion )
} else {
gvks , _ , err := scheme . Scheme . ObjectKinds ( info . Object )
if err == nil {
for _ , gvk := range gvks {
2018-05-07 12:32:20 +00:00
for _ , version := range scheme . Scheme . PrioritizedVersionsForGroup ( gvk . Group ) {
2017-12-13 13:48:14 +00:00
targetVersions = append ( targetVersions , version )
}
}
}
2017-10-13 02:32:41 +00:00
}
2018-04-24 22:31:41 +00:00
converted , err := tryConvert ( scheme . Scheme , info . Object , targetVersions ... )
2017-10-13 02:32:41 +00:00
if err != nil {
return nil , err
}
objects = append ( objects , converted )
}
return objects , nil
}
// tryConvert attempts to convert the given object to the provided versions in order. This function assumes
// the object is in internal version.
func tryConvert ( converter runtime . ObjectConvertor , object runtime . Object , versions ... schema . GroupVersion ) ( runtime . Object , error ) {
var last error
for _ , version := range versions {
if version . Empty ( ) {
return object , nil
}
obj , err := converter . ConvertToVersion ( object , version )
if err != nil {
last = err
continue
}
return obj , nil
}
return nil , last
}