diff --git a/x-pack/elastic-agent/pkg/agent/application/handler_action_policy_change.go b/x-pack/elastic-agent/pkg/agent/application/handler_action_policy_change.go index b921569a19b4..bb9297998e7b 100644 --- a/x-pack/elastic-agent/pkg/agent/application/handler_action_policy_change.go +++ b/x-pack/elastic-agent/pkg/agent/application/handler_action_policy_change.go @@ -62,6 +62,11 @@ func (h *handlerPolicyChange) Handle(ctx context.Context, a action, acker fleetA } func (h *handlerPolicyChange) handleKibanaHosts(c *config.Config) (err error) { + // do not update kibana host from policy; no setters provided with local Fleet Server + if len(h.setters) == 0 { + return nil + } + cfg, err := configuration.NewFromConfig(c) if err != nil { return errors.New(err, "could not parse the configuration from the policy", errors.TypeConfig) diff --git a/x-pack/elastic-agent/pkg/agent/application/managed_mode.go b/x-pack/elastic-agent/pkg/agent/application/managed_mode.go index c12c2451e3a8..242b556594c2 100644 --- a/x-pack/elastic-agent/pkg/agent/application/managed_mode.go +++ b/x-pack/elastic-agent/pkg/agent/application/managed_mode.go @@ -181,7 +181,10 @@ func newManaged( agentInfo: agentInfo, config: cfg, store: store, - setters: []clientSetter{acker}, + } + if cfg.Fleet.Server == nil { + // setters only set when not running a local Fleet Server + policyChanger.setters = []clientSetter{acker} } actionDispatcher.MustRegister( &fleetapi.ActionPolicyChange{}, diff --git a/x-pack/elastic-agent/pkg/agent/cmd/container.go b/x-pack/elastic-agent/pkg/agent/cmd/container.go index 091d5cea9f4d..b35442df504a 100644 --- a/x-pack/elastic-agent/pkg/agent/cmd/container.go +++ b/x-pack/elastic-agent/pkg/agent/cmd/container.go @@ -19,6 +19,7 @@ import ( "github.com/elastic/beats/v7/libbeat/kibana" + "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/application/info" "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/errors" "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/cli" ) @@ -92,6 +93,10 @@ be used when the same credentials will be used across all the possible actions a KIBANA_HOST - kibana host [http://kibana:5601] KIBANA_USERNAME - kibana username [$ELASTICSEARCH_USERNAME] KIBANA_PASSWORD - kibana password [$ELASTICSEARCH_PASSWORD] + +By default when this command starts it will check for an existing fleet.yml. If that file already exists then +all the above actions will be skipped, because the Elastic Agent has already been enrolled. To ensure that enrollment +occurs on every start of the container set FLEET_FORCE to 1. `, Run: func(c *cobra.Command, args []string) { if err := containerCmd(streams, c, flags, args); err != nil { @@ -109,6 +114,13 @@ func containerCmd(streams *cli.IOStreams, cmd *cobra.Command, flags *globalFlags if err != nil { return err } + + _, err = os.Stat(info.AgentConfigFile()) + if !os.IsNotExist(err) && !envBool("FLEET_FORCE") { + // already enrolled, just run the standard run + return run(flags, streams) + } + // Remove FLEET_SETUP in 8.x // The FLEET_SETUP environment variable boolean is a fallback to the old name. The name was updated to // reflect that its setting up Fleet in Kibana versus setting up Fleet Server.