Skip to content

Commit

Permalink
Merge pull request #354 from seankhliao/json-logs
Browse files Browse the repository at this point in the history
add flag to set log-format
  • Loading branch information
cert-manager-prow[bot] authored Jul 22, 2024
2 parents cc9f32a + b461749 commit d53f5a2
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 9 deletions.
27 changes: 18 additions & 9 deletions cmd/app/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package options

import (
"flag"
"fmt"
"time"

Expand All @@ -27,6 +26,7 @@ import (
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/client-go/rest"
cliflag "k8s.io/component-base/cli/flag"
logsapi "k8s.io/component-base/logs/api/v1"
"k8s.io/klog/v2"

"github.com/cert-manager/istio-csr/pkg/certmanager"
Expand All @@ -38,7 +38,8 @@ import (

// Options is a struct to hold options for cert-manager-istio-csr
type Options struct {
logLevel string
logLevel uint32
logFormat string
kubeConfigFlags *genericclioptions.ConfigFlags

// ReadyzPort if the port used to expose Prometheus metrics.
Expand Down Expand Up @@ -84,11 +85,17 @@ func (o *Options) Prepare(cmd *cobra.Command) *Options {
}

func (o *Options) Complete() error {
logOpts := logsapi.NewLoggingConfiguration()
if o.logFormat != "" {
logOpts.Format = o.logFormat
}
logOpts.Verbosity = logsapi.VerbosityLevel(o.logLevel)
err := logsapi.ValidateAndApply(logOpts, nil)
if err != nil {
return fmt.Errorf("failed to set log config: %w", err)
}
klog.InitFlags(nil)
log := klog.TODO()
if err := flag.Set("v", o.logLevel); err != nil {
return fmt.Errorf("failed to set log level: %s", err)
}
o.Logr = log

// Ensure there is at least one DNS name to set in the serving certificate
Expand All @@ -97,7 +104,6 @@ func (o *Options) Complete() error {
return fmt.Errorf("the list of DNS names to add to the serving certificate is empty")
}

var err error
o.RestConfig, err = o.kubeConfigFlags.ToRESTConfig()
if err != nil {
return fmt.Errorf("failed to build kubernetes rest config: %s", err)
Expand Down Expand Up @@ -159,10 +165,14 @@ func (o *Options) addFlags(cmd *cobra.Command) {
}

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

fs.StringVar(&o.logFormat,
"log-format", "text",
"log output format: text|json")

fs.IntVar(&o.ReadyzPort,
"readiness-probe-port", 6060,
"Port to expose the readiness probe.")
Expand Down Expand Up @@ -235,7 +245,6 @@ func (o *Options) addCertManagerFlags(fs *pflag.FlagSet) {

fs.StringVar(&o.CertManager.IssuanceConfigMapNamespace, "runtime-issuance-config-map-namespace", "",
"Namespace for ConfigMap to be watched at runtime for issuer details")

}

func (o *Options) addAdditionalAnnotationsFlags(fs *pflag.FlagSet) {
Expand Down
7 changes: 7 additions & 0 deletions deploy/charts/istio-csr/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ Service nodePort to expose istio-csr gRPC service.
> ```
Verbosity of istio-csr logging.
#### **app.logFormat** ~ `string`
> Default value:
> ```yaml
> text
> ```
Output format of istio-csr logging.
#### **app.metrics.port** ~ `number`
> Default value:
> ```yaml
Expand Down
1 change: 1 addition & 0 deletions deploy/charts/istio-csr/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ spec:
periodSeconds: 7
args:
- "--log-level={{.Values.app.logLevel}}"
- "--log-format={{.Values.app.logFormat}}"
- "--metrics-port={{.Values.app.metrics.port}}"
- "--readiness-probe-port={{.Values.app.readinessProbe.port}}"
- "--readiness-probe-path={{.Values.app.readinessProbe.path}}"
Expand Down
8 changes: 8 additions & 0 deletions deploy/charts/istio-csr/values.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@
"istio": {
"$ref": "#/$defs/helm-values.app.istio"
},
"logFormat": {
"$ref": "#/$defs/helm-values.app.logFormat"
},
"logLevel": {
"$ref": "#/$defs/helm-values.app.logLevel"
},
Expand Down Expand Up @@ -197,6 +200,11 @@
"default": "default",
"type": "string"
},
"helm-values.app.logFormat": {
"default": "text",
"description": "Output format of istio-csr logging.",
"type": "string"
},
"helm-values.app.logLevel": {
"default": 1,
"description": "Verbosity of istio-csr logging.",
Expand Down
3 changes: 3 additions & 0 deletions deploy/charts/istio-csr/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ app:
# Verbosity of istio-csr logging.
logLevel: 1 # 1-5

# Output format of istio-csr logging.
logFormat: text # text or json

metrics:
# Port for exposing Prometheus metrics on 0.0.0.0 on path '/metrics'.
port: 9402
Expand Down

0 comments on commit d53f5a2

Please sign in to comment.