-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Agent] Grpc support for agent to beat communication #14835
Merged
Merged
Changes from all commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
5a8be14
initial
michalpristas d0612be
yaml parsing
michalpristas 67b7c5d
registering fleet
michalpristas f555f26
cleanup
michalpristas 99e776b
splitting []iface into []map[string]iface
michalpristas 5ed6102
moved from filebeat to libbeat
michalpristas 03345f1
added vendor
michalpristas c3814f2
removed binaries
michalpristas 681b637
missing comments
michalpristas a63f94f
metricbeat spec
michalpristas f3e0fb2
x-pack/metricbeat supports fleet management
michalpristas c99a358
global support in x-pack beats for management/fleet
michalpristas 2578888
added submodule
michalpristas ff0b8cd
start only on run, prevents running on setup or similar commands
michalpristas 2df6934
removed git submodule
michalpristas ee20f38
fixed fleet imports
michalpristas 69ada7e
removed vendor fleet
michalpristas 0c4d113
fixed tests
michalpristas 990564f
fixed tests
michalpristas 876f9bf
more robust factory method
michalpristas 4bb740f
proper teardown
michalpristas 7524167
resovled conflicts with fleet
michalpristas dcfa125
resolved conflicts
michalpristas 24ce521
resolved comments
michalpristas 2b6ab97
enabled cm
michalpristas 971189f
set cm
michalpristas 84e6ddc
plugin factory introduced
michalpristas 1c1f341
i'm nothing but a hound dog
michalpristas 86c7ba1
missing files
michalpristas 06894d2
comments
michalpristas d56f46d
resolved comments
michalpristas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"BinaryPath":"filebeat","Args":["-e"],"Configurable":"grpc"} | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,8 @@ var Namespace = "libbeat.management" | |
// DebugK used as key for all things central management | ||
var DebugK = "centralmgmt" | ||
|
||
var centralMgmtKey = "x-pack-cm" | ||
|
||
// ConfigManager interacts with the beat to update configurations | ||
// from an external source | ||
type ConfigManager interface { | ||
|
@@ -47,32 +49,47 @@ type ConfigManager interface { | |
CheckRawConfig(cfg *common.Config) error | ||
} | ||
|
||
// PluginFunc for creating FactoryFunc if it matches a config | ||
type PluginFunc func(*common.Config) FactoryFunc | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. exported type PluginFunc should have comment or be unexported
michalpristas marked this conversation as resolved.
Show resolved
Hide resolved
michalpristas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// FactoryFunc for creating a config manager | ||
type FactoryFunc func(*common.Config, *reload.Registry, uuid.UUID) (ConfigManager, error) | ||
|
||
// Register a config manager | ||
func Register(name string, fn FactoryFunc, stability feature.Stability) { | ||
func Register(name string, fn PluginFunc, stability feature.Stability) { | ||
f := feature.New(Namespace, name, fn, feature.NewDetails(name, "", stability)) | ||
feature.MustRegister(f) | ||
} | ||
|
||
// Factory retrieves config manager constructor. If no one is registered | ||
// it will create a nil manager | ||
func Factory() FactoryFunc { | ||
func Factory(cfg *common.Config) FactoryFunc { | ||
factories, err := feature.GlobalRegistry().LookupAll(Namespace) | ||
if err != nil { | ||
return nilFactory | ||
} | ||
|
||
for _, f := range factories { | ||
if factory, ok := f.Factory().(FactoryFunc); ok { | ||
return factory | ||
if plugin, ok := f.Factory().(PluginFunc); ok { | ||
if factory := plugin(cfg); factory != nil { | ||
return factory | ||
} | ||
} | ||
} | ||
|
||
return nilFactory | ||
} | ||
|
||
type modeConfig struct { | ||
Mode string `config:"mode" yaml:"mode"` | ||
} | ||
|
||
func defaultModeConfig() *modeConfig { | ||
return &modeConfig{ | ||
Mode: centralMgmtKey, | ||
} | ||
} | ||
|
||
// nilManager, fallback when no manager is present | ||
type nilManager struct{} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"BinaryPath":"filebeat","Args":["-e"],"Configurable":"grpc"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -68,11 +68,23 @@ const ManagedConfigTemplate = ` | |
#monitoring.elasticsearch: | ||
` | ||
|
||
// Config for central management | ||
const ( | ||
// ModeCentralManagement is a default CM mode, using existing processes. | ||
ModeCentralManagement = "x-pack-cm" | ||
|
||
// ModeFleet is a management mode where fleet is used to retrieve configurations. | ||
ModeFleet = "x-pack-fleet" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: add a period after the sentence. |
||
) | ||
|
||
michalpristas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Config for central management. | ||
type Config struct { | ||
// true when enrolled | ||
Enabled bool `config:"enabled" yaml:"enabled"` | ||
|
||
// Mode specifies whether beat uses Central Management or Fleet. | ||
// Options: [cm, fleet] | ||
Mode string `config:"mode" yaml:"mode"` | ||
|
||
// Poll configs period | ||
Period time.Duration `config:"period" yaml:"period"` | ||
|
||
|
@@ -93,6 +105,7 @@ type EventReporterConfig struct { | |
|
||
func defaultConfig() *Config { | ||
return &Config{ | ||
Mode: ModeCentralManagement, | ||
Period: 60 * time.Second, | ||
EventsReporter: EventReporterConfig{ | ||
Period: 30 * time.Second, | ||
|
@@ -111,7 +124,7 @@ type templateParams struct { | |
BeatName string | ||
} | ||
|
||
// OverwriteConfigFile will overwrite beat settings file with the enrolled template | ||
// OverwriteConfigFile will overwrite beat settings file with the enrolled template. | ||
func (c *Config) OverwriteConfigFile(wr io.Writer, beatName string) error { | ||
t := template.Must(template.New("beat.management.yml").Parse(ManagedConfigTemplate)) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
ph marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// or more contributor license agreements. Licensed under the Elastic License; | ||
// you may not use this file except in compliance with the Elastic License. | ||
|
||
package fleet | ||
|
||
import ( | ||
xmanagement "github.com/elastic/beats/x-pack/libbeat/management" | ||
) | ||
|
||
// Config for central management | ||
type Config struct { | ||
Enabled bool `config:"enabled" yaml:"enabled"` | ||
Mode string `config:"mode" yaml:"mode"` | ||
Blacklist xmanagement.ConfigBlacklistSettings `config:"blacklist" yaml:"blacklist"` | ||
} | ||
|
||
func defaultConfig() *Config { | ||
return &Config{ | ||
Mode: xmanagement.ModeCentralManagement, | ||
Blacklist: xmanagement.ConfigBlacklistSettings{ | ||
Patterns: map[string]string{ | ||
"output": "console|file", | ||
}, | ||
}, | ||
} | ||
} | ||
michalpristas marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
// or more contributor license agreements. Licensed under the Elastic License; | ||
// you may not use this file except in compliance with the Elastic License. | ||
|
||
package fleet | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/elastic/beats/libbeat/common" | ||
"github.com/elastic/beats/x-pack/agent/pkg/core/remoteconfig/grpc" | ||
) | ||
|
||
const ( | ||
defaultTimeout = 15 * time.Second | ||
) | ||
|
||
// Server is a server for handling communication between | ||
// beat and Elastic Agent. | ||
type Server struct { | ||
configChan chan<- map[string]interface{} | ||
} | ||
|
||
// NewConfigServer creates a new grpc configuration server for receiving | ||
// configurations from Elastic Agent. | ||
func NewConfigServer(configChan chan<- map[string]interface{}) *Server { | ||
return &Server{ | ||
configChan: configChan, | ||
} | ||
} | ||
|
||
// Config is a handler of a call made by agent pushing latest configuration. | ||
func (s *Server) Config(ctx context.Context, req *grpc.ConfigRequest) (*grpc.ConfigResponse, error) { | ||
cfgString := req.GetConfig() | ||
|
||
var configMap common.MapStr | ||
uconfig, err := common.NewConfigFrom(cfgString) | ||
if err != nil { | ||
return &grpc.ConfigResponse{}, fmt.Errorf("config blocks unsuccessfully generated: %+v", err) | ||
} | ||
|
||
err = uconfig.Unpack(&configMap) | ||
if err != nil { | ||
return &grpc.ConfigResponse{}, fmt.Errorf("config blocks unsuccessfully generated: %+v", err) | ||
} | ||
|
||
select { | ||
case s.configChan <- configMap: | ||
case <-time.After(defaultTimeout): | ||
return &grpc.ConfigResponse{}, errors.New("failed to push configuration: Timeout") | ||
} | ||
return &grpc.ConfigResponse{}, nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we move that to YAML spec files?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do you mean classic filebeat.yml?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am talking about the files in x-pack/agent/spec.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
x-pack/agent/spec are transformation rules. this file should describe specific binary.
if we stick to agent.version == beat.version it is fine to have it there. but we probably still need to support
{program}.spec
for not yet known programs. e.g running endpoint or community beat eventually, so agent knows if specific binary supports grpc protocol or if there are some arguments needed to start binary correctly.i'm not sure what is a correct way of handling this. maybe lookup local spec + fallback to
{program}.spec
with local having priority?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the endpoint case, I think we would just have a specific spec for them. Concerning the community beats its a complete other madness and open the door to all sort of problems. We would need a way to add signatures and support the signing of community beats or expose that. :)
For the POC I think we can keep that way but let's create a followup issue to refactor that this sound like a P3 for the priority.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i created a new issue here: #14985