Skip to content

Commit

Permalink
ignore enroll cmd failing to restart daemon if running on a container
Browse files Browse the repository at this point in the history
  • Loading branch information
AndersonQ committed Oct 18, 2023
1 parent defde80 commit 7ab23e6
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
4 changes: 4 additions & 0 deletions internal/pkg/agent/cmd/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
27 changes: 25 additions & 2 deletions internal/pkg/agent/cmd/enroll_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down
8 changes: 8 additions & 0 deletions internal/pkg/agent/cmd/enroll_cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"bytes"
"context"
"crypto/tls"
"fmt"
"io"
"io/ioutil"
"net"
Expand Down Expand Up @@ -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),
Expand Down

0 comments on commit 7ab23e6

Please sign in to comment.