Skip to content

Commit 39ef433

Browse files
authored
Merge pull request #206 from Cryptophobia/master
feat(reloader): add custom --buffer-mount-folder option
2 parents 6bd31a3 + 4d3a77a commit 39ef433

File tree

10 files changed

+96
-51
lines changed

10 files changed

+96
-51
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,7 @@ Flags:
622622
--fluentd-rpc-port=24444 RPC port of Fluentd
623623
--log-level="info" Control verbosity of config-reloader logs
624624
--fluentd-loglevel="info" Control verbosity of fluentd logs
625+
--buffer-mount-folder="" Folder in /var/log/{} where to create all fluentd buffers
625626
--annotation="logging.csp.vmware.com/fluentd-configmap"
626627
Which annotation on the namespace stores the configmap name?
627628
--default-configmap="fluentd-config"
@@ -659,6 +660,7 @@ Flags:
659660
| `image.pullSecret` | Optional pull secret name | `""` |
660661
| `logLevel` | Default log level for config-reloader | `info` |
661662
| `fluentdLogLevel` | Default log level for fluentd | `info` |
663+
| `bufferMountFolder` | Folder in /var/log/{} where to create all fluentd buffers | `""` |
662664
| `kubeletRoot` | The home dir of the kubelet, usually set using `--root-dir` on the kubelet | `/var/lib/kubelet` |
663665
| `namespaces` | List of namespaces to operate on. Empty means all namespaces | `[]` |
664666
| `interval` | How often to check for config changes (seconds) | `45` |

charts/log-router/Chart.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
apiVersion: v1
55
description: Distribution of Fluentd as K8S daemonset
66
name: log-router
7-
version: 0.3.7
7+
version: 0.3.8
88
home: https://github.com/vmware/kube-fluentd-operator
99
sources:
1010
- https://github.com/vmware/kube-fluentd-operator

charts/log-router/templates/daemonset.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,11 @@ spec:
106106
- --interval={{ .Values.interval }}
107107
- --log-level={{ .Values.logLevel }}
108108
- --fluentd-loglevel={{ .Values.fluentdLogLevel }}
109+
{{- if not (empty .Values.bufferMountFolder) }}
110+
- --buffer-mount-folder={{ .Values.bufferMountFolder }}
111+
{{ else }}
112+
- --buffer-mount-folder=""
113+
{{- end }}
109114
- --output-dir=/fluentd/etc
110115
- --templates-dir=/templates
111116
- --id={{ template "fluentd-router.fullname" . }}

charts/log-router/values.yaml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,12 @@ image:
2929
tag: latest
3030
pullSecret: ""
3131

32-
3332
logLevel: debug
3433
fluentdLogLevel: debug
3534
interval: 45
3635
kubeletRoot: /var/lib/kubelet
36+
# bufferMountFolder -- a folder inside /var/log to write all fluentd buffers to
37+
bufferMountFolder: ""
3738

3839
# fullnameOverride -- String to fully override `fluentd-router.fullname` template.
3940
fullnameOverride: ""
@@ -121,10 +122,10 @@ serviceMonitor:
121122

122123
# Scrape interval. If not set, the Prometheus default scrape interval is used.
123124
interval: ""
124-
125+
125126
allowTagExpansion: false
126127

127128
# Change the following value to define a different namespace that is treated as admin
128129
# namespace, i.e. its configs are not validated or processed and virtual plugins can be
129130
# defined to be used in all other namespaces.
130-
adminNamespace: "kube-system"
131+
adminNamespace: "kube-system"

config-reloader/Makefile

