Skip to content

Commit

Permalink
Pass down managed/standalone info
Browse files Browse the repository at this point in the history
Signed-off-by: ChrsMark <[email protected]>
  • Loading branch information
ChrsMark committed Aug 31, 2022
1 parent 9d3e2f8 commit 3761e5b
Show file tree
Hide file tree
Showing 20 changed files with 48 additions and 42 deletions.
2 changes: 1 addition & 1 deletion internal/pkg/agent/application/local_mode.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func newLocal(
}
localApplication.router = router

composableCtrl, err := composable.New(log, rawConfig)
composableCtrl, err := composable.New(log, rawConfig, false)
if err != nil {
return nil, errors.New(err, "failed to initialize composable controller")
}
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/agent/application/managed_mode.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func newManaged(
}
managedApplication.router = router

composableCtrl, err := composable.New(log, rawConfig)
composableCtrl, err := composable.New(log, rawConfig, true)
if err != nil {
return nil, errors.New(err, "failed to initialize composable controller")
}
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/agent/application/managed_mode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func TestManagedModeRouting(t *testing.T) {
router, _ := router.New(log, streamFn)
agentInfo, _ := info.NewAgentInfo(true)
nullStore := &storage.NullStore{}
composableCtrl, _ := composable.New(log, nil)
composableCtrl, _ := composable.New(log, nil, true)
emit, err := emitter.New(ctx, log, agentInfo, composableCtrl, router, &pipeline.ConfigModifiers{Decorators: []pipeline.DecoratorFunc{modifiers.InjectMonitoring}}, nil)
require.NoError(t, err)

Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/agent/cmd/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ func getProgramsFromConfig(log *logger.Logger, agentInfo *info.AgentInfo, cfg *c
router := &inmemRouter{}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
composableCtrl, err := composable.New(log, cfg)
composableCtrl, err := composable.New(log, cfg, false)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/agent/install/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ func applyDynamics(ctx context.Context, log *logger.Logger, cfg *config.Config)
})
}

ctrl, err := composable.New(log, cfg)
ctrl, err := composable.New(log, cfg, false)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/composable/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
)

// ContextProviderBuilder creates a new context provider based on the given config and returns it.
type ContextProviderBuilder func(log *logger.Logger, config *config.Config) (corecomp.ContextProvider, error)
type ContextProviderBuilder func(log *logger.Logger, config *config.Config, managed bool) (corecomp.ContextProvider, error)

