@@ -64,19 +64,25 @@ var (
64
64
65
65
// Options is a struct to hold options for the version-checker.
66
66
type Options struct {
67
+ kubeConfigFlags * genericclioptions.ConfigFlags
68
+
69
+ Client client.Options
67
70
MetricsServingAddress string
68
- DefaultTestAll bool
69
- CacheTimeout time.Duration
70
71
LogLevel string
71
72
72
- PprofBindAddress string
73
+ PprofBindAddress string
74
+ selfhosted selfhosted.Options
75
+
76
+ CacheTimeout time.Duration
73
77
GracefulShutdownTimeout time.Duration
74
78
CacheSyncPeriod time.Duration
75
79
76
- kubeConfigFlags * genericclioptions. ConfigFlags
77
- selfhosted selfhosted. Options
80
+ DefaultTestAll bool
81
+ }
78
82
79
- Client client.Options
83
+ type envMatcher struct {
84
+ re * regexp.Regexp
85
+ action func (matches []string , value string )
80
86
}
81
87
82
88
func (o * Options ) addFlags (cmd * cobra.Command ) {
@@ -363,59 +369,83 @@ func (o *Options) assignSelfhosted(envs []string) {
363
369
}
364
370
}
365
371
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
+ },
370
380
},
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
+ },
374
387
},
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
+ },
378
395
},
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
+ },
382
402
},
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
+ },
386
409
},
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
+ },
392
418
},
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
+ },
396
425
},
397
426
}
398
427
399
428
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 ] == "" {
402
431
continue
403
432
}
433
+ key := strings .ToUpper (parts [0 ])
434
+ val := parts [1 ]
404
435
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 )
411
439
break
412
440
}
413
441
}
414
442
}
415
443
444
+ // If we have some selfhosted flags, lets set them here...
416
445
if len (o .selfhosted .Host ) > 0 {
417
446
o .Client .Selfhosted [o .selfhosted .Host ] = & o .selfhosted
418
447
}
448
+
419
449
if ! validSelfHostedOpts (o ) {
420
450
panic (fmt .Errorf ("invalid self hosted configuration" ))
421
451
}
0 commit comments