Skip to content

Commit ce5ac71

Browse files
committed
Move env var to join the others
1 parent 535b1c1 commit ce5ac71

File tree

5 files changed

+14
-63
lines changed

5 files changed

+14
-63
lines changed

cmd/gateway/commands.go

+6
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,11 @@ func createStaticModeCommand() *cobra.Command {
146146
return errors.New("POD_NAME environment variable must be set")
147147
}
148148

149+
imageSource := os.Getenv("BUILD_AGENT")
150+
if imageSource != "gha" && imageSource != "local" {
151+
imageSource = "unknown"
152+
}
153+
149154
period, err := time.ParseDuration(telemetryReportPeriod)
150155
if err != nil {
151156
return fmt.Errorf("error parsing telemetry report period: %w", err)
@@ -203,6 +208,7 @@ func createStaticModeCommand() *cobra.Command {
203208
TelemetryReportPeriod: period,
204209
Version: version,
205210
ExperimentalFeatures: gwExperimentalFeatures,
211+
ImageSource: imageSource,
206212
}
207213

208214
if err := static.StartManager(conf); err != nil {

internal/mode/static/config/config.go

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
type Config struct {
1212
// Version is the running NGF version.
1313
Version string
14+
// ImageSource is the source of the NGINX Gateway image.
15+
ImageSource string
1416
// AtomicLevel is an atomically changeable, dynamic logging level.
1517
AtomicLevel zap.AtomicLevel
1618
// GatewayNsName is the namespaced name of a Gateway resource that the Gateway will use.

internal/mode/static/manager.go

+1
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ func StartManager(cfg config.Config) error {
251251
Namespace: cfg.GatewayPodConfig.Namespace,
252252
Name: cfg.GatewayPodConfig.Name,
253253
},
254+
ImageSource: cfg.ImageSource,
254255
})
255256
if err = mgr.Add(createTelemetryJob(cfg, dataCollector, nginxChecker.getReadyCh())); err != nil {
256257
return fmt.Errorf("cannot register telemetry job: %w", err)

internal/mode/static/telemetry/collector.go

+3-15
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"errors"
66
"fmt"
7-
"os"
87

98
appsv1 "k8s.io/api/apps/v1"
109
v1 "k8s.io/api/core/v1"
@@ -70,6 +69,8 @@ type DataCollectorConfig struct {
7069
Version string
7170
// PodNSName is the NamespacedName of the NGF Pod.
7271
PodNSName types.NamespacedName
72+
// ImageSource is the source of the NGF image.
73+
ImageSource string
7374
}
7475

7576
// DataCollectorImpl is am implementation of DataCollector.
@@ -108,8 +109,6 @@ func (c DataCollectorImpl) Collect(ctx context.Context) (Data, error) {
108109
return Data{}, fmt.Errorf("failed to collect clusterID: %w", err)
109110
}
110111

111-
imageSource := CollectImageSource()
112-
113112
data := Data{
114113
NodeCount: nodeCount,
115114
NGFResourceCounts: graphResourceCount,
@@ -119,7 +118,7 @@ func (c DataCollectorImpl) Collect(ctx context.Context) (Data, error) {
119118
},
120119
NGFReplicaCount: ngfReplicaCount,
121120
ClusterID: clusterID,
122-
ImageSource: imageSource,
121+
ImageSource: c.cfg.ImageSource,
123122
}
124123

125124
return data, nil
@@ -219,14 +218,3 @@ func CollectClusterID(ctx context.Context, k8sClient client.Reader) (string, err
219218
}
220219
return string(kubeNamespace.GetUID()), nil
221220
}
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

+2-48
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"errors"
66
"fmt"
7-
"os"
87
"reflect"
98

109
. "github.com./onsi/ginkgo/v2"
@@ -64,10 +63,6 @@ func createGetCallsFunc(objects ...client.Object) getCallsFunc {
6463
}
6564
}
6665

67-
func setEnvVar(key, value string) {
68-
os.Setenv(key, value)
69-
}
70-
7166
var _ = Describe("Collector", Ordered, func() {
7267
var (
7368
k8sClientReader *eventsfakes.FakeReader
@@ -128,7 +123,7 @@ var _ = Describe("Collector", Ordered, func() {
128123
NGFResourceCounts: telemetry.NGFResourceCounts{},
129124
NGFReplicaCount: 1,
130125
ClusterID: string(kubeNamespace.GetUID()),
131-
ImageSource: "unknown",
126+
ImageSource: "local",
132127
}
133128

134129
k8sClientReader = &eventsfakes.FakeReader{}
@@ -144,6 +139,7 @@ var _ = Describe("Collector", Ordered, func() {
144139
ConfigurationGetter: fakeConfigurationGetter,
145140
Version: version,
146141
PodNSName: podNSName,
142+
ImageSource: "local",
147143
})
148144

149145
baseGetCalls = createGetCallsFunc(ngfPod, ngfReplicaSet, kubeNamespace)
@@ -481,48 +477,6 @@ var _ = Describe("Collector", Ordered, func() {
481477
})
482478
})
483479

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-
526480
Describe("NGF replica count collector", func() {
527481
When("collecting NGF replica count", func() {
528482
When("it encounters an error while collecting data", func() {

0 commit comments

Comments
 (0)