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: 's avatarMonis Khan <mkhan@redhat.com>
parent d07bc148
...@@ -35,9 +35,9 @@ import ( ...@@ -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 // 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() // referencing the cmd.Flags()
type ReconcileOptions struct { type ReconcileOptions struct {
ResourceBuilder *resource.Builder Visitor resource.Visitor
RBACClient internalrbacclient.RbacInterface RBACClient internalrbacclient.RbacInterface
CoreClient internalcoreclient.CoreInterface NamespaceClient internalcoreclient.NamespaceInterface
Print func(*resource.Info) error Print func(*resource.Info) error
...@@ -92,18 +92,24 @@ func (o *ReconcileOptions) Complete(cmd *cobra.Command, f cmdutil.Factory, args ...@@ -92,18 +92,24 @@ func (o *ReconcileOptions) Complete(cmd *cobra.Command, f cmdutil.Factory, args
if err != nil { if err != nil {
return err return err
} }
o.ResourceBuilder = f.NewBuilder().
r := f.NewBuilder().
ContinueOnError(). ContinueOnError().
NamespaceParam(namespace).DefaultNamespace(). NamespaceParam(namespace).DefaultNamespace().
FilenameParam(enforceNamespace, options). FilenameParam(enforceNamespace, options).
Flatten() Flatten().
Do()
if err := r.Err(); err != nil {
return err
}
o.Visitor = r
client, err := f.ClientSet() client, err := f.ClientSet()
if err != nil { if err != nil {
return err return err
} }
o.RBACClient = client.Rbac() o.RBACClient = client.Rbac()
o.CoreClient = client.Core() o.NamespaceClient = client.Core().Namespaces()
mapper, _ := f.Object() mapper, _ := f.Object()
dryRun := false dryRun := false
...@@ -121,17 +127,29 @@ func (o *ReconcileOptions) Complete(cmd *cobra.Command, f cmdutil.Factory, args ...@@ -121,17 +127,29 @@ func (o *ReconcileOptions) Complete(cmd *cobra.Command, f cmdutil.Factory, args
} }
func (o *ReconcileOptions) Validate() error { 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 return nil
} }
func (o *ReconcileOptions) RunReconcile() error { func (o *ReconcileOptions) RunReconcile() error {
r := o.ResourceBuilder.Do() return o.Visitor.Visit(func(info *resource.Info, err error) error {
err := r.Err()
if err != nil {
return err
}
err = r.Visit(func(info *resource.Info, err error) error {
if err != nil { if err != nil {
return err return err
} }
...@@ -147,7 +165,7 @@ func (o *ReconcileOptions) RunReconcile() error { ...@@ -147,7 +165,7 @@ func (o *ReconcileOptions) RunReconcile() error {
RemoveExtraPermissions: false, RemoveExtraPermissions: false,
Role: reconciliation.RoleRuleOwner{Role: t}, Role: reconciliation.RoleRuleOwner{Role: t},
Client: reconciliation.RoleModifier{ Client: reconciliation.RoleModifier{
NamespaceClient: o.CoreClient.Namespaces(), NamespaceClient: o.NamespaceClient,
Client: o.RBACClient, Client: o.RBACClient,
}, },
} }
...@@ -181,7 +199,7 @@ func (o *ReconcileOptions) RunReconcile() error { ...@@ -181,7 +199,7 @@ func (o *ReconcileOptions) RunReconcile() error {
RoleBinding: reconciliation.RoleBindingAdapter{RoleBinding: t}, RoleBinding: reconciliation.RoleBindingAdapter{RoleBinding: t},
Client: reconciliation.RoleBindingClientAdapter{ Client: reconciliation.RoleBindingClientAdapter{
Client: o.RBACClient, Client: o.RBACClient,
NamespaceClient: o.CoreClient.Namespaces(), NamespaceClient: o.NamespaceClient,
}, },
} }
result, err := reconcileOptions.Run() result, err := reconcileOptions.Run()
...@@ -214,6 +232,4 @@ func (o *ReconcileOptions) RunReconcile() error { ...@@ -214,6 +232,4 @@ func (o *ReconcileOptions) RunReconcile() error {
return nil return nil
}) })
return err
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment