diff --git a/internal/pkg/agent/cmd/container.go b/internal/pkg/agent/cmd/container.go index 321e2fd07b3..345b49794ae 100644 --- a/internal/pkg/agent/cmd/container.go +++ b/internal/pkg/agent/cmd/container.go @@ -318,6 +318,10 @@ func runContainerCmd(streams *cli.IOStreams, cfg setupConfig) error { } err = enroll.Wait() if err != nil { + if errors.Is(err, &ErrDaemonReload{}) { + // there is no daemon to be reloaded on a container. + return nil + } return errors.New("enrollment failed", err) } } diff --git a/internal/pkg/agent/cmd/enroll_cmd.go b/internal/pkg/agent/cmd/enroll_cmd.go index d57c91d0da6..f72183c9745 100644 --- a/internal/pkg/agent/cmd/enroll_cmd.go +++ b/internal/pkg/agent/cmd/enroll_cmd.go @@ -116,6 +116,23 @@ type enrollCmdOption struct { Tags []string `yaml:"omitempty"` } +type ErrDaemonReload struct { + err error +} + +func (e *ErrDaemonReload) Error() string { + return e.err.Error() +} + +func (e *ErrDaemonReload) Unwrap() error { + return e.err +} + +func (e *ErrDaemonReload) Is(err error) bool { + var errDaemonReload *ErrDaemonReload + return errors.As(err, &errDaemonReload) +} + // remoteConfig returns the configuration used to connect the agent to a fleet process. func (e *enrollCmdOption) remoteConfig() (remote.Config, error) { cfg, err := remote.NewConfigFromURL(e.URL) @@ -481,9 +498,15 @@ func (c *enrollCmd) daemonReloadWithBackoff(ctx context.Context) error { return fmt.Errorf("could not reload agent's daemon, all retries failed. Last error: %w", err) } -func (c *enrollCmd) daemonReload(ctx context.Context) error { +func (c *enrollCmd) daemonReload(ctx context.Context) (err error) { + defer func() { + if err != nil { + err = &ErrDaemonReload{err: err} + } + }() + daemon := client.New() - err := daemon.Connect(ctx) + err = daemon.Connect(ctx) if err != nil { return err } diff --git a/internal/pkg/agent/cmd/enroll_cmd_test.go b/internal/pkg/agent/cmd/enroll_cmd_test.go index b38d89f9cf2..ca62f82024d 100644 --- a/internal/pkg/agent/cmd/enroll_cmd_test.go +++ b/internal/pkg/agent/cmd/enroll_cmd_test.go @@ -8,6 +8,7 @@ import ( "bytes" "context" "crypto/tls" + "fmt" "io" "io/ioutil" "net" @@ -444,6 +445,13 @@ func TestValidateEnrollFlags(t *testing.T) { }) } +func TestErrDaemonReload(t *testing.T) { + err := fmt.Errorf("some wrapping: %w", + &ErrDaemonReload{err: errors.New("some error")}) + + assert.True(t, errors.Is(err, &ErrDaemonReload{})) +} + func withServer( m func(t *testing.T) *http.ServeMux, test func(t *testing.T, host string),