Skip to content

Commit

Permalink
cmd/contour: add -c config-path flag
Browse files Browse the repository at this point in the history
Fixes #1130

This PR adds the ability to supply configuration values to contour serve
from a configuration file. Some gymnastics are required to make this
work with kingpin and keep the precidence of config file -> env vars ->
cli flags.

At the moment only a few values are plumbed into the config file, mainly
to keep the linter's happy. More will be added in 0.15.

Signed-off-by: Dave Cheney <dave@cheney.net>
  • Loading branch information
davecheney committed Jul 19, 2019
1 parent 17ff3c2 commit 5e6249a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
4 changes: 4 additions & 0 deletions cmd/contour/contour.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ func main() {
stream := client.RouteStream()
watchstream(stream, cache.SecretType, resources)
case serve.FullCommand():
// parse args a second time so cli flags are applied
// on top of any values sourced from -c's config file.
_, err := app.Parse(args)
check(err)
log.Infof("args: %v", args)
doServe(log, serveCtx)
default:
Expand Down
33 changes: 32 additions & 1 deletion cmd/contour/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"crypto/rand"
"crypto/tls"
"crypto/x509"
"encoding/json"
"io/ioutil"
"log"
"net"
Expand Down Expand Up @@ -44,8 +45,38 @@ import (
// registerServe registers the serve subcommand and flags
// with the Application provided.
func registerServe(app *kingpin.Application) (*kingpin.CmdClause, *serveContext) {
var ctx serveContext
serve := app.Command("serve", "Serve xDS API traffic")

// The precedence of configuration for contour serve is as follows:
// config file, overridden by env vars, overridden by cli flags.
// however, as -c is a cli flag, we don't know its valye til cli flags
// have been parsed. To correct this ordering we assign a post parse
// action to -c, then parse cli flags twice (see main.main). On the second
// parse our action will return early, resulting in the precendence order
// we want.
var (
configFile string
parsed bool
ctx serveContext
)

parseConfig := func(_ *kingpin.ParseContext) error {
if parsed || configFile == "" {
// if there is no config file supplied, or we've
// already parsed it, return immediately.
return nil
}
f, err := os.Open(configFile)
if err != nil {
return err
}
defer f.Close()
dec := json.NewDecoder(f)
parsed = true
return dec.Decode(&ctx)
}
serve.Flag("config-path", "path to base configuration").Short('c').Action(parseConfig).ExistingFileVar(&configFile)

serve.Flag("incluster", "use in cluster configuration.").BoolVar(&ctx.inCluster)
serve.Flag("kubeconfig", "path to kubeconfig (if not in running inside a cluster)").Default(filepath.Join(os.Getenv("HOME"), ".kube", "config")).StringVar(&ctx.kubeconfig)

Expand Down

0 comments on commit 5e6249a

Please sign in to comment.