Skip to content

Commit

Permalink
* feat: custom user agent (#990)
Browse files Browse the repository at this point in the history
* fix: move build meta to own package
  this allows it to be referenced from other packages without causing a cyclic dependency
* feat: custom user agent
  • Loading branch information
piksel authored Jun 11, 2021
1 parent b196629 commit f508c92
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 12 deletions.
2 changes: 1 addition & 1 deletion build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

VERSION=$(git describe --tags)
echo "Building $VERSION..."
go build -o watchtower -ldflags "-X github.com/containrrr/watchtower/cmd.version=$VERSION"
go build -o watchtower -ldflags "-X github.com/containrrr/watchtower/internal/meta.Version=$VERSION"
4 changes: 2 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"github.com/containrrr/watchtower/internal/meta"
"math"
"os"
"os/signal"
Expand Down Expand Up @@ -39,7 +40,6 @@ var (
rollingRestart bool
scope string
// Set on build using ldflags
version = "v0.0.0-unknown"
)

var rootCmd = NewRootCommand()
Expand Down Expand Up @@ -273,7 +273,7 @@ func writeStartupMessage(c *cobra.Command, sched time.Time, filtering string) {
notifs = "Using notifications: " + notifList
}

log.Info("Watchtower ", version, "\n", notifs, "\n", filtering, "\n", schedMessage)
log.Info("Watchtower ", meta.Version, "\n", notifs, "\n", filtering, "\n", schedMessage)
if log.IsLevelEnabled(log.TraceLevel) {
log.Warn("trace level enabled: log will include sensitive information as credentials and tokens")
}
Expand Down
2 changes: 1 addition & 1 deletion goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ build:
- arm
- arm64
ldflags:
- -s -w -X github.com/containrrr/watchtower/cmd.version={{.Version}}
- -s -w -X github.com/containrrr/watchtower/internal/meta.Version={{.Version}}
archives:
-
name_template: "{{.ProjectName}}_{{.Os}}_{{.Arch}}"
Expand Down
13 changes: 13 additions & 0 deletions internal/meta/meta.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package meta

var (
// Version is the compile-time set version of Watchtower
Version = "v0.0.0-unknown"

// UserAgent is the http client identifier derived from Version
UserAgent string
)

func init() {
UserAgent = "Watchtower/" + Version
}
2 changes: 2 additions & 0 deletions pkg/registry/digest/digest.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/containrrr/watchtower/internal/meta"
"github.com/containrrr/watchtower/pkg/registry/auth"
"github.com/containrrr/watchtower/pkg/registry/manifest"
"github.com/containrrr/watchtower/pkg/types"
Expand Down Expand Up @@ -86,6 +87,7 @@ func GetDigest(url string, token string) (string, error) {
client := &http.Client{Transport: tr}

req, _ := http.NewRequest("HEAD", url, nil)
req.Header.Set("User-Agent", meta.UserAgent)

if token != "" {
logrus.WithField("token", token).Trace("Setting request token")
Expand Down
48 changes: 40 additions & 8 deletions pkg/registry/digest/digest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
wtTypes "github.com/containrrr/watchtower/pkg/types"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/ghttp"
"net/http"
"os"
"testing"
"time"
Expand All @@ -18,14 +20,16 @@ func TestDigest(t *testing.T) {
RunSpecs(GinkgoT(), "Digest Suite")
}

var DockerHubCredentials = &wtTypes.RegistryCredentials{
Username: os.Getenv("CI_INTEGRATION_TEST_REGISTRY_DH_USERNAME"),
Password: os.Getenv("CI_INTEGRATION_TEST_REGISTRY_DH_PASSWORD"),
}
var GHCRCredentials = &wtTypes.RegistryCredentials{
Username: os.Getenv("CI_INTEGRATION_TEST_REGISTRY_GH_USERNAME"),
Password: os.Getenv("CI_INTEGRATION_TEST_REGISTRY_GH_PASSWORD"),
}
var (
DockerHubCredentials = &wtTypes.RegistryCredentials{
Username: os.Getenv("CI_INTEGRATION_TEST_REGISTRY_DH_USERNAME"),
Password: os.Getenv("CI_INTEGRATION_TEST_REGISTRY_DH_PASSWORD"),
}
GHCRCredentials = &wtTypes.RegistryCredentials{
Username: os.Getenv("CI_INTEGRATION_TEST_REGISTRY_GH_USERNAME"),
Password: os.Getenv("CI_INTEGRATION_TEST_REGISTRY_GH_PASSWORD"),
}
)

func SkipIfCredentialsEmpty(credentials *wtTypes.RegistryCredentials, fn func()) func() {
if credentials.Username == "" {
Expand Down Expand Up @@ -84,4 +88,32 @@ var _ = Describe("Digests", func() {
}),
)
})
When("sending a HEAD request", func() {
var server *ghttp.Server
BeforeEach(func() {
server = ghttp.NewServer()
})
AfterEach(func() {
server.Close()
})
It("should use a custom user-agent", func() {
server.AppendHandlers(
ghttp.CombineHandlers(
ghttp.VerifyHeader(http.Header{
"User-Agent": []string{"Watchtower/v0.0.0-unknown"},
}),
ghttp.RespondWith(http.StatusOK, "", http.Header{
digest.ContentDigestHeader: []string{
mockDigest,
},
}),
),
)
dig, err := digest.GetDigest(server.URL(), "token")
println(dig)
Expect(server.ReceivedRequests()).Should(HaveLen(1))
Expect(err).NotTo(HaveOccurred())
Expect(dig).To(Equal(mockDigest))
})
})
})

0 comments on commit f508c92

Please sign in to comment.