Skip to content

Commit 07bff8a

Browse files
authored
Extend the user agent and fix the build version for the listener app (actions#2892)
1 parent ea2fb32 commit 07bff8a

19 files changed

+186
-94
lines changed

.github/workflows/arc-publish.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ on:
1818
default: false
1919

2020
permissions:
21-
contents: write
21+
contents: write
2222
packages: write
2323

2424
env:
@@ -34,16 +34,16 @@ jobs:
3434
name: Release
3535
runs-on: ubuntu-latest
3636
# gha-runner-scale-set has its own release workflow.
37-
# We don't want to publish a new actions-runner-controller image
37+
# We don't want to publish a new actions-runner-controller image
3838
# we release gha-runner-scale-set.
3939
if: ${{ !startsWith(github.event.inputs.release_tag_name, 'gha-runner-scale-set-') }}
4040
steps:
4141
- name: Checkout
4242
uses: actions/checkout@v3
4343

44-
- uses: actions/setup-go@v3
44+
- uses: actions/setup-go@v4
4545
with:
46-
go-version: '1.18.2'
46+
go-version-file: 'go.mod'
4747

4848
- name: Install tools
4949
run: |

Dockerfile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ RUN go mod download
2424
# With the above commmand,
2525
# TARGETOS can be "linux", TARGETARCH can be "amd64", "arm64", and "arm", TARGETVARIANT can be "v7".
2626

27-
ARG TARGETPLATFORM TARGETOS TARGETARCH TARGETVARIANT VERSION=dev
27+
ARG TARGETPLATFORM TARGETOS TARGETARCH TARGETVARIANT VERSION=dev COMMIT_SHA=dev
2828

2929
# We intentionally avoid `--mount=type=cache,mode=0777,target=/go/pkg/mod` in the `go mod download` and the `go build` runs
3030
# to avoid https://github.com/moby/buildkit/issues/2334
@@ -36,8 +36,8 @@ ENV GOCACHE /build/${TARGETPLATFORM}/root/.cache/go-build
3636
RUN --mount=target=. \
3737
--mount=type=cache,mode=0777,target=${GOCACHE} \
3838
export GOOS=${TARGETOS} GOARCH=${TARGETARCH} GOARM=${TARGETVARIANT#v} && \
39-
go build -trimpath -ldflags="-s -w -X 'github.com/actions/actions-runner-controller/build.Version=${VERSION}'" -o /out/manager main.go && \
40-
go build -trimpath -ldflags="-s -w" -o /out/github-runnerscaleset-listener ./cmd/githubrunnerscalesetlistener && \
39+
go build -trimpath -ldflags="-s -w -X 'github.com/actions/actions-runner-controller/build.Version=${VERSION}' -X 'github.com/actions/actions-runner-controller/build.CommitSHA=${COMMIT_SHA}'" -o /out/manager main.go && \
40+
go build -trimpath -ldflags="-s -w -X 'github.com/actions/actions-runner-controller/build.Version=${VERSION}' -X 'github.com/actions/actions-runner-controller/build.CommitSHA=${COMMIT_SHA}'" -o /out/github-runnerscaleset-listener ./cmd/githubrunnerscalesetlistener && \
4141
go build -trimpath -ldflags="-s -w" -o /out/github-webhook-server ./cmd/githubwebhookserver && \
4242
go build -trimpath -ldflags="-s -w" -o /out/actions-metrics-server ./cmd/actionsmetricsserver && \
4343
go build -trimpath -ldflags="-s -w" -o /out/sleep ./cmd/sleep

Makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ else
55
endif
66
DOCKER_USER ?= $(shell echo ${DOCKER_IMAGE_NAME} | cut -d / -f1)
77
VERSION ?= dev
8+
COMMIT_SHA = $(shell git rev-parse HEAD)
89
RUNNER_VERSION ?= 2.309.0
910
TARGETPLATFORM ?= $(shell arch)
1011
RUNNER_NAME ?= ${DOCKER_USER}/actions-runner
@@ -67,7 +68,7 @@ endif
6768
all: manager
6869

6970
lint:
70-
docker run --rm -v $(PWD):/app -w /app golangci/golangci-lint:v1.49.0 golangci-lint run
71+
docker run --rm -v $(PWD):/app -w /app golangci/golangci-lint:v1.54.2 golangci-lint run
7172

7273
GO_TEST_ARGS ?= -short
7374

@@ -215,6 +216,7 @@ docker-buildx:
215216
--build-arg RUNNER_VERSION=${RUNNER_VERSION} \
216217
--build-arg DOCKER_VERSION=${DOCKER_VERSION} \
217218
--build-arg VERSION=${VERSION} \
219+
--build-arg COMMIT_SHA=${COMMIT_SHA} \
218220
-t "${DOCKER_IMAGE_NAME}:${VERSION}" \
219221
-f Dockerfile \
220222
. ${PUSH_ARG}

build/version.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@ package build
22

33
// This is overridden at build-time using go-build ldflags. dev is the fallback value
44
var Version = "NA"
5+
6+
var CommitSHA = "NA"

cmd/githubrunnerscalesetlistener/main.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,12 @@ func run(ctx context.Context, rc RunnerScaleSetListenerConfig, logger logr.Logge
190190
rc,
191191
creds,
192192
actions.WithLogger(logger),
193-
actions.WithUserAgent(fmt.Sprintf("actions-runner-controller/%s", build.Version)),
194193
)
194+
actionsServiceClient.SetUserAgent(actions.UserAgentInfo{
195+
Version: build.Version,
196+
CommitSHA: build.CommitSHA,
197+
ScaleSetID: rc.RunnerScaleSetId,
198+
})
195199
if err != nil {
196200
return fmt.Errorf("failed to create an Actions Service client: %w", err)
197201
}

cmd/githubrunnerscalesetlistener/mock_KubernetesManager.go

Lines changed: 5 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/githubrunnerscalesetlistener/mock_RunnerScaleSetClient.go

Lines changed: 5 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

controllers/actions.github.com/autoscalingrunnerset_controller.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,12 @@ func (r *AutoscalingRunnerSetReconciler) createRunnerScaleSet(ctx context.Contex
463463
}
464464
}
465465

466+
actionsClient.SetUserAgent(actions.UserAgentInfo{
467+
Version: build.Version,
468+
CommitSHA: build.CommitSHA,
469+
ScaleSetID: runnerScaleSet.Id,
470+
})
471+
466472
logger.Info("Created/Reused a runner scale set", "id", runnerScaleSet.Id, "runnerGroupName", runnerScaleSet.RunnerGroupName)
467473
if autoscalingRunnerSet.Annotations == nil {
468474
autoscalingRunnerSet.Annotations = map[string]string{}

controllers/actions.github.com/autoscalingrunnerset_controller_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -888,8 +888,9 @@ var _ = Describe("Test client optional configuration", Ordered, func() {
888888
Log: logf.Log,
889889
ControllerNamespace: autoscalingNS.Name,
890890
DefaultRunnerScaleSetListenerImage: "ghcr.io/actions/arc",
891-
ActionsClient: actions.NewMultiClient("test", logr.Discard()),
891+
ActionsClient: actions.NewMultiClient(logr.Discard()),
892892
}
893+
893894
err := controller.SetupWithManager(mgr)
894895
Expect(err).NotTo(HaveOccurred(), "failed to setup controller")
895896

@@ -1079,7 +1080,7 @@ var _ = Describe("Test client optional configuration", Ordered, func() {
10791080
})
10801081

10811082
It("should be able to make requests to a server using root CAs", func() {
1082-
controller.ActionsClient = actions.NewMultiClient("test", logr.Discard())
1083+
controller.ActionsClient = actions.NewMultiClient(logr.Discard())
10831084

10841085
certsFolder := filepath.Join(
10851086
"../../",

controllers/actions.github.com/ephemeralrunner_controller_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -729,7 +729,7 @@ var _ = Describe("EphemeralRunner", func() {
729729

730730
It("uses an actions client with proxy transport", func() {
731731
// Use an actual client
732-
controller.ActionsClient = actions.NewMultiClient("test", logr.Discard())
732+
controller.ActionsClient = actions.NewMultiClient(logr.Discard())
733733

734734
proxySuccessfulllyCalled := false
735735
proxy := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
@@ -914,7 +914,7 @@ var _ = Describe("EphemeralRunner", func() {
914914
server.StartTLS()
915915

916916
// Use an actual client
917-
controller.ActionsClient = actions.NewMultiClient("test", logr.Discard())
917+
controller.ActionsClient = actions.NewMultiClient(logr.Discard())
918918

919919
ephemeralRunner := newExampleRunner("test-runner", autoScalingNS.Name, configSecret.Name)
920920
ephemeralRunner.Spec.GitHubConfigUrl = server.ConfigURLForOrg("my-org")

controllers/actions.github.com/ephemeralrunnerset_controller_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -753,7 +753,7 @@ var _ = Describe("Test EphemeralRunnerSet controller with proxy settings", func(
753753
Client: mgr.GetClient(),
754754
Scheme: mgr.GetScheme(),
755755
Log: logf.Log,
756-
ActionsClient: actions.NewMultiClient("test", logr.Discard()),
756+
ActionsClient: actions.NewMultiClient(logr.Discard()),
757757
}
758758
err := controller.SetupWithManager(mgr)
759759
Expect(err).NotTo(HaveOccurred(), "failed to setup controller")
@@ -1052,7 +1052,7 @@ var _ = Describe("Test EphemeralRunnerSet controller with custom root CA", func(
10521052
Client: mgr.GetClient(),
10531053
Scheme: mgr.GetScheme(),
10541054
Log: logf.Log,
1055-
ActionsClient: actions.NewMultiClient("test", logr.Discard()),
1055+
ActionsClient: actions.NewMultiClient(logr.Discard()),
10561056
}
10571057
err = controller.SetupWithManager(mgr)
10581058
Expect(err).NotTo(HaveOccurred(), "failed to setup controller")

github/actions/client.go

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"sync"
1818
"time"
1919

20+
"github.com/actions/actions-runner-controller/build"
2021
"github.com/go-logr/logr"
2122
"github.com/golang-jwt/jwt/v4"
2223
"github.com/google/uuid"
@@ -53,6 +54,8 @@ type ActionsService interface {
5354
GetRunner(ctx context.Context, runnerId int64) (*RunnerReference, error)
5455
GetRunnerByName(ctx context.Context, runnerName string) (*RunnerReference, error)
5556
RemoveRunner(ctx context.Context, runnerId int64) error
57+
58+
SetUserAgent(info UserAgentInfo)
5659
}
5760

5861
type Client struct {
@@ -72,7 +75,7 @@ type Client struct {
7275
creds *ActionsAuth
7376
config *GitHubConfig
7477
logger logr.Logger
75-
userAgent string
78+
userAgent UserAgentInfo
7679

7780
rootCAs *x509.CertPool
7881
tlsInsecureSkipVerify bool
@@ -84,10 +87,24 @@ type ProxyFunc func(req *http.Request) (*url.URL, error)
8487

8588
type ClientOption func(*Client)
8689

87-
func WithUserAgent(userAgent string) ClientOption {
88-
return func(c *Client) {
89-
c.userAgent = userAgent
90+
type UserAgentInfo struct {
91+
Version string
92+
CommitSHA string
93+
ScaleSetID int
94+
}
95+
96+
func (u UserAgentInfo) String() string {
97+
var scaleSetID = "NA"
98+
if u.ScaleSetID > 0 {
99+
scaleSetID = strconv.Itoa(u.ScaleSetID)
90100
}
101+
102+
return fmt.Sprintf(
103+
"actions-runner-controller/%s CommitSHA/%s ScaleSetID/%s",
104+
u.Version,
105+
u.CommitSHA,
106+
scaleSetID,
107+
)
91108
}
92109

93110
func WithLogger(logger logr.Logger) ClientOption {
@@ -140,6 +157,11 @@ func NewClient(githubConfigURL string, creds *ActionsAuth, options ...ClientOpti
140157
// retryablehttp defaults
141158
retryMax: 4,
142159
retryWaitMax: 30 * time.Second,
160+
userAgent: UserAgentInfo{
161+
Version: build.Version,
162+
CommitSHA: build.CommitSHA,
163+
ScaleSetID: 0,
164+
},
143165
}
144166

145167
for _, option := range options {
@@ -178,6 +200,10 @@ func NewClient(githubConfigURL string, creds *ActionsAuth, options ...ClientOpti
178200
return ac, nil
179201
}
180202

203+
func (c *Client) SetUserAgent(info UserAgentInfo) {
204+
c.userAgent = info
205+
}
206+
181207
// Identifier returns a string to help identify a client uniquely.
182208
// This is used for caching client instances and understanding when a config
183209
// change warrants creating a new client. Any changes to Client that would
@@ -186,7 +212,7 @@ func (c *Client) Identifier() string {
186212
identifier := fmt.Sprintf("configURL:%q,", c.config.ConfigURL.String())
187213

188214
if c.creds.Token != "" {
189-
identifier += fmt.Sprintf("token:%q", c.creds.Token)
215+
identifier += fmt.Sprintf("token:%q,", c.creds.Token)
190216
}
191217

192218
if c.creds.AppCreds != nil {
@@ -234,9 +260,7 @@ func (c *Client) NewGitHubAPIRequest(ctx context.Context, method, path string, b
234260
return nil, err
235261
}
236262

237-
if c.userAgent != "" {
238-
req.Header.Set("User-Agent", c.userAgent)
239-
}
263+
req.Header.Set("User-Agent", c.userAgent.String())
240264

241265
return req, nil
242266
}
@@ -278,9 +302,7 @@ func (c *Client) NewActionsServiceRequest(ctx context.Context, method, path stri
278302

279303
req.Header.Set("Content-Type", "application/json")
280304
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.ActionsServiceAdminToken))
281-
if c.userAgent != "" {
282-
req.Header.Set("User-Agent", c.userAgent)
283-
}
305+
req.Header.Set("User-Agent", c.userAgent.String())
284306

285307
return req, nil
286308
}
@@ -473,9 +495,7 @@ func (c *Client) GetMessage(ctx context.Context, messageQueueUrl, messageQueueAc
473495

474496
req.Header.Set("Accept", "application/json; api-version=6.0-preview")
475497
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", messageQueueAccessToken))
476-
if c.userAgent != "" {
477-
req.Header.Set("User-Agent", c.userAgent)
478-
}
498+
req.Header.Set("User-Agent", c.userAgent.String())
479499

480500
resp, err := c.Do(req)
481501
if err != nil {
@@ -524,9 +544,7 @@ func (c *Client) DeleteMessage(ctx context.Context, messageQueueUrl, messageQueu
524544

525545
req.Header.Set("Content-Type", "application/json")
526546
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", messageQueueAccessToken))
527-
if c.userAgent != "" {
528-
req.Header.Set("User-Agent", c.userAgent)
529-
}
547+
req.Header.Set("User-Agent", c.userAgent.String())
530548

531549
resp, err := c.Do(req)
532550
if err != nil {
@@ -624,9 +642,7 @@ func (c *Client) AcquireJobs(ctx context.Context, runnerScaleSetId int, messageQ
624642

625643
req.Header.Set("Content-Type", "application/json")
626644
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", messageQueueAccessToken))
627-
if c.userAgent != "" {
628-
req.Header.Set("User-Agent", c.userAgent)
629-
}
645+
req.Header.Set("User-Agent", c.userAgent.String())
630646

631647
resp, err := c.Do(req)
632648
if err != nil {

github/actions/fake/client.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,3 +282,5 @@ func (f *FakeClient) GetRunnerByName(ctx context.Context, runnerName string) (*a
282282
func (f *FakeClient) RemoveRunner(ctx context.Context, runnerId int64) error {
283283
return f.removeRunnerResult.err
284284
}
285+
286+
func (f *FakeClient) SetUserAgent(_ actions.UserAgentInfo) {}

0 commit comments

Comments
 (0)