From 005d362a7a3d3e308ed215b9e3aa43761c6cfef0 Mon Sep 17 00:00:00 2001 From: Silvia Mitter Date: Wed, 14 Apr 2021 21:40:37 +0200 Subject: [PATCH] [elastic-agent] ensure container is backwards compatible (#25092) only run setup and the elastic agent if configuration is provided when in cloud mode closes #25005 (cherry picked from commit cc7fcd1d9ffe520965171d773c0483d8c996bbee) --- x-pack/elastic-agent/pkg/agent/cmd/container.go | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/x-pack/elastic-agent/pkg/agent/cmd/container.go b/x-pack/elastic-agent/pkg/agent/cmd/container.go index 83ae0cf1039..9dfe03a9241 100644 --- a/x-pack/elastic-agent/pkg/agent/cmd/container.go +++ b/x-pack/elastic-agent/pkg/agent/cmd/container.go @@ -143,6 +143,9 @@ func containerCmd(streams *cli.IOStreams, cmd *cobra.Command) error { return err } + elasticCloud := envBool("ELASTIC_AGENT_CLOUD") + // if not in cloud mode, always run the agent + runAgent := !elasticCloud // create access configuration from ENV and config files cfg := defaultAccessConfig() for _, f := range []string{"fleet-setup.yml", "credentials.yml"} { @@ -155,13 +158,14 @@ func containerCmd(streams *cli.IOStreams, cmd *cobra.Command) error { if err != nil { return fmt.Errorf("unpacking config file(%s): %s", f, err) } + // if in elastic cloud mode, only run the agent when configured + runAgent = true } } // start apm-server legacy process when in cloud mode var wg sync.WaitGroup var apmProc *process.Info - _, elasticCloud := os.LookupEnv("ELASTIC_AGENT_CLOUD") apmPath := os.Getenv("APM_SERVER_PATH") if elasticCloud { logInfo(streams, "Starting in elastic cloud mode") @@ -202,13 +206,18 @@ func containerCmd(streams *cli.IOStreams, cmd *cobra.Command) error { apmProc.Stop() logInfo(streams, "Initiate shutdown legacy apm-server.") } - wg.Wait() }() } } - // run the main elastic-agent container command - return runContainerCmd(streams, cmd, cfg) + var err error + if runAgent { + // run the main elastic-agent container command + err = runContainerCmd(streams, cmd, cfg) + } + // wait until APM Server shut down + wg.Wait() + return err } func runContainerCmd(streams *cli.IOStreams, cmd *cobra.Command, cfg setupConfig) error {