Skip to content

Commit 08c7d52

Browse files
authored
Conf gen updates (#262)
* return typed model instead of generic map * exported a few more functions * allowed missing config.yaml file * added ConfigFileSgtruct * restored output of Buckets * changed config2 to config
1 parent 03e1e4e commit 08c7d52

File tree

10 files changed

+160
-143
lines changed

10 files changed

+160
-143
lines changed

docs/api.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,6 @@ Following is the supported API format for writing to standard output:
145145
<pre>
146146
stdout:
147147
format: the format of each line: printf (default - writes using golang's default map printing), fields (writes one key and value field per line) or json
148-
149148
</pre>
150149
## Aggregate metrics API
151150
Following is the supported API format for specifying metrics aggregations:

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ require (
1616
github.com/netobserv/loki-client-go v0.0.0-20211018150932-cb17208397a9
1717
github.com/netobserv/netobserv-ebpf-agent v0.1.1-0.20220608092850-3fd4695b7cc2
1818
github.com/netsampler/goflow2 v1.1.1-0.20220509155230-5300494e4785
19+
github.com/pkg/errors v0.9.1
1920
github.com/prometheus/client_golang v1.12.1
2021
github.com/prometheus/common v0.32.1
2122
github.com/segmentio/kafka-go v0.4.28
@@ -72,7 +73,6 @@ require (
7273
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f // indirect
7374
github.com/pelletier/go-toml v1.9.4 // indirect
7475
github.com/pierrec/lz4 v2.6.1+incompatible // indirect
75-
github.com/pkg/errors v0.9.1 // indirect
7676
github.com/pmezard/go-difflib v1.0.0 // indirect
7777
github.com/prometheus/client_model v0.2.0 // indirect
7878
github.com/prometheus/procfs v0.7.3 // indirect

pkg/api/encode_prom.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@
1818
package api
1919

2020
type PromEncode struct {
21-
Metrics PromMetricsItems `yaml:"metrics" json:"metrics,omitempty" doc:"list of prometheus metric definitions, each includes:"`
22-
Port int `yaml:"port" json:"port,omitempty" doc:"port number to expose \"/metrics\" endpoint"`
23-
Prefix string `yaml:"prefix" json:"prefix,omitempty" doc:"prefix added to each metric name"`
24-
ExpiryTime int `yaml:"expiryTime" json:"expiryTime,omitempty" doc:"seconds of no-flow to wait before deleting prometheus data item"`
21+
Metrics PromMetricsItems `yaml:"metrics,omitempty" json:"metrics,omitempty" doc:"list of prometheus metric definitions, each includes:"`
22+
Port int `yaml:"port,omitempty" json:"port,omitempty" doc:"port number to expose \"/metrics\" endpoint"`
23+
Prefix string `yaml:"prefix,omitempty" json:"prefix,omitempty" doc:"prefix added to each metric name"`
24+
ExpiryTime int `yaml:"expiryTime,omitempty" json:"expiryTime,omitempty" doc:"seconds of no-flow to wait before deleting prometheus data item"`
2525
}
2626

2727
type PromEncodeOperationEnum struct {

pkg/confgen/confgen.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,13 @@ type DefFile struct {
7171

7272
func (cg *ConfGen) Run() error {
7373
var err error
74-
cg.config, err = cg.parseConfigFile(Opt.SrcFolder + "/" + configFileName)
74+
cg.config, err = cg.ParseConfigFile(Opt.SrcFolder + "/" + configFileName)
7575
if err != nil {
76-
log.Debugf("cg.parseConfigFile err: %v ", err)
76+
log.Debugf("cg.ParseConfigFile err: %v ", err)
7777
return err
7878
}
7979

80-
definitionFiles := cg.getDefinitionFiles(Opt.SrcFolder)
80+
definitionFiles := cg.GetDefinitionFiles(Opt.SrcFolder)
8181
for _, definitionFile := range definitionFiles {
8282
err := cg.parseFile(definitionFile)
8383
if err != nil {
@@ -86,7 +86,7 @@ func (cg *ConfGen) Run() error {
8686
}
8787
}
8888

89-
cg.dedupe()
89+
cg.Dedupe()
9090

9191
if len(Opt.GenerateStages) != 0 {
9292
config := cg.GenerateTruncatedConfig(Opt.GenerateStages)
@@ -215,7 +215,7 @@ func (cg *ConfGen) parseFile(fileName string) error {
215215
return nil
216216
}
217217

218-
func (*ConfGen) getDefinitionFiles(rootPath string) []string {
218+
func (*ConfGen) GetDefinitionFiles(rootPath string) []string {
219219

220220
var files []string
221221

pkg/confgen/confgen_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ func Test_getDefinitionFiles(t *testing.T) {
113113
require.NoError(t, err)
114114
err = os.WriteFile(filepath.Join(dirPath, filename), []byte(networkDefinitionConfiguration), 0644)
115115
require.NoError(t, err)
116-
files := cg.getDefinitionFiles(dirPath)
116+
files := cg.GetDefinitionFiles(dirPath)
117117
require.Equal(t, 1, len(files))
118118
expected := []string{path.Join(dirPath, filename)}
119119
require.ElementsMatch(t, expected, files)

pkg/confgen/config.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ package confgen
1919

2020
import (
2121
"io/ioutil"
22+
"os"
2223

2324
"github.com/netobserv/flowlogs-pipeline/pkg/api"
25+
"github.com/pkg/errors"
2426
log "github.com/sirupsen/logrus"
2527
"gopkg.in/yaml.v2"
2628
)
@@ -68,9 +70,17 @@ type Config struct {
6870
Visualization ConfigVisualization `yaml:"visualization"`
6971
}
7072

71-
func (cg *ConfGen) parseConfigFile(fileName string) (*Config, error) {
73+
func (cg *ConfGen) ParseConfigFile(fileName string) (*Config, error) {
7274
// parse config file yaml
75+
// provide a minimal config for when config file is missing (as for Netobserv Openshift Operator)
7376
var config Config
77+
if _, err := os.Stat(fileName); errors.Is(err, os.ErrNotExist) {
78+
if len(Opt.GenerateStages) == 0 {
79+
log.Errorf("config file %s does not exist", fileName)
80+
return nil, err
81+
}
82+
return &Config{}, nil
83+
}
7484
yamlFile, err := ioutil.ReadFile(fileName)
7585
if err != nil {
7686
log.Debugf("ioutil.ReadFile err: %v ", err)

pkg/confgen/config_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func Test_parseConfigFile(t *testing.T) {
6262
cg := getConfGen()
6363
err := os.WriteFile(filename, []byte(testConfig), 0644)
6464
require.Equal(t, err, nil)
65-
config, err := cg.parseConfigFile(filename)
65+
config, err := cg.ParseConfigFile(filename)
6666
require.NoError(t, err)
6767
require.Equal(t, config, expectedConfig())
6868
}

pkg/confgen/dedup.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525
log "github.com/sirupsen/logrus"
2626
)
2727

28-
func (cg *ConfGen) dedupe() {
28+
func (cg *ConfGen) Dedupe() {
2929
cg.transformRules = dedupeNetworkTransformRules(cg.transformRules)
3030
cg.aggregateDefinitions = dedupeAggregateDefinitions(cg.aggregateDefinitions)
3131
}
@@ -54,16 +54,16 @@ func dedupeNetworkTransformRules(rules api.NetworkTransformRules) api.NetworkTra
5454
// dedupeAggregateDefinitions is inefficient because we can't use a map to look for duplicates.
5555
// The reason is that aggregate.AggregateDefinition is not hashable due to its AggregateBy field which is a slice.
5656
func dedupeAggregateDefinitions(aggregateDefinitions aggregate.Definitions) aggregate.Definitions {
57-
var dedpueSlice []api.AggregateDefinition
57+
var dedupeSlice []api.AggregateDefinition
5858
for i, aggregateDefinition := range aggregateDefinitions {
59-
if containsAggregateDefinitions(dedpueSlice, aggregateDefinition) {
59+
if containsAggregateDefinitions(dedupeSlice, aggregateDefinition) {
6060
// duplicate aggregateDefinition
6161
log.Debugf("Remove duplicate AggregateDefinitions %v at index %v", aggregateDefinition, i)
6262
continue
6363
}
64-
dedpueSlice = append(dedpueSlice, aggregateDefinition)
64+
dedupeSlice = append(dedupeSlice, aggregateDefinition)
6565
}
66-
return dedpueSlice
66+
return dedupeSlice
6767
}
6868

6969
func containsAggregateDefinitions(slice []api.AggregateDefinition, searchItem api.AggregateDefinition) bool {

0 commit comments

Comments
 (0)