mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-07 03:03:59 +00:00
Merge pull request #2326 from marianitadn/read-json-directories-as-object-streams
Add directory as option for createall command
This commit is contained in:
commit
f05c4a69e8
@ -69,13 +69,16 @@ func DataToObjects(m meta.RESTMapper, t runtime.ObjectTyper, data []byte) (resul
|
|||||||
|
|
||||||
func (f *Factory) NewCmdCreateAll(out io.Writer) *cobra.Command {
|
func (f *Factory) NewCmdCreateAll(out io.Writer) *cobra.Command {
|
||||||
cmd := &cobra.Command{
|
cmd := &cobra.Command{
|
||||||
Use: "createall -f filename",
|
Use: "createall [-d directory] [-f filename]",
|
||||||
Short: "Create all resources specified in filename or stdin",
|
Short: "Create all resources specified in a directory, filename or stdin",
|
||||||
Long: `Create all resources contained in JSON file specified in filename or stdin
|
Long: `Create all resources contained in JSON file specified in a directory, filename or stdin
|
||||||
|
|
||||||
JSON and YAML formats are accepted.
|
JSON and YAML formats are accepted.
|
||||||
|
|
||||||
Examples:
|
Examples:
|
||||||
|
$ kubectl createall -d configs/
|
||||||
|
<creates all resources listed in JSON or YAML files, found recursively under the configs directory>
|
||||||
|
|
||||||
$ kubectl createall -f config.json
|
$ kubectl createall -f config.json
|
||||||
<creates all resources listed in config.json>
|
<creates all resources listed in config.json>
|
||||||
|
|
||||||
@ -87,23 +90,36 @@ Examples:
|
|||||||
}
|
}
|
||||||
|
|
||||||
filename := GetFlagString(cmd, "filename")
|
filename := GetFlagString(cmd, "filename")
|
||||||
if len(filename) == 0 {
|
directory := GetFlagString(cmd, "directory")
|
||||||
usageError(cmd, "Must pass a filename to update")
|
if (len(filename) == 0 && len(directory) == 0) || (len(filename) != 0 && len(directory) != 0) {
|
||||||
|
usageError(cmd, "Must pass a directory or filename to update")
|
||||||
}
|
}
|
||||||
|
|
||||||
data, err := ReadConfigData(filename)
|
files := []string{}
|
||||||
checkErr(err)
|
if len(filename) != 0 {
|
||||||
|
files = append(files, filename)
|
||||||
|
|
||||||
items, errs := DataToObjects(f.Mapper, f.Typer, data)
|
} else {
|
||||||
applyErrs := config.CreateObjects(f.Typer, f.Mapper, clientFunc, items)
|
files = append(GetFilesFromDir(directory, ".json"), GetFilesFromDir(directory, ".yaml")...)
|
||||||
errs = append(errs, applyErrs...)
|
}
|
||||||
if len(errs) > 0 {
|
|
||||||
for _, e := range errs {
|
for _, filename := range files {
|
||||||
glog.Error(e)
|
data, err := ReadConfigData(filename)
|
||||||
|
checkErr(err)
|
||||||
|
|
||||||
|
items, errs := DataToObjects(f.Mapper, f.Typer, data)
|
||||||
|
applyErrs := config.CreateObjects(f.Typer, f.Mapper, clientFunc, items)
|
||||||
|
|
||||||
|
errs = append(errs, applyErrs...)
|
||||||
|
if len(errs) > 0 {
|
||||||
|
for _, e := range errs {
|
||||||
|
glog.Error(e)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
cmd.Flags().StringP("directory", "d", "", "Directory of JSON or YAML files to use to update the resource")
|
||||||
cmd.Flags().StringP("filename", "f", "", "Filename or URL to file to use to update the resource")
|
cmd.Flags().StringP("filename", "f", "", "Filename or URL to file to use to update the resource")
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
@ -21,6 +21,7 @@ import (
|
|||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@ -92,6 +93,21 @@ func FirstNonEmptyString(args ...string) string {
|
|||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Return a list of file names of a certain type within a given directory.
|
||||||
|
func GetFilesFromDir(directory string, fileType string) []string {
|
||||||
|
files := []string{}
|
||||||
|
|
||||||
|
err := filepath.Walk(directory, func(path string, f os.FileInfo, err error) error {
|
||||||
|
if filepath.Ext(path) == fileType {
|
||||||
|
files = append(files, path)
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
})
|
||||||
|
|
||||||
|
checkErr(err)
|
||||||
|
return files
|
||||||
|
}
|
||||||
|
|
||||||
// ReadConfigData reads the bytes from the specified filesytem or network
|
// ReadConfigData reads the bytes from the specified filesytem or network
|
||||||
// location or from stdin if location == "-".
|
// location or from stdin if location == "-".
|
||||||
func ReadConfigData(location string) ([]byte, error) {
|
func ReadConfigData(location string) ([]byte, error) {
|
||||||
|
Loading…
Reference in New Issue
Block a user