Lines changed: 50 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -87,74 +87,77 @@ delete-test-ns:
8787
run-loop-fs: install
8888
rm -fr tmp
8989
config-reloader \
90-
--interval 5 \
91-
--log-level=debug \
92-
--output-dir=tmp \
93-
--meta-key=prefix \
94-
--meta-values=aws_region=us-west-2,csp_cluster=mon \
95-
--templates-dir=templates \
96-
--datasource=fs \
97-
--fs-dir=examples \
98-
--fluentd-binary "fluentd/fake-fluentd.sh -p /plugins"
90+
--interval 5 \
91+
--log-level=debug \
92+
--output-dir=tmp \
93+
--meta-key=prefix \
94+
--meta-values=aws_region=us-west-2,csp_cluster=mon \
95+
--templates-dir=templates \
96+
--datasource=fs \
97+
--fs-dir=examples \
98+
--fluentd-binary "fluentd/fake-fluentd.sh -p /plugins"
9999

100100
run-once-fs: install
101101
rm -fr tmp
102102
config-reloader \
103-
--interval 0 \
104-
--log-level=debug \
105-
--fluentd-loglevel=debug \
106-
--output-dir=tmp \
107-
--meta-key=prefix2 \
108-
--meta-values=aws_region=us-west-2,csp_cluster=mon \
109-
--templates-dir=templates \
110-
--datasource=fs \
111-
--fs-dir=examples \
112-
--fluentd-binary "fluentd/fake-fluentd.sh -p /plugins"
103+
--interval 0 \
104+
--log-level=debug \
105+
--fluentd-loglevel=debug \
106+
--buffer-mount-folder="" \
107+
--output-dir=tmp \
108+
--meta-key=prefix2 \
109+
--meta-values=aws_region=us-west-2,csp_cluster=mon \
110+
--templates-dir=templates \
111+
--datasource=fs \
112+
--fs-dir=examples \
113+
--fluentd-binary "fluentd/fake-fluentd.sh -p /plugins"
113114

114115
run-once: install
115116
rm -fr tmp
116117
config-reloader \
117-
--interval 0 \
118-
--log-level=debug \
119-
--fluentd-loglevel=debug \
120-
--output-dir=tmp \
121-
--templates-dir=templates \
122-
--meta-key=run-once \
123-
--meta-values=aws_region=us-west-2,csp_cluster=mon \
124-
--fluentd-binary "fluentd/fake-fluentd.sh -p /plugins"
118+
--interval 0 \
119+
--log-level=debug \
120+
--fluentd-loglevel=debug \
121+
--buffer-mount-folder="" \
122+
--output-dir=tmp \
123+
--templates-dir=templates \
124+
--meta-key=run-once \
125+
--meta-values=aws_region=us-west-2,csp_cluster=mon \
126+
--fluentd-binary "fluentd/fake-fluentd.sh -p /plugins"
125127

126128
run-loop: install
127129
rm -fr tmp
128130
config-reloader \
129-
--interval 5 \
130-
--log-level=debug \
131-
--fluentd-loglevel=debug \
132-
--output-dir=tmp \
133-
--meta-key=prefix3 \
134-
--meta-values=aws_region=us-west-2,csp_cluster=mon \
135-
--templates-dir=templates
131+
--interval 5 \
132+
--log-level=debug \
133+
--fluentd-loglevel=debug \
134+
--buffer-mount-folder="" \
135+
--output-dir=tmp \
136+
--meta-key=prefix3 \
137+
--meta-values=aws_region=us-west-2,csp_cluster=mon \
138+
--templates-dir=templates
136139

137140
run-fluentd:
138141
docker run --entrypoint=fluentd \
139-
-ti --rm -v `pwd`:/workspace --net=host \
140-
$(IMAGE):$(TAG) \
141-
-p /fluentd/plugins -v -c /workspace/local-fluent.conf
142+
-ti --rm -v `pwd`:/workspace --net=host \
143+
$(IMAGE):$(TAG) \
144+
-p /fluentd/plugins -v -c /workspace/local-fluent.conf
142145

