2015-07-16 09:20:53 +00:00
|
|
|
/*
|
|
|
|
Copyright 2014 The Kubernetes Authors All rights reserved.
|
|
|
|
|
|
|
|
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"
|
|
|
|
"io"
|
|
|
|
|
2016-01-13 22:40:56 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api"
|
2015-11-16 16:42:09 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api/unversioned"
|
2016-01-13 22:40:56 +00:00
|
|
|
"k8s.io/kubernetes/pkg/apimachinery/registered"
|
2015-07-16 09:20:53 +00:00
|
|
|
"k8s.io/kubernetes/pkg/kubectl"
|
|
|
|
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
|
|
|
|
"k8s.io/kubernetes/pkg/kubectl/resource"
|
2015-12-21 05:37:49 +00:00
|
|
|
"k8s.io/kubernetes/pkg/runtime"
|
2015-07-16 09:20:53 +00:00
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
convert_long = `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.
|
|
|
|
|
|
|
|
The default output will be printed to stdout in YAML format. One can use -o option
|
|
|
|
to change to output destination.
|
|
|
|
`
|
|
|
|
convert_example = `# Convert 'pod.yaml' to latest version and print to stdout.
|
2016-02-29 14:41:09 +00:00
|
|
|
kubectl convert -f pod.yaml
|
2015-07-16 09:20:53 +00:00
|
|
|
|
|
|
|
# Convert the live state of the resource specified by 'pod.yaml' to the latest version
|
|
|
|
# and print to stdout in json format.
|
2016-02-29 14:41:09 +00:00
|
|
|
kubectl convert -f pod.yaml --local -o json
|
2015-07-16 09:20:53 +00:00
|
|
|
|
|
|
|
# Convert all files under current directory to latest version and create them all.
|
2016-02-29 14:41:09 +00:00
|
|
|
kubectl convert -f . | kubectl create -f -
|
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.
|
|
|
|
func NewCmdConvert(f *cmdutil.Factory, out io.Writer) *cobra.Command {
|
|
|
|
options := &ConvertOptions{}
|
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "convert -f FILENAME",
|
|
|
|
Short: "Convert config files between different API versions",
|
|
|
|
Long: convert_long,
|
|
|
|
Example: convert_example,
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
err := options.Complete(f, out, cmd, args)
|
|
|
|
cmdutil.CheckErr(err)
|
|
|
|
err = options.RunConvert()
|
|
|
|
cmdutil.CheckErr(err)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
usage := "Filename, directory, or URL to file to need to get converted."
|
|
|
|
kubectl.AddJsonFilenameFlag(cmd, &options.filenames, usage)
|
2016-03-28 19:44:21 +00:00
|
|
|
cmdutil.AddRecursiveFlag(cmd, &options.recursive)
|
2015-07-16 09:20:53 +00:00
|
|
|
cmd.MarkFlagRequired("filename")
|
|
|
|
cmdutil.AddValidateFlags(cmd)
|
|
|
|
cmdutil.AddPrinterFlags(cmd)
|
|
|
|
cmd.Flags().BoolVar(&options.local, "local", true, "If true, convert will NOT try to contact api-server but run locally.")
|
2016-03-10 01:27:19 +00:00
|
|
|
cmdutil.AddInclude3rdPartyFlags(cmd)
|
2015-07-16 09:20:53 +00:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
// ConvertOptions have the data required to perform the convert operation
|
|
|
|
type ConvertOptions struct {
|
|
|
|
builder *resource.Builder
|
|
|
|
filenames []string
|
|
|
|
local bool
|
|
|
|
|
2015-12-21 05:37:49 +00:00
|
|
|
encoder runtime.Encoder
|
2015-07-16 09:20:53 +00:00
|
|
|
out io.Writer
|
|
|
|
printer kubectl.ResourcePrinter
|
|
|
|
|
2015-12-01 16:52:11 +00:00
|
|
|
outputVersion unversioned.GroupVersion
|
2016-03-28 19:44:21 +00:00
|
|
|
|
|
|
|
recursive bool
|
2015-07-16 09:20:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Complete collects information required to run Convert command from command line.
|
|
|
|
func (o *ConvertOptions) Complete(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string) (err error) {
|
2016-01-13 22:40:56 +00:00
|
|
|
o.outputVersion, err = cmdutil.OutputVersion(cmd, ®istered.EnabledVersionsForGroup(api.GroupName)[0])
|
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
|
|
|
}
|
2015-12-19 05:08:34 +00:00
|
|
|
if !registered.IsEnabledVersion(o.outputVersion) {
|
2015-07-16 09:20:53 +00:00
|
|
|
cmdutil.UsageError(cmd, "'%s' is not a registered version.", o.outputVersion)
|
|
|
|
}
|
|
|
|
|
|
|
|
// build the builder
|
2016-03-10 01:27:19 +00:00
|
|
|
mapper, typer := f.Object(cmdutil.GetIncludeThirdPartyAPIs(cmd))
|
2015-12-21 05:37:49 +00:00
|
|
|
clientMapper := resource.ClientMapperFunc(f.ClientForMapping)
|
2016-03-10 01:27:19 +00:00
|
|
|
|
2015-07-16 09:20:53 +00:00
|
|
|
if o.local {
|
|
|
|
fmt.Fprintln(out, "running in local mode...")
|
2016-03-23 23:45:24 +00:00
|
|
|
o.builder = resource.NewBuilder(mapper, typer, resource.DisabledClientForMapping{ClientMapper: clientMapper}, f.Decoder(true))
|
2015-07-16 09:20:53 +00:00
|
|
|
} else {
|
2015-12-21 05:37:49 +00:00
|
|
|
o.builder = resource.NewBuilder(mapper, typer, clientMapper, f.Decoder(true))
|
2015-07-16 09:20:53 +00:00
|
|
|
schema, err := f.Validator(cmdutil.GetFlagBool(cmd, "validate"), cmdutil.GetFlagString(cmd, "schema-cache-dir"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
o.builder = o.builder.Schema(schema)
|
|
|
|
}
|
|
|
|
cmdNamespace, _, err := f.DefaultNamespace()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
o.builder = o.builder.NamespaceParam(cmdNamespace).
|
|
|
|
ContinueOnError().
|
2016-03-28 19:44:21 +00:00
|
|
|
FilenameParam(false, o.recursive, o.filenames...).
|
2015-07-16 09:20:53 +00:00
|
|
|
Flatten()
|
|
|
|
|
|
|
|
// build the printer
|
|
|
|
o.out = out
|
|
|
|
outputFormat := cmdutil.GetFlagString(cmd, "output")
|
|
|
|
templateFile := cmdutil.GetFlagString(cmd, "template")
|
|
|
|
if len(outputFormat) == 0 {
|
|
|
|
if len(templateFile) == 0 {
|
|
|
|
outputFormat = "yaml"
|
|
|
|
} else {
|
|
|
|
outputFormat = "template"
|
|
|
|
}
|
|
|
|
}
|
2015-12-21 05:37:49 +00:00
|
|
|
o.encoder = f.JSONEncoder()
|
2015-07-16 09:20:53 +00:00
|
|
|
o.printer, _, err = kubectl.GetPrinter(outputFormat, templateFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RunConvert implements the generic Convert command
|
|
|
|
func (o *ConvertOptions) RunConvert() error {
|
|
|
|
infos, err := o.builder.Do().Infos()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-12-21 05:37:49 +00:00
|
|
|
objects, err := resource.AsVersionedObject(infos, false, o.outputVersion.String(), o.encoder)
|
2015-07-16 09:20:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return o.printer.PrintObj(objects, o.out)
|
|
|
|
}
|