Skip to content
This repository was archived by the owner on May 17, 2024. It is now read-only.

Commit 92f809e

Browse files
committed
Cleans up command options package
Signed-off-by: JoshVanL <[email protected]>
1 parent b5d7a59 commit 92f809e

File tree

11 files changed

+303
-143
lines changed

11 files changed

+303
-143
lines changed

cmd/app/options/kube_oidc_proxy.go renamed to cmd/app/options/app.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@ import (
55
"github.com/spf13/pflag"
66

77
"github.com/jetstack/kube-oidc-proxy/pkg/util/flags"
8+
cliflag "k8s.io/component-base/cli/flag"
89
)
910

1011
type KubeOIDCProxyOptions struct {
1112
DisableImpersonation bool
1213
ReadinessProbePort int
1314

14-
TokenPassthrough TokenPassthroughOptions
1515
ExtraHeaderOptions ExtraHeaderOptions
16+
TokenPassthrough TokenPassthroughOptions
1617
}
1718

1819
type TokenPassthroughOptions struct {
@@ -26,7 +27,11 @@ type ExtraHeaderOptions struct {
2627
ExtraUserHeaders map[string][]string
2728
}
2829

29-
func (k *KubeOIDCProxyOptions) AddFlags(fs *pflag.FlagSet) {
30+
func NewKubeOIDCProxyOptions(nfs *cliflag.NamedFlagSets) *KubeOIDCProxyOptions {
31+
return new(KubeOIDCProxyOptions).AddFlags(nfs.FlagSet("Kube-OIDC-Proxy"))
32+
}
33+
34+
func (k *KubeOIDCProxyOptions) AddFlags(fs *pflag.FlagSet) *KubeOIDCProxyOptions {
3035
fs.BoolVar(&k.DisableImpersonation, "disable-impersonation", k.DisableImpersonation,
3136
"(Alpha) Disable the impersonation of authenticated requests. All "+
3237
"authenticated requests will be forwarded as is.")
@@ -36,6 +41,8 @@ func (k *KubeOIDCProxyOptions) AddFlags(fs *pflag.FlagSet) {
3641

3742
k.TokenPassthrough.AddFlags(fs)
3843
k.ExtraHeaderOptions.AddFlags(fs)
44+
45+
return k
3946
}
4047

4148
func (t *TokenPassthroughOptions) AddFlags(fs *pflag.FlagSet) {

cmd/app/options/audit.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright Jetstack Ltd. See LICENSE for details.
2+
package options
3+
4+
import (
5+
"github.com/spf13/pflag"
6+
apiserveroptions "k8s.io/apiserver/pkg/server/options"
7+
cliflag "k8s.io/component-base/cli/flag"
8+
)
9+
10+
type AuditOptions struct {
11+
*apiserveroptions.AuditOptions
12+
}
13+
14+
func NewAuditOptions(nfs *cliflag.NamedFlagSets) *AuditOptions {
15+
a := &AuditOptions{
16+
AuditOptions: apiserveroptions.NewAuditOptions(),
17+
}
18+
19+
return a.AddFlags(nfs.FlagSet("Audit"))
20+
}
21+
22+
func (a *AuditOptions) AddFlags(fs *pflag.FlagSet) *AuditOptions {
23+
a.AuditOptions.AddFlags(fs)
24+
return a
25+
}

cmd/app/options/client.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,29 @@ import (
55
"github.com/spf13/cobra"
66
"github.com/spf13/pflag"
77
"k8s.io/cli-runtime/pkg/genericclioptions"
8+
cliflag "k8s.io/component-base/cli/flag"
89
)
910

1011
type ClientOptions struct {
1112
*genericclioptions.ConfigFlags
1213
}
1314

14-
func NewClientFlags() *ClientOptions {
15-
return &ClientOptions{
15+
func NewClientOptions(nfs *cliflag.NamedFlagSets) *ClientOptions {
16+
c := &ClientOptions{
1617
ConfigFlags: genericclioptions.NewConfigFlags(true),
1718
}
19+
20+
// Disable unwanted options
21+
c.CacheDir = nil
22+
c.Impersonate = nil
23+
c.ImpersonateGroup = nil
24+
25+
return c.AddFlags(nfs.FlagSet("Client"))
1826
}
1927

20-
func (c *ClientOptions) AddFlags(flags *pflag.FlagSet) {
21-
c.ConfigFlags.AddFlags(flags)
28+
func (c *ClientOptions) AddFlags(fs *pflag.FlagSet) *ClientOptions {
29+
c.ConfigFlags.AddFlags(fs)
30+
return c
2231
}
2332

2433
func (c *ClientOptions) ClientFlagsChanged(cmd *cobra.Command) bool {

cmd/app/options/misc.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Copyright Jetstack Ltd. See LICENSE for details.
2+
package options
3+
4+
import (
5+
"fmt"
6+
"os"
7+
"runtime"
8+
9+
"github.com/spf13/pflag"
10+
apimachineryversion "k8s.io/apimachinery/pkg/version"
11+
cliflag "k8s.io/component-base/cli/flag"
12+
"k8s.io/component-base/cli/globalflag"
13+
)
14+
15+
type MiscOptions struct {
16+
gitMajor string // major version, always numeric
17+
gitMinor string // minor version, numeric possibly followed by "+"
18+
gitVersion string
19+
gitCommit string // sha1 from git, output of $(git rev-parse HEAD)
20+
gitTreeState string // state of git tree, either "clean" or "dirty"
21+
22+
buildDate string // build date in ISO8601 format, output of $(date -u +'%Y-%m-%dT%H:%M:%SZ')
23+
}
24+
25+
var (
26+
gitMajor string // major version, always numeric
27+
gitMinor string // minor version, numeric possibly followed by "+"
28+
gitVersion = "v0.0.0-master+$Format:%h$"
29+
gitCommit = "$Format:%H$" // sha1 from git, output of $(git rev-parse HEAD)
30+
gitTreeState = "" // state of git tree, either "clean" or "dirty"
31+
32+
buildDate = "1970-01-01T00:00:00Z" // build date in ISO8601 format, output of $(date -u +'%Y-%m-%dT%H:%M:%SZ')
33+
)
34+
35+
func NewMiscOptions(nfs *cliflag.NamedFlagSets) *MiscOptions {
36+
m := &MiscOptions{
37+
gitMajor: gitMajor,
38+
gitMinor: gitMinor,
39+
gitVersion: gitVersion,
40+
gitCommit: gitCommit,
41+
gitTreeState: gitTreeState,
42+
buildDate: buildDate,
43+
}
44+
45+
return m.AddFlags(nfs.FlagSet("Misc"))
46+
}
47+
48+
func (m *MiscOptions) AddFlags(fs *pflag.FlagSet) *MiscOptions {
49+
globalflag.AddGlobalFlags(fs, AppName)
50+
fs.Bool("version", false, "Print version information and quit")
51+
return m
52+
}
53+
54+
func (m *MiscOptions) PrintVersionAndExit() {
55+
fmt.Printf("%s version: %#v\n", AppName,
56+
apimachineryversion.Info{
57+
Major: m.gitMajor,
58+
Minor: m.gitMinor,
59+
GitVersion: m.gitVersion,
60+
GitCommit: m.gitCommit,
61+
GitTreeState: m.gitTreeState,
62+
BuildDate: m.buildDate,
63+
GoVersion: runtime.Version(),
64+
Compiler: runtime.Compiler,
65+
Platform: fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH),
66+
},
67+
)
68+
69+
os.Exit(0)
70+
}

cmd/app/options/oidc.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ type OIDCAuthenticationOptions struct {
2222
RequiredClaims map[string]string
2323
}
2424

25+
func NewOIDCAuthenticationOptions(nfs *cliflag.NamedFlagSets) *OIDCAuthenticationOptions {
26+
return new(OIDCAuthenticationOptions).AddFlags(nfs.FlagSet("OIDC"))
27+
}
28+
2529
func (o *OIDCAuthenticationOptions) Validate() error {
2630
if o != nil && (len(o.IssuerURL) > 0) != (len(o.ClientID) > 0) {
2731
return fmt.Errorf("oidc-issuer-url and oidc-client-id should be specified together")
@@ -30,7 +34,7 @@ func (o *OIDCAuthenticationOptions) Validate() error {
3034
return nil
3135
}
3236

33-
func (o *OIDCAuthenticationOptions) AddFlags(fs *pflag.FlagSet) {
37+
func (o *OIDCAuthenticationOptions) AddFlags(fs *pflag.FlagSet) *OIDCAuthenticationOptions {
3438
fs.StringSliceVar(&o.APIAudiences, "api-audiences", o.APIAudiences, ""+
3539
"Identifiers of the API. This can be used as an additional list of "+
3640
"identifiers that exist in the target audiences of requests when "+
@@ -72,4 +76,6 @@ func (o *OIDCAuthenticationOptions) AddFlags(fs *pflag.FlagSet) {
7276
"A key=value pair that describes a required claim in the ID Token. "+
7377
"If set, the claim is verified to be present in the ID Token with a matching value. "+
7478
"Repeat this flag to specify multiple claims.")
79+
80+
return o
7581
}

cmd/app/options/options.go

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

cmd/app/options/serving.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright Jetstack Ltd. See LICENSE for details.
2+
package options
3+
4+
import (
5+
"net"
6+
7+
"github.com/spf13/pflag"
8+
apiserveroptions "k8s.io/apiserver/pkg/server/options"
9+
cliflag "k8s.io/component-base/cli/flag"
10+
)
11+
12+
type SecureServingOptions struct {
13+
*apiserveroptions.SecureServingOptions
14+
}
15+
16+
func NewSecureServingOptions(nfs *cliflag.NamedFlagSets) *SecureServingOptions {
17+
s := &SecureServingOptions{
18+
SecureServingOptions: &apiserveroptions.SecureServingOptions{
19+
BindAddress: net.ParseIP("0.0.0.0"),
20+
BindPort: 6443,
21+
Required: true,
22+
ServerCert: apiserveroptions.GeneratableKeyCert{
23+
PairName: AppName,
24+
CertDirectory: "/var/run/kubernetes",
25+
},
26+
},
27+
}
28+
29+
return s.AddFlags(nfs.FlagSet("Secure Serving"))
30+
}
31+
32+
func (s *SecureServingOptions) AddFlags(fs *pflag.FlagSet) *SecureServingOptions {
33+
s.SecureServingOptions.AddFlags(fs)
34+
return s
35+
}

0 commit comments

Comments
 (0)