143146
dep-graph:
144147
godepgraph -s \
145-
-l 4 \
146-
-p github.com/alecthomas/units,github.com/alecthomas/template,github.com/spf13,github.com/jackc,k8s.io/kubernetes,k8s.io/apimachinery,github.com/palantir,github.com/sirupsen,github.com/prometheus,golang.org,gopkg.in \
147-
github.com/vmware/kube-fluentd-operator/config-reloader \
148-
| sed 's|github.com/vmware/kube-fluentd-operator/config-reloader/||g'\
149-
| dot -Tpng -o godepgraph.png
148+
-l 4 \
149+
-p github.com/alecthomas/units,github.com/alecthomas/template,github.com/spf13,github.com/jackc,k8s.io/kubernetes,k8s.io/apimachinery,github.com/palantir,github.com/sirupsen,github.com/prometheus,golang.org,gopkg.in \
150+
github.com/vmware/kube-fluentd-operator/config-reloader \
151+
| sed 's|github.com/vmware/kube-fluentd-operator/config-reloader/||g'\
152+
| dot -Tpng -o godepgraph.png
150153

151154
validate-config:
152155
docker run --entrypoint=fluentd \
153-
-ti --rm -v `pwd`:/workspace --net=host \
154-
$(IMAGE):$(TAG) \
155-
--dry-run -p /fluentd/plugins -v -c /workspace/tmp/fluent.conf
156+
-ti --rm -v `pwd`:/workspace --net=host \
157+
$(IMAGE):$(TAG) \
158+
--dry-run -p /fluentd/plugins -v -c /workspace/tmp/fluent.conf
156159

157160
shell:
158161
docker run --entrypoint=/bin/bash \
159-
-ti --rm -v `pwd`:/workspace --net=host \
160-
$(IMAGE):$(TAG)
162+
-ti --rm -v `pwd`:/workspace --net=host \
163+
$(IMAGE):$(TAG)

config-reloader/config/config.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"regexp"
1010
"strconv"
1111
"strings"
12+
"unicode"
1213

