Skip to content

Commit

Permalink
Merge pull request #8280 from liggitt/resolve-group-resources
Browse files Browse the repository at this point in the history
Merged by openshift-bot
  • Loading branch information
OpenShift Bot committed Mar 31, 2016
2 parents 799d2a4 + 49ad7cc commit 7af1063
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 81 deletions.
6 changes: 3 additions & 3 deletions pkg/cmd/admin/policy/reconcile_clusterrolebindings.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,12 @@ func (o *ReconcileClusterRoleBindingsOptions) Complete(cmd *cobra.Command, f *cl

mapper, _ := f.Object()
for _, resourceString := range args {
resource, name, err := ocmdutil.ResolveResource("clusterroles", resourceString, mapper)
resource, name, err := ocmdutil.ResolveResource(authorizationapi.Resource("clusterroles"), resourceString, mapper)
if err != nil {
return err
}
if resource != "clusterroles" {
return fmt.Errorf("%s is not a valid resource type for this command", resource)
if resource != authorizationapi.Resource("clusterroles") {
return fmt.Errorf("%v is not a valid resource type for this command", resource)
}
if len(name) == 0 {
return fmt.Errorf("%s did not contain a name", resourceString)
Expand Down
6 changes: 3 additions & 3 deletions pkg/cmd/admin/policy/reconcile_clusterroles.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,12 @@ func (o *ReconcileClusterRolesOptions) Complete(cmd *cobra.Command, f *clientcmd

mapper, _ := f.Object()
for _, resourceString := range args {
resource, name, err := osutil.ResolveResource("clusterroles", resourceString, mapper)
resource, name, err := osutil.ResolveResource(authorizationapi.Resource("clusterroles"), resourceString, mapper)
if err != nil {
return err
}
if resource != "clusterroles" {
return fmt.Errorf("%s is not a valid resource type for this command", resource)
if resource != authorizationapi.Resource("clusterroles") {
return fmt.Errorf("%v is not a valid resource type for this command", resource)
}
if len(name) == 0 {
return fmt.Errorf("%s did not contain a name", resourceString)
Expand Down
6 changes: 3 additions & 3 deletions pkg/cmd/cli/cmd/create_route.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,12 +416,12 @@ func resolveServiceName(f *clientcmd.Factory, resource string) (string, error) {
return "", fmt.Errorf("you need to provide a service name via --service")
}
mapper, _ := f.Object()
rType, name, err := cmdutil.ResolveResource("services", resource, mapper)
rType, name, err := cmdutil.ResolveResource(kapi.Resource("services"), resource, mapper)
if err != nil {
return "", err
}
if rType != "services" {
return "", fmt.Errorf("cannot expose %s as routes", rType)
if rType != kapi.Resource("services") {
return "", fmt.Errorf("cannot expose %v as routes", rType)
}
return name, nil
}
Expand Down
24 changes: 12 additions & 12 deletions pkg/cmd/cli/cmd/startbuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,22 +142,22 @@ func RunStartBuild(f *clientcmd.Factory, in io.Reader, out io.Writer, cmd *cobra

var (
name = buildName
resource = "builds"
resource = buildapi.Resource("builds")
)

if len(name) == 0 && len(args) > 0 && len(args[0]) > 0 {
mapper, _ := f.Object()
resource, name, err = cmdutil.ResolveResource("buildconfigs", args[0], mapper)
resource, name, err = cmdutil.ResolveResource(buildapi.Resource("buildconfigs"), args[0], mapper)
if err != nil {
return err
}
switch resource {
case "buildconfigs":
case buildapi.Resource("buildconfigs"):
// no special handling required
case "builds":
case buildapi.Resource("builds"):
return fmt.Errorf("use --from-build to rerun your builds")
default:
return fmt.Errorf("invalid resource provided: %s", resource)
return fmt.Errorf("invalid resource provided: %v", resource)
}
}
if len(name) == 0 {
Expand Down Expand Up @@ -214,16 +214,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 resource == "builds":
case resource == buildapi.Resource("builds"):
if newBuild, err = client.Builds(namespace).Clone(request); err != nil {
return err
}
case resource == "buildconfigs":
case resource == buildapi.Resource("buildconfigs"):
if newBuild, err = client.BuildConfigs(namespace).Instantiate(request); err != nil {
return err
}
default:
return fmt.Errorf("invalid resource provided: %s", resource)
return fmt.Errorf("invalid resource provided: %v", resource)
}

fmt.Fprintln(out, newBuild.Name)
Expand Down Expand Up @@ -291,7 +291,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, resource, webhookFilter string) error {
func RunListBuildWebHooks(f *clientcmd.Factory, out, errOut io.Writer, name string, resource unversioned.GroupResource, webhookFilter string) error {
generic, github := false, false
prefix := false
switch webhookFilter {
Expand All @@ -315,9 +315,9 @@ func RunListBuildWebHooks(f *clientcmd.Factory, out, errOut io.Writer, name, res
}

switch resource {
case "buildconfigs":
case buildapi.Resource("buildconfigs"):
// no special handling required
case "builds":
case buildapi.Resource("builds"):
build, err := client.Builds(namespace).Get(name)
if err != nil {
return err
Expand All @@ -331,7 +331,7 @@ func RunListBuildWebHooks(f *clientcmd.Factory, out, errOut io.Writer, name, res
}
name = ref.Name
default:
return fmt.Errorf("invalid resource provided: %s", resource)
return fmt.Errorf("invalid resource provided: %v", resource)
}

config, err := client.BuildConfigs(namespace).Get(name)
Expand Down
9 changes: 5 additions & 4 deletions pkg/cmd/experimental/buildchain/buildchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/golang/glog"
"github.com/spf13/cobra"
kapi "k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/unversioned"
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
"k8s.io/kubernetes/pkg/util/sets"

Expand Down Expand Up @@ -93,19 +94,19 @@ func (o *BuildChainOptions) Complete(f *clientcmd.Factory, cmd *cobra.Command, a
}
o.c, o.t = oc, oc

resource := ""
resource := unversioned.GroupResource{}
mapper, _ := f.Object()
resource, o.name, err = osutil.ResolveResource("imagestreamtags", args[0], mapper)
resource, o.name, err = osutil.ResolveResource(imageapi.Resource("imagestreamtags"), args[0], mapper)
if err != nil {
return err
}

switch resource {
case "imagestreamtags":
case imageapi.Resource("imagestreamtags"):
o.name = imageapi.NormalizeImageStreamTag(o.name)
glog.V(4).Infof("Using %q as the image stream tag to look dependencies for", o.name)
default:
return fmt.Errorf("invalid resource provided: %s", resource)
return fmt.Errorf("invalid resource provided: %v", resource)
}

// Setup namespace
Expand Down
31 changes: 13 additions & 18 deletions pkg/cmd/util/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ func GetDisplayFilename(filename string) string {

// 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) {
func ResolveResource(defaultResource unversioned.GroupResource, resourceString string, mapper meta.RESTMapper) (unversioned.GroupResource, string, error) {
if mapper == nil {
return "", "", errors.New("mapper cannot be nil")
return unversioned.GroupResource{}, "", errors.New("mapper cannot be nil")
}

var name string
Expand All @@ -57,25 +57,20 @@ func ResolveResource(defaultResource, resourceString string, mapper meta.RESTMap
case 1:
name = parts[0]
case 2:
partialResource := unversioned.GroupVersionResource{Resource: strings.ToLower(parts[0])}
gvrs, err := mapper.ResourcesFor(partialResource)
if err != nil {
return "", "", err
}
if len(gvrs) == 0 {
return gvrs[0].Resource, parts[1], nil
}
name = parts[1]

groupResource := gvrs[0].GroupResource()
for _, gvr := range gvrs[1:] {
if groupResource != gvr.GroupResource() {
return "", "", &meta.AmbiguousResourceError{PartialResource: partialResource, MatchingResources: gvrs}
}
}
// Allow specifying the group the same way kubectl does, as "resource.group.name"
groupResource := unversioned.ParseGroupResource(parts[0])
// normalize resource case
groupResource.Resource = strings.ToLower(groupResource.Resource)

return gvrs[0].Resource, parts[1], nil
gvr, err := mapper.ResourceFor(groupResource.WithVersion(""))
if err != nil {
return unversioned.GroupResource{}, "", err
}
return gvr.GroupResource(), name, nil
default:
return "", "", fmt.Errorf("invalid resource format: %s", resourceString)
return unversioned.GroupResource{}, "", fmt.Errorf("invalid resource format: %s", resourceString)
}

return defaultResource, name, nil
Expand Down
Loading

0 comments on commit 7af1063

Please sign in to comment.