Skip to content

Commit

Permalink
Merge pull request #354 from erikgb/slog
Browse files Browse the repository at this point in the history
Add support for JSON logging format
  • Loading branch information
cert-manager-prow[bot] authored May 21, 2024
2 parents e943b75 + 9542fa1 commit b2a6689
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 10 deletions.
77 changes: 67 additions & 10 deletions cmd/trust-manager/app/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ limitations under the License.
package options

import (
"flag"
"errors"
"fmt"
"log/slog"
"os"

"github.com/go-logr/logr"
"github.com/spf13/cobra"
Expand All @@ -28,14 +30,12 @@ import (
"k8s.io/client-go/rest"
cliflag "k8s.io/component-base/cli/flag"
"k8s.io/klog/v2"
"k8s.io/klog/v2/klogr"

"github.com/cert-manager/trust-manager/pkg/bundle"
)

// Options is a struct to hold options for trust-manager
type Options struct {
logLevel string
kubeConfigFlags *genericclioptions.ConfigFlags

// ReadyzPort if the port used to expose Prometheus metrics.
Expand All @@ -59,6 +59,45 @@ type Options struct {

// Bundle are options specific to the Bundle controller.
Bundle bundle.Options

// log are options controlling logging
log logOptions
}

type logOptions struct {
format logFormat
level int
}

const (
logFormatText logFormat = "text"
logFormatJSON logFormat = "json"
)

type logFormat string

// String is used both by fmt.Print and by Cobra in help text
func (e *logFormat) String() string {
if len(*e) == 0 {
return string(logFormatText)
}
return string(*e)
}

// Set must have pointer receiver to avoid changing the value of a copy
func (e *logFormat) Set(v string) error {
switch v {
case "text", "json":
*e = logFormat(v)
return nil
default:
return errors.New(`must be one of "text" or "json"`)
}
}

// Type is only used in help text
func (e *logFormat) Type() string {
return "string"
}

// Webhook holds options specific to running the trust Webhook service.
Expand All @@ -82,9 +121,20 @@ func (o *Options) Prepare(cmd *cobra.Command) *Options {
// Complete will populate the remaining Options from the CLI flags. Must be run
// before consuming Options.
func (o *Options) Complete() error {
klog.InitFlags(nil)
log := klogr.New()
flag.Set("v", o.logLevel)
opts := &slog.HandlerOptions{
// To avoid a breaking change in application configuration,
// we negate the (configured) logr verbosity level to get the corresponding slog level
Level: slog.Level(-o.log.level),
}
var handler slog.Handler = slog.NewTextHandler(os.Stdout, opts)
if o.log.format == logFormatJSON {
handler = slog.NewJSONHandler(os.Stdout, opts)
}

slog.SetDefault(slog.New(handler))

log := logr.FromSlogHandler(handler)
klog.SetLogger(log)
o.Logr = log.WithName("trust")

var err error
Expand All @@ -104,6 +154,7 @@ func (o *Options) addFlags(cmd *cobra.Command) {

o.addAppFlags(nfs.FlagSet("App"))
o.addBundleFlags(nfs.FlagSet("Bundle"))
o.addLoggingFlags(nfs.FlagSet("Logging"))
o.addWebhookFlags(nfs.FlagSet("Webhook"))
o.kubeConfigFlags = genericclioptions.NewConfigFlags(true)
o.kubeConfigFlags.AddFlags(nfs.FlagSet("Kubernetes"))
Expand All @@ -127,10 +178,6 @@ func (o *Options) addFlags(cmd *cobra.Command) {
}

func (o *Options) addAppFlags(fs *pflag.FlagSet) {
fs.StringVarP(&o.logLevel,
"log-level", "v", "1",
"Log level (1-5).")

fs.IntVar(&o.ReadyzPort,
"readiness-probe-port", 6060,
"Port to expose the readiness probe.")
Expand Down Expand Up @@ -162,6 +209,16 @@ func (o *Options) addBundleFlags(fs *pflag.FlagSet) {
"Filter expired certificates from the bundle.")
}

func (o *Options) addLoggingFlags(fs *pflag.FlagSet) {
fs.Var(&o.log.format,
"log-format",
"Log format (text or json)")

fs.IntVarP(&o.log.level,
"log-level", "v", 1,
"Log level (1-5).")
}

func (o *Options) addWebhookFlags(fs *pflag.FlagSet) {
fs.StringVar(&o.Webhook.Host,
"webhook-host", "0.0.0.0",
Expand Down
7 changes: 7 additions & 0 deletions deploy/charts/trust-manager/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,13 @@ topologySpreadConstraints:
> ```
Whether to filter expired certificates from the trust bundle.
#### **app.logFormat** ~ `string`
> Default value:
> ```yaml
> text
> ```
The format of trust-manager logging. Accepted values are text or json.
#### **app.logLevel** ~ `number`
> Default value:
> ```yaml
Expand Down
1 change: 1 addition & 0 deletions deploy/charts/trust-manager/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ spec:
initialDelaySeconds: 3
periodSeconds: 7
args:
- "--log-format={{.Values.app.logFormat}}"
- "--log-level={{.Values.app.logLevel}}"
- "--metrics-port={{.Values.app.metrics.port}}"
- "--readiness-probe-port={{.Values.app.readinessProbe.port}}"
Expand Down
8 changes: 8 additions & 0 deletions deploy/charts/trust-manager/values.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@
"helm-values.app": {
"additionalProperties": false,
"properties": {
"logFormat": {
"$ref": "#/$defs/helm-values.app.logFormat"
},
"logLevel": {
"$ref": "#/$defs/helm-values.app.logLevel"
},
Expand Down Expand Up @@ -101,6 +104,11 @@
},
"type": "object"
},
"helm-values.app.logFormat": {
"default": "text",
"description": "The format of trust-manager logging. Accepted values are text or json.",
"type": "string"
},
"helm-values.app.logLevel": {
"default": 1,
"description": "The verbosity of trust-manager logging. This takes a value from 1-5, with the higher value being more verbose.",
Expand Down
3 changes: 3 additions & 0 deletions deploy/charts/trust-manager/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,9 @@ filterExpiredCertificates:
enabled: false

app:
# The format of trust-manager logging. Accepted values are text or json.
logFormat: text

# The verbosity of trust-manager logging. This takes a value from 1-5, with the higher value being more verbose.
logLevel: 1

Expand Down

0 comments on commit b2a6689

Please sign in to comment.