Skip to content

Commit 535b1c1

Browse files
committed
Add image source
1 parent 92081af commit 535b1c1

File tree

4 files changed

+68
-2
lines changed

4 files changed

+68
-2
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ build-images-with-plus: build-ngf-image build-nginx-plus-image ## Build the NGF
4545

4646
.PHONY: build-ngf-image
4747
build-ngf-image: check-for-docker build ## Build the NGF docker image
48-
docker build --platform linux/$(GOARCH) --target $(strip $(TARGET)) -f build/Dockerfile -t $(strip $(PREFIX)):$(strip $(TAG)) .
48+
docker build --platform linux/$(GOARCH) --build-arg BUILD_AGENT=$(BUILD_AGENT) --target $(strip $(TARGET)) -f build/Dockerfile -t $(strip $(PREFIX)):$(strip $(TAG)) .
4949

5050
.PHONY: build-nginx-image
5151
build-nginx-image: check-for-docker ## Build the custom nginx image

build/Dockerfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ RUN setcap 'cap_kill=+ep' /usr/bin/gateway
2727

2828
FROM scratch as common
2929
USER 102:1001
30+
ARG BUILD_AGENT
31+
ENV BUILD_AGENT=${BUILD_AGENT}
3032
ENTRYPOINT [ "/usr/bin/gateway" ]
3133

3234
FROM common as container

internal/mode/static/telemetry/collector.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"errors"
66
"fmt"
7+
"os"
78

89
appsv1 "k8s.io/api/apps/v1"
910
v1 "k8s.io/api/core/v1"
@@ -51,8 +52,9 @@ type ProjectMetadata struct {
5152
type Data struct {
5253
ProjectMetadata ProjectMetadata
5354
ClusterID string
54-
NodeCount int
55+
ImageSource string
5556
NGFResourceCounts NGFResourceCounts
57+
NodeCount int
5658
NGFReplicaCount int
5759
}
5860

@@ -106,6 +108,8 @@ func (c DataCollectorImpl) Collect(ctx context.Context) (Data, error) {
106108
return Data{}, fmt.Errorf("failed to collect clusterID: %w", err)
107109
}
108110

111+
imageSource := CollectImageSource()
112+
109113
data := Data{
110114
NodeCount: nodeCount,
111115
NGFResourceCounts: graphResourceCount,
@@ -115,6 +119,7 @@ func (c DataCollectorImpl) Collect(ctx context.Context) (Data, error) {
115119
},
116120
NGFReplicaCount: ngfReplicaCount,
117121
ClusterID: clusterID,
122+
ImageSource: imageSource,
118123
}
119124

120125
return data, nil
@@ -214,3 +219,14 @@ func CollectClusterID(ctx context.Context, k8sClient client.Reader) (string, err
214219
}
215220
return string(kubeNamespace.GetUID()), nil
216221
}
222+
223+
// CollectImageSource gets the source of the NGF image. This is done by inspecting the BUILD_AGENT environment variable.
224+
// Valid sources are: "gha" for images built using GitHub Actions in the pipeline, or "local".
225+
// If the environment variable is not set to one of these, the source is "unknown"
226+
func CollectImageSource() string {
227+
buildAgent := os.Getenv("BUILD_AGENT")
228+
if buildAgent != "gha" && buildAgent != "local" {
229+
buildAgent = "unknown"
230+
}
231+
return buildAgent
232+
}

internal/mode/static/telemetry/collector_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"errors"
66
"fmt"
7+
"os"
78
"reflect"
89

910
. "github.com/onsi/ginkgo/v2"
@@ -63,6 +64,10 @@ func createGetCallsFunc(objects ...client.Object) getCallsFunc {
6364
}
6465
}
6566

67+
func setEnvVar(key, value string) {
68+
os.Setenv(key, value)
69+
}
70+
6671
var _ = Describe("Collector", Ordered, func() {
6772
var (
6873
k8sClientReader *eventsfakes.FakeReader
@@ -123,6 +128,7 @@ var _ = Describe("Collector", Ordered, func() {
123128
NGFResourceCounts: telemetry.NGFResourceCounts{},
124129
NGFReplicaCount: 1,
125130
ClusterID: string(kubeNamespace.GetUID()),
131+
ImageSource: "unknown",
126132
}
127133

128134
k8sClientReader = &eventsfakes.FakeReader{}
@@ -475,6 +481,48 @@ var _ = Describe("Collector", Ordered, func() {
475481
})
476482
})
477483

484+
Describe("Image source collector", func() {
485+
When("collecting image source", func() {
486+
When("the NGF pod's build agent env var is set to gha", func() {
487+
It("collects the image source as gha", func() {
488+
setEnvVar("BUILD_AGENT", "gha")
489+
expData.ImageSource = "gha"
490+
491+
data, err := dataCollector.Collect(ctx)
492+
493+
Expect(err).To(BeNil())
494+
Expect(expData).To(Equal(data))
495+
})
496+
})
497+
})
498+
When("collecting image source", func() {
499+
When("the NGF pod's build agent env var is set to local", func() {
500+
It("collects the image source as local", func() {
501+
setEnvVar("BUILD_AGENT", "local")
502+
expData.ImageSource = "local"
503+
504+
data, err := dataCollector.Collect(ctx)
505+
506+
Expect(err).To(BeNil())
507+
Expect(expData).To(Equal(data))
508+
})
509+
})
510+
})
511+
When("collecting image source", func() {
512+
When("the NGF pod's build agent env var is set to anything else", func() {
513+
It("collects the image source as unknown", func() {
514+
setEnvVar("BUILD_AGENT", "something-else")
515+
expData.ImageSource = "unknown"
516+
517+
data, err := dataCollector.Collect(ctx)
518+
519+
Expect(err).To(BeNil())
520+
Expect(expData).To(Equal(data))
521+
})
522+
})
523+
})
524+
})
525+
478526
Describe("NGF replica count collector", func() {
479527
When("collecting NGF replica count", func() {
480528
When("it encounters an error while collecting data", func() {

0 commit comments

Comments
 (0)