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 <mkhan@redhat.com>
This commit is contained in:
Monis Khan 2017-11-05 13:31:37 -05:00
parent d07bc1485c
commit 136cb482aa
No known key found for this signature in database
GPG Key ID: 52C90ADA01B269B8

View File

@ -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
}