|
| 1 | +// Copyright Jetstack Ltd. See LICENSE for details. |
| 2 | +package options |
| 3 | + |
| 4 | +import ( |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + |
| 8 | + "github.com/spf13/cobra" |
| 9 | + k8sErrors "k8s.io/apimachinery/pkg/util/errors" |
| 10 | + "k8s.io/apiserver/pkg/util/term" |
| 11 | + cliflag "k8s.io/component-base/cli/flag" |
| 12 | +) |
| 13 | + |
| 14 | +const ( |
| 15 | + AppName = "kube-oidc-proxy" |
| 16 | +) |
| 17 | + |
| 18 | +type Options struct { |
| 19 | + App *KubeOIDCProxyOptions |
| 20 | + OIDCAuthentication *OIDCAuthenticationOptions |
| 21 | + SecureServing *SecureServingOptions |
| 22 | + Client *ClientOptions |
| 23 | + Misc *MiscOptions |
| 24 | + |
| 25 | + nfs *cliflag.NamedFlagSets |
| 26 | +} |
| 27 | + |
| 28 | +func New() *Options { |
| 29 | + nfs := new(cliflag.NamedFlagSets) |
| 30 | + |
| 31 | + // Add flags to command sets |
| 32 | + return &Options{ |
| 33 | + App: NewKubeOIDCProxyOptions(nfs), |
| 34 | + OIDCAuthentication: NewOIDCAuthenticationOptions(nfs), |
| 35 | + SecureServing: NewSecureServingOptions(nfs), |
| 36 | + Client: NewClientOptions(nfs), |
| 37 | + Misc: NewMiscOptions(nfs), |
| 38 | + |
| 39 | + nfs: nfs, |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +func (o *Options) AddFlags(cmd *cobra.Command) { |
| 44 | + // pretty output from kube-apiserver |
| 45 | + usageFmt := "Usage:\n %s\n" |
| 46 | + cols, _, _ := term.TerminalSize(cmd.OutOrStdout()) |
| 47 | + cmd.SetUsageFunc(func(cmd *cobra.Command) error { |
| 48 | + fmt.Fprintf(cmd.OutOrStderr(), usageFmt, cmd.UseLine()) |
| 49 | + cliflag.PrintSections(cmd.OutOrStderr(), *o.nfs, cols) |
| 50 | + return nil |
| 51 | + }) |
| 52 | + |
| 53 | + cmd.SetHelpFunc(func(cmd *cobra.Command, args []string) { |
| 54 | + fmt.Fprintf(cmd.OutOrStdout(), "%s\n\n"+usageFmt, cmd.Long, cmd.UseLine()) |
| 55 | + cliflag.PrintSections(cmd.OutOrStdout(), *o.nfs, cols) |
| 56 | + }) |
| 57 | + |
| 58 | + fs := cmd.Flags() |
| 59 | + for _, f := range o.nfs.FlagSets { |
| 60 | + fs.AddFlagSet(f) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +func (o *Options) Validate(cmd *cobra.Command) error { |
| 65 | + if cmd.Flag("version").Value.String() == "true" { |
| 66 | + o.Misc.PrintVersionAndExit() |
| 67 | + } |
| 68 | + |
| 69 | + var errs []error |
| 70 | + |
| 71 | + if err := o.OIDCAuthentication.Validate(); err != nil { |
| 72 | + errs = append(errs, err) |
| 73 | + } |
| 74 | + |
| 75 | + if err := o.SecureServing.Validate(); len(err) > 0 { |
| 76 | + errs = append(errs, err...) |
| 77 | + } |
| 78 | + |
| 79 | + if o.SecureServing.BindPort == o.App.ReadinessProbePort { |
| 80 | + errs = append(errs, errors.New("unable to securely serve on port 8080 (used by readiness probe)")) |
| 81 | + } |
| 82 | + |
| 83 | + if len(errs) > 0 { |
| 84 | + return k8sErrors.NewAggregate(errs) |
| 85 | + } |
| 86 | + |
| 87 | + return nil |
| 88 | +} |
0 commit comments