Skip to content

Commit c0fb25b

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

File tree

5 files changed

+38
-0
lines changed

5 files changed

+38
-0
lines changed

cmd/provisioner-localpv/app/start.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,11 @@ import (
1212
"github.com/openebs/maya/pkg/version"
1313
"github.com/pkg/errors"
1414
"github.com/spf13/cobra"
15+
"k8s.io/client-go/rest"
1516
"k8s.io/klog/v2"
1617
pvController "sigs.k8s.io/sig-storage-lib-external-provisioner/v9/controller"
18+
19+
"github.com/openebs/dynamic-localpv-provisioner/pkg/logger"
1720
)
1821

1922
var (
@@ -23,6 +26,7 @@ var (
2326
// localpv provisioner
2427
LeaderElectionKey = "LEADER_ELECTION_ENABLED"
2528
usage = cmdName
29+
dedupeWarnings bool
2630
)
2731

2832
// StartProvisioner will start a new dynamic Host Path PV provisioner
@@ -39,13 +43,24 @@ func StartProvisioner() (*cobra.Command, error) {
3943
},
4044
}
4145

46+
cmd.PersistentFlags().BoolVar(&dedupeWarnings, "dedupe-warnings", false, "De-duplicate warning messages")
47+
4248
return cmd, nil
4349
}
4450

4551
// Start will initialize and run the dynamic provisioner daemon
4652
func Start(cmd *cobra.Command) error {
4753
klog.Infof("Starting Provisioner...")
4854

55+
// De-duplicate warning messages, to avoid flooding logs.
56+
if dedupeWarnings {
57+
rest.SetDefaultWarningHandler(
58+
rest.NewWarningWriter(logger.KlogWarner{}, rest.WarningWriterOptions{
59+
Deduplicate: true,
60+
Color: false,
61+
}),
62+
)
63+
}
4964
// Dynamic Provisioner can run successfully if it can establish
5065
// connection to the Kubernetes Cluster. mKube helps with
5166
// 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)