/
fs.go
73 lines (59 loc) · 1.73 KB
/
fs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Copyright © 2018 VMware, Inc. All Rights Reserved.
// SPDX-License-Identifier: BSD-2-Clause
package datasource
import (
"context"
"fmt"
"os"
"path/filepath"
"github.com/vmware/kube-fluentd-operator/config-reloader/util"
"github.com/sirupsen/logrus"
)
type fsDatasource struct {
hashes map[string]string
rootDir string
statusOutputDir string
}
// NewFileSystemDatasource turns all files matching *.conf patter in the given dir into namespace configs
func NewFileSystemDatasource(ctx context.Context, rootDir string, statusOutputDir string) Datasource {
return &fsDatasource{
hashes: make(map[string]string),
rootDir: rootDir,
statusOutputDir: statusOutputDir,
}
}
func (d *fsDatasource) GetNamespaces(ctx context.Context) ([]*NamespaceConfig, error) {
res := []*NamespaceConfig{}
files, err := filepath.Glob(fmt.Sprintf("%s/*.conf", d.rootDir))
if err != nil {
return nil, err
}
for _, f := range files {
base := filepath.Base(f)
ns := base[0 : len(base)-5]
contents, err := os.ReadFile(f)
if err != nil {
logrus.Infof("Cannot read file %s: %+v", f, err)
continue
}
cfg := &NamespaceConfig{
Name: ns,
FluentdConfig: string(contents),
PreviousConfigHash: d.hashes[ns],
}
logrus.Infof("Loading namespace %s from file %s", ns, f)
res = append(res, cfg)
}
return res, nil
}
func (d *fsDatasource) WriteCurrentConfigHash(namespace string, hash string) {
d.hashes[namespace] = hash
}
func (d *fsDatasource) UpdateStatus(ctx context.Context, namespace string, status string) {
fname := filepath.Join(d.statusOutputDir, fmt.Sprintf("ns-%s.status", namespace))
if status != "" {
util.WriteStringToFile(fname, status)
} else {
os.Remove(fname)
}
}