@@ -4,12 +4,14 @@ import (
4
4
"errors"
5
5
"fmt"
6
6
"io"
7
+ "strings"
7
8
8
9
"github.com/spf13/cobra"
9
10
10
11
kapi "k8s.io/kubernetes/pkg/api"
11
12
kapierrors "k8s.io/kubernetes/pkg/api/errors"
12
13
kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
14
+ kutilerrors "k8s.io/kubernetes/pkg/util/errors"
13
15
"k8s.io/kubernetes/pkg/util/sets"
14
16
15
17
authorizationapi "github.com/openshift/origin/pkg/authorization/api"
@@ -23,6 +25,7 @@ import (
23
25
// ReconcileClusterRoleBindingsRecommendedName is the recommended command name
24
26
const ReconcileClusterRoleBindingsRecommendedName = "reconcile-cluster-role-bindings"
25
27
28
+ // ReconcileClusterRoleBindingsOptions contains all the necessary functionality for the OpenShift cli reconcile-cluster-role-bindings command
26
29
type ReconcileClusterRoleBindingsOptions struct {
27
30
// RolesToReconcile says which roles should have their default bindings reconciled.
28
31
// An empty or nil slice means reconcile all of them.
@@ -34,6 +37,7 @@ type ReconcileClusterRoleBindingsOptions struct {
34
37
ExcludeSubjects []kapi.ObjectReference
35
38
36
39
Out io.Writer
40
+ Err io.Writer
37
41
Output string
38
42
39
43
RoleBindingClient client.ClusterRoleBindingInterface
@@ -66,9 +70,10 @@ You can see which recommended cluster role bindings have changed by choosing an
66
70
)
67
71
68
72
// NewCmdReconcileClusterRoleBindings implements the OpenShift cli reconcile-cluster-role-bindings command
69
- func NewCmdReconcileClusterRoleBindings (name , fullName string , f * clientcmd.Factory , out io.Writer ) * cobra.Command {
73
+ func NewCmdReconcileClusterRoleBindings (name , fullName string , f * clientcmd.Factory , out , err io.Writer ) * cobra.Command {
70
74
o := & ReconcileClusterRoleBindingsOptions {
71
75
Out : out ,
76
+ Err : err ,
72
77
Union : true ,
73
78
}
74
79
@@ -143,15 +148,15 @@ func (o *ReconcileClusterRoleBindingsOptions) Validate() error {
143
148
return nil
144
149
}
145
150
146
- // ReconcileClusterRoleBindingsOptions contains all the necessary functionality for the OpenShift cli reconcile-cluster-role-bindings command
147
151
func (o * ReconcileClusterRoleBindingsOptions ) RunReconcileClusterRoleBindings (cmd * cobra.Command , f * clientcmd.Factory ) error {
148
- changedClusterRoleBindings , err := o .ChangedClusterRoleBindings ()
149
- if err != nil {
150
- return err
152
+ changedClusterRoleBindings , fetchErr := o .ChangedClusterRoleBindings ()
153
+ if fetchErr != nil && ! IsClusterRoleBindingLookupError (fetchErr ) {
154
+ // we got an error that isn't due to a partial match, so we can't continue
155
+ return fetchErr
151
156
}
152
157
153
158
if len (changedClusterRoleBindings ) == 0 {
154
- return nil
159
+ return fetchErr
155
160
}
156
161
157
162
if (len (o .Output ) != 0 ) && ! o .Confirmed {
@@ -162,19 +167,22 @@ func (o *ReconcileClusterRoleBindingsOptions) RunReconcileClusterRoleBindings(cm
162
167
mapper , _ := f .Object (false )
163
168
fn := cmdutil .VersionedPrintObject (f .PrintObject , cmd , mapper , o .Out )
164
169
if err := fn (list ); err != nil {
165
- return err
170
+ return kutilerrors . NewAggregate ([] error { fetchErr , err })
166
171
}
167
172
}
168
173
169
174
if o .Confirmed {
170
- return o .ReplaceChangedRoleBindings (changedClusterRoleBindings )
175
+ if err := o .ReplaceChangedRoleBindings (changedClusterRoleBindings ); err != nil {
176
+ return kutilerrors .NewAggregate ([]error {fetchErr , err })
177
+ }
171
178
}
172
179
173
- return nil
180
+ return fetchErr
174
181
}
175
182
176
183
// ChangedClusterRoleBindings returns the role bindings that must be created and/or updated to
177
- // match the recommended bootstrap policy
184
+ // match the recommended bootstrap policy. If roles to reconcile are provided, but not all are
185
+ // found, all partial results are returned.
178
186
func (o * ReconcileClusterRoleBindingsOptions ) ChangedClusterRoleBindings () ([]* authorizationapi.ClusterRoleBinding , error ) {
179
187
changedRoleBindings := []* authorizationapi.ClusterRoleBinding {}
180
188
@@ -212,7 +220,7 @@ func (o *ReconcileClusterRoleBindingsOptions) ChangedClusterRoleBindings() ([]*a
212
220
213
221
if len (rolesNotFound ) != 0 {
214
222
// return the known changes and the error so that a caller can decide if he wants a partial update
215
- return changedRoleBindings , fmt . Errorf ( "did not find requested cluster role %s" , rolesNotFound .List ())
223
+ return changedRoleBindings , NewClusterRoleBindingLookupError ( rolesNotFound .List ())
216
224
}
217
225
218
226
return changedRoleBindings , nil
@@ -331,3 +339,26 @@ func DiffObjectReferenceLists(list1 []kapi.ObjectReference, list2 []kapi.ObjectR
331
339
}
332
340
return
333
341
}
342
+
343
+ func NewClusterRoleBindingLookupError (rolesNotFound []string ) error {
344
+ return & clusterRoleBindingLookupError {
345
+ rolesNotFound : rolesNotFound ,
346
+ }
347
+ }
348
+
349
+ type clusterRoleBindingLookupError struct {
350
+ rolesNotFound []string
351
+ }
352
+
353
+ func (e * clusterRoleBindingLookupError ) Error () string {
354
+ return fmt .Sprintf ("did not find requested cluster roles: %s" , strings .Join (e .rolesNotFound , ", " ))
355
+ }
356
+
357
+ func IsClusterRoleBindingLookupError (err error ) bool {
358
+ if err == nil {
359
+ return false
360
+ }
361
+
362
+ _ , ok := err .(* clusterRoleBindingLookupError )
363
+ return ok
364
+ }
0 commit comments