Skip to content

Commit

Permalink
Do not reload the Agent daemon if enrolling from a container
Browse files Browse the repository at this point in the history
The enroll command would always try to restart the daemon, however
when enrolling as part of the container command, there is no running
daemon to reload.

This commit adds a CLI flag, --skip-daemon-reload, to the enroll
command to skip the reloading step, the container command now makes
use of this flag.
  • Loading branch information
belimawr committed Nov 24, 2023
1 parent 6249b7b commit eb35ba8
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ summary: Surface errors during Agent's enroll process, failing if any happens.
# NOTE: This field will be rendered only for breaking-change and known-issue kinds at the moment.
#description:

# Affected component; a word indicating the component this changeset affects.
component: install/enroll
# Affected component; usually one of "elastic-agent", "fleet-server", "filebeat", "metricbeat", "auditbeat", "all", etc.
component: elastic-agent

# PR URL; optional; the PR number that added the changeset.
# If not present is automatically filled by the tooling finding the PR where this changelog fragment has been added.
# NOTE: the tooling supports backports, so it's able to fill the original PR number instead of the backport PR number.
# Please provide it if you are adding a fragment for a different PR.
pr: https://github.com/elastic/elastic-agent/pull/3207
pr: https://github.com/elastic/elastic-agent/pull/3815/

# Issue URL; optional; the GitHub issue related to this changeset (either closes or is part of).
# If not present is automatically filled by the tooling with the issue linked to the PR number.
#issue: https://github.com/owner/repo/1234
issue: https://github.com/elastic/elastic-agent/issues/3664
1 change: 1 addition & 0 deletions internal/pkg/agent/cmd/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,7 @@ func buildEnrollArgs(cfg setupConfig, token string, policyID string) ([]string,
"--path.home", paths.Top(), // --path.home actually maps to paths.Top()
"--path.config", paths.Config(),
"--path.logs", paths.Logs(),
"--skip-daemon-reload",
}
if paths.Downloads() != "" {
args = append(args, "--path.downloads", paths.Downloads())
Expand Down
7 changes: 7 additions & 0 deletions internal/pkg/agent/cmd/enroll.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ func addEnrollFlags(cmd *cobra.Command) {
cmd.Flags().BoolP("delay-enroll", "", false, "Delays enrollment to occur on first start of the Elastic Agent service")
cmd.Flags().DurationP("daemon-timeout", "", 0, "Timeout waiting for Elastic Agent daemon")
cmd.Flags().DurationP("fleet-server-timeout", "", 0, "Timeout waiting for Fleet Server to be ready to start enrollment")
cmd.Flags().Bool("skip-daemon-reload", false, "Skip daemon reload after enrolling")
cmd.Flags().StringSliceP("tag", "", []string{}, "User set tags")
}

Expand Down Expand Up @@ -141,6 +142,7 @@ func buildEnrollmentFlags(cmd *cobra.Command, url string, token string) []string
delayEnroll, _ := cmd.Flags().GetBool("delay-enroll")
daemonTimeout, _ := cmd.Flags().GetDuration("daemon-timeout")
fTimeout, _ := cmd.Flags().GetDuration("fleet-server-timeout")
skipDaemonReload, _ := cmd.Flags().GetBool("skip-daemon-reload")
fTags, _ := cmd.Flags().GetStringSlice("tag")
args := []string{}
if url != "" {
Expand Down Expand Up @@ -249,6 +251,9 @@ func buildEnrollmentFlags(cmd *cobra.Command, url string, token string) []string
args = append(args, "--fleet-server-es-insecure")
}

if skipDaemonReload {
args = append(args, "--skip-daemon-reload")
}
for _, v := range fTags {
args = append(args, "--tag", v)
}
Expand Down Expand Up @@ -338,6 +343,7 @@ func enroll(streams *cli.IOStreams, cmd *cobra.Command) error {
delayEnroll, _ := cmd.Flags().GetBool("delay-enroll")
daemonTimeout, _ := cmd.Flags().GetDuration("daemon-timeout")
fTimeout, _ := cmd.Flags().GetDuration("fleet-server-timeout")
skipDaemonReload, _ := cmd.Flags().GetBool("skip-daemon-reload")
tags, _ := cmd.Flags().GetStringSlice("tag")

caStr, _ := cmd.Flags().GetString("certificate-authorities")
Expand Down Expand Up @@ -370,6 +376,7 @@ func enroll(streams *cli.IOStreams, cmd *cobra.Command) error {
ProxyHeaders: mapFromEnvList(proxyHeaders),
DelayEnroll: delayEnroll,
DaemonTimeout: daemonTimeout,
SkipDaemonRestart: skipDaemonReload,
Tags: tags,
FleetServer: enrollCmdFleetServerOption{
ConnStr: fServer,
Expand Down
3 changes: 2 additions & 1 deletion internal/pkg/agent/cmd/enroll_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ type enrollCmdOption struct {
DelayEnroll bool `yaml:"-"`
FleetServer enrollCmdFleetServerOption `yaml:"-"`
SkipCreateSecret bool `yaml:"-"`
SkipDaemonRestart bool `yaml:"-"`
Tags []string `yaml:"omitempty"`
}

Expand Down Expand Up @@ -277,7 +278,7 @@ func (c *enrollCmd) Execute(ctx context.Context, streams *cli.IOStreams) error {
}
}()

if c.agentProc == nil {
if c.agentProc == nil && !c.options.SkipDaemonRestart {
if err = c.daemonReloadWithBackoff(ctx); err != nil {
c.log.Errorf("Elastic Agent might not be running; unable to trigger restart: %v", err)
return fmt.Errorf("could not reload agent daemon, unable to trigger restart: %w", err)
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/agent/install/perms_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func FixPermissions(topPath string, ownership utils.FileOwner) error {

// remove any world permissions from the file
if err := os.Chmod(name, info.Mode().Perm()&0770); err != nil {
return fmt.Errorf("could not update permissions of %q: %w:", topPath, err)
return fmt.Errorf("could not update permissions of %q: %w", topPath, err)
}

return nil
Expand Down

0 comments on commit eb35ba8

Please sign in to comment.