Skip to content

Commit

Permalink
Agent v2 (#458)
Browse files Browse the repository at this point in the history
* Set OPA default log level (#505)

* feat: align to elastic-agent v2 api

* chore: update relace script

* wip

* chore: create config from standalone cloudbeat deployment

* replace restart with kill

* tidy

Co-authored-by: Amir Ben Nun <34831306+amirbenun@users.noreply.github.com>
  • Loading branch information
olegsu and amirbenun authored Nov 9, 2022
1 parent 240ced3 commit 675c6cf
Show file tree
Hide file tree
Showing 14 changed files with 245 additions and 245 deletions.
2 changes: 1 addition & 1 deletion beater/cloudbeat.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func New(b *beat.Beat, cfg *agentconfig.C) (beat.Beater, error) {
return nil, err
}

reload.Register.MustRegisterList("inputs", reloader)
reload.RegisterV2.MustRegisterInput(reloader)
return s, nil
}

Expand Down
7 changes: 1 addition & 6 deletions beater/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,9 @@ import (
agentconfig "github.com/elastic/elastic-agent-libs/config"
)

type validator struct {
}
type validator struct{}

func (v *validator) Validate(cfg *agentconfig.C) error {
if !cfg.HasField("streams") {
return fmt.Errorf("no streams in config")
}

c, err := config.New(cfg)
if err != nil {
return fmt.Errorf("could not parse reconfiguration %v, skipping with error: %v", cfg.FlattenedKeys(), err)
Expand Down
49 changes: 10 additions & 39 deletions beater/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,39 +45,15 @@ func TestValidatorTestSuite(t *testing.T) {
}

func (s *ValidatorTestSuite) TestConfig() {
configNoStreams := config.MustNewConfigFrom(`
not_streams:
- runtime_cfg:
activated_rules:
cis_k8s:
- a
- b
- c
- d
- e
`)

configNoRuntimeCfg := config.MustNewConfigFrom(`
streams:
- not_runtime_cfg:
activated_rules:
cis_k8s:
- a
- b
- c
- d
- e
`)
configWithRuntimeCfg := config.MustNewConfigFrom(`
streams:
- runtime_cfg:
activated_rules:
cis_k8s:
- a
- b
- c
- d
- e
runtime_cfg:
activated_rules:
cis_k8s:
- a
- b
- c
- d
- e
`)

testcases := []struct {
Expand All @@ -87,13 +63,8 @@ streams:
{
true,
config.NewConfig(),
}, {
true,
configNoRuntimeCfg,
}, {
true,
configNoStreams,
}, {
},
{
false,
configWithRuntimeCfg,
},
Expand Down
25 changes: 25 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,41 @@
package cmd

import (
"fmt"

"github.com/elastic/cloudbeat/beater"
"github.com/elastic/elastic-agent-client/v7/pkg/client"
"github.com/elastic/elastic-agent-client/v7/pkg/proto"

cmd "github.com/elastic/beats/v7/libbeat/cmd"
"github.com/elastic/beats/v7/libbeat/cmd/instance"
"github.com/elastic/beats/v7/libbeat/common/reload"

_ "github.com/elastic/beats/v7/x-pack/libbeat/include"
"github.com/elastic/beats/v7/x-pack/libbeat/management"
)

// Name of this beat
var Name = "cloudbeat"

// RootCmd to handle beats cli
var RootCmd = cmd.GenRootCmdWithSettings(beater.New, instance.Settings{Name: Name, Version: defaultBeatVersion})

func cloudbeatCfg(rawIn *proto.UnitExpectedConfig, agentInfo *client.AgentInfo) ([]*reload.ConfigWithMeta, error) {
modules, err := management.CreateInputsFromStreams(rawIn, "logs", agentInfo)
if err != nil {
return nil, fmt.Errorf("error creating input list from raw expected config: %w", err)
}

// format for the reloadable list needed bythe cm.Reload() method
configList, err := management.CreateReloadConfigFromInputs(modules)
if err != nil {
return nil, fmt.Errorf("error creating reloader config: %w", err)
}

return configList, nil
}

func init() {
management.ConfigTransform.SetTransform(cloudbeatCfg)
}
42 changes: 28 additions & 14 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,6 @@ type Fetcher struct {
Name string `config:"name"` // Name of the fetcher
}

type AgentInput struct {
Streams []Stream `config:"streams"`
Type string `config:"type"`
}

type Stream struct {
AWSConfig aws.ConfigAWS `config:",inline"`
RuntimeCfg *RuntimeConfig `config:"runtime_cfg"`
Expand Down Expand Up @@ -77,23 +72,26 @@ type Benchmarks struct {
CisEks []string `config:"cis_eks,omitempty" yaml:"cis_eks,omitempty" json:"cis_eks,omitempty"`
}

var DefaultConfig = AgentInput{
Type: InputTypeVanillaK8s,
Streams: []Stream{{
Period: 4 * time.Hour,
}},
var DefaultConfig = Stream{
Period: 4 * time.Hour,
}

func New(cfg *config.C) (Config, error) {
// work with v1 cloudbeat.yml in dev mod
if cfg.HasField("streams") {
return newStandaloneConfig(cfg)
}
c := DefaultConfig

if err := cfg.Unpack(&c); err != nil {
return Config{}, err
}

inputType := InputTypeVanillaK8s
if c.RuntimeCfg != nil && c.RuntimeCfg.ActivatedRules != nil && len(c.RuntimeCfg.ActivatedRules.CisEks) > 0 {
inputType = InputTypeEks
}
return Config{
Stream: c.Streams[0],
Type: c.Type,
Stream: c,
Type: inputType,
}, nil
}

Expand All @@ -105,6 +103,22 @@ func Datastream(namespace string, indexPrefix string) string {
return indexPrefix + "-" + namespace
}

// stanalone config is used for development flows
// see an example deploy/kustomize/overlays/cloudbeat-vanilla/cloudbeat.yml
func newStandaloneConfig(cfg *config.C) (Config, error) {
c := struct {
Period time.Duration
Streams []Stream
}{4 * time.Hour, []Stream{}}
if err := cfg.Unpack(&c); err != nil {
return Config{}, err
}
return Config{
Type: InputTypeVanillaK8s,
Stream: c.Streams[0],
}, nil
}

type AwsConfigProvider interface {
InitializeAWSConfig(ctx context.Context, cfg aws.ConfigAWS) (awssdk.Config, error)
}
Loading

0 comments on commit 675c6cf

Please sign in to comment.