Skip to content

Commit 870ddef

Browse files
committed
Refactor AgentLabels to be a map
1 parent 4b097b3 commit 870ddef

File tree

4 files changed

+25
-47
lines changed

4 files changed

+25
-47
lines changed

internal/controller/provisioner/objects.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
"github.com/nginx/nginx-gateway-fabric/v2/internal/controller/config"
2626
"github.com/nginx/nginx-gateway-fabric/v2/internal/controller/state/dataplane"
2727
"github.com/nginx/nginx-gateway-fabric/v2/internal/controller/state/graph"
28-
"github.com/nginx/nginx-gateway-fabric/v2/internal/controller/telemetry"
2928
"github.com/nginx/nginx-gateway-fabric/v2/internal/framework/controller"
3029
"github.com/nginx/nginx-gateway-fabric/v2/internal/framework/helpers"
3130
)
@@ -409,7 +408,7 @@ func (p *NginxProvisioner) buildNginxConfigMaps(
409408
"Namespace": p.cfg.GatewayPodConfig.Namespace,
410409
"EnableMetrics": enableMetrics,
411410
"MetricsPort": metricsPort,
412-
"AgentLabels": telemetry.AgentLabelsToMap(p.cfg.AgentLabels),
411+
"AgentLabels": p.cfg.AgentLabels,
413412
}
414413

