Skip to content

Commit

Permalink
replace use of resource#NewBuilder with factory#NewBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
juanvallejo committed Aug 2, 2017
1 parent f568a4e commit 717e308
Show file tree
Hide file tree
Showing 29 changed files with 68 additions and 112 deletions.
6 changes: 3 additions & 3 deletions pkg/cmd/util/clientcmd/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,16 @@ func NewFactory(optionalClientConfig kclientcmd.ClientConfig) *Factory {

// PrintResourceInfos receives a list of resource infos and prints versioned objects if a generic output format was specified
// otherwise, it iterates through info objects, printing each resource with a unique printer for its mapping
func (f *Factory) PrintResourceInfos(cmd *cobra.Command, infos []*resource.Info, out io.Writer) error {
func (f *Factory) PrintResourceInfos(cmd *cobra.Command, isLocal bool, infos []*resource.Info, out io.Writer) error {
// mirrors PrintResourceInfoForCommand upstream
printer, err := f.PrinterForCommand(cmd, false, nil, printers.PrintOptions{})
printer, err := f.PrinterForCommand(cmd, isLocal, nil, printers.PrintOptions{})
if err != nil {
return nil
}
if !printer.IsGeneric() {
for _, info := range infos {
mapping := info.ResourceMapping()
printer, err := f.PrinterForMapping(cmd, false, nil, mapping, false)
printer, err := f.PrinterForMapping(cmd, isLocal, nil, mapping, false)
if err != nil {
return err
}
Expand Down
14 changes: 4 additions & 10 deletions pkg/oc/admin/network/project_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@ import (
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
kerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/util/wait"
kapi "k8s.io/kubernetes/pkg/api"
kclientset "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
"k8s.io/kubernetes/pkg/kubectl/resource"

Expand All @@ -34,11 +32,10 @@ type ProjectOptions struct {
Kclient kclientset.Interface
Out io.Writer

Mapper meta.RESTMapper
Typer runtime.ObjectTyper
CategoryExpander resource.CategoryExpander
RESTClientFactory func(mapping *meta.RESTMapping) (resource.RESTClient, error)

Builder *resource.Builder

ProjectNames []string

// Common optional params
Expand All @@ -55,15 +52,12 @@ func (p *ProjectOptions) Complete(f *clientcmd.Factory, c *cobra.Command, args [
if err != nil {
return err
}
mapper, typer := f.Object()

p.Builder = f.NewBuilder(true)
p.DefaultNamespace = defaultNamespace
p.Oclient = oc
p.Kclient = kc
p.Out = out
p.Mapper = mapper
p.Typer = typer
p.CategoryExpander = f.CategoryExpander()
p.RESTClientFactory = f.ClientForMapping
p.ProjectNames = []string{}
if len(args) != 0 {
Expand Down Expand Up @@ -108,7 +102,7 @@ func (p *ProjectOptions) GetProjects() ([]*projectapi.Project, error) {
nameArgs = append(nameArgs, p.ProjectNames...)
}

r := resource.NewBuilder(p.Mapper, p.CategoryExpander, p.Typer, resource.ClientMapperFunc(p.RESTClientFactory), kapi.Codecs.UniversalDecoder()).
r := p.Builder.
ContinueOnError().
NamespaceParam(p.DefaultNamespace).
SelectorParam(p.Selector).
Expand Down
5 changes: 4 additions & 1 deletion pkg/oc/admin/node/node_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ type NodeOptions struct {
CmdPrinter kprinters.ResourcePrinter
CmdPrinterOutput bool

Builder *resource.Builder

NodeNames []string

// Common optional params
Expand Down Expand Up @@ -73,6 +75,7 @@ func (n *NodeOptions) Complete(f *clientcmd.Factory, c *cobra.Command, args []st
}
mapper, typer := f.Object()

n.Builder = f.NewBuilder(true)
n.DefaultNamespace = defaultNamespace
n.ExternalKubeClient = externalkc
n.KubeClient = kc
Expand Down Expand Up @@ -128,7 +131,7 @@ func (n *NodeOptions) GetNodes() ([]*kapi.Node, error) {
nameArgs = append(nameArgs, n.NodeNames...)
}

r := resource.NewBuilder(n.Mapper, n.CategoryExpander, n.Typer, resource.ClientMapperFunc(n.RESTClientFactory), kapi.Codecs.UniversalDecoder()).
r := n.Builder.
ContinueOnError().
NamespaceParam(n.DefaultNamespace).
SelectorParam(n.Selector).
Expand Down
9 changes: 3 additions & 6 deletions pkg/oc/admin/policy/review.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,7 @@ type sccReviewOptions struct {
namespace string
enforceNamespace bool
out io.Writer
mapper meta.RESTMapper
typer runtime.ObjectTyper
categoryExpander resource.CategoryExpander
builder *resource.Builder
RESTClientFactory func(mapping *meta.RESTMapping) (resource.RESTClient, error)
printer sccReviewPrinter
FilenameOptions resource.FilenameOptions
Expand Down Expand Up @@ -108,8 +106,7 @@ func (o *sccReviewOptions) Complete(f *clientcmd.Factory, args []string, cmd *co
if err != nil {
return fmt.Errorf("unable to obtain client: %v", err)
}
o.mapper, o.typer = f.Object()
o.categoryExpander = f.CategoryExpander()
o.builder = f.NewBuilder(true)
o.RESTClientFactory = f.ClientForMapping

if len(kcmdutil.GetFlagString(cmd, "output")) != 0 {
Expand All @@ -126,7 +123,7 @@ func (o *sccReviewOptions) Complete(f *clientcmd.Factory, args []string, cmd *co
}

func (o *sccReviewOptions) Run(args []string) error {
r := resource.NewBuilder(o.mapper, o.categoryExpander, o.typer, resource.ClientMapperFunc(o.RESTClientFactory), kapi.Codecs.UniversalDecoder()).
r := o.builder.
NamespaceParam(o.namespace).
FilenameParam(o.enforceNamespace, &o.FilenameOptions).
ResourceTypeOrNameArgs(true, args...).
Expand Down
9 changes: 3 additions & 6 deletions pkg/oc/admin/policy/subject_review.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ type sccSubjectReviewOptions struct {
namespace string
enforceNamespace bool
out io.Writer
mapper meta.RESTMapper
typer runtime.ObjectTyper
categoryExpander resource.CategoryExpander
builder *resource.Builder
RESTClientFactory func(mapping *meta.RESTMapping) (resource.RESTClient, error)
printer sccSubjectReviewPrinter
FilenameOptions resource.FilenameOptions
Expand Down Expand Up @@ -110,8 +108,7 @@ func (o *sccSubjectReviewOptions) Complete(f *clientcmd.Factory, args []string,
}
o.sccSubjectReviewClient = oclient
o.sccSelfSubjectReviewClient = oclient
o.mapper, o.typer = f.Object()
o.categoryExpander = f.CategoryExpander()
o.builder = f.NewBuilder(true)
o.RESTClientFactory = f.ClientForMapping

if len(kcmdutil.GetFlagString(cmd, "output")) != 0 {
Expand All @@ -132,7 +129,7 @@ func (o *sccSubjectReviewOptions) Run(args []string) error {
if len(o.serviceAccount) > 0 {
userOrSA = o.serviceAccount
}
r := resource.NewBuilder(o.mapper, o.categoryExpander, o.typer, resource.ClientMapperFunc(o.RESTClientFactory), kapi.Codecs.UniversalDecoder()).
r := o.builder.
NamespaceParam(o.namespace).
FilenameParam(o.enforceNamespace, &o.FilenameOptions).
ResourceTypeOrNameArgs(true, args...).
Expand Down
3 changes: 1 addition & 2 deletions pkg/oc/bootstrap/docker/openshift/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
// ImportObjects imports objects into OpenShift from a particular location
// into a given namespace
func ImportObjects(f *clientcmd.Factory, ns, location string) error {
mapper, typer := f.Object()
schema, err := f.Validator(false, "")
if err != nil {
return err
Expand All @@ -25,7 +24,7 @@ func ImportObjects(f *clientcmd.Factory, ns, location string) error {
return err
}
glog.V(8).Infof("Importing data:\n%s\n", string(data))
r := resource.NewBuilder(mapper, f.CategoryExpander(), typer, resource.ClientMapperFunc(f.ClientForMapping), f.Decoder(true)).
r := f.NewBuilder(true).
Schema(schema).
ContinueOnError().
NamespaceParam(ns).
Expand Down
4 changes: 2 additions & 2 deletions pkg/oc/cli/cmd/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,8 @@ func (o *DebugOptions) Complete(cmd *cobra.Command, f *clientcmd.Factory, args [
return err
}

mapper, typer := f.Object()
b := resource.NewBuilder(mapper, f.CategoryExpander(), typer, resource.ClientMapperFunc(f.ClientForMapping), kapi.Codecs.UniversalDecoder()).
mapper, _ := f.Object()
b := f.NewBuilder(true).
NamespaceParam(cmdNamespace).DefaultNamespace().
SingleResourceType().
ResourceNames("pods", resources...).
Expand Down
4 changes: 1 addition & 3 deletions pkg/oc/cli/cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,7 @@ func (o *DeployOptions) Complete(f *clientcmd.Factory, args []string, out io.Wri
return err
}

mapper, typer := f.Object()
o.builder = resource.NewBuilder(mapper, f.CategoryExpander(), typer, resource.ClientMapperFunc(f.ClientForMapping), kapi.Codecs.UniversalDecoder())

o.builder = f.NewBuilder(true)
o.out = out

if len(args) > 0 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/oc/cli/cmd/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func RunExport(f *clientcmd.Factory, exporter Exporter, in io.Reader, out io.Wri
}

mapper, typer := f.Object()
b := resource.NewBuilder(mapper, f.CategoryExpander(), typer, resource.ClientMapperFunc(f.ClientForMapping), kapi.Codecs.UniversalDecoder()).
b := f.NewBuilder(true).
NamespaceParam(cmdNamespace).DefaultNamespace().AllNamespaces(allNamespaces).
FilenameParam(explicit, &resource.FilenameOptions{Recursive: false, Filenames: filenames}).
SelectorParam(selector).
Expand Down
3 changes: 1 addition & 2 deletions pkg/oc/cli/cmd/expose.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,7 @@ func validate(cmd *cobra.Command, f *clientcmd.Factory, args []string) error {
return err
}

mapper, typer := f.Object()
r := resource.NewBuilder(mapper, f.CategoryExpander(), typer, resource.ClientMapperFunc(f.ClientForMapping), kapi.Codecs.UniversalDecoder()).
r := f.NewBuilder(true).
ContinueOnError().
NamespaceParam(namespace).DefaultNamespace().
FilenameParam(enforceNamespace, &resource.FilenameOptions{Recursive: false, Filenames: kcmdutil.GetFlagStringSlice(cmd, "filename")}).
Expand Down
4 changes: 1 addition & 3 deletions pkg/oc/cli/cmd/extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (

"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/sets"
kapi "k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
"k8s.io/kubernetes/pkg/kubectl/resource"
Expand Down Expand Up @@ -94,8 +93,7 @@ func (o *ExtractOptions) Complete(f *clientcmd.Factory, in io.Reader, out io.Wri
return err
}

mapper, typer := f.Object()
b := resource.NewBuilder(mapper, f.CategoryExpander(), typer, resource.ClientMapperFunc(f.ClientForMapping), kapi.Codecs.UniversalDecoder()).
b := f.NewBuilder(true).
NamespaceParam(cmdNamespace).DefaultNamespace().
FilenameParam(explicit, &resource.FilenameOptions{Recursive: false, Filenames: o.Filenames}).
ResourceNames("", args...).
Expand Down
3 changes: 1 addition & 2 deletions pkg/oc/cli/cmd/idle.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,7 @@ func (o *IdleOptions) Complete(f *clientcmd.Factory, cmd *cobra.Command, args []
return fmt.Errorf("resource names, selectors, and the all flag may not be be specified if a filename is specified")
}

mapper, typer := f.Object()
o.svcBuilder = resource.NewBuilder(mapper, f.CategoryExpander(), typer, resource.ClientMapperFunc(f.ClientForMapping), kapi.Codecs.UniversalDecoder()).
o.svcBuilder = f.NewBuilder(true).
ContinueOnError().
NamespaceParam(namespace).DefaultNamespace().AllNamespaces(o.allNamespaces).
Flatten().
Expand Down
4 changes: 1 addition & 3 deletions pkg/oc/cli/cmd/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
kcmd "k8s.io/kubernetes/pkg/kubectl/cmd"
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
"k8s.io/kubernetes/pkg/kubectl/resource"

buildapi "github.com/openshift/origin/pkg/build/apis/build"
buildutil "github.com/openshift/origin/pkg/build/util"
Expand Down Expand Up @@ -127,8 +126,7 @@ func (o *OpenShiftLogsOptions) Complete(f *clientcmd.Factory, cmd *cobra.Command

podLogOptions := o.KubeLogOptions.Options.(*kapi.PodLogOptions)

mapper, typer := f.Object()
infos, err := resource.NewBuilder(mapper, f.CategoryExpander(), typer, resource.ClientMapperFunc(f.ClientForMapping), kapi.Codecs.UniversalDecoder()).
infos, err := f.NewBuilder(true).
NamespaceParam(o.Namespace).DefaultNamespace().
ResourceNames("pods", args...).
SingleResourceType().RequireObject(false).
Expand Down
12 changes: 3 additions & 9 deletions pkg/oc/cli/cmd/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,26 +171,20 @@ func RunProcess(f *clientcmd.Factory, in io.Reader, out, errout io.Writer, cmd *
infos []*resource.Info

mapper meta.RESTMapper
typer runtime.ObjectTyper

client *client.Client
clientMappingFn resource.ClientMapperFunc
client *client.Client
)

if local {
// TODO: Change f.Object() so that it can fall back to local RESTMapper safely (currently glog.Fatals)
mapper = kapi.Registry.RESTMapper()
typer = kapi.Scheme
clientMappingFn = func(*meta.RESTMapping) (resource.RESTClient, error) { return nil, nil }
// client is deliberately left nil
} else {
client, _, err = f.Clients()
if err != nil {
return err
}

mapper, typer = f.Object()
clientMappingFn = f.ClientForMapping
mapper, _ = f.Object()
}
mapping, err := mapper.RESTMapping(templateapi.Kind("Template"))
if err != nil {
Expand Down Expand Up @@ -226,7 +220,7 @@ func RunProcess(f *clientcmd.Factory, in io.Reader, out, errout io.Writer, cmd *
templateObj.CreationTimestamp = metav1.Now()
infos = append(infos, &resource.Info{Object: templateObj})
} else {
infos, err = resource.NewBuilder(mapper, f.CategoryExpander(), typer, clientMappingFn, kapi.Codecs.UniversalDecoder()).
infos, err = f.NewBuilder(!local).
FilenameParam(explicit, &resource.FilenameOptions{Recursive: false, Filenames: []string{filename}}).
Do().
Infos()
Expand Down
3 changes: 1 addition & 2 deletions pkg/oc/cli/cmd/rollback.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,8 @@ func (o *RollbackOptions) Complete(f *clientcmd.Factory, cmd *cobra.Command, arg
o.Namespace = namespace

// Set up client based support.
mapper, typer := f.Object()
o.getBuilder = func() *resource.Builder {
return resource.NewBuilder(mapper, f.CategoryExpander(), typer, resource.ClientMapperFunc(f.ClientForMapping), kapi.Codecs.UniversalDecoder())
return f.NewBuilder(true)
}

oClient, kClient, err := f.Clients()
Expand Down
2 changes: 1 addition & 1 deletion pkg/oc/cli/cmd/rollout/cancel.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (o *CancelOptions) Complete(f *clientcmd.Factory, cmd *cobra.Command, out i
return err
}

r := resource.NewBuilder(o.Mapper, f.CategoryExpander(), o.Typer, resource.ClientMapperFunc(f.ClientForMapping), f.Decoder(true)).
r := f.NewBuilder(true).
NamespaceParam(cmdNamespace).DefaultNamespace().
FilenameParam(enforceNamespace, &o.FilenameOptions).
ResourceTypeOrNameArgs(true, args...).
Expand Down
2 changes: 1 addition & 1 deletion pkg/oc/cli/cmd/rollout/latest.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func (o *RolloutLatestOptions) Complete(f *clientcmd.Factory, cmd *cobra.Command
}

o.mapper, o.typer = f.Object()
o.infos, err = resource.NewBuilder(o.mapper, f.CategoryExpander(), o.typer, resource.ClientMapperFunc(f.ClientForMapping), f.Decoder(true)).
o.infos, err = f.NewBuilder(true).
ContinueOnError().
NamespaceParam(namespace).
ResourceNames("deploymentconfigs", args[0]).
Expand Down
2 changes: 1 addition & 1 deletion pkg/oc/cli/cmd/rollout/retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (o *RetryOptions) Complete(f *clientcmd.Factory, cmd *cobra.Command, out io
return err
}

r := resource.NewBuilder(o.Mapper, f.CategoryExpander(), o.Typer, resource.ClientMapperFunc(f.ClientForMapping), f.Decoder(true)).
r := f.NewBuilder(true).
NamespaceParam(cmdNamespace).DefaultNamespace().
FilenameParam(enforceNamespace, &o.FilenameOptions).
ResourceTypeOrNameArgs(true, args...).
Expand Down
7 changes: 3 additions & 4 deletions pkg/oc/cli/cmd/set/buildhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
kapi "k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
"k8s.io/kubernetes/pkg/kubectl/resource"
Expand Down Expand Up @@ -138,8 +137,8 @@ func (o *BuildHookOptions) Complete(f *clientcmd.Factory, cmd *cobra.Command, ar

o.Cmd = cmd

mapper, typer := f.Object()
o.Builder = resource.NewBuilder(mapper, f.CategoryExpander(), typer, resource.ClientMapperFunc(f.ClientForMapping), kapi.Codecs.UniversalDecoder()).
mapper, _ := f.Object()
o.Builder = f.NewBuilder(!o.Local).
ContinueOnError().
NamespaceParam(cmdNamespace).DefaultNamespace().
FilenameParam(explicit, &resource.FilenameOptions{Recursive: false, Filenames: o.Filenames}).
Expand All @@ -158,7 +157,7 @@ func (o *BuildHookOptions) Complete(f *clientcmd.Factory, cmd *cobra.Command, ar

o.Output = kcmdutil.GetFlagString(cmd, "output")
o.PrintObject = func(infos []*resource.Info) error {
return f.PrintResourceInfos(cmd, infos, o.Out)
return f.PrintResourceInfos(cmd, o.Local, infos, o.Out)
}

o.Encoder = f.JSONEncoder()
Expand Down
6 changes: 3 additions & 3 deletions pkg/oc/cli/cmd/set/buildsecret.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func NewCmdBuildSecret(fullName string, f *clientcmd.Factory, out, errOut io.Wri
var supportedBuildTypes = []string{"buildconfigs"}

func (o *BuildSecretOptions) secretFromArg(f *clientcmd.Factory, mapper meta.RESTMapper, typer runtime.ObjectTyper, namespace, arg string) (string, error) {
builder := resource.NewBuilder(mapper, f.CategoryExpander(), typer, resource.ClientMapperFunc(f.ClientForMapping), kapi.Codecs.UniversalDecoder()).
builder := f.NewBuilder(!o.Local).
NamespaceParam(namespace).DefaultNamespace().
RequireObject(false).
ContinueOnError().
Expand Down Expand Up @@ -174,7 +174,7 @@ func (o *BuildSecretOptions) Complete(f *clientcmd.Factory, cmd *cobra.Command,
return err
}
}
o.Builder = resource.NewBuilder(mapper, f.CategoryExpander(), typer, resource.ClientMapperFunc(f.ClientForMapping), kapi.Codecs.UniversalDecoder()).
o.Builder = f.NewBuilder(!o.Local).
ContinueOnError().
NamespaceParam(cmdNamespace).DefaultNamespace().
FilenameParam(explicit, &resource.FilenameOptions{Recursive: false, Filenames: o.Filenames}).
Expand All @@ -193,7 +193,7 @@ func (o *BuildSecretOptions) Complete(f *clientcmd.Factory, cmd *cobra.Command,

o.Output = kcmdutil.GetFlagString(cmd, "output")
o.PrintObject = func(infos []*resource.Info) error {
return f.PrintResourceInfos(cmd, infos, o.Out)
return f.PrintResourceInfos(cmd, o.Local, infos, o.Out)
}

o.Encoder = f.JSONEncoder()
Expand Down
Loading

0 comments on commit 717e308

Please sign in to comment.