Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions x-pack/elastic-agent/pkg/agent/cmd/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"} {
Expand All @@ -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")
Expand Down Expand Up @@ -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 {
Expand Down