415414
if logging != nil && logging.AgentLevel != nil {

internal/controller/provisioner/provisioner.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ type Config struct {
4949
PlusUsageConfig *config.UsageReportConfig
5050
StatusQueue *status.Queue
5151
GatewayPodConfig *config.GatewayPodConfig
52-
AgentLabels telemetry.AgentLabels
52+
AgentLabels map[string]string
5353
Logger logr.Logger
5454
NGINXSCCName string
5555
GCName string
@@ -90,7 +90,7 @@ func defaultLabelCollectorFactory(mgr manager.Manager, cfg Config) AgentLabelCol
9090
}
9191

9292
type AgentLabelCollector interface {
93-
Collect(ctx context.Context) (telemetry.AgentLabels, error)
93+
Collect(ctx context.Context) (map[string]string, error)
9494
}
9595

9696
// NewNginxProvisioner returns a new instance of a Provisioner that will deploy nginx resources.

internal/controller/provisioner/provisioner_test.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
"github.com/nginx/nginx-gateway-fabric/v2/internal/controller/nginx/agent/agentfakes"
2525
"github.com/nginx/nginx-gateway-fabric/v2/internal/controller/provisioner/openshift/openshiftfakes"
2626
"github.com/nginx/nginx-gateway-fabric/v2/internal/controller/state/graph"
27-
"github.com/nginx/nginx-gateway-fabric/v2/internal/controller/telemetry"
2827
"github.com/nginx/nginx-gateway-fabric/v2/internal/framework/controller"
2928
"github.com/nginx/nginx-gateway-fabric/v2/internal/framework/helpers"
3029
)
@@ -190,13 +189,13 @@ func defaultNginxProvisioner(
190189
EndpointPort: 443,
191190
EndpointTLSSkipVerify: false,
192191
},
193-
AgentLabels: telemetry.AgentLabels{
194-
ProductType: "ngf",
195-
ProductVersion: "ngf-version",
196-
ClusterID: "my-cluster-id",
197-
ControlName: "my-control-plane-name",
198-
ControlID: "my-control-plane-id",
199-
ControlNamespace: "my-control-plane-namespace",
192+
AgentLabels: map[string]string{
193+
"product-type": "ngf",
194+
"product-version": "ngf-version",
195+
"cluster-id": "my-cluster-id",
196+
"control-name": "my-control-plane-name",
197+
"control-id": "my-control-plane-id",
198+
"control-namespace": "my-control-plane-namespace",
200199
},
201200
},
202201
leader: true,
@@ -205,8 +204,8 @@ func defaultNginxProvisioner(
205204

206205
type fakeLabelCollector struct{}
207206

208-
func (f *fakeLabelCollector) Collect(_ context.Context) (telemetry.AgentLabels, error) {
209-
return telemetry.AgentLabels{ProductType: "fake"}, nil
207+
func (f *fakeLabelCollector) Collect(_ context.Context) (map[string]string, error) {
208+
return map[string]string{"product-type": "fake"}, nil
210209
}
211210

212211
func TestNewNginxProvisioner(t *testing.T) {

internal/controller/telemetry/agent_labels.go

Lines changed: 13 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,6 @@ import (
88
"sigs.k8s.io/controller-runtime/pkg/client"
99
)
1010

11-
// AgentLabels contains the metadata information needed for reporting to Agent v3.
12-
type AgentLabels struct {
13-
ProductType string `json:"product-type"`
14-
ProductVersion string `json:"product-version"`
15-
ClusterID string `json:"cluster-id"`
16-
ControlName string `json:"control-name"`
17-
ControlID string `json:"control-id"`
18-
ControlNamespace string `json:"control-namespace"`
19-
}
20-
2111
// LabelCollectorConfig holds configuration parameters for LabelCollector.
2212
type LabelCollectorConfig struct {
2313
// K8sClientReader is a Kubernetes API client Reader.
@@ -42,41 +32,31 @@ func NewLabelCollector(
4232
}
4333
}
4434

45-
func (l *LabelCollector) Collect(ctx context.Context) (AgentLabels, error) {
35+
// Collect gathers metadata labels needed for reporting to Agent v3.
36+
func (l *LabelCollector) Collect(ctx context.Context) (map[string]string, error) {
37+
agentLabels := make(map[string]string)
38+
4639
clusterID, err := collectClusterID(ctx, l.cfg.K8sClientReader)
4740
if err != nil {
48-
return AgentLabels{}, fmt.Errorf("failed to collect cluster information: %w", err)
41+
return nil, fmt.Errorf("failed to collect cluster information: %w", err)
4942
}
5043

5144
replicaSet, err := getPodReplicaSet(ctx, l.cfg.K8sClientReader, l.cfg.PodNSName)
5245
if err != nil {
53-
return AgentLabels{}, fmt.Errorf("failed to get replica set for pod %v: %w", l.cfg.PodNSName, err)
46+
return nil, fmt.Errorf("failed to get replica set for pod %v: %w", l.cfg.PodNSName, err)
5447
}
5548

5649
deploymentID, err := getDeploymentID(replicaSet)
5750
if err != nil {
58-
return AgentLabels{}, fmt.Errorf("failed to get NGF deploymentID: %w", err)
51+
return nil, fmt.Errorf("failed to get NGF deploymentID: %w", err)
5952
}
6053

61-
agentLabels := AgentLabels{
62-
ProductType: "ngf",
63-
ProductVersion: l.cfg.Version,
64-
ClusterID: clusterID,
65-
ControlName: l.cfg.PodNSName.Name,
66-
ControlNamespace: l.cfg.PodNSName.Namespace,
67-
ControlID: deploymentID,
68-
}
54+
agentLabels["product-type"] = "ngf"
55+
agentLabels["product-version"] = l.cfg.Version
56+
agentLabels["cluster-id"] = clusterID
57+
agentLabels["control-name"] = l.cfg.PodNSName.Name
58+
agentLabels["control-namespace"] = l.cfg.PodNSName.Namespace
59+
agentLabels["control-id"] = deploymentID
6960

7061
return agentLabels, nil
7162
}
72-
73-
func AgentLabelsToMap(labels AgentLabels) map[string]string {
74-
return map[string]string{
75-
"product-type": labels.ProductType,
76-
"product-version": labels.ProductVersion,
77-
"cluster-id": labels.ClusterID,
78-
"control-name": labels.ControlName,
79-
"control-namespace": labels.ControlNamespace,
80-
"control-id": labels.ControlID,
81-
}
82-
}

0 commit comments

Comments
 (0)