Skip to content

Commit 930cee6

Browse files
authored
Merge pull request #195 from ccremer/auto-detect-k8s
Auto-detect Kubernetes API URL for web UI
2 parents 512ba8a + eb1754d commit 930cee6

File tree

4 files changed

+71
-6
lines changed

4 files changed

+71
-6
lines changed

go.mod

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ require (
77
github.com/go-logr/logr v1.2.3
88
github.com/go-logr/zapr v1.2.3
99
github.com/labstack/echo/v4 v4.9.0
10+
github.com/lestrrat-go/jwx/v2 v2.0.6
1011
github.com/stretchr/testify v1.8.0
1112
github.com/urfave/cli/v2 v2.11.1
1213
go.uber.org/zap v1.23.0
@@ -25,6 +26,7 @@ require (
2526
github.com/cespare/xxhash/v2 v2.1.2 // indirect
2627
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
2728
github.com/davecgh/go-spew v1.1.1 // indirect
29+
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.1.0 // indirect
2830
github.com/emicklei/go-restful/v3 v3.8.0 // indirect
2931
github.com/evanphx/json-patch/v5 v5.6.0 // indirect
3032
github.com/fatih/color v1.12.0 // indirect
@@ -33,6 +35,7 @@ require (
3335
github.com/go-openapi/jsonreference v0.19.5 // indirect
3436
github.com/go-openapi/swag v0.19.14 // indirect
3537
github.com/gobuffalo/flect v0.2.5 // indirect
38+
github.com/goccy/go-json v0.9.11 // indirect
3639
github.com/gogo/protobuf v1.3.2 // indirect
3740
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
3841
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
@@ -46,6 +49,11 @@ require (
4649
github.com/josharian/intern v1.0.0 // indirect
4750
github.com/json-iterator/go v1.1.12 // indirect
4851
github.com/labstack/gommon v0.3.1 // indirect
52+
github.com/lestrrat-go/blackmagic v1.0.1 // indirect
53+
github.com/lestrrat-go/httpcc v1.0.1 // indirect
54+
github.com/lestrrat-go/httprc v1.0.4 // indirect
55+
github.com/lestrrat-go/iter v1.0.2 // indirect
56+
github.com/lestrrat-go/option v1.0.0 // indirect
4957
github.com/mailru/easyjson v0.7.6 // indirect
5058
github.com/mattn/go-colorable v0.1.11 // indirect
5159
github.com/mattn/go-isatty v0.0.14 // indirect
@@ -67,7 +75,7 @@ require (
6775
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
6876
go.uber.org/atomic v1.7.0 // indirect
6977
go.uber.org/multierr v1.6.0 // indirect
70-
golang.org/x/crypto v0.0.0-20220315160706-3147a52a75dd // indirect
78+
golang.org/x/crypto v0.0.0-20220427172511-eb4f295cb31f // indirect
7179
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect
7280
golang.org/x/net v0.0.0-20220722155237-a158d28d115b // indirect
7381
golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8 // indirect

go.sum

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3
6666
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
6767
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
6868
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
69+
github.com/decred/dcrd/crypto/blake256 v1.0.0/go.mod h1:sQl2p6Y26YV+ZOcSTP6thNdn47hh8kt6rqSlvmrXFAc=
70+
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.1.0 h1:HbphB4TFFXpv7MNrT52FGrrgVXF1owhMVTHFZIlnvd4=
71+
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.1.0/go.mod h1:DZGJHZMqrU4JJqFAWUS2UO1+lbSKsdiOoYi9Zzey7Fc=
6972
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815/go.mod h1:WwZ+bS3ebgob9U8Nd0kOddGdZWjyMGR8Wziv+TBNwSE=
7073
github.com/emicklei/go-restful/v3 v3.8.0 h1:eCZ8ulSerjdAiaNpF7GxXIE7ZCMo1moN1qX+S609eVw=
7174
github.com/emicklei/go-restful/v3 v3.8.0/go.mod h1:6n3XBCmQQb25CM2LCACGz8ukIrRry+4bhvbpWn3mrbc=
@@ -108,6 +111,8 @@ github.com/go-openapi/swag v0.19.14/go.mod h1:QYRuS/SOXUCsnplDa677K7+DxSOj6IPNl/
108111
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
109112
github.com/gobuffalo/flect v0.2.5 h1:H6vvsv2an0lalEaCDRThvtBfmg44W/QHXBCYUXf/6S4=
110113
github.com/gobuffalo/flect v0.2.5/go.mod h1:1ZyCLIbg0YD7sDkzvFdPoOydPtD8y9JQnrOROolUcM8=
114+
github.com/goccy/go-json v0.9.11 h1:/pAaQDLHEoCq/5FFmSKBswWmK6H0e8g4159Kc/X/nqk=
115+
github.com/goccy/go-json v0.9.11/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
111116
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
112117
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
113118
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
@@ -210,6 +215,18 @@ github.com/labstack/echo/v4 v4.9.0 h1:wPOF1CE6gvt/kmbMR4dGzWvHMPT+sAEUJOwOTtvITV
210215
github.com/labstack/echo/v4 v4.9.0/go.mod h1:xkCDAdFCIf8jsFQ5NnbK7oqaF/yU1A1X20Ltm0OvSks=
211216
github.com/labstack/gommon v0.3.1 h1:OomWaJXm7xR6L1HmEtGyQf26TEn7V6X88mktX9kee9o=
212217
github.com/labstack/gommon v0.3.1/go.mod h1:uW6kP17uPlLJsD3ijUYn3/M5bAxtlZhMI6m3MFxTMTM=
218+
github.com/lestrrat-go/blackmagic v1.0.1 h1:lS5Zts+5HIC/8og6cGHb0uCcNCa3OUt1ygh3Qz2Fe80=
219+
github.com/lestrrat-go/blackmagic v1.0.1/go.mod h1:UrEqBzIR2U6CnzVyUtfM6oZNMt/7O7Vohk2J0OGSAtU=
220+
github.com/lestrrat-go/httpcc v1.0.1 h1:ydWCStUeJLkpYyjLDHihupbn2tYmZ7m22BGkcvZZrIE=
221+
github.com/lestrrat-go/httpcc v1.0.1/go.mod h1:qiltp3Mt56+55GPVCbTdM9MlqhvzyuL6W/NMDA8vA5E=
222+
github.com/lestrrat-go/httprc v1.0.4 h1:bAZymwoZQb+Oq8MEbyipag7iSq6YIga8Wj6GOiJGdI8=
223+
github.com/lestrrat-go/httprc v1.0.4/go.mod h1:mwwz3JMTPBjHUkkDv/IGJ39aALInZLrhBp0X7KGUZlo=
224+
github.com/lestrrat-go/iter v1.0.2 h1:gMXo1q4c2pHmC3dn8LzRhJfP1ceCbgSiT9lUydIzltI=
225+
github.com/lestrrat-go/iter v1.0.2/go.mod h1:Momfcq3AnRlRjI5b5O8/G5/BvpzrhoFTZcn06fEOPt4=
226+
github.com/lestrrat-go/jwx/v2 v2.0.6 h1:RlyYNLV892Ed7+FTfj1ROoF6x7WxL965PGTHso/60G0=
227+
github.com/lestrrat-go/jwx/v2 v2.0.6/go.mod h1:aVrGuwEr3cp2Prw6TtQvr8sQxe+84gruID5C9TxT64Q=
228+
github.com/lestrrat-go/option v1.0.0 h1:WqAWL8kh8VcSoD6xjSH34/1m8yxluXQbDeKNfvFeEO4=
229+
github.com/lestrrat-go/option v1.0.0/go.mod h1:5ZHFbivi4xwXxhxY9XHDe2FHo6/Z7WWmtT7T5nBBp3I=
213230
github.com/mailru/easyjson v0.0.0-20190614124828-94de47d64c63/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
214231
github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
215232
github.com/mailru/easyjson v0.7.6 h1:8yTIVnZgCoiM1TgqoeTl+LfU5Jg6/xL3QhGQnimLYnA=
@@ -324,8 +341,8 @@ golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8U
324341
golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
325342
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
326343
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
327-
golang.org/x/crypto v0.0.0-20220315160706-3147a52a75dd h1:XcWmESyNjXJMLahc3mqVQJcgSTDxFxhETVlfk9uGc38=
328-
golang.org/x/crypto v0.0.0-20220315160706-3147a52a75dd/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
344+
golang.org/x/crypto v0.0.0-20220427172511-eb4f295cb31f h1:OeJjE6G4dgCY4PIXvIRQbE8+RX+uXZyGhUy/ksMGJoc=
345+
golang.org/x/crypto v0.0.0-20220427172511-eb4f295cb31f/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
329346
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
330347
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
331348
golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8=
@@ -389,6 +406,7 @@ golang.org/x/net v0.0.0-20200707034311-ab3426394381/go.mod h1:/O7V0waA8r7cgGh81R
389406
golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
390407
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
391408
golang.org/x/net v0.0.0-20210525063256-abc453219eb5/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
409+
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
392410
golang.org/x/net v0.0.0-20220722155237-a158d28d115b h1:PxfKdU9lEEDYjdIzOtC4qFWgkU2rGHdKlKowJSMN9h0=
393411
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
394412
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
@@ -446,6 +464,7 @@ golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7w
446464
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
447465
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
448466
golang.org/x/sys v0.0.0-20210603081109-ebe580a85c40/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
467+
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
449468
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
450469
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
451470
golang.org/x/sys v0.0.0-20211103235746-7861aae1554b/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

pkg/webui/server.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ func (c *Command) setupRoutes(ctx *commandContext) error {
7777
}
7878

7979
func (c *Command) setupProxy(ctx *commandContext) error {
80+
c.Log.Info("Setting up proxy", "url", c.ApiURL, "skip_tls_verify", c.ApiTLSSkipVerify)
8081
u, err := url.Parse(c.ApiURL)
8182
if err != nil {
8283
return err

webui_command.go

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,66 @@
11
package main
22

33
import (
4+
"os"
5+
46
"github.com/ccremer/clustercode/pkg/webui"
7+
"github.com/lestrrat-go/jwx/v2/jwt"
58
"github.com/urfave/cli/v2"
69
)
710

11+
const apiUrlFlag = "api-url"
12+
813
func newWebuiCommand() *cli.Command {
914
command := &webui.Command{}
1015
return &cli.Command{
1116
Name: "webui",
1217
Usage: "Start clustercode frontend web server",
13-
Before: LogMetadata,
18+
Before: discoverKubernetesAPI,
1419
Action: func(ctx *cli.Context) error {
1520
command.Log = AppLogger(ctx).WithName(ctx.Command.Name)
1621
return command.Execute(ctx.Context)
1722
},
1823
Flags: []cli.Flag{
19-
&cli.StringFlag{Name: "api-url", EnvVars: envVars("API_URL"),
20-
Usage: "Full base URL of the Kubernetes API server that is being proxied. If empty, the proxy is disabled.",
24+
&cli.StringFlag{Name: apiUrlFlag, EnvVars: envVars("API_URL"),
25+
Usage: "Full base URL of the Kubernetes API server that is being proxied. If empty, the proxy is disabled. If set to 'auto', it will try to discover it using the service account token.",
26+
Value: "auto",
2127
Destination: &command.ApiURL,
2228
},
2329
&cli.BoolFlag{Name: "api-tls-skip-verify", EnvVars: envVars("API_TLS_SKIP_VERIFY"),
2430
Usage: "Whether the certificate verification of the Kubernetes API server should be verified",
2531
Destination: &command.ApiTLSSkipVerify,
2632
},
33+
&cli.PathFlag{Name: "sa-token-path", EnvVars: envVars("API_SA_TOKEN_PATH"),
34+
Usage: "Path to the Kubernetes Service Account token secret for auto-discovery",
35+
Value: "/var/run/secrets/kubernetes.io/serviceaccount/token",
36+
},
2737
},
2838
}
2939
}
40+
41+
func discoverKubernetesAPI(ctx *cli.Context) error {
42+
_ = LogMetadata(ctx)
43+
log := AppLogger(ctx).WithName(ctx.Command.Name)
44+
45+
if ctx.String(apiUrlFlag) != "auto" {
46+
return nil
47+
}
48+
49+
path := ctx.String("sa-token-path")
50+
raw, err := os.ReadFile(path)
51+
if err != nil {
52+
log.Info("Cannot read the token", "error", err.Error())
53+
return ctx.Set(apiUrlFlag, "")
54+
}
55+
token, err := jwt.Parse(raw, jwt.WithVerify(false))
56+
if err != nil {
57+
log.Info("Cannot parse the token", "error", err.Error())
58+
return ctx.Set(apiUrlFlag, "")
59+
}
60+
aud := token.Audience()
61+
if len(aud) > 0 {
62+
log.Info("Discovered Kubernetes API URL", "url", aud[0])
63+
return ctx.Set(apiUrlFlag, aud[0])
64+
}
65+
return nil
66+
}

0 commit comments

Comments
 (0)