Skip to content

Commit

Permalink
Move env var to join the others
Browse files Browse the repository at this point in the history
  • Loading branch information
ciarams87 committed Feb 21, 2024
1 parent 535b1c1 commit ce5ac71
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 63 deletions.
6 changes: 6 additions & 0 deletions cmd/gateway/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ func createStaticModeCommand() *cobra.Command {
return errors.New("POD_NAME environment variable must be set")
}

imageSource := os.Getenv("BUILD_AGENT")
if imageSource != "gha" && imageSource != "local" {
imageSource = "unknown"
}

period, err := time.ParseDuration(telemetryReportPeriod)
if err != nil {
return fmt.Errorf("error parsing telemetry report period: %w", err)
Expand Down Expand Up @@ -203,6 +208,7 @@ func createStaticModeCommand() *cobra.Command {
TelemetryReportPeriod: period,
Version: version,
ExperimentalFeatures: gwExperimentalFeatures,
ImageSource: imageSource,
}

if err := static.StartManager(conf); err != nil {
Expand Down
2 changes: 2 additions & 0 deletions internal/mode/static/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
type Config struct {
// Version is the running NGF version.
Version string
// ImageSource is the source of the NGINX Gateway image.
ImageSource string
// AtomicLevel is an atomically changeable, dynamic logging level.
AtomicLevel zap.AtomicLevel
// GatewayNsName is the namespaced name of a Gateway resource that the Gateway will use.
Expand Down
1 change: 1 addition & 0 deletions internal/mode/static/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ func StartManager(cfg config.Config) error {
Namespace: cfg.GatewayPodConfig.Namespace,
Name: cfg.GatewayPodConfig.Name,
},
ImageSource: cfg.ImageSource,
})
if err = mgr.Add(createTelemetryJob(cfg, dataCollector, nginxChecker.getReadyCh())); err != nil {
return fmt.Errorf("cannot register telemetry job: %w", err)
Expand Down
18 changes: 3 additions & 15 deletions internal/mode/static/telemetry/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"errors"
"fmt"
"os"

appsv1 "k8s.io/api/apps/v1"
v1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -70,6 +69,8 @@ type DataCollectorConfig struct {
Version string
// PodNSName is the NamespacedName of the NGF Pod.
PodNSName types.NamespacedName
// ImageSource is the source of the NGF image.
ImageSource string
}

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

imageSource := CollectImageSource()

data := Data{
NodeCount: nodeCount,
NGFResourceCounts: graphResourceCount,
Expand All @@ -119,7 +118,7 @@ func (c DataCollectorImpl) Collect(ctx context.Context) (Data, error) {
},
NGFReplicaCount: ngfReplicaCount,
ClusterID: clusterID,
ImageSource: imageSource,
ImageSource: c.cfg.ImageSource,
}

return data, nil
Expand Down Expand Up @@ -219,14 +218,3 @@ func CollectClusterID(ctx context.Context, k8sClient client.Reader) (string, err
}
return string(kubeNamespace.GetUID()), nil
}

// CollectImageSource gets the source of the NGF image. This is done by inspecting the BUILD_AGENT environment variable.
// Valid sources are: "gha" for images built using GitHub Actions in the pipeline, or "local".
// If the environment variable is not set to one of these, the source is "unknown"
func CollectImageSource() string {
buildAgent := os.Getenv("BUILD_AGENT")
if buildAgent != "gha" && buildAgent != "local" {
buildAgent = "unknown"
}
return buildAgent
}
50 changes: 2 additions & 48 deletions internal/mode/static/telemetry/collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"errors"
"fmt"
"os"
"reflect"

. "github.com/onsi/ginkgo/v2"
Expand Down Expand Up @@ -64,10 +63,6 @@ func createGetCallsFunc(objects ...client.Object) getCallsFunc {
}
}

func setEnvVar(key, value string) {
os.Setenv(key, value)
}

var _ = Describe("Collector", Ordered, func() {
var (
k8sClientReader *eventsfakes.FakeReader
Expand Down Expand Up @@ -128,7 +123,7 @@ var _ = Describe("Collector", Ordered, func() {
NGFResourceCounts: telemetry.NGFResourceCounts{},
NGFReplicaCount: 1,
ClusterID: string(kubeNamespace.GetUID()),
ImageSource: "unknown",
ImageSource: "local",
}

k8sClientReader = &eventsfakes.FakeReader{}
Expand All @@ -144,6 +139,7 @@ var _ = Describe("Collector", Ordered, func() {
ConfigurationGetter: fakeConfigurationGetter,
Version: version,
PodNSName: podNSName,
ImageSource: "local",
})

baseGetCalls = createGetCallsFunc(ngfPod, ngfReplicaSet, kubeNamespace)
Expand Down Expand Up @@ -481,48 +477,6 @@ var _ = Describe("Collector", Ordered, func() {
})
})

Describe("Image source collector", func() {
When("collecting image source", func() {
When("the NGF pod's build agent env var is set to gha", func() {
It("collects the image source as gha", func() {
setEnvVar("BUILD_AGENT", "gha")
expData.ImageSource = "gha"

data, err := dataCollector.Collect(ctx)

Expect(err).To(BeNil())
Expect(expData).To(Equal(data))
})
})
})
When("collecting image source", func() {
When("the NGF pod's build agent env var is set to local", func() {
It("collects the image source as local", func() {
setEnvVar("BUILD_AGENT", "local")
expData.ImageSource = "local"

data, err := dataCollector.Collect(ctx)

Expect(err).To(BeNil())
Expect(expData).To(Equal(data))
})
})
})
When("collecting image source", func() {
When("the NGF pod's build agent env var is set to anything else", func() {
It("collects the image source as unknown", func() {
setEnvVar("BUILD_AGENT", "something-else")
expData.ImageSource = "unknown"

data, err := dataCollector.Collect(ctx)

Expect(err).To(BeNil())
Expect(expData).To(Equal(data))
})
})
})
})

Describe("NGF replica count collector", func() {
When("collecting NGF replica count", func() {
When("it encounters an error while collecting data", func() {
Expand Down

0 comments on commit ce5ac71

Please sign in to comment.