@@ -64,19 +64,25 @@ var (
6464
6565// Options is a struct to hold options for the version-checker.
6666type Options struct {
67+ kubeConfigFlags * genericclioptions.ConfigFlags
68+
69+ Client client.Options
6770 MetricsServingAddress string
68- DefaultTestAll bool
69- CacheTimeout time.Duration
7071 LogLevel string
7172
72- PprofBindAddress string
73+ PprofBindAddress string
74+ selfhosted selfhosted.Options
75+
76+ CacheTimeout time.Duration
7377 GracefulShutdownTimeout time.Duration
7478 CacheSyncPeriod time.Duration
7579
76- kubeConfigFlags * genericclioptions. ConfigFlags
77- selfhosted selfhosted. Options
80+ DefaultTestAll bool
81+ }
7882
79- Client client.Options
83+ type envMatcher struct {
84+ re * regexp.Regexp
85+ action func (matches []string , value string )
8086}
8187
8288func (o * Options ) addFlags (cmd * cobra.Command ) {
@@ -363,59 +369,83 @@ func (o *Options) assignSelfhosted(envs []string) {
363369 }
364370 }
365371
366- regexActions := map [* regexp.Regexp ]func (matches []string , value string ){
367- selfhostedHostReg : func (matches []string , value string ) {
368- initOptions (matches [1 ])
369- o .Client .Selfhosted [matches [1 ]].Host = value
372+ // Go maps iterate in random order - Using a slice to consistency
373+ regexActions := []envMatcher {
374+ {
375+ re : selfhostedTokenPath ,
376+ action : func (matches []string , value string ) {
377+ initOptions (matches [1 ])
378+ o .Client .Selfhosted [matches [1 ]].TokenPath = value
379+ },
370380 },
371- selfhostedUsernameReg : func (matches []string , value string ) {
372- initOptions (matches [1 ])
373- o .Client .Selfhosted [matches [1 ]].Username = value
381+ {
382+ re : selfhostedTokenReg ,
383+ action : func (matches []string , value string ) {
384+ initOptions (matches [1 ])
385+ o .Client .Selfhosted [matches [1 ]].Bearer = value
386+ },
374387 },
375- selfhostedPasswordReg : func (matches []string , value string ) {
376- initOptions (matches [1 ])
377- o .Client .Selfhosted [matches [1 ]].Password = value
388+ // All your other patterns (host, username, password, insecure, capath...)
389+ {
390+ re : selfhostedHostReg ,
391+ action : func (matches []string , value string ) {
392+ initOptions (matches [1 ])
393+ o .Client .Selfhosted [matches [1 ]].Host = value
394+ },
378395 },
379- selfhostedTokenPath : func (matches []string , value string ) {
380- initOptions (matches [1 ])
381- o .Client .Selfhosted [matches [1 ]].TokenPath = value
396+ {
397+ re : selfhostedUsernameReg ,
398+ action : func (matches []string , value string ) {
399+ initOptions (matches [1 ])
400+ o .Client .Selfhosted [matches [1 ]].Username = value
401+ },
382402 },
383- selfhostedTokenReg : func (matches []string , value string ) {
384- initOptions (matches [1 ])
385- o .Client .Selfhosted [matches [1 ]].Bearer = value
403+ {
404+ re : selfhostedPasswordReg ,
405+ action : func (matches []string , value string ) {
406+ initOptions (matches [1 ])
407+ o .Client .Selfhosted [matches [1 ]].Password = value
408+ },
386409 },
387- selfhostedInsecureReg : func (matches []string , value string ) {
388- initOptions (matches [1 ])
389- if val , err := strconv .ParseBool (value ); err == nil {
390- o .Client .Selfhosted [matches [1 ]].Insecure = val
391- }
410+ {
411+ re : selfhostedInsecureReg ,
412+ action : func (matches []string , value string ) {
413+ initOptions (matches [1 ])
414+ if b , err := strconv .ParseBool (value ); err == nil {
415+ o .Client .Selfhosted [matches [1 ]].Insecure = b
416+ }
417+ },
392418 },
393- selfhostedCAPath : func (matches []string , value string ) {
394- initOptions (matches [1 ])
395- o .Client .Selfhosted [matches [1 ]].CAPath = value
419+ {
420+ re : selfhostedCAPath ,
421+ action : func (matches []string , value string ) {
422+ initOptions (matches [1 ])
423+ o .Client .Selfhosted [matches [1 ]].CAPath = value
424+ },
396425 },
397426 }
398427
399428 for _ , env := range envs {
400- pair := strings .SplitN (env , "=" , 2 )
401- if len (pair ) != 2 || len ( pair [1 ]) == 0 {
429+ parts := strings .SplitN (env , "=" , 2 )
430+ if len (parts ) != 2 || parts [1 ] == "" {
402431 continue
403432 }
433+ key := strings .ToUpper (parts [0 ])
434+ val := parts [1 ]
404435
405- key := strings .ToUpper (pair [0 ])
406- value := pair [1 ]
407-
408- for regex , action := range regexActions {
409- if matches := regex .FindStringSubmatch (key ); len (matches ) == 2 {
410- action (matches , value )
436+ for _ , p := range regexActions {
437+ if match := p .re .FindStringSubmatch (key ); len (match ) == 2 {
438+ p .action (match , val )
411439 break
412440 }
413441 }
414442 }
415443
444+ // If we have some selfhosted flags, lets set them here...
416445 if len (o .selfhosted .Host ) > 0 {
417446 o .Client .Selfhosted [o .selfhosted .Host ] = & o .selfhosted
418447 }
448+
419449 if ! validSelfHostedOpts (o ) {
420450 panic (fmt .Errorf ("invalid self hosted configuration" ))
421451 }
0 commit comments