Skip to content

Commit e991bf0

Browse files
authored
[Cloud Connect] Use Display Name to register cluster (#47440)
This overrides the cluster name submitted to the Cloud Connected API with the `cluster.metadata.display_name` setting -- if set -- with the proper precendence of transient, then persistent setting. For any existing registration, this will overwrite the old name with the new name upon restart.
1 parent 0e87812 commit e991bf0

File tree

3 files changed

+265
-82
lines changed

3 files changed

+265
-82
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# REQUIRED
2+
# - bug-fix: fixes a problem in a previous version
3+
kind: bug-fix
4+
summary: "[Cloud Connect] Use cluster.metadata.display_name as cluster name if set"
5+
component: metricbeat
6+
7+
# AUTOMATED
8+
# OPTIONAL to manually add other PR URLs
9+
# PR URL: A link the PR that added the changeset.
10+
# If not present is automatically filled by the tooling finding the PR where this changelog fragment has been added.
11+
# NOTE: the tooling supports backports, so it's able to fill the original PR number instead of the backport PR number.
12+
# Please provide it if you are adding a fragment for a different PR.
13+
# pr: https://github.com/owner/repo/1234
14+
15+
# AUTOMATED
16+
# OPTIONAL to manually add other issue URLs
17+
# Issue URL; optional; the GitHub issue related to this changeset (either closes or is part of).
18+
# If not present is automatically filled by the tooling with the issue linked to the PR number.
19+
# issue: https://github.com/owner/repo/1234

x-pack/metricbeat/module/autoops_es/metricset/register.go

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,25 @@ import (
1818
)
1919

2020
const (
21-
licensePath = "/_license"
22-
licensePathV7 = "/_license?accept_enterprise"
21+
clusterSettingsPath = "/_cluster/settings?filter_path=**.cluster.metadata.display_name"
22+
licensePath = "/_license"
23+
licensePathV7 = "/_license?accept_enterprise"
2324
)
2425

26+
type clusterSettingsResponse struct {
27+
Persistent clusterSettingsDisplayName `json:"persistent"`
28+
Transient clusterSettingsDisplayName `json:"transient"`
29+
}
30+
31+
// Partial structure to extract the cluster display name from the cluster settings API response.
32+
type clusterSettingsDisplayName struct {
33+
Cluster struct {
34+
Metadata struct {
35+
DisplayName string `json:"display_name"`
36+
} `json:"metadata"`
37+
} `json:"cluster"`
38+
}
39+
2540
// License information returned from `GET /_license`, contained within the "license" object.
2641
type license struct {
2742
UID string `json:"uid"`
@@ -75,20 +90,38 @@ func maybeRegisterCloudConnectedCluster(m *elasticsearch.MetricSet, getClusterIn
7590
cloudApiKey, err := getCloudConnectedModeApiKey(m)
7691

7792
if err != nil {
78-
return fmt.Errorf("failed to get Cloud Connected Mode API key: %w", err)
93+
return fmt.Errorf("failed to get Cloud Connected API Key: %w", err)
7994
} else if cloudApiKey == "" {
8095
// if there is no Cloud API Key configured, then there is no request to make
8196
return nil
8297
}
8398

84-
m.Logger().Debugf("Attempting to get cluster info for Cloud Connected Mode...")
99+
m.Logger().Debugf("Attempting to get cluster info for Cloud Connected...")
85100
clusterInfo, err := getClusterInfo(m)
86101

87102
if err != nil {
88103
return fmt.Errorf("failed to load cluster info: %w", err)
89104
}
90105

91-
m.Logger().Debugf("Attempting to fetch license for Cloud Connected Mode...")
106+
m.Logger().Debugf("Attempting to fetch cluster settings for Cloud Connected...")
107+
clusterSettings, err := utils.FetchAPIData[clusterSettingsResponse](m, clusterSettingsPath)
108+
109+
if err != nil {
110+
// Log the error, but do not fail the registration - we can continue with the existing cluster name
111+
// Note: You can restart the agent to re-fetch the name later if needed
112+
m.Logger().Warnf("Ignoring failure to fetch cluster settings for Cloud Connected: %v", err)
113+
// prefer transient setting over persistent setting
114+
} else if clusterSettings.Transient.Cluster.Metadata.DisplayName != "" {
115+
m.Logger().Debugf("Using transient cluster display name %s for Cloud Connected in place of cluster name %s", clusterSettings.Transient.Cluster.Metadata.DisplayName, clusterInfo.ClusterName)
116+
117+
clusterInfo.ClusterName = clusterSettings.Transient.Cluster.Metadata.DisplayName
118+
} else if clusterSettings.Persistent.Cluster.Metadata.DisplayName != "" {
119+
m.Logger().Debugf("Using persistent cluster display name %s for Cloud Connected in place of cluster name %s", clusterSettings.Persistent.Cluster.Metadata.DisplayName, clusterInfo.ClusterName)
120+
121+
clusterInfo.ClusterName = clusterSettings.Persistent.Cluster.Metadata.DisplayName
122+
}
123+
124+
m.Logger().Debugf("Attempting to fetch license for Cloud Connected...")
92125
versionedLicensePath := licensePath
93126

94127
// TODO: Drop support for this when we drop support for 7.17
@@ -107,7 +140,7 @@ func maybeRegisterCloudConnectedCluster(m *elasticsearch.MetricSet, getClusterIn
107140
return fmt.Errorf("cluster license type is not supported: %s", licenseWrapper.License.Type)
108141
}
109142

110-
m.Logger().Debugf("Successfully fetched license for Cloud Connected Mode: UUID=%s License=%s", clusterInfo.ClusterID, licenseWrapper.License.UID)
143+
m.Logger().Debugf("Successfully fetched license for Cloud Connected: UUID=%s License=%s", clusterInfo.ClusterID, licenseWrapper.License.UID)
111144

112145
return registerCloudConnectedCluster(cloudApiKey, clusterInfo, &licenseWrapper.License, m.Logger())
113146
}
@@ -128,14 +161,14 @@ func registerCloudConnectedCluster(cloudApiKey string, clusterInfo *utils.Cluste
128161
})
129162

130163
if err != nil {
131-
return fmt.Errorf("failed to serialize payload for Cloud Connected Mode: %w", err)
164+
return fmt.Errorf("failed to serialize payload for Cloud Connected: %w", err)
132165
}
133166

134167
requestURL := getCloudConnectedModeAPIURL() + "/api/v1/cloud-connected/clusters"
135168
req, err := http.NewRequestWithContext(context.Background(), "POST", requestURL, bytes.NewBuffer(jsonData))
136169

137170
if err != nil {
138-
return fmt.Errorf("failed to create HTTP request for Cloud Connected Mode: %w", err)
171+
return fmt.Errorf("failed to create HTTP request for Cloud Connected: %w", err)
139172
}
140173

141174
req.Header.Set("Accept", "application/json")
@@ -146,9 +179,9 @@ func registerCloudConnectedCluster(cloudApiKey string, clusterInfo *utils.Cluste
146179

147180
if err == nil {
148181
utils.SetResourceID(data.ID)
149-
logger.Infof("Registered cluster %s for Cloud Connected Mode: %s", clusterInfo.ClusterID, data.ID)
182+
logger.Infof("Registered cluster %s for Cloud Connected: %s", clusterInfo.ClusterID, data.ID)
150183
return nil
151184
}
152185

153-
return fmt.Errorf("failed to register for Cloud Connected Mode: %w", err)
186+
return fmt.Errorf("failed to register for Cloud Connected: %w", err)
154187
}

0 commit comments

Comments
 (0)