Skip to content

Commit 73a8e1e

Browse files
committed
OCPBUGS-45514: Report email_domain to telemetry
1 parent a36b4a7 commit 73a8e1e

File tree

3 files changed

+30
-19
lines changed

3 files changed

+30
-19
lines changed

pkg/console/operator/operator.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,9 @@ type consoleOperator struct {
101101
type trackables struct {
102102
// used to keep track of OLM capability
103103
isOLMDisabled bool
104-
// track organization ID
104+
// track organization ID and mail
105105
organizationID string
106+
accountMail string
106107
}
107108

108109
func NewConsoleOperator(

pkg/console/operator/sync_v400.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,12 +480,14 @@ func (co *consoleOperator) GetTelemetryConfiguration(ctx context.Context, operat
480480
if err != nil {
481481
return nil, err
482482
}
483-
organizationID, refreshCache := telemetry.GetOrganizationID(telemetryConfig, co.trackables.organizationID, clusterID, accessToken)
483+
organizationID, accountMail, refreshCache := telemetry.GetOrganizationMeta(telemetryConfig, co.trackables.organizationID, co.trackables.accountMail, clusterID, accessToken)
484484
// cache fetched ORGANIZATION_ID
485485
if refreshCache {
486486
co.trackables.organizationID = organizationID
487+
co.trackables.accountMail = accountMail
487488
}
488489
telemetryConfig["ORGANIZATION_ID"] = organizationID
490+
telemetryConfig["ACCOUNT_MAIL"] = accountMail
489491

490492
return telemetryConfig, nil
491493
}

pkg/console/telemetry/telemetry.go

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -84,23 +84,23 @@ func GetAccessToken(secretsLister v1.SecretLister) (string, error) {
8484
// 1. custom ORGANIZATION_ID is awailable as telemetry annotation on console-operator config or in telemetry-config configmap
8585
// 2. cached ORGANIZATION_ID is available on the operator controller instance
8686
// else fetch the ORGANIZATION_ID from OCM
87-
func GetOrganizationID(telemetryConfig map[string]string, cachedOrganizationID, clusterID, accessToken string) (string, bool) {
87+
func GetOrganizationMeta(telemetryConfig map[string]string, cachedOrganizationID, cachedAccountEmail, clusterID, accessToken string) (string, string, bool) {
8888
customOrganizationID, isCustomOrgIDSet := telemetryConfig["ORGANIZATION_ID"]
8989
if isCustomOrgIDSet {
9090
klog.V(4).Infoln("telemetry config: using custom organization ID")
91-
return customOrganizationID, false
91+
return customOrganizationID, "", false
9292
}
9393

94-
if cachedOrganizationID != "" {
95-
klog.V(4).Infoln("telemetry config: using cached organization ID")
96-
return cachedOrganizationID, false
94+
if cachedOrganizationID != "" && cachedAccountEmail != "" {
95+
klog.V(4).Infoln("telemetry config: using cached organization metadata")
96+
return cachedOrganizationID, cachedAccountEmail, false
9797
}
9898

99-
fetchedOrganizationID, err := FetchOrganizationID(clusterID, accessToken)
99+
fetchedOCMRespose, err := FetchSubscription(clusterID, accessToken)
100100
if err != nil {
101101
klog.Errorf("telemetry config error: %s", err)
102102
}
103-
return fetchedOrganizationID, true
103+
return fetchedOCMRespose.Organization.ExternalId, fetchedOCMRespose.Creator.Email, true
104104
}
105105

106106
// Needed to create our own types for OCM Subscriptions since their types and client are useless
@@ -111,23 +111,28 @@ type OCMAPIResponse struct {
111111
}
112112
type Subscription struct {
113113
Organization Organization `json:"organization,omitempty"`
114+
Creator Creator `json:"creator,omitempty"`
115+
}
116+
117+
type Creator struct {
118+
Email string `json:"email,omitempty"`
114119
}
115120

116121
type Organization struct {
117122
ExternalId string `json:"external_id,omitempty"`
118123
}
119124

120-
// FetchOrganizationID fetches the organization ID using the cluster ID and access token
121-
func FetchOrganizationID(clusterID, accessToken string) (string, error) {
125+
// FetchOrganizationMeta fetches the organization ID and Accout email using the cluster ID and access token
126+
func FetchSubscription(clusterID, accessToken string) (*Subscription, error) {
122127
klog.V(4).Infoln("telemetry config: fetching organization ID")
123128
u, err := buildURL(clusterID)
124129
if err != nil {
125-
return "", err // more contextual error handling can be added here if needed
130+
return nil, err // more contextual error handling can be added here if needed
126131
}
127132

128133
req, err := createRequest(u, clusterID, accessToken)
129134
if err != nil {
130-
return "", err
135+
return nil, err
131136
}
132137

133138
client := &http.Client{
@@ -138,29 +143,31 @@ func FetchOrganizationID(clusterID, accessToken string) (string, error) {
138143
}
139144
resp, err := client.Do(req)
140145
if err != nil {
141-
return "", fmt.Errorf("failed to GET (%s): %v", u.String(), err)
146+
return nil, fmt.Errorf("failed to GET (%s): %v", u.String(), err)
142147
}
143148
defer resp.Body.Close()
144149

145150
if resp.StatusCode != http.StatusOK {
146-
return "", fmt.Errorf("HTTP request failed with status '%s'", resp.Status)
151+
return nil, fmt.Errorf("HTTP request failed with status '%s'", resp.Status)
147152
}
148153

149154
body, err := io.ReadAll(resp.Body)
150155
if err != nil {
151-
return "", fmt.Errorf("failed to read response body: %v", err)
156+
return nil, fmt.Errorf("failed to read response body: %v", err)
152157
}
153158

154159
var ocmResponse OCMAPIResponse
155160
if err = json.Unmarshal(body, &ocmResponse); err != nil {
156-
return "", fmt.Errorf("error decoding JSON: %v", err)
161+
return nil, fmt.Errorf("error decoding JSON: %v", err)
157162
}
163+
klog.Infof("---> data: %#v\n", string(body))
164+
klog.Infof("---> ocmResponse: %#v\n", ocmResponse)
158165

159166
if len(ocmResponse.Items) == 0 {
160-
return "", fmt.Errorf("empty OCM response")
167+
return nil, fmt.Errorf("empty OCM response")
161168
}
162169

163-
return ocmResponse.Items[0].Organization.ExternalId, nil
170+
return &ocmResponse.Items[0], nil
164171
}
165172

166173
// buildURL constructs the URL for the API request
@@ -172,6 +179,7 @@ func buildURL(clusterID string) (*url.URL, error) {
172179
}
173180
q := u.Query()
174181
q.Add("fetchOrganization", "true")
182+
q.Add("fetchAccounts", "true")
175183
q.Add("search", fmt.Sprintf("external_cluster_id='%s'", clusterID))
176184
u.RawQuery = q.Encode()
177185
return u, nil

0 commit comments

Comments
 (0)