From 362d485834c593d7aabb6c04644b086132d6a52d Mon Sep 17 00:00:00 2001 From: Nicolas Ruflin Date: Wed, 28 Apr 2021 07:58:10 +0200 Subject: [PATCH] [Elastic Agent] Fix picking correct policy id for fleet-server (#25352) When running the container command, the policy id configured in the fleet-setup.yml file was only picked up, when fleet-server was not enabled. If a fleet-server policy is set, this will be set as the default. Later on this setup call should be improved that not both setups are possible. Currently it could be that the fleet enroll and fleet server setup policy are not identical. To partially test this PR, I did run the elastic agent locally as following: ``` sudo STATE_PATH=${GOPATH}/src/github.com/elastic/beats/x-pack/elastic-agent/state ./elastic-agent container ``` Adjust the path to your needs. In addition, I put the currently checked in fleet-setup.yml file into state directory. In parallel, an elastic-stack setup was running. --- x-pack/elastic-agent/CHANGELOG.next.asciidoc | 3 ++- x-pack/elastic-agent/pkg/agent/cmd/container.go | 7 ++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/x-pack/elastic-agent/CHANGELOG.next.asciidoc b/x-pack/elastic-agent/CHANGELOG.next.asciidoc index c3dc8b38ce2..403f0a106c6 100644 --- a/x-pack/elastic-agent/CHANGELOG.next.asciidoc +++ b/x-pack/elastic-agent/CHANGELOG.next.asciidoc @@ -55,7 +55,8 @@ - Fixed: limit for retries to Kibana configurable {issue}25063[25063] - Fix issue with status and inspect inside of container {pull}25204[25204] - Remove FLEET_SERVER_POLICY_NAME env variable as it was not used {pull}25149[25149] -- Reduce log level for listener cleanup to debug {pull}25274 +- Passing in policy id to container command works {pull}25352[25352] +- Reduce log level for listener cleanup to debug {pull}25274[25274] ==== New features diff --git a/x-pack/elastic-agent/pkg/agent/cmd/container.go b/x-pack/elastic-agent/pkg/agent/cmd/container.go index 35437e68376..a370c4f3106 100644 --- a/x-pack/elastic-agent/pkg/agent/cmd/container.go +++ b/x-pack/elastic-agent/pkg/agent/cmd/container.go @@ -139,8 +139,8 @@ func logError(streams *cli.IOStreams, err error) { fmt.Fprintf(streams.Err, "Error: %v\n", err) } -func logInfo(streams *cli.IOStreams, msg string) { - fmt.Fprintln(streams.Out, msg) +func logInfo(streams *cli.IOStreams, a ...interface{}) { + fmt.Fprintln(streams.Out, a...) } func logContainerCmd(streams *cli.IOStreams, cmd *cobra.Command) error { @@ -292,10 +292,11 @@ func runContainerCmd(streams *cli.IOStreams, cmd *cobra.Command, cfg setupConfig return err } } - policyID := "" + policyID := cfg.FleetServer.PolicyID if policy != nil { policyID = policy.ID } + logInfo(streams, "Policy selected for enrollment: ", policyID) cmdArgs, err := buildEnrollArgs(cfg, token, policyID) if err != nil { return err