From 5983d58d7c6ff6127fb74a67944ccc4814789f3a Mon Sep 17 00:00:00 2001 From: Turtle Kalus Date: Tue, 22 Dec 2020 15:48:38 -0800 Subject: [PATCH] Log based on registry known-support - reduce noise on notifications (#716) Log based on registry known-poor support of HEAD in checking container manifest. Some private registries do not support HEAD (E.G. GitLab Container Registry). With the current config, this log message is causing a notification to be sent for each container hosted in a registry lacking HEAD support. log.Debug or log.Warning for failed HTTP HEAD-check based on registry hostname where HEAD-check is known to fail. For Docker Hub, a failed HEAD leading to a "regular pull" may count against a user's call-quota whereas other registry implementations do not support HEAD, or whose container manifest may be in a different location. --- pkg/container/client.go | 6 +++++- pkg/registry/registry.go | 26 ++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/pkg/container/client.go b/pkg/container/client.go index 2063332a0..635aa3e8e 100644 --- a/pkg/container/client.go +++ b/pkg/container/client.go @@ -295,7 +295,11 @@ func (client dockerClient) PullImage(ctx context.Context, container Container) e log.WithFields(fields).Debugf("Checking if pull is needed") if match, err := digest.CompareDigest(container, opts.RegistryAuth); err != nil { - log.Info("Could not do a head request, falling back to regular pull.") + if registry.WarnOnAPIConsumption(container) { + log.WithFields(fields).Warning("Could not do a head request, falling back to regular pull.") + } else { + log.Debug("Could not do a head request, falling back to regular pull.") + } log.Debugf("Reason: %s", err.Error()) } else if match { log.Debug("No pull needed. Skipping image.") diff --git a/pkg/registry/registry.go b/pkg/registry/registry.go index 98eab0e72..9edd66f89 100644 --- a/pkg/registry/registry.go +++ b/pkg/registry/registry.go @@ -1,6 +1,9 @@ package registry import ( + "github.com/containrrr/watchtower/pkg/registry/helpers" + watchtowerTypes "github.com/containrrr/watchtower/pkg/types" + ref "github.com/docker/distribution/reference" "github.com/docker/docker/api/types" log "github.com/sirupsen/logrus" ) @@ -31,3 +34,26 @@ func DefaultAuthHandler() (string, error) { log.Debug("Authentication request was rejected. Trying again without authentication") return "", nil } + +// WarnOnAPIConsumption will return true if the registry is known-expected +// to respond well to HTTP HEAD in checking the container digest -- or if there +// are problems parsing the container hostname. +// Will return false if behavior for container is unknown. +func WarnOnAPIConsumption(container watchtowerTypes.Container) bool { + + normalizedName, err := ref.ParseNormalizedNamed(container.ImageName()) + if err != nil { + return true + } + + containerHost, err := helpers.NormalizeRegistry(normalizedName.String()) + if err != nil { + return true + } + + if containerHost == "index.docker.io" || containerHost == "ghcr.io" { + return true + } + + return false +}