Skip to content
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

refactor build in kubectl factory #55092

Merged
merged 1 commit into from
Nov 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 13 additions & 21 deletions pkg/kubectl/cmd/annotate.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,31 +189,23 @@ func (o AnnotateOptions) RunAnnotate(f cmdutil.Factory, cmd *cobra.Command) erro
changeCause := f.Command(cmd, false)

includeUninitialized := cmdutil.ShouldIncludeUninitialized(cmd, false)
b := f.NewBuilder().
ContinueOnError().
NamespaceParam(namespace).DefaultNamespace().
FilenameParam(enforceNamespace, &o.FilenameOptions).
IncludeUninitialized(includeUninitialized).
Flatten()

if !o.local {
// call this method here, as it requires an api call
// and will cause the command to fail when there is
// no connection to a server
mapper, typer, err := f.UnstructuredObject()
if err != nil {
return err
}

b = b.LabelSelectorParam(o.selector).
Unstructured(f.UnstructuredClientForMapping, mapper, typer).
var b *resource.Builder
if o.local {
b = f.NewBuilder().
Local(f.ClientForMapping)
} else {
b = f.NewUnstructuredBuilder().
LabelSelectorParam(o.selector).
ResourceTypeOrNameArgs(o.all, o.resources...).
Latest()
} else {
b = b.Local(f.ClientForMapping)
}

r := b.Do()
r := b.ContinueOnError().
NamespaceParam(namespace).DefaultNamespace().
FilenameParam(enforceNamespace, &o.FilenameOptions).
IncludeUninitialized(includeUninitialized).
Flatten().
Do()
if err := r.Err(); err != nil {
return err
}
Expand Down
5 changes: 2 additions & 3 deletions pkg/kubectl/cmd/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ func RunApply(f cmdutil.Factory, cmd *cobra.Command, out, errOut io.Writer, opti
return err
}

mapper, typer, err := f.UnstructuredObject()
mapper, _, err := f.UnstructuredObject()
if err != nil {
return err
}
Expand All @@ -213,8 +213,7 @@ func RunApply(f cmdutil.Factory, cmd *cobra.Command, out, errOut io.Writer, opti
// include the uninitialized objects by default if --prune is true
// unless explicitly set --include-uninitialized=false
includeUninitialized := cmdutil.ShouldIncludeUninitialized(cmd, options.Prune)
r := f.NewBuilder().
Unstructured(f.UnstructuredClientForMapping, mapper, typer).
r := f.NewUnstructuredBuilder().
Schema(schema).
ContinueOnError().
NamespaceParam(cmdNamespace).DefaultNamespace().
Expand Down
10 changes: 2 additions & 8 deletions pkg/kubectl/cmd/apply_set_last_applied.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,19 +122,13 @@ func (o *SetLastAppliedOptions) Complete(f cmdutil.Factory, cmd *cobra.Command)
}

func (o *SetLastAppliedOptions) Validate(f cmdutil.Factory, cmd *cobra.Command) error {
mapper, typer, err := f.UnstructuredObject()
if err != nil {
return err
}

r := f.NewBuilder().
Unstructured(f.UnstructuredClientForMapping, mapper, typer).
r := f.NewUnstructuredBuilder().
NamespaceParam(o.Namespace).DefaultNamespace().
FilenameParam(o.EnforceNamespace, &o.FilenameOptions).
Latest().
Flatten().
Do()
err = r.Err()
err := r.Err()
if err != nil {
return err
}
Expand Down
8 changes: 1 addition & 7 deletions pkg/kubectl/cmd/apply_view_last_applied.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,7 @@ func (o *ViewLastAppliedOptions) Complete(f cmdutil.Factory, args []string) erro
return err
}

mapper, typer, err := f.UnstructuredObject()
if err != nil {
return err
}

r := f.NewBuilder().
Unstructured(f.UnstructuredClientForMapping, mapper, typer).
r := f.NewUnstructuredBuilder().
NamespaceParam(cmdNamespace).DefaultNamespace().
FilenameParam(enforceNamespace, &o.FilenameOptions).
ResourceTypeOrNameArgs(enforceNamespace, args...).
Expand Down
6 changes: 3 additions & 3 deletions pkg/kubectl/cmd/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,14 +129,14 @@ func (o *ConvertOptions) Complete(f cmdutil.Factory, out io.Writer, cmd *cobra.C

// build the builder
o.builder = f.NewBuilder()
if !o.local {
if o.local {
o.builder = o.builder.Local(f.ClientForMapping)
} else {
schema, err := f.Validator(cmdutil.GetFlagBool(cmd, "validate"))
if err != nil {
return err
}
o.builder = o.builder.Schema(schema)
} else {
o.builder = o.builder.Local(f.ClientForMapping)
}

cmdNamespace, _, err := f.DefaultNamespace()
Expand Down
5 changes: 2 additions & 3 deletions pkg/kubectl/cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,13 +183,12 @@ func RunCreate(f cmdutil.Factory, cmd *cobra.Command, out, errOut io.Writer, opt
return err
}

mapper, typer, err := f.UnstructuredObject()
mapper, _, err := f.UnstructuredObject()
if err != nil {
return err
}

r := f.NewBuilder().
Unstructured(f.UnstructuredClientForMapping, mapper, typer).
r := f.NewUnstructuredBuilder().
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We removed this in a previous PR because a builder needs to be aware of both typed and unstructured content. I'm going to revert this particular change in my PR because we want to move to a model where a single builder can deal with all types.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

90d76ad was where Juan backed this out the first time because we don't want two separate builders, we want one builder that can deal with versioned, internal, and unstructured types.

See 11913e6 in #55660 for a simpler reduction.

Schema(schema).
ContinueOnError().
NamespaceParam(cmdNamespace).DefaultNamespace().
Expand Down
5 changes: 2 additions & 3 deletions pkg/kubectl/cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,15 +170,14 @@ func (o *DeleteOptions) Complete(f cmdutil.Factory, out, errOut io.Writer, args
}

// Set up client based support.
mapper, typer, err := f.UnstructuredObject()
mapper, _, err := f.UnstructuredObject()
if err != nil {
return err
}

o.Mapper = mapper
includeUninitialized := cmdutil.ShouldIncludeUninitialized(cmd, false)
r := f.NewBuilder().
Unstructured(f.UnstructuredClientForMapping, mapper, typer).
r := f.NewUnstructuredBuilder().
ContinueOnError().
NamespaceParam(cmdNamespace).DefaultNamespace().
FilenameParam(enforceNamespace, &o.FilenameOptions).
Expand Down
8 changes: 1 addition & 7 deletions pkg/kubectl/cmd/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,16 +116,10 @@ func RunDescribe(f cmdutil.Factory, out, cmdErr io.Writer, cmd *cobra.Command, a
return cmdutil.UsageErrorf(cmd, "Required resource not specified.")
}

mapper, typer, err := f.UnstructuredObject()
if err != nil {
return err
}

// include the uninitialized objects by default
// unless user explicitly set --include-uninitialized=false
includeUninitialized := cmdutil.ShouldIncludeUninitialized(cmd, true)
r := f.NewBuilder().
Unstructured(f.UnstructuredClientForMapping, mapper, typer).
r := f.NewUnstructuredBuilder().
ContinueOnError().
NamespaceParam(cmdNamespace).DefaultNamespace().AllNamespaces(allNamespaces).
FilenameParam(enforceNamespace, options).
Expand Down
8 changes: 1 addition & 7 deletions pkg/kubectl/cmd/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,18 +408,12 @@ func RunDiff(f cmdutil.Factory, diff *DiffProgram, options *DiffOptions, from, t

printer := Printer{}

mapper, typer, err := f.UnstructuredObject()
if err != nil {
return err
}

cmdNamespace, enforceNamespace, err := f.DefaultNamespace()
if err != nil {
return err
}

r := f.NewBuilder().
Unstructured(f.UnstructuredClientForMapping, mapper, typer).
r := f.NewUnstructuredBuilder().
NamespaceParam(cmdNamespace).DefaultNamespace().
FilenameParam(enforceNamespace, &options.FilenameOptions).
Flatten().
Expand Down
35 changes: 14 additions & 21 deletions pkg/kubectl/cmd/label.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,32 +190,25 @@ func (o *LabelOptions) RunLabel(f cmdutil.Factory, cmd *cobra.Command) error {
changeCause := f.Command(cmd, false)

includeUninitialized := cmdutil.ShouldIncludeUninitialized(cmd, false)
b := f.NewBuilder().
ContinueOnError().
NamespaceParam(cmdNamespace).DefaultNamespace().
FilenameParam(enforceNamespace, &o.FilenameOptions).
IncludeUninitialized(includeUninitialized).
Flatten()

if !o.local {
// call this method here, as it requires an api call
// and will cause the command to fail when there is
// no connection to a server
mapper, typer, err := f.UnstructuredObject()
if err != nil {
return err
}

b = b.LabelSelectorParam(o.selector).
Unstructured(f.UnstructuredClientForMapping, mapper, typer).
var b *resource.Builder
if o.local {
b = f.NewBuilder().
Local(f.ClientForMapping)
} else {
b = f.NewUnstructuredBuilder().
LabelSelectorParam(o.selector).
ResourceTypeOrNameArgs(o.all, o.resources...).
Latest()
} else {
b = b.Local(f.ClientForMapping)
}

one := false
r := b.Do().IntoSingleItemImplied(&one)
r := b.ContinueOnError().
NamespaceParam(cmdNamespace).DefaultNamespace().
FilenameParam(enforceNamespace, &o.FilenameOptions).
IncludeUninitialized(includeUninitialized).
Flatten().
Do().
IntoSingleItemImplied(&one)
if err := r.Err(); err != nil {
return err
}
Expand Down
8 changes: 1 addition & 7 deletions pkg/kubectl/cmd/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,7 @@ func RunPatch(f cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []strin
return fmt.Errorf("unable to parse %q: %v", patch, err)
}

mapper, typer, err := f.UnstructuredObject()
if err != nil {
return err
}

r := f.NewBuilder().
Unstructured(f.UnstructuredClientForMapping, mapper, typer).
r := f.NewUnstructuredBuilder().
ContinueOnError().
NamespaceParam(cmdNamespace).DefaultNamespace().
FilenameParam(enforceNamespace, &options.FilenameOptions).
Expand Down
8 changes: 3 additions & 5 deletions pkg/kubectl/cmd/replace.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,12 @@ func RunReplace(f cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []str
return fmt.Errorf("--timeout must have --force specified")
}

mapper, typer, err := f.UnstructuredObject()
mapper, _, err := f.UnstructuredObject()
if err != nil {
return err
}

r := f.NewBuilder().
Unstructured(f.UnstructuredClientForMapping, mapper, typer).
r := f.NewUnstructuredBuilder().
Schema(schema).
ContinueOnError().
NamespaceParam(cmdNamespace).DefaultNamespace().
Expand Down Expand Up @@ -249,8 +248,7 @@ func forceReplace(f cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []s
return err
}

r = f.NewBuilder().
Unstructured(f.UnstructuredClientForMapping, mapper, typer).
r = f.NewUnstructuredBuilder().
Schema(schema).
ContinueOnError().
NamespaceParam(cmdNamespace).DefaultNamespace().
Expand Down
18 changes: 3 additions & 15 deletions pkg/kubectl/cmd/resource/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,13 +226,7 @@ func (options *GetOptions) Run(f cmdutil.Factory, cmd *cobra.Command, args []str
return options.watch(f, cmd, args)
}

mapper, typer, err := f.UnstructuredObject()
if err != nil {
return err
}

r := f.NewBuilder().
Unstructured(f.UnstructuredClientForMapping, mapper, typer).
r := f.NewUnstructuredBuilder().
NamespaceParam(options.Namespace).DefaultNamespace().AllNamespaces(options.AllNamespaces).
FilenameParam(options.ExplicitNamespace, &options.FilenameOptions).
LabelSelectorParam(options.LabelSelector).
Expand Down Expand Up @@ -439,18 +433,12 @@ func (options *GetOptions) raw(f cmdutil.Factory) error {
// watch starts a client-side watch of one or more resources.
// TODO: remove the need for arguments here.
func (options *GetOptions) watch(f cmdutil.Factory, cmd *cobra.Command, args []string) error {
mapper, typer, err := f.UnstructuredObject()
if err != nil {
return err
}

// TODO: this could be better factored
// include uninitialized objects when watching on a single object
// unless explicitly set --include-uninitialized=false
includeUninitialized := cmdutil.ShouldIncludeUninitialized(cmd, len(args) == 2)

r := f.NewBuilder().
Unstructured(f.UnstructuredClientForMapping, mapper, typer).
r := f.NewUnstructuredBuilder().
NamespaceParam(options.Namespace).DefaultNamespace().AllNamespaces(options.AllNamespaces).
FilenameParam(options.ExplicitNamespace, &options.FilenameOptions).
LabelSelectorParam(options.LabelSelector).
Expand All @@ -462,7 +450,7 @@ func (options *GetOptions) watch(f cmdutil.Factory, cmd *cobra.Command, args []s
SingleResourceType().
Latest().
Do()
err = r.Err()
err := r.Err()
if err != nil {
return err
}
Expand Down
12 changes: 6 additions & 6 deletions pkg/kubectl/cmd/set/set_env.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,13 +238,13 @@ func (o *EnvOptions) RunEnv(f cmdutil.Factory) error {
FilenameParam(enforceNamespace, &o.FilenameOptions).
Flatten()

if !o.Local {
if o.Local {
b = b.Local(f.ClientForMapping)
} else {
b = b.
LabelSelectorParam(o.Selector).
ResourceTypeOrNameArgs(o.All, o.From).
Latest()
} else {
b = b.Local(f.ClientForMapping)
}

infos, err := b.Do().Infos()
Expand Down Expand Up @@ -302,13 +302,13 @@ func (o *EnvOptions) RunEnv(f cmdutil.Factory) error {
FilenameParam(enforceNamespace, &o.FilenameOptions).
Flatten()

if !o.Local {
if o.Local {
b = b.Local(f.ClientForMapping)
} else {
b = b.
LabelSelectorParam(o.Selector).
ResourceTypeOrNameArgs(o.All, o.Resources...).
Latest()
} else {
b = b.Local(f.ClientForMapping)
}

o.Infos, err = b.Do().Infos()
Expand Down
12 changes: 6 additions & 6 deletions pkg/kubectl/cmd/set/set_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,7 @@ func (o *ImageOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []st
IncludeUninitialized(includeUninitialized).
Flatten()

if !o.Local {
builder = builder.
LabelSelectorParam(o.Selector).
ResourceTypeOrNameArgs(o.All, o.Resources...).
Latest()
} else {
if o.Local {
// if a --local flag was provided, and a resource was specified in the form
// <resource>/<name>, fail immediately as --local cannot query the api server
// for the specified resource.
Expand All @@ -161,6 +156,11 @@ func (o *ImageOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []st
}

builder = builder.Local(f.ClientForMapping)
} else {
builder = builder.
LabelSelectorParam(o.Selector).
ResourceTypeOrNameArgs(o.All, o.Resources...).
Latest()
}

o.Infos, err = builder.Do().Infos()
Expand Down