-
Notifications
You must be signed in to change notification settings - Fork 4.8k
oc: Use default resources where it makes sense #4947
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,6 +27,7 @@ import ( | |
|
|
||
| buildapi "github.com/openshift/origin/pkg/build/api" | ||
| osclient "github.com/openshift/origin/pkg/client" | ||
| osutil "github.com/openshift/origin/pkg/cmd/util" | ||
| "github.com/openshift/origin/pkg/cmd/util/clientcmd" | ||
| "github.com/openshift/origin/pkg/generate/git" | ||
| "github.com/openshift/source-to-image/pkg/tar" | ||
|
|
@@ -129,27 +130,44 @@ func RunStartBuild(f *clientcmd.Factory, in io.Reader, out io.Writer, cmd *cobra | |
| return cmdutil.UsageError(cmd, "Must pass a name of a build config or specify build name with '--from-build' flag") | ||
| } | ||
|
|
||
| name := buildName | ||
| isBuild := true | ||
| namespace, _, err := f.DefaultNamespace() | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| var ( | ||
| name = buildName | ||
| resource = "builds" | ||
| ) | ||
|
|
||
| if len(name) == 0 && len(args) > 0 && len(args[0]) > 0 { | ||
| mapper, _ := f.Object() | ||
| resource, name, err = osutil.ResolveResource("buildconfigs", args[0], mapper) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. don't you need to make sure you got back a non-empty name? I would do so after this block, to catch the case where
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok |
||
| if err != nil { | ||
| return err | ||
| } | ||
| switch resource { | ||
| case "buildconfigs": | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add a
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok |
||
| // no special handling required | ||
| case "builds": | ||
| return fmt.Errorf("use --from-build to rerun your builds") | ||
| default: | ||
| return fmt.Errorf("invalid resource provided: %s", resource) | ||
| } | ||
| } | ||
| if len(name) == 0 { | ||
| name = args[0] | ||
| isBuild = false | ||
| return fmt.Errorf("a resource name is required either as an argument or by using --from-build") | ||
| } | ||
|
|
||
| if webhooks.Provided() { | ||
| return RunListBuildWebHooks(f, out, cmd.Out(), name, isBuild, webhooks.String()) | ||
| return RunListBuildWebHooks(f, out, cmd.Out(), name, resource, webhooks.String()) | ||
| } | ||
|
|
||
| client, _, err := f.Clients() | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| namespace, _, err := f.DefaultNamespace() | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| request := &buildapi.BuildRequest{ | ||
| ObjectMeta: kapi.ObjectMeta{Name: name}, | ||
| } | ||
|
|
@@ -166,7 +184,7 @@ func RunStartBuild(f *clientcmd.Factory, in io.Reader, out io.Writer, cmd *cobra | |
|
|
||
| var newBuild *buildapi.Build | ||
| switch { | ||
| case !isBuild && (len(fromFile) > 0 || len(fromDir) > 0 || len(fromRepo) > 0): | ||
| case len(args) > 0 && (len(fromFile) > 0 || len(fromDir) > 0 || len(fromRepo) > 0): | ||
| request := &buildapi.BinaryBuildRequestOptions{ | ||
| ObjectMeta: kapi.ObjectMeta{ | ||
| Name: name, | ||
|
|
@@ -177,16 +195,16 @@ func RunStartBuild(f *clientcmd.Factory, in io.Reader, out io.Writer, cmd *cobra | |
| if newBuild, err = streamPathToBuild(git, in, cmd.Out(), client.BuildConfigs(namespace), fromDir, fromFile, fromRepo, request); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| case isBuild: | ||
| case resource == "builds": | ||
| if newBuild, err = client.Builds(namespace).Clone(request); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| default: | ||
| case resource == "buildconfigs": | ||
| if newBuild, err = client.BuildConfigs(namespace).Instantiate(request); err != nil { | ||
| return err | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add a
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We already have that covered in L126, but uf you really want to check it here too, I can add it. |
||
| default: | ||
| return fmt.Errorf("invalid resource provided: %s", resource) | ||
| } | ||
|
|
||
| fmt.Fprintln(out, newBuild.Name) | ||
|
|
@@ -240,7 +258,7 @@ func RunStartBuild(f *clientcmd.Factory, in io.Reader, out io.Writer, cmd *cobra | |
| } | ||
|
|
||
| // RunListBuildWebHooks prints the webhooks for the provided build config. | ||
| func RunListBuildWebHooks(f *clientcmd.Factory, out, errOut io.Writer, name string, isBuild bool, webhookFilter string) error { | ||
| func RunListBuildWebHooks(f *clientcmd.Factory, out, errOut io.Writer, name, resource, webhookFilter string) error { | ||
| generic, github := false, false | ||
| prefix := false | ||
| switch webhookFilter { | ||
|
|
@@ -263,7 +281,10 @@ func RunListBuildWebHooks(f *clientcmd.Factory, out, errOut io.Writer, name stri | |
| return err | ||
| } | ||
|
|
||
| if isBuild { | ||
| switch resource { | ||
| case "buildconfigs": | ||
| // no special handling required | ||
| case "builds": | ||
| build, err := client.Builds(namespace).Get(name) | ||
| if err != nil { | ||
| return err | ||
|
|
@@ -276,7 +297,10 @@ func RunListBuildWebHooks(f *clientcmd.Factory, out, errOut io.Writer, name stri | |
| namespace = ref.Namespace | ||
| } | ||
| name = ref.Name | ||
| default: | ||
| return fmt.Errorf("invalid resource provided: %s", resource) | ||
| } | ||
|
|
||
| config, err := client.BuildConfigs(namespace).Get(name) | ||
| if err != nil { | ||
| return err | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,15 @@ | ||
| package util | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "io" | ||
| "path/filepath" | ||
| "strings" | ||
|
|
||
| "github.com/spf13/cobra" | ||
|
|
||
| "k8s.io/kubernetes/pkg/api/meta" | ||
| kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util" | ||
| ) | ||
|
|
||
|
|
@@ -31,3 +33,30 @@ func GetDisplayFilename(filename string) string { | |
|
|
||
| return filename | ||
| } | ||
|
|
||
| // ResolveResource returns the resource type and name of the resourceString. | ||
| // If the resource string has no specified type, defaultResource will be returned. | ||
| func ResolveResource(defaultResource, resourceString string, mapper meta.RESTMapper) (string, string, error) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. need lots of tests around this. Off the top of my head:
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
| if mapper == nil { | ||
| return "", "", errors.New("mapper cannot be nil") | ||
| } | ||
|
|
||
| var name string | ||
| parts := strings.Split(resourceString, "/") | ||
| switch len(parts) { | ||
| case 1: | ||
| name = parts[0] | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't the default kind be applied for cases where you only have a name?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What you are asking about is |
||
| case 2: | ||
| _, kind, err := mapper.VersionAndKindForResource(parts[0]) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this validates the resource is known, and handles expanding short forms to long forms, which is good |
||
| if err != nil { | ||
| return "", "", err | ||
| } | ||
| name = parts[1] | ||
| resource, _ := meta.KindToResource(kind, false) | ||
| return resource, name, nil | ||
| default: | ||
| return "", "", fmt.Errorf("invalid resource format: %s", resourceString) | ||
| } | ||
|
|
||
| return defaultResource, name, nil | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
are we discarding args if they give us more than 1? should we error on that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We already do; see
origin/pkg/cmd/cli/cmd/deploy.go
Lines 121 to 123 in c4c0150
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh, ha... that's 18 whole lines away... obviously I can't see that far :)