-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
250 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# kubedb | ||
|
||
```bash | ||
$ kubedb --help | ||
|
||
kubedb controls k8sdb ThirdPartyResource objects. | ||
|
||
Find more information at https://github.com/k8sdb/kubedb. | ||
|
||
Basic Commands (Beginner): | ||
create Create a resource by filename or stdin | ||
|
||
Basic Commands (Intermediate): | ||
get Display one or many resources | ||
|
||
Other Commands: | ||
help Help about any command | ||
|
||
Use "kubedb <command> --help" for more information about a given command. | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# kubedb create | ||
|
||
## Example | ||
|
||
##### Help for create command | ||
|
||
```bash | ||
$ kubedb create --help | ||
|
||
Create a resource by filename or stdin. | ||
|
||
JSON and YAML formats are accepted. | ||
|
||
Examples: | ||
# Create a elastic using the data in elastic.json. | ||
kubedb create -f ./elastic.json | ||
|
||
# Create a elastic based on the JSON passed into stdin. | ||
cat elastic.json | kubedb create -f - | ||
|
||
Options: | ||
-f, --filename=[]: Filename to use to create the resource | ||
-R, --recursive=false: Process the directory used in -f, --filename recursively. | ||
|
||
Usage: | ||
kubedb create [options] | ||
|
||
Use "kubedb create options" for a list of global command-line options (applies to all commands). | ||
``` | ||
|
||
##### Create from file | ||
```bash | ||
$ kubedb create -f ./elastic.json | ||
|
||
elastic "elasticsearch-demo" created | ||
``` | ||
|
||
##### Create from stdin | ||
```bash | ||
$ cat ./elastic.json | kubedb create -f - | ||
|
||
elastic "elasticsearch-demo" created | ||
``` | ||
|
||
##### Create from folder | ||
```bash | ||
$ kubedb create -f resources -R | ||
|
||
es "elasticsearch-demo" created | ||
pg "postgres-demo" created | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
package cmd | ||
|
||
import ( | ||
"errors" | ||
"io" | ||
|
||
"github.com/k8sdb/kubedb/pkg/cmd/util" | ||
"github.com/k8sdb/kubedb/pkg/kube" | ||
"github.com/spf13/cobra" | ||
"k8s.io/kubernetes/pkg/kubectl/cmd/templates" | ||
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util" | ||
"k8s.io/kubernetes/pkg/kubectl/resource" | ||
"k8s.io/kubernetes/pkg/runtime" | ||
) | ||
|
||
// ref: k8s.io/kubernetes/pkg/kubectl/cmd/create.go | ||
|
||
var ( | ||
create_long = templates.LongDesc(` | ||
Create a resource by filename or stdin. | ||
JSON and YAML formats are accepted.`) | ||
|
||
create_example = templates.Examples(` | ||
# Create a elastic using the data in elastic.json. | ||
kubedb create -f ./elastic.json | ||
# Create a elastic based on the JSON passed into stdin. | ||
cat elastic.json | kubedb create -f -`) | ||
) | ||
|
||
func NewCmdCreate(out io.Writer, errOut io.Writer) *cobra.Command { | ||
options := &resource.FilenameOptions{} | ||
|
||
cmd := &cobra.Command{ | ||
Use: "create", | ||
Short: "Create a resource by filename or stdin", | ||
Long: create_long, | ||
Example: create_example, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
if cmdutil.IsFilenameEmpty(options.Filenames) { | ||
defaultRunFunc := cmdutil.DefaultSubCommandRun(errOut) | ||
defaultRunFunc(cmd, args) | ||
return | ||
} | ||
f := kube.NewKubeFactory(cmd) | ||
cmdutil.CheckErr(RunCreate(f, out, options)) | ||
}, | ||
} | ||
|
||
util.AddCreateFlags(cmd, options) | ||
return cmd | ||
} | ||
|
||
func RunCreate(f cmdutil.Factory, out io.Writer, options *resource.FilenameOptions) error { | ||
cmdNamespace, enforceNamespace, err := f.DefaultNamespace() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
mapper, typer, err := f.UnstructuredObject() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
r := resource.NewBuilder( | ||
mapper, | ||
typer, | ||
resource.ClientMapperFunc(f.UnstructuredClientForMapping), | ||
runtime.UnstructuredJSONScheme). | ||
Schema(util.Validator()). | ||
ContinueOnError(). | ||
NamespaceParam(cmdNamespace).DefaultNamespace(). | ||
FilenameParam(enforceNamespace, options). | ||
Flatten(). | ||
Do() | ||
|
||
err = r.Err() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
infoList := make([]*resource.Info, 0) | ||
err = r.Visit(func(info *resource.Info, err error) error { | ||
if err != nil { | ||
return err | ||
} | ||
|
||
kind := info.GetObjectKind().GroupVersionKind().Kind | ||
if err := util.CheckSupportedResource(kind); err != nil { | ||
return err | ||
} | ||
|
||
infoList = append(infoList, info) | ||
return nil | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
showAlias := false | ||
if len(infoList) > 1 { | ||
showAlias = true | ||
} | ||
|
||
count := 0 | ||
for _, info := range infoList { | ||
if err := createAndRefresh(info); err != nil { | ||
return cmdutil.AddSourceToErr("creating", info.Source, err) | ||
} | ||
count++ | ||
resourceName := info.Mapping.Resource | ||
if showAlias { | ||
if alias, ok := util.ResourceShortFormFor(info.Mapping.Resource); ok { | ||
resourceName = alias | ||
} | ||
} | ||
cmdutil.PrintSuccess(mapper, false, out, resourceName, info.Name, false, "created") | ||
} | ||
|
||
if count == 0 { | ||
return errors.New("no objects passed to create") | ||
} | ||
return nil | ||
} | ||
|
||
func createAndRefresh(info *resource.Info) error { | ||
obj, err := resource.NewHelper(info.Client, info.Mapping).Create(info.Namespace, true, info.Object) | ||
if err != nil { | ||
return err | ||
} | ||
info.Refresh(obj, true) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package util | ||
|
||
import ( | ||
"k8s.io/kubernetes/pkg/api/validation" | ||
) | ||
|
||
type ConjunctiveSchema []validation.Schema | ||
|
||
func (c *ConjunctiveSchema) ValidateBytes(data []byte) error { | ||
return nil | ||
} | ||
|
||
func Validator() validation.Schema { | ||
return validation.ConjunctiveSchema{ | ||
validation.NoDoubleKeySchema{}, | ||
} | ||
} |