-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #195 from ccremer/auto-detect-k8s
Auto-detect Kubernetes API URL for web UI
- Loading branch information
Showing
4 changed files
with
71 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,66 @@ | ||
package main | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/ccremer/clustercode/pkg/webui" | ||
"github.com/lestrrat-go/jwx/v2/jwt" | ||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
const apiUrlFlag = "api-url" | ||
|
||
func newWebuiCommand() *cli.Command { | ||
command := &webui.Command{} | ||
return &cli.Command{ | ||
Name: "webui", | ||
Usage: "Start clustercode frontend web server", | ||
Before: LogMetadata, | ||
Before: discoverKubernetesAPI, | ||
Action: func(ctx *cli.Context) error { | ||
command.Log = AppLogger(ctx).WithName(ctx.Command.Name) | ||
return command.Execute(ctx.Context) | ||
}, | ||
Flags: []cli.Flag{ | ||
&cli.StringFlag{Name: "api-url", EnvVars: envVars("API_URL"), | ||
Usage: "Full base URL of the Kubernetes API server that is being proxied. If empty, the proxy is disabled.", | ||
&cli.StringFlag{Name: apiUrlFlag, EnvVars: envVars("API_URL"), | ||
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.", | ||
Value: "auto", | ||
Destination: &command.ApiURL, | ||
}, | ||
&cli.BoolFlag{Name: "api-tls-skip-verify", EnvVars: envVars("API_TLS_SKIP_VERIFY"), | ||
Usage: "Whether the certificate verification of the Kubernetes API server should be verified", | ||
Destination: &command.ApiTLSSkipVerify, | ||
}, | ||
&cli.PathFlag{Name: "sa-token-path", EnvVars: envVars("API_SA_TOKEN_PATH"), | ||
Usage: "Path to the Kubernetes Service Account token secret for auto-discovery", | ||
Value: "/var/run/secrets/kubernetes.io/serviceaccount/token", | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func discoverKubernetesAPI(ctx *cli.Context) error { | ||
_ = LogMetadata(ctx) | ||
log := AppLogger(ctx).WithName(ctx.Command.Name) | ||
|
||
if ctx.String(apiUrlFlag) != "auto" { | ||
return nil | ||
} | ||
|
||
path := ctx.String("sa-token-path") | ||
raw, err := os.ReadFile(path) | ||
if err != nil { | ||
log.Info("Cannot read the token", "error", err.Error()) | ||
return ctx.Set(apiUrlFlag, "") | ||
} | ||
token, err := jwt.Parse(raw, jwt.WithVerify(false)) | ||
if err != nil { | ||
log.Info("Cannot parse the token", "error", err.Error()) | ||
return ctx.Set(apiUrlFlag, "") | ||
} | ||
aud := token.Audience() | ||
if len(aud) > 0 { | ||
log.Info("Discovered Kubernetes API URL", "url", aud[0]) | ||
return ctx.Set(apiUrlFlag, aud[0]) | ||
} | ||
return nil | ||
} |