Skip to content
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] Support for config constraints #17112

Merged

Conversation

michalpristas
Copy link
Contributor

@michalpristas michalpristas commented Mar 19, 2020

What does this PR do?

Followup tohttps://github.com//issues/15690.
This PR introduces contraints as described in this issue: #16409

This PR enhances emitter not only to use decorators to enrich configuration but filters to modify ast by dropping some nodes.

It introduces also constraints filter which loads all datasets and for those which contain constraint node parses this nodes and uses existing boolexp implementation to evaluate expressions.
It also allows user to use some basic variables such as agent.id, agent.version and such.

Nodes containing constraints section and where constraint is evaluated as FALSE, these nodes are dropped.

Checklist

  • My code follows the style guidelines of this project
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have made corresponding change to the default configuration files
  • I have added tests that prove my fix is effective or that my feature works

Fixes: #16409

@elasticmachine
Copy link
Collaborator

Pinging @elastic/ingest-management (Project:fleet)

if err := filter(ast); err != nil {
return errors.New(err, "failed to filter configuration", errors.TypeConfig)
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should log when a constrains fails?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I did not correctly express my requirement here, my bad.

The above code do log when a constraint fails, but I think we should also log when a constraints matches and we filter content.

Copy link
Contributor

@ph ph left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few small changes:

  • Adding logger when a constrains disable configuration?
  • Initialize the var store and the methods once?

osKernelKey = "os.kernel"
osPlatformKey = "os.platform"
osVersionKey = "os.version"
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a documentation with the variables that are accessible in the configuration?
This will be useful when writing the documentation and also when we implement them in UI.

regs := boolexp.NewMethodsReg()
if err := regs.Register(validateVersionFuncName, regValidateVersion); err != nil {
return false, err
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be defined once and reused?

return false, err
}

if err := initVarStore(store); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking a the fields used I think this could be defined once and reused?

The following requires a reboot to change:

	// 	Agent
	store.vars[agentIDKey] = agentInfo.AgentID()
	store.vars[agentVersionKey] = release.Version()

	// Host
	store.vars[hostArchKey] = info.Architecture

	// Operating system
	store.vars[osFamilyKey] = runtime.GOOS
	store.vars[osKernelKey] = info.KernelVersion
	store.vars[osPlatformKey] = info.OS.Family
	store.vars[osVersionKey] = info.OS.Version

@@ -36,6 +36,7 @@ func TestBoolexp(t *testing.T) {
}{
// Variables
{expression: "%{[hello.var]} == 'hello'", result: true},
{expression: "%{[hello.var]} != 'hello'", result: false},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops I've missed that :)

@michalpristas michalpristas force-pushed the agent-constraints-new-master branch from 807fa81 to 981c2c4 Compare March 24, 2020 14:36
if isOK, err := evaluateConstraint(constraint); !isOK || err != nil {
if err == nil {
// log only constraint not matching
log.Infof("constraint '%s' not matching for datasource: %v", constraint, datasourceNode.String())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might want to reduce what we want to output to the log to the type of the datasource and id? This is just to make sure we do not expose credentials in the log this could be a problem with metricbeat where datasource have a higher change of containing secrets.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

constriant is at datasource level and as such it does not contain type, ID is optional for now. i think namespace, output is a identifying configuration and id if present will be logged

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sound good ^

Copy link
Collaborator

@ruflin ruflin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this have somewhere a changelog entry?

Could you update the PR description with more details on what is actually done in this PR and how? At the moment it references to 3 different issues.

@@ -80,6 +80,10 @@ func getInfoFromStore(s ioStore) (*persistentAgentInfo, error) {
errors.M(errors.MetaKeyPath, AgentConfigFile))
}

if err := reader.Close(); err != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is this related to this PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this fixes an issue on windows i experienced on tests by introducing more extensive use of agentInfo.ID()

@@ -118,6 +122,10 @@ func updateAgentInfo(s ioStore, agentInfo *persistentAgentInfo) error {
errors.M(errors.MetaKeyPath, AgentConfigFile))
}

if err := reader.Close(); err != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is this related to this PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as ^^^

@ph
Copy link
Contributor

ph commented Mar 25, 2020

Should this have somewhere a changelog entry?

Could you update the PR description with more details on what is actually done in this PR and how? At the moment it references to 3 different issues.

Good comment, I've missed that.. I need to update my local checklist for PR review.

Copy link
Contributor

@ph ph left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, Missing changelog entry.

@michalpristas michalpristas merged commit e0370f6 into elastic:master Mar 26, 2020
@ph ph added the needs_backport PR is waiting to be backported to other branches. label Apr 7, 2020
@ph ph added v7.8.0 and removed needs_backport PR is waiting to be backported to other branches. labels Apr 7, 2020
ph added a commit that referenced this pull request Apr 7, 2020
[Agent] Support for config constraints (#17112)

(cherry picked from commit e0370f6)

Co-authored-by: Michal Pristas <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Agent] Initial support for constraints
4 participants