1314
"github.com/alecthomas/kingpin"
1415
"github.com/sirupsen/logrus"
@@ -30,6 +31,7 @@ type Config struct {
3031
OutputDir string
3132
LogLevel string
3233
FluentdLogLevel string
34+
BufferMountFolder string
3335
AnnotConfigmapName string
3436
AnnotStatus string
3537
DefaultConfigmapName string
@@ -65,6 +67,7 @@ var defaultConfig = &Config{
6567
Datasource: "default",
6668
LogLevel: logrus.InfoLevel.String(),
6769
FluentdLogLevel: "info",
70+
BufferMountFolder: "",
6871
AnnotConfigmapName: "logging.csp.vmware.com/fluentd-configmap",
6972
AnnotStatus: "logging.csp.vmware.com/fluentd-status",
7073
DefaultConfigmapName: "fluentd-config",
@@ -116,6 +119,10 @@ func (cfg *Config) Validate() error {
116119
return fmt.Errorf("invalid annotation name: '%s'", cfg.AnnotConfigmapName)
117120
}
118121

122+
if cfg.BufferMountFolder != "" && cfg.hasValidBufferMountFolder() {
123+
return fmt.Errorf("invalid fluentd buffer mount folder: '%s%s'", "/var/log/", cfg.BufferMountFolder)
124+
}
125+
119126
// this can be empty
120127
if cfg.AnnotStatus != "" && !reValidAnnotationName.MatchString(cfg.AnnotStatus) {
121128
return fmt.Errorf("invalid annotation name: '%s'", cfg.AnnotStatus)
@@ -221,6 +228,8 @@ func (cfg *Config) ParseFlags(args []string) error {
221228
app.Flag("log-level", "Control verbosity of log level for reloader").Default(defaultConfig.LogLevel).StringVar(&cfg.LogLevel)
222229
app.Flag("fluentd-loglevel", "Control verbosity of log level for fluentd").Default(defaultConfig.FluentdLogLevel).StringVar(&cfg.FluentdLogLevel)
223230

231+
app.Flag("buffer-mount-folder", "Folder in /var/log/{} where to create all fluentd buffers").Default(defaultConfig.BufferMountFolder).StringVar(&cfg.BufferMountFolder)
232+
224233
app.Flag("annotation", "Which annotation on the namespace stores the configmap name?").Default(defaultConfig.AnnotConfigmapName).StringVar(&cfg.AnnotConfigmapName)
225234
app.Flag("default-configmap", "Read the configmap by this name if namespace is not annotated. Use empty string to suppress the default.").Default(defaultConfig.DefaultConfigmapName).StringVar(&cfg.DefaultConfigmapName)
226235
app.Flag("status-annotation", "Store configuration errors in this annotation, leave empty to turn off").Default(defaultConfig.AnnotStatus).StringVar(&cfg.AnnotStatus)
@@ -274,3 +283,15 @@ func (cfg *Config) ParseFluentdLogLevel() (string, error) {
274283

275284
return "", fmt.Errorf("not a valid Fluentd log Level: %q", cfg.FluentdLogLevel)
276285
}
286+
287+
// isValidFolderName takes a folder name and checks if there are any dangerous characters:
288+
func (cfg *Config) hasValidBufferMountFolder() bool {
289+
for _, r := range cfg.BufferMountFolder {
290+
if !unicode.IsLetter(r) && !unicode.IsDigit(r) && r != '-' && r != '_' {
291+
continue
292+
} else {
293+
return false
294+
}
295+
}
296+
return true
297+
}

config-reloader/config/config_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ func TestBadConfigs(t *testing.T) {
1616
{"--id", "???"},
1717
{"--log-level", "hobbit"},
1818
{"--fluentd-loglevel", "hobbit"},
19+
{"--buffer-mount-folder", "../"},
20+
{"--buffer-mount-folder", " "},
1921
{"--annotation", "|kl"},
2022
{"--status-annotation", "/hello"},
2123
{"--meta-key=test"},

config-reloader/generator/generator.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ func (g *Generator) renderMainFile(mainFile string, outputDir string, dest strin
112112
MetaKey string
113113
MetaValue string
114114
FluentdLogLevel string
115+
BufferMountFolder string
115116
PreprocessingDirectives []string
116117
}{}
117118

@@ -124,6 +125,10 @@ func (g *Generator) renderMainFile(mainFile string, outputDir string, dest strin
124125
model.FluentdLogLevel = g.cfg.FluentdLogLevel
125126
}
126127

128+
if g.cfg.BufferMountFolder != "" {
129+
model.BufferMountFolder = g.cfg.BufferMountFolder
130+
}
131+
127132
genCtx := &processors.GenerationContext{
128133
ReferencedBridges: map[string]bool{},
129134
}
@@ -286,6 +291,7 @@ func (g *Generator) makeContext(ns *datasource.NamespaceConfig, genCtx *processo
286291
DeploymentID: g.cfg.ID,
287292
MiniContainers: ns.MiniContainers,
288293
KubeletRoot: g.cfg.KubeletRoot,
294+
BufferMountFolder: g.cfg.BufferMountFolder,
289295
GenerationContext: genCtx,
290296
AllowTagExpansion: g.cfg.AllowTagExpansion,
291297
}

config-reloader/processors/destinations.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ type fixDestinations struct {
2020
}
2121

2222
func makeSafeBufferPath(ctx *ProcessorContext, origBufPath string) string {
23+
// make a custom buffer path directory if BufferMountFolder is set:
24+
if ctx.BufferMountFolder != "" {
25+
return fmt.Sprintf("/var/log/%s/kfo-%s-%s-%s.buf", ctx.BufferMountFolder, util.MakeFluentdSafeName(ctx.DeploymentID), ctx.Namespace, util.Hash("", origBufPath))
26+
}
2327
return fmt.Sprintf("/var/log/kfo-%s-%s-%s.buf", util.MakeFluentdSafeName(ctx.DeploymentID), ctx.Namespace, util.Hash("", origBufPath))
2428
}
2529

config-reloader/processors/processor.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ type ProcessorContext struct {
4343
DeploymentID string
4444
MiniContainers []*datasource.MiniContainer
4545
KubeletRoot string
46+
BufferMountFolder string
4647
GenerationContext *GenerationContext
4748
AllowTagExpansion bool
4849
}

0 commit comments

Comments
 (0)