Skip to content

Commit

Permalink
Make path to config file configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
nathancoleman committed Sep 24, 2024
1 parent 34bbb73 commit 316985a
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
1 change: 1 addition & 0 deletions charts/consul/templates/connect-inject-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ spec:
- "-ec"
- |
exec consul-k8s-control-plane inject-connect \
-config-file=/consul/config/config.json \
{{- if .Values.global.federation.enabled }}
-enable-federation \
{{- end }}
Expand Down
2 changes: 2 additions & 0 deletions control-plane/subcommand/inject-connect/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type Command struct {
flagListen string
flagCertDir string // Directory with TLS certs for listening (PEM)
flagDefaultInject bool // True to inject by default
flagConfigFile string // Path to a config file in JSON format
flagConsulImage string // Docker image for Consul
flagConsulDataplaneImage string // Docker image for Envoy
flagConsulK8sImage string // Docker image for consul-k8s
Expand Down Expand Up @@ -174,6 +175,7 @@ func init() {
func (c *Command) init() {
c.flagSet = flag.NewFlagSet("", flag.ContinueOnError)
c.flagSet.StringVar(&c.flagListen, "listen", ":8080", "Address to bind listener to.")
c.flagSet.StringVar(&c.flagConfigFile, "config-file", "", "Path to a JSON config file.")
c.flagSet.Var((*flags.FlagMapValue)(&c.flagNodeMeta), "node-meta",
"Metadata to set on the node, formatted as key=value. This flag may be specified multiple times to set multiple meta fields.")
c.flagSet.BoolVar(&c.flagDefaultInject, "default-inject", true, "Inject by default.")
Expand Down
14 changes: 8 additions & 6 deletions control-plane/subcommand/inject-connect/v1controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@ func (c *Command) configureControllers(ctx context.Context, mgr manager.Manager,
}

var cfgFile FileConfig
if file, err := os.ReadFile("/consul/config/config.json"); err != nil {
setupLog.Info("Failed to read config file, may not be present", "error", err)
} else {
if err := json.Unmarshal(file, &cfgFile); err != nil {
setupLog.Error(err, "Config file present but could not be deserialized, will use defaults")
if c.flagConfigFile != "" {
if file, err := os.ReadFile(c.flagConfigFile); err != nil {
setupLog.Info("Failed to read specified -config-file", "file", c.flagConfigFile, "error", err)
} else {
setupLog.Info("Config file present and deserialized", "config", cfgFile)
if err := json.Unmarshal(file, &cfgFile); err != nil {
setupLog.Error(err, "Config file present but could not be deserialized, will use defaults", "file", c.flagConfigFile)
} else {
setupLog.Info("Config file present and deserialized", "file", c.flagConfigFile, "config", cfgFile)
}
}
}

Expand Down

0 comments on commit 316985a

Please sign in to comment.