Skip to content

Commit d78a1f1

Browse files
Merge pull request #20426 from soltysh/external_prune
Switch prune to externals
2 parents 9581804 + 8a171ff commit d78a1f1

40 files changed

+824
-732
lines changed

hack/import-restrictions.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,6 @@
514514
"github.com/openshift/origin/pkg/authorization/util",
515515
"github.com/openshift/origin/pkg/build/apis/build",
516516
"github.com/openshift/origin/pkg/build/apis/build/install",
517-
"github.com/openshift/origin/pkg/build/apis/build/v1",
518517
"github.com/openshift/origin/pkg/build/buildapihelpers",
519518
"github.com/openshift/origin/pkg/build/client",
520519
"github.com/openshift/origin/pkg/build/client/internalversion",

pkg/oc/cli/admin/groups/sync/interfaces.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package sync
22

33
import (
4+
userv1client "github.com/openshift/client-go/user/clientset/versioned/typed/user/v1"
45
"github.com/openshift/origin/pkg/oc/lib/groupsync/interfaces"
5-
usertypedclient "github.com/openshift/origin/pkg/user/generated/internalclientset/typed/user/internalversion"
66
)
77

88
// SyncBuilder describes an object that can build all the schema-specific parts of an LDAPGroupSyncer
@@ -30,7 +30,7 @@ type GroupNameRestrictions interface {
3030
// a client that can retrieve OpenShift groups to satisfy those lists
3131
type OpenShiftGroupNameRestrictions interface {
3232
GroupNameRestrictions
33-
GetClient() usertypedclient.GroupInterface
33+
GetClient() userv1client.GroupInterface
3434
}
3535

3636
// MappedNameRestrictions describes an object that holds user name mappings for a group sync job

pkg/oc/cli/admin/groups/sync/prune.go

Lines changed: 29 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package sync
33
import (
44
"errors"
55
"fmt"
6-
"io"
76
"os"
87

98
"github.com/spf13/cobra"
@@ -14,13 +13,12 @@ import (
1413
kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
1514
"k8s.io/kubernetes/pkg/kubectl/genericclioptions"
1615

16+
userv1client "github.com/openshift/client-go/user/clientset/versioned/typed/user/v1"
1717
"github.com/openshift/origin/pkg/cmd/server/apis/config"
1818
"github.com/openshift/origin/pkg/cmd/server/apis/config/validation/ldap"
1919
"github.com/openshift/origin/pkg/oauthserver/ldaputil"
2020
"github.com/openshift/origin/pkg/oauthserver/ldaputil/ldapclient"
2121
"github.com/openshift/origin/pkg/oc/lib/groupsync"
22-
userclientinternal "github.com/openshift/origin/pkg/user/generated/internalclientset"
23-
usertypedclient "github.com/openshift/origin/pkg/user/generated/internalclientset/typed/user/internalversion"
2422
)
2523

2624
const PruneRecommendedName = "prune"
@@ -53,92 +51,73 @@ var (
5351

5452
type PruneOptions struct {
5553
// Config is the LDAP sync config read from file
56-
Config *config.LDAPSyncConfig
54+
Config *config.LDAPSyncConfig
55+
ConfigFile string
5756

5857
// Whitelist are the names of OpenShift group or LDAP group UIDs to use for syncing
59-
Whitelist []string
58+
Whitelist []string
59+
WhitelistFile string
6060

6161
// Blacklist are the names of OpenShift group or LDAP group UIDs to exclude
62-
Blacklist []string
62+
Blacklist []string
63+
BlacklistFile string
6364

6465
// Confirm determines whether or not to write to OpenShift
6566
Confirm bool
6667

6768
// GroupInterface is the interface used to interact with OpenShift Group objects
68-
GroupInterface usertypedclient.GroupInterface
69+
GroupInterface userv1client.GroupInterface
6970

70-
// Stderr is the writer to write warnings and errors to
71-
Stderr io.Writer
72-
73-
// Out is the writer to write output to
74-
Out io.Writer
71+
genericclioptions.IOStreams
7572
}
7673

77-
func NewPruneOptions() *PruneOptions {
74+
func NewPruneOptions(streams genericclioptions.IOStreams) *PruneOptions {
7875
return &PruneOptions{
79-
Stderr: os.Stderr,
8076
Whitelist: []string{},
77+
IOStreams: streams,
8178
}
8279
}
8380

8481
func NewCmdPrune(name, fullName string, f kcmdutil.Factory, streams genericclioptions.IOStreams) *cobra.Command {
85-
options := NewPruneOptions()
86-
options.Out = streams.Out
87-
88-
whitelistFile := ""
89-
blacklistFile := ""
90-
configFile := ""
91-
82+
o := NewPruneOptions(streams)
9283
cmd := &cobra.Command{
9384
Use: fmt.Sprintf("%s [WHITELIST] [--whitelist=WHITELIST-FILE] [--blacklist=BLACKLIST-FILE] --sync-config=CONFIG-SOURCE", name),
9485
Short: "Remove old OpenShift groups referencing missing records on an external provider",
9586
Long: pruneLong,
9687
Example: fmt.Sprintf(pruneExamples, fullName),
97-
Run: func(c *cobra.Command, args []string) {
98-
kcmdutil.CheckErr(options.Complete(whitelistFile, blacklistFile, configFile, args, f))
99-
kcmdutil.CheckErr(options.Validate())
100-
err := options.Run(c, f)
101-
if err != nil {
102-
if aggregate, ok := err.(kerrs.Aggregate); ok {
103-
for _, err := range aggregate.Errors() {
104-
fmt.Printf("%s\n", err)
105-
}
106-
os.Exit(1)
107-
}
108-
}
109-
kcmdutil.CheckErr(err)
88+
Run: func(cmd *cobra.Command, args []string) {
89+
kcmdutil.CheckErr(o.Complete(f, cmd, args))
90+
kcmdutil.CheckErr(o.Validate())
91+
kcmdutil.CheckErr(o.Run())
11092
},
11193
}
11294

113-
cmd.Flags().StringVar(&whitelistFile, "whitelist", whitelistFile, "path to the group whitelist file")
95+
cmd.Flags().StringVar(&o.WhitelistFile, "whitelist", o.WhitelistFile, "path to the group whitelist file")
11496
cmd.MarkFlagFilename("whitelist", "txt")
115-
cmd.Flags().StringVar(&blacklistFile, "blacklist", whitelistFile, "path to the group blacklist file")
97+
cmd.Flags().StringVar(&o.BlacklistFile, "blacklist", o.BlacklistFile, "path to the group blacklist file")
11698
cmd.MarkFlagFilename("blacklist", "txt")
11799
// TODO(deads): enable this once we're able to support string slice elements that have commas
118-
// cmd.Flags().StringSliceVar(&options.Blacklist, "blacklist-group", options.Blacklist, "group to blacklist")
119-
120-
cmd.Flags().StringVar(&configFile, "sync-config", configFile, "path to the sync config")
100+
// cmd.Flags().StringSliceVar(&o.Blacklist, "blacklist-group", o.Blacklist, "group to blacklist")
101+
cmd.Flags().StringVar(&o.ConfigFile, "sync-config", o.ConfigFile, "path to the sync config")
121102
cmd.MarkFlagFilename("sync-config", "yaml", "yml")
122-
123-
cmd.Flags().BoolVar(&options.Confirm, "confirm", false, "if true, modify OpenShift groups; if false, display groups")
103+
cmd.Flags().BoolVar(&o.Confirm, "confirm", o.Confirm, "if true, modify OpenShift groups; if false, display groups")
124104

125105
return cmd
126106
}
127107

128-
func (o *PruneOptions) Complete(whitelistFile, blacklistFile, configFile string, args []string, f kcmdutil.Factory) error {
108+
func (o *PruneOptions) Complete(f kcmdutil.Factory, cmd *cobra.Command, args []string) error {
129109
var err error
130-
131-
o.Config, err = decodeSyncConfigFromFile(configFile)
110+
o.Config, err = decodeSyncConfigFromFile(o.ConfigFile)
132111
if err != nil {
133112
return err
134113
}
135114

136-
o.Whitelist, err = buildOpenShiftGroupNameList(args, whitelistFile, o.Config.LDAPGroupUIDToOpenShiftGroupNameMapping)
115+
o.Whitelist, err = buildOpenShiftGroupNameList(args, o.WhitelistFile, o.Config.LDAPGroupUIDToOpenShiftGroupNameMapping)
137116
if err != nil {
138117
return err
139118
}
140119

141-
o.Blacklist, err = buildOpenShiftGroupNameList([]string{}, blacklistFile, o.Config.LDAPGroupUIDToOpenShiftGroupNameMapping)
120+
o.Blacklist, err = buildOpenShiftGroupNameList([]string{}, o.BlacklistFile, o.Config.LDAPGroupUIDToOpenShiftGroupNameMapping)
142121
if err != nil {
143122
return err
144123
}
@@ -147,11 +126,11 @@ func (o *PruneOptions) Complete(whitelistFile, blacklistFile, configFile string,
147126
if err != nil {
148127
return err
149128
}
150-
userClient, err := userclientinternal.NewForConfig(clientConfig)
129+
userClient, err := userv1client.NewForConfig(clientConfig)
151130
if err != nil {
152131
return err
153132
}
154-
o.GroupInterface = userClient.User().Groups()
133+
o.GroupInterface = userClient.Groups()
155134

156135
return nil
157136
}
@@ -170,7 +149,7 @@ func (o *PruneOptions) Validate() error {
170149

171150
// Run creates the GroupSyncer specified and runs it to sync groups
172151
// the arguments are only here because its the only way to get the printer we need
173-
func (o *PruneOptions) Run(cmd *cobra.Command, f kcmdutil.Factory) error {
152+
func (o *PruneOptions) Run() error {
174153
bindPassword, err := config.ResolveStringValue(o.Config.BindPassword)
175154
if err != nil {
176155
return err
@@ -236,7 +215,7 @@ func (o *PruneOptions) GetBlacklist() []string {
236215
return o.Blacklist
237216
}
238217

239-
func (o *PruneOptions) GetClient() usertypedclient.GroupInterface {
218+
func (o *PruneOptions) GetClient() userv1client.GroupInterface {
240219
return o.GroupInterface
241220
}
242221

pkg/oc/cli/admin/groups/sync/sync.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
2121
"k8s.io/kubernetes/pkg/kubectl/genericclioptions"
2222

23+
userv1client "github.com/openshift/client-go/user/clientset/versioned/typed/user/v1"
2324
"github.com/openshift/origin/pkg/cmd/server/apis/config"
2425
configapilatest "github.com/openshift/origin/pkg/cmd/server/apis/config/latest"
2526
"github.com/openshift/origin/pkg/cmd/server/apis/config/validation/ldap"
@@ -29,8 +30,6 @@ import (
2930
"github.com/openshift/origin/pkg/oc/lib/groupsync"
3031
"github.com/openshift/origin/pkg/oc/lib/groupsync/interfaces"
3132
"github.com/openshift/origin/pkg/oc/lib/groupsync/syncerror"
32-
userclientinternal "github.com/openshift/origin/pkg/user/generated/internalclientset"
33-
usertypedclient "github.com/openshift/origin/pkg/user/generated/internalclientset/typed/user/internalversion"
3433
)
3534

3635
const SyncRecommendedName = "sync"
@@ -97,7 +96,7 @@ type SyncOptions struct {
9796
Confirm bool
9897

9998
// GroupInterface is the interface used to interact with OpenShift Group objects
100-
GroupInterface usertypedclient.GroupInterface
99+
GroupInterface userv1client.GroupInterface
101100

102101
// Stderr is the writer to write warnings and errors to
103102
Stderr io.Writer
@@ -226,11 +225,11 @@ func (o *SyncOptions) Complete(typeArg, whitelistFile, blacklistFile, configFile
226225
if err != nil {
227226
return err
228227
}
229-
userClient, err := userclientinternal.NewForConfig(clientConfig)
228+
userClient, err := userv1client.NewForConfig(clientConfig)
230229
if err != nil {
231230
return err
232231
}
233-
o.GroupInterface = userClient.User().Groups()
232+
o.GroupInterface = userClient.Groups()
234233

235234
return nil
236235
}
@@ -506,7 +505,7 @@ func (o *SyncOptions) GetBlacklist() []string {
506505
return o.Blacklist
507506
}
508507

509-
func (o *SyncOptions) GetClient() usertypedclient.GroupInterface {
508+
func (o *SyncOptions) GetClient() userv1client.GroupInterface {
510509
return o.GroupInterface
511510
}
512511

pkg/oc/cli/admin/prune/auth/bindings.go

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,26 @@ import (
44
"fmt"
55
"io"
66

7+
corev1 "k8s.io/api/core/v1"
78
kerrors "k8s.io/apimachinery/pkg/api/errors"
89
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
9-
kapi "k8s.io/kubernetes/pkg/apis/core"
10+
"k8s.io/kubernetes/pkg/apis/core"
11+
corev1conversions "k8s.io/kubernetes/pkg/apis/core/v1"
1012

11-
authclient "github.com/openshift/origin/pkg/authorization/generated/internalclientset"
13+
authv1client "github.com/openshift/client-go/authorization/clientset/versioned/typed/authorization/v1"
14+
authorizationapi "github.com/openshift/origin/pkg/authorization/apis/authorization"
1215
)
1316

1417
// reapClusterBindings removes the subject from cluster-level role bindings
15-
func reapClusterBindings(removedSubject kapi.ObjectReference, c authclient.Interface, out io.Writer) []error {
18+
func reapClusterBindings(removedSubject corev1.ObjectReference, c authv1client.AuthorizationV1Interface, out io.Writer) []error {
1619
errors := []error{}
1720

18-
clusterBindings, err := c.Authorization().ClusterRoleBindings().List(metav1.ListOptions{})
21+
clusterBindings, err := c.ClusterRoleBindings().List(metav1.ListOptions{})
1922
if err != nil {
2023
return []error{err}
2124
}
2225
for _, binding := range clusterBindings.Items {
23-
retainedSubjects := []kapi.ObjectReference{}
26+
retainedSubjects := []corev1.ObjectReference{}
2427
for _, subject := range binding.Subjects {
2528
if subject != removedSubject {
2629
retainedSubjects = append(retainedSubjects, subject)
@@ -29,7 +32,13 @@ func reapClusterBindings(removedSubject kapi.ObjectReference, c authclient.Inter
2932
if len(retainedSubjects) != len(binding.Subjects) {
3033
updatedBinding := binding
3134
updatedBinding.Subjects = retainedSubjects
32-
if _, err := c.Authorization().ClusterRoleBindings().Update(&updatedBinding); err != nil && !kerrors.IsNotFound(err) {
35+
coreSubjects, err := convertObjectReference(retainedSubjects)
36+
if err != nil {
37+
errors = append(errors, err)
38+
continue
39+
}
40+
updatedBinding.UserNames, updatedBinding.GroupNames = authorizationapi.StringSubjectsFor(binding.Namespace, coreSubjects)
41+
if _, err := c.ClusterRoleBindings().Update(&updatedBinding); err != nil && !kerrors.IsNotFound(err) {
3342
errors = append(errors, err)
3443
} else {
3544
fmt.Fprintf(out, "clusterrolebinding.rbac.authorization.k8s.io/"+updatedBinding.Name+" updated\n")
@@ -40,15 +49,15 @@ func reapClusterBindings(removedSubject kapi.ObjectReference, c authclient.Inter
4049
}
4150

4251
// reapNamespacedBindings removes the subject from namespaced role bindings
43-
func reapNamespacedBindings(removedSubject kapi.ObjectReference, c authclient.Interface, out io.Writer) []error {
52+
func reapNamespacedBindings(removedSubject corev1.ObjectReference, c authv1client.AuthorizationV1Interface, out io.Writer) []error {
4453
errors := []error{}
4554

46-
namespacedBindings, err := c.Authorization().RoleBindings(metav1.NamespaceAll).List(metav1.ListOptions{})
55+
namespacedBindings, err := c.RoleBindings(metav1.NamespaceAll).List(metav1.ListOptions{})
4756
if err != nil {
4857
return []error{err}
4958
}
5059
for _, binding := range namespacedBindings.Items {
51-
retainedSubjects := []kapi.ObjectReference{}
60+
retainedSubjects := []corev1.ObjectReference{}
5261
for _, subject := range binding.Subjects {
5362
if subject != removedSubject {
5463
retainedSubjects = append(retainedSubjects, subject)
@@ -57,7 +66,13 @@ func reapNamespacedBindings(removedSubject kapi.ObjectReference, c authclient.In
5766
if len(retainedSubjects) != len(binding.Subjects) {
5867
updatedBinding := binding
5968
updatedBinding.Subjects = retainedSubjects
60-
if _, err := c.Authorization().RoleBindings(binding.Namespace).Update(&updatedBinding); err != nil && !kerrors.IsNotFound(err) {
69+
coreSubjects, err := convertObjectReference(retainedSubjects)
70+
if err != nil {
71+
errors = append(errors, err)
72+
continue
73+
}
74+
updatedBinding.UserNames, updatedBinding.GroupNames = authorizationapi.StringSubjectsFor(binding.Namespace, coreSubjects)
75+
if _, err := c.RoleBindings(binding.Namespace).Update(&updatedBinding); err != nil && !kerrors.IsNotFound(err) {
6176
errors = append(errors, err)
6277
} else {
6378
fmt.Fprintf(out, "rolebinding.rbac.authorization.k8s.io/"+updatedBinding.Name+" updated\n")
@@ -66,3 +81,15 @@ func reapNamespacedBindings(removedSubject kapi.ObjectReference, c authclient.In
6681
}
6782
return errors
6883
}
84+
85+
func convertObjectReference(ins []corev1.ObjectReference) ([]core.ObjectReference, error) {
86+
result := []core.ObjectReference{}
87+
for _, subject := range ins {
88+
ref := &core.ObjectReference{}
89+
if err := corev1conversions.Convert_v1_ObjectReference_To_core_ObjectReference(&subject, ref, nil); err != nil {
90+
return nil, err
91+
}
92+
result = append(result, *ref)
93+
}
94+
return result, nil
95+
}

pkg/oc/cli/admin/prune/auth/group.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,24 @@ import (
44
"fmt"
55
"io"
66

7+
corev1 "k8s.io/api/core/v1"
78
kerrors "k8s.io/apimachinery/pkg/api/errors"
89
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
910
utilerrors "k8s.io/apimachinery/pkg/util/errors"
10-
kapi "k8s.io/kubernetes/pkg/apis/core"
1111

12-
authclient "github.com/openshift/origin/pkg/authorization/generated/internalclientset"
13-
securitytypedclient "github.com/openshift/origin/pkg/security/generated/internalclientset/typed/security/internalversion"
12+
authv1client "github.com/openshift/client-go/authorization/clientset/versioned/typed/authorization/v1"
13+
securityv1client "github.com/openshift/client-go/security/clientset/versioned/typed/security/v1"
1414
)
1515

1616
func reapForGroup(
17-
authorizationClient authclient.Interface,
18-
securityClient securitytypedclient.SecurityContextConstraintsInterface,
17+
authorizationClient authv1client.AuthorizationV1Interface,
18+
securityClient securityv1client.SecurityContextConstraintsInterface,
1919
name string,
2020
out io.Writer) error {
2121

2222
errors := []error{}
2323

24-
removedSubject := kapi.ObjectReference{Kind: "Group", Name: name}
24+
removedSubject := corev1.ObjectReference{Kind: "Group", Name: name}
2525
errors = append(errors, reapClusterBindings(removedSubject, authorizationClient, out)...)
2626
errors = append(errors, reapNamespacedBindings(removedSubject, authorizationClient, out)...)
2727

0 commit comments

Comments
 (0)