@@ -3,7 +3,6 @@ package sync
33import (
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
2624const PruneRecommendedName = "prune"
@@ -53,92 +51,73 @@ var (
5351
5452type 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
8481func 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
0 commit comments