Skip to content

Commit f508c92

Browse files
authored
* feat: custom user agent (#990)
* 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
1 parent b196629 commit f508c92

File tree

6 files changed

+59
-12
lines changed

6 files changed

+59
-12
lines changed

build.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
VERSION=$(git describe --tags)
44
echo "Building $VERSION..."
5-
go build -o watchtower -ldflags "-X github.com/containrrr/watchtower/cmd.version=$VERSION"
5+
go build -o watchtower -ldflags "-X github.com/containrrr/watchtower/internal/meta.Version=$VERSION"

cmd/root.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cmd
22

33
import (
4+
"github.com/containrrr/watchtower/internal/meta"
45
"math"
56
"os"
67
"os/signal"
@@ -39,7 +40,6 @@ var (
3940
rollingRestart bool
4041
scope string
4142
// Set on build using ldflags
42-
version = "v0.0.0-unknown"
4343
)
4444

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

276-
log.Info("Watchtower ", version, "\n", notifs, "\n", filtering, "\n", schedMessage)
276+
log.Info("Watchtower ", meta.Version, "\n", notifs, "\n", filtering, "\n", schedMessage)
277277
if log.IsLevelEnabled(log.TraceLevel) {
278278
log.Warn("trace level enabled: log will include sensitive information as credentials and tokens")
279279
}

goreleaser.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ build:
1010
- arm
1111
- arm64
1212
ldflags:
13-
- -s -w -X github.com/containrrr/watchtower/cmd.version={{.Version}}
13+
- -s -w -X github.com/containrrr/watchtower/internal/meta.Version={{.Version}}
1414
archives:
1515
-
1616
name_template: "{{.ProjectName}}_{{.Os}}_{{.Arch}}"

internal/meta/meta.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package meta
2+
3+
var (
4+
// Version is the compile-time set version of Watchtower
5+
Version = "v0.0.0-unknown"
6+
7+
// UserAgent is the http client identifier derived from Version
8+
UserAgent string
9+
)
10+
11+
func init() {
12+
UserAgent = "Watchtower/" + Version
13+
}

pkg/registry/digest/digest.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"encoding/json"
77
"errors"
88
"fmt"
9+
"github.com/containrrr/watchtower/internal/meta"
910
"github.com/containrrr/watchtower/pkg/registry/auth"
1011
"github.com/containrrr/watchtower/pkg/registry/manifest"
1112
"github.com/containrrr/watchtower/pkg/types"
@@ -86,6 +87,7 @@ func GetDigest(url string, token string) (string, error) {
8687
client := &http.Client{Transport: tr}
8788

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

9092
if token != "" {
9193
logrus.WithField("token", token).Trace("Setting request token")

pkg/registry/digest/digest_test.go

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
wtTypes "github.com/containrrr/watchtower/pkg/types"
88
. "github.com/onsi/ginkgo"
99
. "github.com/onsi/gomega"
10+
"github.com/onsi/gomega/ghttp"
11+
"net/http"
1012
"os"
1113
"testing"
1214
"time"
@@ -18,14 +20,16 @@ func TestDigest(t *testing.T) {
1820
RunSpecs(GinkgoT(), "Digest Suite")
1921
}
2022

21-
var DockerHubCredentials = &wtTypes.RegistryCredentials{
22-
Username: os.Getenv("CI_INTEGRATION_TEST_REGISTRY_DH_USERNAME"),
23-
Password: os.Getenv("CI_INTEGRATION_TEST_REGISTRY_DH_PASSWORD"),
24-
}
25-
var GHCRCredentials = &wtTypes.RegistryCredentials{
26-
Username: os.Getenv("CI_INTEGRATION_TEST_REGISTRY_GH_USERNAME"),
27-
Password: os.Getenv("CI_INTEGRATION_TEST_REGISTRY_GH_PASSWORD"),
28-
}
23+
var (
24+
DockerHubCredentials = &wtTypes.RegistryCredentials{
25+
Username: os.Getenv("CI_INTEGRATION_TEST_REGISTRY_DH_USERNAME"),
26+
Password: os.Getenv("CI_INTEGRATION_TEST_REGISTRY_DH_PASSWORD"),
27+
}
28+
GHCRCredentials = &wtTypes.RegistryCredentials{
29+
Username: os.Getenv("CI_INTEGRATION_TEST_REGISTRY_GH_USERNAME"),
30+
Password: os.Getenv("CI_INTEGRATION_TEST_REGISTRY_GH_PASSWORD"),
31+
}
32+
)
2933

3034
func SkipIfCredentialsEmpty(credentials *wtTypes.RegistryCredentials, fn func()) func() {
3135
if credentials.Username == "" {
@@ -84,4 +88,32 @@ var _ = Describe("Digests", func() {
8488
}),
8589
)
8690
})
91+
When("sending a HEAD request", func() {
92+
var server *ghttp.Server
93+
BeforeEach(func() {
94+
server = ghttp.NewServer()
95+
})
96+
AfterEach(func() {
97+
server.Close()
98+
})
99+
It("should use a custom user-agent", func() {
100+
server.AppendHandlers(
101+
ghttp.CombineHandlers(
102+
ghttp.VerifyHeader(http.Header{
103+
"User-Agent": []string{"Watchtower/v0.0.0-unknown"},
104+
}),
105+
ghttp.RespondWith(http.StatusOK, "", http.Header{
106+
digest.ContentDigestHeader: []string{
107+
mockDigest,
108+
},
109+
}),
110+
),
111+
)
112+
dig, err := digest.GetDigest(server.URL(), "token")
113+
println(dig)
114+
Expect(server.ReceivedRequests()).Should(HaveLen(1))
115+
Expect(err).NotTo(HaveOccurred())
116+
Expect(dig).To(Equal(mockDigest))
117+
})
118+
})
87119
})

0 commit comments

Comments
 (0)