From 136cb482aacf8a0b45326e246177445b51707c69 Mon Sep 17 00:00:00 2001 From: Monis Khan Date: Sun, 5 Nov 2017 13:31:37 -0500 Subject: [PATCH] Make ReconcileOptions reusable This change makes ReconcileOptions fully reusable by: 1. Replacing ResourceBuilder with a Visitor that can be generically plugged in. This decouples the use of file input from the options. 2. Replacing core client with namespace client since that smaller interface is what is needed by RunReconcile. 3. All fields are now checked by Validate as a way to document that they should be set before calling RunReconcile. Signed-off-by: Monis Khan --- pkg/kubectl/cmd/auth/reconcile.go | 48 ++++++++++++++++++++----------- 1 file changed, 32 insertions(+), 16 deletions(-) diff --git a/pkg/kubectl/cmd/auth/reconcile.go b/pkg/kubectl/cmd/auth/reconcile.go index 948d8f3fac5..da2b57ebbce 100644 --- a/pkg/kubectl/cmd/auth/reconcile.go +++ b/pkg/kubectl/cmd/auth/reconcile.go @@ -35,9 +35,9 @@ import ( // ReconcileOptions is the start of the data required to perform the operation. As new fields are added, add them here instead of // referencing the cmd.Flags() type ReconcileOptions struct { - ResourceBuilder *resource.Builder + Visitor resource.Visitor RBACClient internalrbacclient.RbacInterface - CoreClient internalcoreclient.CoreInterface + NamespaceClient internalcoreclient.NamespaceInterface Print func(*resource.Info) error @@ -92,18 +92,24 @@ func (o *ReconcileOptions) Complete(cmd *cobra.Command, f cmdutil.Factory, args if err != nil { return err } - o.ResourceBuilder = f.NewBuilder(). + + r := f.NewBuilder(). ContinueOnError(). NamespaceParam(namespace).DefaultNamespace(). FilenameParam(enforceNamespace, options). - Flatten() + Flatten(). + Do() + if err := r.Err(); err != nil { + return err + } + o.Visitor = r client, err := f.ClientSet() if err != nil { return err } o.RBACClient = client.Rbac() - o.CoreClient = client.Core() + o.NamespaceClient = client.Core().Namespaces() mapper, _ := f.Object() dryRun := false @@ -121,17 +127,29 @@ func (o *ReconcileOptions) Complete(cmd *cobra.Command, f cmdutil.Factory, args } func (o *ReconcileOptions) Validate() error { + if o.Visitor == nil { + return errors.New("ReconcileOptions.Visitor must be set") + } + if o.RBACClient == nil { + return errors.New("ReconcileOptions.RBACClient must be set") + } + if o.NamespaceClient == nil { + return errors.New("ReconcileOptions.NamespaceClient must be set") + } + if o.Print == nil { + return errors.New("ReconcileOptions.Print must be set") + } + if o.Out == nil { + return errors.New("ReconcileOptions.Out must be set") + } + if o.Err == nil { + return errors.New("ReconcileOptions.Err must be set") + } return nil } func (o *ReconcileOptions) RunReconcile() error { - r := o.ResourceBuilder.Do() - err := r.Err() - if err != nil { - return err - } - - err = r.Visit(func(info *resource.Info, err error) error { + return o.Visitor.Visit(func(info *resource.Info, err error) error { if err != nil { return err } @@ -147,7 +165,7 @@ func (o *ReconcileOptions) RunReconcile() error { RemoveExtraPermissions: false, Role: reconciliation.RoleRuleOwner{Role: t}, Client: reconciliation.RoleModifier{ - NamespaceClient: o.CoreClient.Namespaces(), + NamespaceClient: o.NamespaceClient, Client: o.RBACClient, }, } @@ -181,7 +199,7 @@ func (o *ReconcileOptions) RunReconcile() error { RoleBinding: reconciliation.RoleBindingAdapter{RoleBinding: t}, Client: reconciliation.RoleBindingClientAdapter{ Client: o.RBACClient, - NamespaceClient: o.CoreClient.Namespaces(), + NamespaceClient: o.NamespaceClient, }, } result, err := reconcileOptions.Run() @@ -214,6 +232,4 @@ func (o *ReconcileOptions) RunReconcile() error { return nil }) - - return err }