// AddContextProvider adds a new ContextProviderBuilder
func (r *providerRegistry) AddContextProvider(name string, builder ContextProviderBuilder) error {
Expand Down
6 changes: 3 additions & 3 deletions internal/pkg/composable/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type controller struct {
}

// New creates a new controller.
func New(log *logger.Logger, c *config.Config) (Controller, error) {
func New(log *logger.Logger, c *config.Config, managed bool) (Controller, error) {
l := log.Named("composable")

var providersCfg Config
Expand All @@ -59,7 +59,7 @@ func New(log *logger.Logger, c *config.Config) (Controller, error) {
// explicitly disabled; skipping
continue
}
provider, err := builder(l, pCfg)
provider, err := builder(l, pCfg, managed)
if err != nil {
return nil, errors.New(err, fmt.Sprintf("failed to build provider '%s'", name), errors.TypeConfig, errors.M("provider", name))
}
Expand All @@ -76,7 +76,7 @@ func New(log *logger.Logger, c *config.Config) (Controller, error) {
// explicitly disabled; skipping
continue
}
provider, err := builder(l.Named(strings.Join([]string{"providers", name}, ".")), pCfg)
provider, err := builder(l.Named(strings.Join([]string{"providers", name}, ".")), pCfg, managed)
if err != nil {
return nil, errors.New(err, fmt.Sprintf("failed to build provider '%s'", name), errors.TypeConfig, errors.M("provider", name))
}
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/composable/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func TestController(t *testing.T) {

log, err := logger.New("", false)
require.NoError(t, err)
c, err := composable.New(log, cfg)
c, err := composable.New(log, cfg, false)
require.NoError(t, err)

var wg sync.WaitGroup
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/composable/dynamic.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type DynamicProvider interface {
}

// DynamicProviderBuilder creates a new dynamic provider based on the given config and returns it.
type DynamicProviderBuilder func(log *logger.Logger, config *config.Config) (DynamicProvider, error)
type DynamicProviderBuilder func(log *logger.Logger, config *config.Config, managed bool) (DynamicProvider, error)

// AddDynamicProvider adds a new DynamicProviderBuilder
func (r *providerRegistry) AddDynamicProvider(name string, builder DynamicProviderBuilder) error {
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/composable/providers/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@ func (*contextProvider) Run(comm corecomp.ContextProviderComm) error {
}

// ContextProviderBuilder builds the context provider.
func ContextProviderBuilder(_ *logger.Logger, _ *config.Config) (corecomp.ContextProvider, error) {
func ContextProviderBuilder(_ *logger.Logger, _ *config.Config, managed bool) (corecomp.ContextProvider, error) {
return &contextProvider{}, nil
}
2 changes: 1 addition & 1 deletion internal/pkg/composable/providers/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func (c *dynamicProvider) Run(comm composable.DynamicProviderComm) error {
}

// DynamicProviderBuilder builds the dynamic provider.
func DynamicProviderBuilder(logger *logger.Logger, c *config.Config) (composable.DynamicProvider, error) {
func DynamicProviderBuilder(logger *logger.Logger, c *config.Config, managed bool) (composable.DynamicProvider, error) {
var cfg Config
if c == nil {
c = config.New()
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/composable/providers/env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (*contextProvider) Run(comm corecomp.ContextProviderComm) error {
}

// ContextProviderBuilder builds the context provider.
func ContextProviderBuilder(_ *logger.Logger, _ *config.Config) (corecomp.ContextProvider, error) {
func ContextProviderBuilder(_ *logger.Logger, _ *config.Config, managed bool) (corecomp.ContextProvider, error) {
return &contextProvider{}, nil
}

Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/composable/providers/host/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (c *contextProvider) Run(comm corecomp.ContextProviderComm) error {
}

// ContextProviderBuilder builds the context provider.
func ContextProviderBuilder(log *logger.Logger, c *config.Config) (corecomp.ContextProvider, error) {
func ContextProviderBuilder(log *logger.Logger, c *config.Config, managed bool) (corecomp.ContextProvider, error) {
p := &contextProvider{
logger: log,
fetcher: getHostInfo,
Expand Down
15 changes: 8 additions & 7 deletions internal/pkg/composable/providers/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,13 @@ func init() {
}

type dynamicProvider struct {
logger *logger.Logger
config *Config
logger *logger.Logger
config *Config
managed bool
}

// DynamicProviderBuilder builds the dynamic provider.
func DynamicProviderBuilder(logger *logger.Logger, c *config.Config) (composable.DynamicProvider, error) {
func DynamicProviderBuilder(logger *logger.Logger, c *config.Config, managed bool) (composable.DynamicProvider, error) {
var cfg Config
if c == nil {
c = config.New()
Expand All @@ -49,7 +50,7 @@ func DynamicProviderBuilder(logger *logger.Logger, c *config.Config) (composable
return nil, errors.New(err, "failed to unpack configuration")
}

return &dynamicProvider{logger, &cfg}, nil
return &dynamicProvider{logger, &cfg, managed}, nil
}

// Run runs the kubernetes context provider.
Expand Down Expand Up @@ -139,19 +140,19 @@ func (p *dynamicProvider) newEventer(
client k8s.Interface) (Eventer, error) {
switch resourceType {
case "pod":
eventer, err := NewPodEventer(comm, p.config, p.logger, client, p.config.Scope)
eventer, err := NewPodEventer(comm, p.config, p.logger, client, p.config.Scope, p.managed)
if err != nil {
return nil, err
}
return eventer, nil
case nodeScope:
eventer, err := NewNodeEventer(comm, p.config, p.logger, client, p.config.Scope)
eventer, err := NewNodeEventer(comm, p.config, p.logger, client, p.config.Scope, p.managed)
if err != nil {
return nil, err
}
return eventer, nil
case "service":
eventer, err := NewServiceEventer(comm, p.config, p.logger, client, p.config.Scope)
eventer, err := NewServiceEventer(comm, p.config, p.logger, client, p.config.Scope, p.managed)
if err != nil {
return nil, err
}
Expand Down
35 changes: 20 additions & 15 deletions internal/pkg/composable/providers/kubernetes/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ package kubernetes

import (
"fmt"
"github.com/elastic/elastic-agent-autodiscover/utils"
"sync"
"time"

"github.com/elastic/elastic-agent-autodiscover/kubernetes"
"github.com/elastic/elastic-agent-autodiscover/kubernetes/metadata"
"github.com/elastic/elastic-agent-autodiscover/utils"
c "github.com/elastic/elastic-agent-libs/config"
"github.com/elastic/elastic-agent-libs/logp"
"github.com/elastic/elastic-agent-libs/mapstr"
Expand All @@ -32,6 +32,7 @@ type pod struct {
config *Config
logger *logp.Logger
scope string
managed bool
cleanupTimeout time.Duration

// Mutex used by configuration updates not triggered by the main watcher,
Expand All @@ -52,7 +53,8 @@ func NewPodEventer(
cfg *Config,
logger *logp.Logger,
client k8s.Interface,
scope string) (Eventer, error) {
scope string,
managed bool) (Eventer, error) {
watcher, err := kubernetes.NewNamedWatcher("agent-pod", client, &kubernetes.Pod{}, kubernetes.WatchOptions{
SyncTimeout: cfg.SyncPeriod,
Node: cfg.Node,
Expand Down Expand Up @@ -96,6 +98,7 @@ func NewPodEventer(
watcher: watcher,
nodeWatcher: nodeWatcher,
namespaceWatcher: namespaceWatcher,
managed: managed,
}

watcher.AddEventHandler(p)
Expand Down Expand Up @@ -152,19 +155,21 @@ func (p *pod) emitRunning(pod *kubernetes.Pod) {
data.mapping["scope"] = p.scope

if p.config.Hints.Enabled() { // This is "hints based autodiscovery flow"
if ann, ok := data.mapping["annotations"]; ok {
annotations := ann.(mapstr.M)
hints := utils.GenerateHints(annotations, "", p.config.Prefix)
if len(hints) > 0 {
p.logger.Errorf("Extracted hints are :%v", hints)
hintsMapping := GenerateHintsMapping(hints, data.mapping, p.logger)
p.logger.Errorf("Generated hints mappings are :%v", hintsMapping)
_ = p.comm.AddOrUpdate(
data.uid,
PodPriority,
map[string]interface{}{"hints": hintsMapping},
data.processors, // TODO: add processors here explicitely ->>> NOOOO
)
if !p.managed {
if ann, ok := data.mapping["annotations"]; ok {
annotations := ann.(mapstr.M)
hints := utils.GenerateHints(annotations, "", p.config.Prefix)
if len(hints) > 0 {
p.logger.Errorf("Extracted hints are :%v", hints)
hintsMapping := GenerateHintsMapping(hints, data.mapping, p.logger)
p.logger.Errorf("Generated hints mappings are :%v", hintsMapping)
_ = p.comm.AddOrUpdate(
data.uid,
PodPriority,
map[string]interface{}{"hints": hintsMapping},
data.processors, // TODO: add processors here explicitely ->>> NOOOO
)
}
}
}
} else { // This is the "template-based autodiscovery" flow
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type contextProvider struct {
}

// ContextProviderBuilder builds the provider.
func ContextProviderBuilder(logger *logger.Logger, c *config.Config) (corecomp.ContextProvider, error) {
func ContextProviderBuilder(logger *logger.Logger, c *config.Config, managed bool) (corecomp.ContextProvider, error) {
var cfg Config
if c == nil {
c = config.New()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type contextProviderK8sSecrets struct {
}

// ContextProviderBuilder builds the context provider.
func ContextProviderBuilder(logger *logger.Logger, c *config.Config) (corecomp.ContextProvider, error) {
func ContextProviderBuilder(logger *logger.Logger, c *config.Config, managed bool) (corecomp.ContextProvider, error) {
var cfg Config
if c == nil {
c = config.New()
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/composable/providers/local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (c *contextProvider) Run(comm corecomp.ContextProviderComm) error {
}

// ContextProviderBuilder builds the context provider.
func ContextProviderBuilder(_ *logger.Logger, c *config.Config) (corecomp.ContextProvider, error) {
func ContextProviderBuilder(_ *logger.Logger, c *config.Config, managed bool) (corecomp.ContextProvider, error) {
p := &contextProvider{}
if c != nil {
err := c.Unpack(p)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (c *dynamicProvider) Run(comm composable.DynamicProviderComm) error {
}

// DynamicProviderBuilder builds the dynamic provider.
func DynamicProviderBuilder(_ *logger.Logger, c *config.Config) (composable.DynamicProvider, error) {
func DynamicProviderBuilder(_ *logger.Logger, c *config.Config, managed bool) (composable.DynamicProvider, error) {
p := &dynamicProvider{}
if c != nil {
err := c.Unpack(p)
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/composable/providers/path/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ func (*contextProvider) Run(comm corecomp.ContextProviderComm) error {
}

// ContextProviderBuilder builds the context provider.
func ContextProviderBuilder(_ *logger.Logger, _ *config.Config) (corecomp.ContextProvider, error) {
func ContextProviderBuilder(_ *logger.Logger, _ *config.Config, managed bool) (corecomp.ContextProvider, error) {
return &contextProvider{}, nil
}

0 comments on commit 3761e5b

Please sign in to comment.