Skip to content

Commit 6979487

Browse files
authored
[AGENTONB-2760] use UUID instead of HostName in metadata payload (#2468)
* remove hostname from metadata payload, use UUID (clusterUID) instead * update tests
1 parent f9a6b3a commit 6979487

File tree

7 files changed

+14
-80
lines changed

7 files changed

+14
-80
lines changed

pkg/controller/utils/metadata/crd_metadata.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,6 @@ func NewCRDMetadataForwarder(logger logr.Logger, k8sClient client.Reader, kubern
9090

9191
// Start starts the CRD metadata forwarder
9292
func (cmf *CRDMetadataForwarder) Start() {
93-
if cmf.hostName == "" {
94-
cmf.logger.Error(ErrEmptyHostName, "Could not set host name; not starting metadata forwarder")
95-
return
96-
}
97-
9893
cmf.logger.Info("Starting metadata forwarder")
9994

10095
ticker := time.NewTicker(crdMetadataInterval)

pkg/controller/utils/metadata/credential_setup_test.go

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ func TestSetupRequestPrerequisites(t *testing.T) {
5858
setupEnv: func() {
5959
os.Setenv(constants.DDAPIKey, "operator-api-key")
6060
os.Setenv(constants.DDAppKey, "operator-app-key")
61-
os.Setenv(constants.DDHostName, "test-hostname")
6261
os.Setenv(constants.DDClusterName, "test-cluster")
6362
},
6463
setupDDA: func() []client.Object {
@@ -107,7 +106,6 @@ func TestSetupRequestPrerequisites(t *testing.T) {
107106
{
108107
name: "DDA with plaintext API key and default site",
109108
setupEnv: func() {
110-
os.Setenv(constants.DDHostName, "test-hostname")
111109
// No operator credentials
112110
},
113111
setupDDA: func() []client.Object {
@@ -136,7 +134,6 @@ func TestSetupRequestPrerequisites(t *testing.T) {
136134
{
137135
name: "DDA with API key and custom site",
138136
setupEnv: func() {
139-
os.Setenv(constants.DDHostName, "test-hostname")
140137
// No operator credentials
141138
},
142139
setupDDA: func() []client.Object {
@@ -166,7 +163,6 @@ func TestSetupRequestPrerequisites(t *testing.T) {
166163
{
167164
name: "DDA with secret reference",
168165
setupEnv: func() {
169-
os.Setenv(constants.DDHostName, "test-hostname")
170166
// No operator credentials
171167
},
172168
setupDDA: func() []client.Object {
@@ -211,7 +207,6 @@ func TestSetupRequestPrerequisites(t *testing.T) {
211207
{
212208
name: "DDA with encrypted API key",
213209
setupEnv: func() {
214-
os.Setenv(constants.DDHostName, "test-hostname")
215210
// No operator credentials
216211
},
217212
setupDDA: func() []client.Object {
@@ -243,7 +238,6 @@ func TestSetupRequestPrerequisites(t *testing.T) {
243238
setupEnv: func() {
244239
os.Setenv(constants.DDAPIKey, "operator-api-key")
245240
os.Setenv(constants.DDAppKey, "operator-app-key")
246-
os.Setenv(constants.DDHostName, "test-hostname")
247241
// Note: No DD_CLUSTER_NAME set to trigger fallback
248242
},
249243
setupDDA: func() []client.Object {
@@ -270,23 +264,9 @@ func TestSetupRequestPrerequisites(t *testing.T) {
270264
wantErr: false,
271265
},
272266
// Error cases
273-
{
274-
name: "missing hostname should fail",
275-
setupEnv: func() {
276-
os.Setenv(constants.DDAPIKey, "operator-api-key")
277-
os.Setenv(constants.DDAppKey, "operator-app-key")
278-
// No DDHostName set
279-
},
280-
setupDDA: func() []client.Object {
281-
return []client.Object{} // No DDA
282-
},
283-
wantClusterName: "", // Not relevant for error case
284-
wantErr: true,
285-
},
286267
{
287268
name: "no credentials anywhere should fail",
288269
setupEnv: func() {
289-
os.Setenv(constants.DDHostName, "test-hostname")
290270
// No operator credentials
291271
},
292272
setupDDA: func() []client.Object {

pkg/controller/utils/metadata/helm_metadata.go

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,18 @@ type HelmMetadataForwarder struct {
5050
}
5151

5252
type HelmMetadataPayload struct {
53-
Hostname string `json:"hostname"`
53+
UUID string `json:"uuid"`
5454
Timestamp int64 `json:"timestamp"`
5555
ClusterID string `json:"cluster_id"`
5656
Metadata HelmMetadata `json:"datadog_operator_helm_metadata"`
5757
}
5858

5959
type HelmMetadata struct {
60-
OperatorVersion string `json:"operator_version"`
61-
KubernetesVersion string `json:"kubernetes_version"`
62-
ClusterID string `json:"cluster_id"`
60+
// Shared
61+
OperatorVersion string `json:"operator_version"`
62+
KubernetesVersion string `json:"kubernetes_version"`
63+
ClusterID string `json:"cluster_id"`
64+
6365
ChartName string `json:"chart_name"`
6466
ChartReleaseName string `json:"chart_release_name"`
6567
ChartAppVersion string `json:"chart_app_version"`
@@ -137,11 +139,6 @@ func getWatchNamespacesForHelm(logger logr.Logger) []string {
137139

138140
// Start starts the helm metadata forwarder
139141
func (hmf *HelmMetadataForwarder) Start() {
140-
if hmf.hostName == "" {
141-
hmf.logger.Error(ErrEmptyHostName, "Could not set host name; not starting metadata forwarder")
142-
return
143-
}
144-
145142
hmf.logger.Info("Starting metadata forwarder")
146143

147144
ticker := time.NewTicker(defaultInterval)
@@ -252,7 +249,7 @@ func (hmf *HelmMetadataForwarder) buildPayload(release HelmReleaseData, clusterU
252249
}
253250

254251
payload := HelmMetadataPayload{
255-
Hostname: hmf.hostName,
252+
UUID: clusterUID,
256253
Timestamp: now,
257254
ClusterID: clusterUID,
258255
Metadata: helmMetadata,

pkg/controller/utils/metadata/helm_metadata_test.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
func Test_HelmMetadataForwarder_getPayload(t *testing.T) {
2121
expectedKubernetesVersion := "v1.28.0"
2222
expectedOperatorVersion := "v1.19.0"
23-
expectedHostname := "test-host"
2423
expectedClusterUID := "test-cluster-uid-123"
2524
expectedReleaseName := "my-release"
2625
expectedNamespace := "default"
@@ -30,9 +29,6 @@ func Test_HelmMetadataForwarder_getPayload(t *testing.T) {
3029

3130
hmf := NewHelmMetadataForwarder(zap.New(zap.UseDevMode(true)), nil, expectedKubernetesVersion, expectedOperatorVersion, config.NewCredentialManager(fake.NewFakeClient()))
3231

33-
// Set required fields
34-
hmf.hostName = expectedHostname
35-
3632
release := HelmReleaseData{
3733
ReleaseName: expectedReleaseName,
3834
Namespace: expectedNamespace,
@@ -60,10 +56,6 @@ func Test_HelmMetadataForwarder_getPayload(t *testing.T) {
6056
}
6157

6258
// Validate top-level fields
63-
if hostname, ok := parsed["hostname"].(string); !ok || hostname != expectedHostname {
64-
t.Errorf("buildPayload() hostname = %v, want %v", hostname, expectedHostname)
65-
}
66-
6759
if timestamp, ok := parsed["timestamp"].(float64); !ok || timestamp <= 0 {
6860
t.Errorf("buildPayload() timestamp = %v, want positive number", timestamp)
6961
}

pkg/controller/utils/metadata/operator_metadata.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,18 @@ type OperatorMetadataForwarder struct {
3434
}
3535

3636
type OperatorMetadataPayload struct {
37-
Hostname string `json:"hostname"`
37+
UUID string `json:"uuid"`
3838
Timestamp int64 `json:"timestamp"`
3939
ClusterID string `json:"cluster_id"`
4040
Metadata OperatorMetadata `json:"datadog_operator_metadata"`
4141
}
4242

4343
type OperatorMetadata struct {
44-
OperatorVersion string `json:"operator_version"`
45-
KubernetesVersion string `json:"kubernetes_version"`
44+
// Shared
45+
OperatorVersion string `json:"operator_version"`
46+
KubernetesVersion string `json:"kubernetes_version"`
47+
ClusterID string `json:"cluster_id"`
48+
4649
InstallMethodTool string `json:"install_method_tool"`
4750
InstallMethodToolVersion string `json:"install_method_tool_version"`
4851
IsLeader bool `json:"is_leader"`
@@ -57,7 +60,6 @@ type OperatorMetadata struct {
5760
ExtendedDaemonSetEnabled bool `json:"extendeddaemonset_enabled"`
5861
RemoteConfigEnabled bool `json:"remote_config_enabled"`
5962
IntrospectionEnabled bool `json:"introspection_enabled"`
60-
ClusterID string `json:"cluster_id"`
6163
ConfigDDURL string `json:"config_dd_url"`
6264
ConfigDDSite string `json:"config_site"`
6365
ResourceCounts map[string]int `json:"resource_count"`
@@ -74,10 +76,6 @@ func NewOperatorMetadataForwarder(logger logr.Logger, k8sClient client.Reader, k
7476

7577
// Start starts the operator metadata forwarder
7678
func (omf *OperatorMetadataForwarder) Start() {
77-
if omf.hostName == "" {
78-
omf.logger.Error(ErrEmptyHostName, "Could not set host name; not starting metadata forwarder")
79-
return
80-
}
8179
omf.updateResourceCounts()
8280

8381
omf.logger.Info("Starting metadata forwarder")
@@ -131,7 +129,7 @@ func (omf *OperatorMetadataForwarder) GetPayload(clusterUID string) []byte {
131129
omf.OperatorMetadata.KubernetesVersion = omf.kubernetesVersion
132130

133131
payload := OperatorMetadataPayload{
134-
Hostname: omf.hostName,
132+
UUID: clusterUID,
135133
Timestamp: now,
136134
ClusterID: clusterUID,
137135
Metadata: omf.OperatorMetadata,

pkg/controller/utils/metadata/operator_metadata_test.go

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ func Test_getURL(t *testing.T) {
3636
os.Setenv("DD_CLUSTER_NAME", "cluster")
3737
os.Setenv("DD_API_KEY", "api-key")
3838
os.Setenv("DD_APP_KEY", "app-key")
39-
os.Setenv("DD_HOSTNAME", "host-name")
4039
},
4140
wantURL: "https://app.datadoghq.com/api/v1/metadata",
4241
},
@@ -47,7 +46,6 @@ func Test_getURL(t *testing.T) {
4746
os.Setenv("DD_SITE", "datad0g.com")
4847
os.Setenv("DD_API_KEY", "api-key")
4948
os.Setenv("DD_APP_KEY", "app-key")
50-
os.Setenv("DD_HOSTNAME", "host-name")
5149
},
5250
wantURL: "https://app.datad0g.com/api/v1/metadata",
5351
},
@@ -58,7 +56,6 @@ func Test_getURL(t *testing.T) {
5856
os.Setenv("DD_URL", "https://app.datad0g.com")
5957
os.Setenv("DD_API_KEY", "api-key")
6058
os.Setenv("DD_APP_KEY", "app-key")
61-
os.Setenv("DD_HOSTNAME", "host-name")
6259
},
6360
wantURL: "https://app.datad0g.com/api/v1/metadata",
6461
},
@@ -116,7 +113,6 @@ func Test_setup(t *testing.T) {
116113
os.Setenv("DD_API_KEY", fakeAPIKeyOperator)
117114
os.Setenv("DD_APP_KEY", fakeAPPKeyDDA)
118115
os.Setenv("DD_CLUSTER_NAME", fakeClusterNameOperator)
119-
os.Setenv("DD_HOSTNAME", "host-name")
120116
},
121117
dda: &v2alpha1.DatadogAgent{},
122118
wantClusterName: "fake_cluster_name_operator",
@@ -128,7 +124,6 @@ func Test_setup(t *testing.T) {
128124
loadFunc: func() {
129125
os.Clearenv()
130126
os.Setenv("DD_CLUSTER_NAME", fakeClusterNameOperator)
131-
os.Setenv("DD_HOSTNAME", "host-name")
132127

133128
},
134129
dda: &v2alpha1.DatadogAgent{
@@ -149,7 +144,6 @@ func Test_setup(t *testing.T) {
149144
name: "credentials and site set in DDA",
150145
loadFunc: func() {
151146
os.Clearenv()
152-
os.Setenv("DD_HOSTNAME", "host-name")
153147
},
154148
dda: &v2alpha1.DatadogAgent{
155149
Spec: v2alpha1.DatadogAgentSpec{
@@ -209,7 +203,6 @@ func Test_GetPayload(t *testing.T) {
209203
expectedKubernetesVersion := "v1.28.0"
210204
expectedOperatorVersion := "v1.19.0"
211205
expectedClusterUID := "test-cluster-uid-12345"
212-
expectedHostname := "test-host"
213206

214207
s := testutils_test.TestScheme()
215208
kubeSystem := &corev1.Namespace{
@@ -227,9 +220,6 @@ func Test_GetPayload(t *testing.T) {
227220
},
228221
}
229222

230-
// Set hostname in SharedMetadata to simulate it being populated
231-
omf.hostName = expectedHostname
232-
233223
payload := omf.GetPayload(expectedClusterUID)
234224

235225
// Verify payload is valid JSON
@@ -244,10 +234,6 @@ func Test_GetPayload(t *testing.T) {
244234
}
245235

246236
// Validate top-level fields
247-
if hostname, ok := parsed["hostname"].(string); !ok || hostname != expectedHostname {
248-
t.Errorf("GetPayload() hostname = %v, want %v", hostname, expectedHostname)
249-
}
250-
251237
if timestamp, ok := parsed["timestamp"].(float64); !ok || timestamp <= 0 {
252238
t.Errorf("GetPayload() timestamp = %v, want positive number", timestamp)
253239
}

pkg/controller/utils/metadata/shared_metadata.go

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"fmt"
1313
"net/http"
1414
"net/url"
15-
"os"
1615
"time"
1716

1817
"github.com/go-logr/logr"
@@ -22,7 +21,6 @@ import (
2221

2322
"github.com/DataDog/datadog-operator/api/datadoghq/v2alpha1"
2423
"github.com/DataDog/datadog-operator/pkg/config"
25-
"github.com/DataDog/datadog-operator/pkg/constants"
2624
"github.com/DataDog/datadog-operator/pkg/version"
2725
)
2826

@@ -38,11 +36,6 @@ const (
3836
defaultURLPath = "api/v1/metadata"
3937
)
4038

41-
var (
42-
// ErrEmptyHostName empty HostName error
43-
ErrEmptyHostName = errors.New("empty host name")
44-
)
45-
4639
// SharedMetadata contains the common metadata shared across all forwarders
4740
type SharedMetadata struct {
4841
k8sClient client.Reader
@@ -52,7 +45,6 @@ type SharedMetadata struct {
5245
clusterUID string
5346
operatorVersion string
5447
kubernetesVersion string
55-
hostName string
5648
httpClient *http.Client
5749

5850
// Shared credential management
@@ -66,7 +58,6 @@ func NewSharedMetadata(logger logr.Logger, k8sClient client.Reader, kubernetesVe
6658
logger: logger,
6759
operatorVersion: operatorVersion,
6860
kubernetesVersion: kubernetesVersion,
69-
hostName: os.Getenv(constants.DDHostName),
7061
httpClient: &http.Client{
7162
Timeout: 10 * time.Second,
7263
},
@@ -75,11 +66,6 @@ func NewSharedMetadata(logger logr.Logger, k8sClient client.Reader, kubernetesVe
7566
}
7667

7768
func (sm *SharedMetadata) createRequest(payload []byte) (*http.Request, error) {
78-
if sm.hostName == "" {
79-
sm.logger.Error(ErrEmptyHostName, "Could not set host name; not starting metadata forwarder")
80-
return nil, ErrEmptyHostName
81-
}
82-
8369
apiKey, requestURL, err := sm.getApiKeyAndURL()
8470
if err != nil {
8571
sm.logger.V(1).Info("Could not get credentials", "error", err)

0 commit comments

Comments
 (0)