Skip to content

Commit 13f1d1e

Browse files
committed
refact pkg/acquisition: k8saudit configuration
1 parent d88be56 commit 13f1d1e

File tree

1 file changed

+40
-15
lines changed
  • pkg/acquisition/modules/kubernetesaudit

1 file changed

+40
-15
lines changed

pkg/acquisition/modules/kubernetesaudit/config.go

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,39 +20,64 @@ type Configuration struct {
2020
configuration.DataSourceCommonCfg `yaml:",inline"`
2121
}
2222

23-
func (s *Source) UnmarshalConfig(yamlConfig []byte) error {
24-
k8sConfig := Configuration{}
23+
func ConfigurationFromYAML(y []byte) (Configuration, error) {
24+
var cfg Configuration
25+
26+
if err := yaml.UnmarshalWithOptions(y, &cfg, yaml.Strict()); err != nil {
27+
return cfg, fmt.Errorf("cannot parse: %s", yaml.FormatError(err, false, false))
28+
}
2529

26-
err := yaml.UnmarshalWithOptions(yamlConfig, &k8sConfig, yaml.Strict())
30+
cfg.SetDefaults()
31+
cfg.Normalize()
32+
33+
err := cfg.Validate()
2734
if err != nil {
28-
return fmt.Errorf("cannot parse k8s-audit configuration: %s", yaml.FormatError(err, false, false))
35+
return cfg, err
2936
}
3037

31-
s.config = k8sConfig
38+
return cfg, nil
39+
}
3240

33-
if s.config.ListenAddr == "" {
34-
return errors.New("listen_addr cannot be empty")
41+
func (c *Configuration) SetDefaults() {
42+
if c.Mode == "" {
43+
c.Mode = configuration.TAIL_MODE
3544
}
45+
}
3646

37-
if s.config.ListenPort == 0 {
38-
return errors.New("listen_port cannot be empty")
47+
func (s *Source) UnmarshalConfig(yamlConfig []byte) error {
48+
cfg, err := ConfigurationFromYAML(yamlConfig)
49+
if err != nil {
50+
return err
3951
}
4052

41-
if s.config.WebhookPath == "" {
42-
return errors.New("webhook_path cannot be empty")
53+
s.config = cfg
54+
55+
return nil
56+
}
57+
58+
func (c *Configuration) Validate() error {
59+
if c.ListenAddr == "" {
60+
return errors.New("listen_addr cannot be empty")
4361
}
4462

45-
if s.config.WebhookPath[0] != '/' {
46-
s.config.WebhookPath = "/" + s.config.WebhookPath
63+
if c.ListenPort == 0 {
64+
return errors.New("listen_port cannot be empty")
4765
}
4866

49-
if s.config.Mode == "" {
50-
s.config.Mode = configuration.TAIL_MODE
67+
if c.WebhookPath == "" {
68+
return errors.New("webhook_path cannot be empty")
5169
}
5270

5371
return nil
5472
}
5573

74+
75+
func (c *Configuration) Normalize() {
76+
if c.WebhookPath != "" && c.WebhookPath[0] != '/' {
77+
c.WebhookPath = "/" + c.WebhookPath
78+
}
79+
}
80+
5681
func (s *Source) Configure(_ context.Context, config []byte, logger *log.Entry, metricsLevel metrics.AcquisitionMetricsLevel) error {
5782
s.logger = logger
5883
s.metricsLevel = metricsLevel

0 commit comments

Comments
 (0)