Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(banner): standardize user agent and output #5154

Merged
merged 13 commits into from
Mar 24, 2025
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,10 @@ IMAGE_STAGING = gcr.io/k8s-staging-external-dns/$(BINARY)
REGISTRY ?= us.gcr.io/k8s-artifacts-prod/external-dns
IMAGE ?= $(REGISTRY)/$(BINARY)
VERSION ?= $(shell git describe --tags --always --dirty --match "v*")
GIT_COMMIT ?= $(shell git rev-parse --short HEAD)
BUILD_FLAGS ?= -v
LDFLAGS ?= -X sigs.k8s.io/external-dns/pkg/apis/externaldns.Version=$(VERSION) -w -s
LDFLAGS += -X sigs.k8s.io/external-dns/pkg/apis/externaldns.GitCommit=$(GIT_COMMIT)
ARCH ?= amd64
SHELL = /bin/bash
IMG_PLATFORM ?= linux/amd64,linux/arm64,linux/arm/v7
Expand Down
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ func main() {
klog.SetLogger(logr.Discard())
}

log.Info(externaldns.Banner())

ctx, cancel := context.WithCancel(context.Background())

go serveMetrics(cfg.MetricsAddress)
Expand Down Expand Up @@ -258,7 +260,7 @@ func main() {
case "ovh":
p, err = ovh.NewOVHProvider(ctx, domainFilter, cfg.OVHEndpoint, cfg.OVHApiRateLimit, cfg.DryRun)
case "linode":
p, err = linode.NewLinodeProvider(domainFilter, cfg.DryRun, externaldns.Version)
p, err = linode.NewLinodeProvider(domainFilter, cfg.DryRun)
case "dnsimple":
p, err = dnsimple.NewDnsimpleProvider(domainFilter, zoneIDFilter, cfg.DryRun)
case "coredns", "skydns":
Expand Down
3 changes: 0 additions & 3 deletions pkg/apis/externaldns/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@ const (
passwordMask = "******"
)

// Version is the current version of the app, generated at build time
var Version = "unknown"

// Config is a project-wide configuration
type Config struct {
APIServerURL string
Expand Down
48 changes: 48 additions & 0 deletions pkg/apis/externaldns/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
Copyright 2025 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package externaldns

import (
"fmt"
"runtime"
)

const (
bannerTemplate = `GitCommitShort=%s, GoVersion=%s, Platform=%s, UserAgent=%s`
)

var (
Version = "unknown" // Set at the build time via `-ldflags "-X main.Version=<value>"`
GitCommit = "unknown" // Set at the build time via `-ldflags "-X main.GitCommitSHA=<value>"`
UserAgentProduct = "ExternalDNS"
goVersion = runtime.Version()
)

func UserAgent() string {
return fmt.Sprintf("%s/%s", UserAgentProduct, Version)
}

func Banner() string {
platform := fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH)
return fmt.Sprintf(
bannerTemplate,
GitCommit,
goVersion,
platform,
UserAgent(),
)
}
35 changes: 35 additions & 0 deletions pkg/apis/externaldns/version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
Copyright 2025 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package externaldns

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestBanner(t *testing.T) {
// Set variables to known values
Version = "1.0.0"
goVersion = "go1.17"
GitCommit = "49a0c57c7"

want := Banner()
assert.Contains(t, want, "GoVersion=go1.17")
assert.Contains(t, want, "GitCommitShort=49a0c57c7")
assert.Contains(t, want, "UserAgent=ExternalDNS/1.0.0")
}
2 changes: 1 addition & 1 deletion provider/civo/civo.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func NewCivoProvider(domainFilter endpoint.DomainFilter, dryRun bool) (*CivoProv
}

userAgent := &civogo.Component{
Name: "external-dns",
Name: externaldns.UserAgentProduct,
Version: externaldns.Version,
}
civoClient.SetUserAgent(userAgent)
Expand Down
2 changes: 1 addition & 1 deletion provider/digitalocean/digital_ocean.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func NewDigitalOceanProvider(ctx context.Context, domainFilter endpoint.DomainFi
oauthClient := oauth2.NewClient(ctx, oauth2.StaticTokenSource(&oauth2.Token{
AccessToken: token,
}))
client, err := godo.New(oauthClient, godo.SetUserAgent("ExternalDNS/"+externaldns.Version))
client, err := godo.New(oauthClient, godo.SetUserAgent(externaldns.UserAgent()))
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion provider/dnsimple/dnsimple.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func NewDnsimpleProvider(domainFilter endpoint.DomainFilter, zoneIDFilter provid
tc := oauth2.NewClient(context.Background(), ts)

client := dnsimple.NewClient(tc)
client.SetUserAgent(fmt.Sprintf("Kubernetes ExternalDNS/%s", externaldns.Version))
client.SetUserAgent(externaldns.UserAgent())

provider := &dnsimpleProvider{
client: dnsimpleZoneService{service: client.Zones},
Expand Down
2 changes: 1 addition & 1 deletion provider/godaddy/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ func (c *Client) NewRequest(method, path string, reqBody interface{}) (*http.Req
}
req.Header.Set("Authorization", fmt.Sprintf("sso-key %s:%s", c.APIKey, c.APISecret))
req.Header.Set("Accept", "application/json")
req.Header.Set("User-Agent", "ExternalDNS/"+externaldns.Version)
req.Header.Set("User-Agent", externaldns.UserAgent())

// Send the request with requested timeout
c.Client.Timeout = c.Timeout
Expand Down
6 changes: 4 additions & 2 deletions provider/linode/linode.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import (
"sigs.k8s.io/external-dns/endpoint"
"sigs.k8s.io/external-dns/plan"
"sigs.k8s.io/external-dns/provider"

"sigs.k8s.io/external-dns/pkg/apis/externaldns"
)

// LinodeDomainClient interface to ease testing
Expand Down Expand Up @@ -77,7 +79,7 @@ type LinodeChangeDelete struct {
}

// NewLinodeProvider initializes a new Linode DNS based Provider.
func NewLinodeProvider(domainFilter endpoint.DomainFilter, dryRun bool, appVersion string) (*LinodeProvider, error) {
func NewLinodeProvider(domainFilter endpoint.DomainFilter, dryRun bool) (*LinodeProvider, error) {
token, ok := os.LookupEnv("LINODE_TOKEN")
if !ok {
return nil, fmt.Errorf("no token found")
Expand All @@ -92,7 +94,7 @@ func NewLinodeProvider(domainFilter endpoint.DomainFilter, dryRun bool, appVersi
}

linodeClient := linodego.NewClient(oauth2Client)
linodeClient.SetUserAgent(fmt.Sprintf("ExternalDNS/%s linodego/%s", appVersion, linodego.Version))
linodeClient.SetUserAgent(fmt.Sprintf("%s linodego/%s", externaldns.UserAgent(), linodego.Version))

provider := &LinodeProvider{
Client: &linodeClient,
Expand Down
4 changes: 2 additions & 2 deletions provider/linode/linode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,11 @@ func TestLinodeConvertRecordType(t *testing.T) {

func TestNewLinodeProvider(t *testing.T) {
_ = os.Setenv("LINODE_TOKEN", "xxxxxxxxxxxxxxxxx")
_, err := NewLinodeProvider(endpoint.NewDomainFilter([]string{"ext-dns-test.zalando.to."}), true, "1.0")
_, err := NewLinodeProvider(endpoint.NewDomainFilter([]string{"ext-dns-test.zalando.to."}), true)
require.NoError(t, err)

_ = os.Unsetenv("LINODE_TOKEN")
_, err = NewLinodeProvider(endpoint.NewDomainFilter([]string{"ext-dns-test.zalando.to."}), true, "1.0")
_, err = NewLinodeProvider(endpoint.NewDomainFilter([]string{"ext-dns-test.zalando.to."}), true)
require.Error(t, err)
}

Expand Down
2 changes: 1 addition & 1 deletion provider/ovh/ovh.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func NewOVHProvider(ctx context.Context, domainFilter endpoint.DomainFilter, end
return nil, err
}

client.UserAgent = externaldns.Version
client.UserAgent = externaldns.UserAgent()

// TODO: Add Dry Run support
if dryRun {
Expand Down
2 changes: 1 addition & 1 deletion provider/scaleway/scaleway.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func NewScalewayProvider(ctx context.Context, domainFilter endpoint.DomainFilter
scwClient, err := scw.NewClient(
scw.WithProfile(p),
scw.WithEnv(),
scw.WithUserAgent("ExternalDNS/"+externaldns.Version),
scw.WithUserAgent(externaldns.UserAgent()),
scw.WithDefaultPageSize(uint32(defaultPageSize)),
)
if err != nil {
Expand Down
Loading