diff --git a/README.md b/README.md index bdabacf7..3b6a54eb 100644 --- a/README.md +++ b/README.md @@ -103,6 +103,7 @@ spec: - you may override the secret annotation with the `--secret-annotation` flag - you may want to prevent watching certain namespaces with the `--namespaces-to-ignore` flag - you may want to prevent watching certain resources with the `--resources-to-ignore` flag +- you can configure logging in JSON format with the `--log-format=json` option ## Deploying to Kubernetes @@ -180,6 +181,8 @@ Reloader can be configured to ignore the resources `secrets` and `configmaps` by `Note`: At one time only one of these resource can be ignored, trying to do it will cause error in helm template compilation. +You can also set the log format of Reloader to json by setting `logFormat` to `json` in values.yaml and apply the chart + ## Help ### Documentation diff --git a/deployments/kubernetes/chart/reloader/templates/deployment.yaml b/deployments/kubernetes/chart/reloader/templates/deployment.yaml index c03a86f5..e56c9783 100644 --- a/deployments/kubernetes/chart/reloader/templates/deployment.yaml +++ b/deployments/kubernetes/chart/reloader/templates/deployment.yaml @@ -92,6 +92,9 @@ spec: name: tmp-volume {{- end }} args: + {{- if .Values.reloader.logFormat }} + - "--log-format={{ .Values.reloader.logFormat }}" + {{- end }} {{- if .Values.reloader.ignoreSecrets }} - "--resources-to-ignore=secrets" {{- end }} diff --git a/deployments/kubernetes/chart/reloader/values.yaml b/deployments/kubernetes/chart/reloader/values.yaml index 704ad4f5..0724ae6c 100644 --- a/deployments/kubernetes/chart/reloader/values.yaml +++ b/deployments/kubernetes/chart/reloader/values.yaml @@ -12,6 +12,7 @@ reloader: isOpenshift: false ignoreSecrets: false ignoreConfigMaps: false + logFormat: "" #json watchGlobally: true # Set to true if you have a pod security policy that enforces readOnlyRootFilesystem readOnlyRootFileSystem: false diff --git a/deployments/kubernetes/templates/chart/values.yaml.tmpl b/deployments/kubernetes/templates/chart/values.yaml.tmpl index d6d74a99..42fac169 100644 --- a/deployments/kubernetes/templates/chart/values.yaml.tmpl +++ b/deployments/kubernetes/templates/chart/values.yaml.tmpl @@ -12,6 +12,7 @@ reloader: isOpenshift: false ignoreSecrets: false ignoreConfigMaps: false + logFormat: "" #json watchGlobally: true # Set to true if you have a pod security policy that enforces readOnlyRootFilesystem readOnlyRootFileSystem: false diff --git a/internal/pkg/cmd/reloader.go b/internal/pkg/cmd/reloader.go index b28ab81c..cee9312c 100644 --- a/internal/pkg/cmd/reloader.go +++ b/internal/pkg/cmd/reloader.go @@ -26,12 +26,31 @@ func NewReloaderCommand() *cobra.Command { cmd.PersistentFlags().StringVar(&options.ConfigmapUpdateOnChangeAnnotation, "configmap-annotation", "configmap.reloader.stakater.com/reload", "annotation to detect changes in configmaps") cmd.PersistentFlags().StringVar(&options.SecretUpdateOnChangeAnnotation, "secret-annotation", "secret.reloader.stakater.com/reload", "annotation to detect changes in secrets") cmd.PersistentFlags().StringVar(&options.ReloaderAutoAnnotation, "auto-annotation", "reloader.stakater.com/auto", "annotation to detect changes in secrets") + cmd.PersistentFlags().StringVar(&options.LogFormat, "log-format", "", "Log format to use (empty string for text, or JSON") cmd.PersistentFlags().StringSlice("resources-to-ignore", []string{}, "list of resources to ignore (valid options 'configMaps' or 'secrets')") cmd.PersistentFlags().StringSlice("namespaces-to-ignore", []string{}, "list of namespaces to ignore") return cmd } +func configureLogging(logFormat string) error { + switch logFormat { + case "json": + logrus.SetFormatter(&logrus.JSONFormatter{}) + default: + // just let the library use default on empty string. + if logFormat != "" { + return fmt.Errorf("unsupported logging formatter: %q", logFormat) + } + } + return nil +} + func startReloader(cmd *cobra.Command, args []string) { + err := configureLogging(options.LogFormat) + if err != nil { + logrus.Warn(err) + } + logrus.Info("Starting Reloader") currentNamespace := os.Getenv("KUBERNETES_NAMESPACE") if len(currentNamespace) == 0 { diff --git a/internal/pkg/options/flags.go b/internal/pkg/options/flags.go index f150a11a..41c2daba 100644 --- a/internal/pkg/options/flags.go +++ b/internal/pkg/options/flags.go @@ -7,4 +7,6 @@ var ( SecretUpdateOnChangeAnnotation = "secret.reloader.stakater.com/reload" // ReloaderAutoAnnotation is an annotation to detect changes in secrets ReloaderAutoAnnotation = "reloader.stakater.com/auto" + // LogFormat is the log format to use (json, or empty string for default) + LogFormat = "" )