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

Commit ca2ee62

Browse files
committed
Adds flag to enable options around in cluster configuration
Signed-off-by: JoshVanL <[email protected]>
1 parent a82bdfc commit ca2ee62

File tree

6 files changed

+158
-11
lines changed

6 files changed

+158
-11
lines changed

cmd/options/client.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
"github.com/spf13/pflag"
10+
"k8s.io/cli-runtime/pkg/genericclioptions"
11+
)
12+
13+
const (
14+
flagInClusterConfig = "in-cluster-config"
15+
)
16+
17+
type ClientExtraOptions struct {
18+
InClusterConfig bool
19+
*genericclioptions.ConfigFlags
20+
}
21+
22+
func NewClientExtraFlags() *ClientExtraOptions {
23+
return &ClientExtraOptions{
24+
InClusterConfig: false,
25+
ConfigFlags: genericclioptions.NewConfigFlags(true),
26+
}
27+
}
28+
29+
func (c *ClientExtraOptions) AddFlags(flags *pflag.FlagSet) {
30+
flags.BoolVar(&c.InClusterConfig, flagInClusterConfig, c.InClusterConfig, "Use in-cluster configuration to authenticate and connect to a Kubernetes API server")
31+
c.ConfigFlags.AddFlags(flags)
32+
}
33+
34+
func (c *ClientExtraOptions) Validate(cmd *cobra.Command) error {
35+
clientFCh := c.clientFlagsChanged(cmd)
36+
37+
if clientFCh && c.InClusterConfig {
38+
return fmt.Errorf("if --%s is enabled, no other client flag options my be specified", flagInClusterConfig)
39+
}
40+
41+
if !clientFCh && !c.InClusterConfig {
42+
return errors.New("no client flag options specified")
43+
}
44+
45+
return nil
46+
}
47+
48+
func (c *ClientExtraOptions) clientFlagsChanged(cmd *cobra.Command) bool {
49+
for _, f := range clientOptionFlags() {
50+
if ff := cmd.Flag(f); ff != nil && ff.Changed {
51+
return true
52+
}
53+
}
54+
55+
return false
56+
}
57+
58+
func clientOptionFlags() []string {
59+
return []string{"certificate-authority", "client-certificate", "client-key", "cluster",
60+
"context", "insecure-skip-tls-verify", "kubeconfig", "namespace",
61+
"request-timeout", "server", "token", "user",
62+
}
63+
}

cmd/options/client_test.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+
"errors"
6+
"testing"
7+
8+
"github.com/spf13/cobra"
9+
)
10+
11+
func TestClientExtraOptionsValidate(t *testing.T) {
12+
13+
type testT struct {
14+
input []string
15+
expError error
16+
}
17+
18+
for name, test := range map[string]testT{
19+
"if no flags are provided, should error": {
20+
input: []string{},
21+
expError: errors.New("no client flag options specified"),
22+
},
23+
24+
"if only client flags provided then pass": {
25+
input: []string{"--server=foo", "--kubeconfig=bla"},
26+
expError: nil,
27+
},
28+
29+
"if only in cluster config provided then pass": {
30+
input: []string{"--in-cluster-config"},
31+
expError: nil,
32+
},
33+
34+
"if only in cluster config provided but set to false and other client flags added then pass": {
35+
input: []string{"--in-cluster-config=false", "--context=foo", "--namespace=bla"},
36+
expError: nil,
37+
},
38+
39+
"if both in cluster config and other client flags provided then error": {
40+
input: []string{"--in-cluster-config",
41+
"--client-certificate=foo", "--client-key=bla"},
42+
expError: errors.New("if --in-cluster-config is enabled, no other client flag options my be specified"),
43+
},
44+
} {
45+
t.Run(name, func(t *testing.T) {
46+
c := NewClientExtraFlags()
47+
cmd := new(cobra.Command)
48+
c.AddFlags(cmd.Flags())
49+
50+
if err := cmd.ParseFlags(test.input); err != nil {
51+
t.Error(err)
52+
t.FailNow()
53+
}
54+
55+
err := c.Validate(cmd)
56+
57+
if test.expError == nil {
58+
if err != nil {
59+
t.Errorf("expected error to be nil, got=%s", err)
60+
}
61+
62+
} else {
63+
if err == nil || test.expError.Error() != err.Error() {
64+
t.Errorf("unexpected error, exp=%s got=%s",
65+
test.expError, err)
66+
}
67+
}
68+
})
69+
}
70+
}

