Skip to content

Commit 665b092

Browse files
committed
feat(provisioner): add support for warning log de-duplication
Signed-off-by: Niladri Halder <[email protected]>
1 parent ed84837 commit 665b092

File tree

5 files changed

+37
-0
lines changed

5 files changed

+37
-0
lines changed

cmd/provisioner-localpv/app/start.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package app
22

33
import (
44
"context"
5+
"github.com/openebs/dynamic-localpv-provisioner/pkg/logger"
6+
"k8s.io/client-go/rest"
57
"os"
68
"strings"
79

@@ -23,6 +25,7 @@ var (
2325
// localpv provisioner
2426
LeaderElectionKey = "LEADER_ELECTION_ENABLED"
2527
usage = cmdName
28+
dedupeWarnings bool
2629
)
2730

2831
// StartProvisioner will start a new dynamic Host Path PV provisioner
@@ -39,13 +42,24 @@ func StartProvisioner() (*cobra.Command, error) {
3942
},
4043
}
4144

45+
cmd.PersistentFlags().BoolVar(&dedupeWarnings, "dedupe-warnings", false, "De-duplicate warning messages")
46+
4247
return cmd, nil
4348
}
4449

4550
// Start will initialize and run the dynamic provisioner daemon
4651
func Start(cmd *cobra.Command) error {
4752
klog.Infof("Starting Provisioner...")
4853

54+
// De-duplicate warning messages, to avoid flooding logs.
55+
if dedupeWarnings {
56+
rest.SetDefaultWarningHandler(
57+
rest.NewWarningWriter(logger.KlogWarner{}, rest.WarningWriterOptions{
58+
Deduplicate: true,
59+
Color: false,
60+
}),
61+
)
62+
}
4963
// Dynamic Provisioner can run successfully if it can establish
5064
// connection to the Kubernetes Cluster. mKube helps with
5165
// establishing the connection either via InCluster or

cmd/provisioner-localpv/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ func run() error {
6767

6868
// Merge all flags from the Cobra Command to the global FlagSet
6969
// and Parse them
70+
pflag.CommandLine.AddFlagSet(cmd.PersistentFlags())
7071
pflag.CommandLine.AddFlagSet(cmd.Flags())
7172
pflag.Parse()
7273

deploy/helm/charts/templates/deployment.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,16 @@ spec:
5050
- name: {{ template "localpv.fullname" . }}
5151
image: "{{ with .Values.localpv.image.registry | default .Values.global.imageRegistry | trimSuffix "/" }}{{ . }}/{{ end }}{{ .Values.localpv.image.repository }}:{{ .Values.localpv.image.tag }}"
5252
imagePullPolicy: {{ .Values.localpv.image.pullPolicy }}
53+
{{- $args := list }}
54+
{{- if .Values.localpv.logging.dedupeWarnings }}
55+
{{- $args = append $args "--dedupe-warnings" }}
56+
{{- end }}
57+
{{- if gt (len $args) 0 }}
58+
args:
59+
{{- range $args }}
60+
- {{ . | quote }}
61+
{{- end }}
62+
{{- end }}
5363
resources:
5464
{{ toYaml .Values.localpv.resources | indent 10 }}
5565
env:

deploy/helm/charts/values.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ localpv:
4040
## Labels to be added to localpv provisioner deployment pods
4141
podLabels:
4242
name: openebs-localpv-provisioner
43+
logging:
44+
# Disable duplicate warning messages
45+
dedupeWarnings: false
4346
healthCheck:
4447
initialDelaySeconds: 30
4548
periodSeconds: 60

pkg/logger/logger.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,22 @@ var (
3535
loggerKillSwitch = make(chan struct{})
3636
)
3737

38+
// KlogWriter writes to Klog's Info stream.
3839
type KlogWriter struct{}
3940

4041
func (k KlogWriter) Write(data []byte) (n int, err error) {
4142
klog.Info(string(data))
4243
return len(data), nil
4344
}
4445

46+
// KlogWarner writes to Klog's Warning stream.
47+
type KlogWarner struct{}
48+
49+
func (w KlogWarner) Write(data []byte) (n int, err error) {
50+
klog.Warning(string(data))
51+
return len(data), nil
52+
}
53+
4554
// This needs to be set correctly to the default log flush duration
4655
// in case it is not equal to KLOG_FLUSH_INTERVAL.
4756
// This sets the default flush interval for logs

0 commit comments

Comments
 (0)