diff --git a/daemon/events/testutils/testutils.go b/daemon/events/testutils/testutils.go index 408514b2e6..ebda7ac7d6 100644 --- a/daemon/events/testutils/testutils.go +++ b/daemon/events/testutils/testutils.go @@ -2,26 +2,25 @@ package testutils // import "github.com/docker/docker/daemon/events/testutils" import ( "fmt" - "regexp" "strings" "time" "github.com/docker/docker/api/types/events" timetypes "github.com/docker/docker/api/types/time" + "github.com/docker/docker/internal/lazyregexp" ) -var ( +const ( reTimestamp = `(?P\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{9}(:?(:?(:?-|\+)\d{2}:\d{2})|Z))` reEventType = `(?P\w+)` reAction = `(?P\w+)` reID = `(?P[^\s]+)` reAttributes = `(\s\((?P[^\)]+)\))?` - reString = fmt.Sprintf(`\A%s\s%s\s%s\s%s%s\z`, reTimestamp, reEventType, reAction, reID, reAttributes) - - // eventCliRegexp is a regular expression that matches all possible event outputs in the cli - eventCliRegexp = regexp.MustCompile(reString) ) +// eventCliRegexp is a regular expression that matches all possible event outputs in the cli +var eventCliRegexp = lazyregexp.New(fmt.Sprintf(`\A%s\s%s\s%s\s%s%s\z`, reTimestamp, reEventType, reAction, reID, reAttributes)) + // ScanMap turns an event string like the default ones formatted in the cli output // and turns it into map. func ScanMap(text string) map[string]string { diff --git a/daemon/logger/journald/internal/fake/sender.go b/daemon/logger/journald/internal/fake/sender.go index 4fa6866f81..5af0178d25 100644 --- a/daemon/logger/journald/internal/fake/sender.go +++ b/daemon/logger/journald/internal/fake/sender.go @@ -15,13 +15,13 @@ import ( "fmt" "os" "os/exec" - "regexp" "strconv" "testing" "time" "code.cloudfoundry.org/clock" "github.com/coreos/go-systemd/v22/journal" + "github.com/docker/docker/internal/lazyregexp" "github.com/google/uuid" "gotest.tools/v3/assert" @@ -109,7 +109,7 @@ func NewT(t *testing.T, outpath string) *Sender { return s } -var validVarName = regexp.MustCompile("^[A-Z0-9][A-Z0-9_]*$") +var validVarName = lazyregexp.New("^[A-Z0-9][A-Z0-9_]*$") // Send is a drop-in replacement for // github.com/coreos/go-systemd/v22/journal.Send. diff --git a/daemon/prune.go b/daemon/prune.go index 9634d436aa..3c1fd75569 100644 --- a/daemon/prune.go +++ b/daemon/prune.go @@ -2,7 +2,6 @@ package daemon // import "github.com/docker/docker/daemon" import ( "context" - "regexp" "strconv" "time" @@ -15,6 +14,7 @@ import ( timetypes "github.com/docker/docker/api/types/time" networkSettings "github.com/docker/docker/daemon/network" "github.com/docker/docker/errdefs" + "github.com/docker/docker/internal/lazyregexp" "github.com/docker/docker/libnetwork" "github.com/pkg/errors" ) @@ -136,6 +136,8 @@ func (daemon *Daemon) localNetworksPrune(ctx context.Context, pruneFilters filte return rep } +var networkIsInUse = lazyregexp.New(`network ([[:alnum:]]+) is in use`) + // clusterNetworksPrune removes unused cluster networks func (daemon *Daemon) clusterNetworksPrune(ctx context.Context, pruneFilters filters.Args) (*network.PruneReport, error) { rep := &network.PruneReport{} @@ -152,7 +154,7 @@ func (daemon *Daemon) clusterNetworksPrune(ctx context.Context, pruneFilters fil if err != nil { return rep, err } - networkIsInUse := regexp.MustCompile(`network ([[:alnum:]]+) is in use`) + for _, nw := range networks { select { case <-ctx.Done(): diff --git a/daemon/top_unix.go b/daemon/top_unix.go index 75b862ad30..fae2183795 100644 --- a/daemon/top_unix.go +++ b/daemon/top_unix.go @@ -7,24 +7,26 @@ import ( "context" "fmt" "os/exec" - "regexp" "strconv" "strings" "github.com/docker/docker/api/types/container" "github.com/docker/docker/api/types/events" "github.com/docker/docker/errdefs" + "github.com/docker/docker/internal/lazyregexp" libcontainerdtypes "github.com/docker/docker/libcontainerd/types" "github.com/pkg/errors" ) +// NOTE: \\s does not detect unicode whitespaces. +// So we use fieldsASCII instead of strings.Fields in parsePSOutput. +// See https://github.com/docker/docker/pull/24358 +// +//nolint:gosimple +var psArgsRegexp = lazyregexp.New("\\s+([^\\s]*)=\\s*(PID[^\\s]*)") + func validatePSArgs(psArgs string) error { - // NOTE: \\s does not detect unicode whitespaces. - // So we use fieldsASCII instead of strings.Fields in parsePSOutput. - // See https://github.com/docker/docker/pull/24358 - //nolint: gosimple - re := regexp.MustCompile("\\s+([^\\s]*)=\\s*(PID[^\\s]*)") - for _, group := range re.FindAllStringSubmatch(psArgs, -1) { + for _, group := range psArgsRegexp.FindAllStringSubmatch(psArgs, -1) { if len(group) >= 3 { k := group[1] v := group[2]