cmd/run.go

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
apiserveroptions "k8s.io/apiserver/pkg/server/options"
1515
"k8s.io/apiserver/pkg/util/term"
1616
"k8s.io/apiserver/plugin/pkg/authenticator/token/oidc"
17-
"k8s.io/cli-runtime/pkg/genericclioptions"
1817
"k8s.io/client-go/rest"
1918
cliflag "k8s.io/component-base/cli/flag"
2019
"k8s.io/component-base/cli/globalflag"
@@ -47,7 +46,7 @@ func NewRunCommand(stopCh <-chan struct{}) *cobra.Command {
4746

4847
kopOptions := new(options.KubeOIDCProxyOptions)
4948

50-
clientConfigFlags := genericclioptions.NewConfigFlags(true)
49+
clientConfigOptions := options.NewClientExtraFlags()
5150

5251
healthCheck := probe.New(strconv.Itoa(readinessProbePort))
5352

@@ -56,6 +55,8 @@ func NewRunCommand(stopCh <-chan struct{}) *cobra.Command {
5655
Use: "kube-oidc-proxy",
5756
Long: "kube-oidc-proxy is a reverse proxy to authenticate users to Kubernetes API servers with Open ID Connect Authentication.",
5857
RunE: func(cmd *cobra.Command, args []string) error {
58+
var err error
59+
5960
if cmd.Flag("version").Value.String() == "true" {
6061
version.PrintVersionAndExit()
6162
}
@@ -64,16 +65,26 @@ func NewRunCommand(stopCh <-chan struct{}) *cobra.Command {
6465
return err
6566
}
6667

68+
if err := clientConfigOptions.Validate(cmd); err != nil {
69+
return err
70+
}
71+
6772
if ssoptionsWithLB.SecureServingOptions.BindPort == readinessProbePort {
6873
return errors.New("unable to securely serve on port 8080, used by readiness prob")
6974
}
7075

71-
// client rest config
72-
restConfig, err := rest.InClusterConfig()
73-
if err != nil {
76+
var restConfig *rest.Config
77+
if clientConfigOptions.InClusterConfig {
78+
// In cluster config
79+
restConfig, err = rest.InClusterConfig()
80+
if err != nil {
81+
return err
82+
}
83+
84+
} else {
7485

75-
// fall back to cli flags if in cluster fails
76-
restConfig, err = clientConfigFlags.ToRESTConfig()
86+
// CLI flags if in cluster fails
87+
restConfig, err = clientConfigOptions.ToRESTConfig()
7788
if err != nil {
7889
return err
7990
}
@@ -147,10 +158,10 @@ func NewRunCommand(stopCh <-chan struct{}) *cobra.Command {
147158

148159
ssoptionsWithLB.AddFlags(namedFlagSets.FlagSet("secure serving"))
149160

150-
clientConfigFlags.CacheDir = nil
151-
clientConfigFlags.Impersonate = nil
152-
clientConfigFlags.ImpersonateGroup = nil
153-
clientConfigFlags.AddFlags(namedFlagSets.FlagSet("client"))
161+
clientConfigOptions.CacheDir = nil
162+
clientConfigOptions.Impersonate = nil
163+
clientConfigOptions.ImpersonateGroup = nil
164+
clientConfigOptions.AddFlags(namedFlagSets.FlagSet("client"))
154165

155166
globalflag.AddGlobalFlags(namedFlagSets.FlagSet("misc"), cmd.Name())
156167
namedFlagSets.FlagSet("misc").Bool("version",

demo/manifests/components/kube-oidc-proxy.jsonnet

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ local READINESS_PORT = 8080;
111111
command: [
112112
'kube-oidc-proxy',
113113
'--secure-port=' + $.config.secureServing.port,
114+
'--in-cluster-config',
114115
'--tls-cert-file=' + $.config.secureServing.tlsCertFile,
115116
'--tls-private-key-file=' + $.config.secureServing.tlsKeyFile,
116117
'--oidc-groups-prefix=' + $.config.oidc.groupsPrefix,

demo/yaml/kube-oidc-proxy.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ spec:
4343
command: ["kube-oidc-proxy"]
4444
args:
4545
- "--secure-port=443"
46+
- "--in-cluster-config"
4647
- "--tls-cert-file=/etc/oidc/tls/crt.pem"
4748
- "--tls-private-key-file=/etc/oidc/tls/key.pem"
4849
- "--oidc-client-id=$(OIDC_CLIENT_ID)"

deploy/charts/kube-oidc-proxy/templates/deployment.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ spec:
3535
periodSeconds: 10
3636
command: ["kube-oidc-proxy"]
3737
args:
38+
- "--in-cluster-config"
3839
- "--secure-port=443"
3940
- "--tls-cert-file=/etc/oidc/tls/crt.pem"
4041
- "--tls-private-key-file=/etc/oidc/tls/key.pem"

0 commit comments

Comments
 (0)