Skip to content

Commit 2ec56b1

Browse files
Merge pull request #904 from jhadvig/OCPBUGS-33715
OCPBUGS-33715: Cache organization ID
2 parents efe7822 + 635a416 commit 2ec56b1

File tree

3 files changed

+37
-12
lines changed

3 files changed

+37
-12
lines changed

pkg/console/operator/operator.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,18 @@ type consoleOperator struct {
9090

9191
resourceSyncer resourcesynccontroller.ResourceSyncer
9292

93-
// used to keep track of OLM capability
94-
isOLMDisabled bool
93+
trackables trackables
9594

9695
monitoringDeploymentLister appsv1listers.DeploymentLister
9796
}
9897

98+
type trackables struct {
99+
// used to keep track of OLM capability
100+
isOLMDisabled bool
101+
// track organization ID
102+
organizationID string
103+
}
104+
99105
func NewConsoleOperator(
100106
ctx context.Context,
101107
// top level config
@@ -203,7 +209,7 @@ func NewConsoleOperator(
203209
informers = append(informers, olmConfigInformer.Informer())
204210
} else {
205211
klog.Info("olmconfigs resource does not exist in cluster, launching poll and disabling olmconfigs informer")
206-
c.isOLMDisabled = true
212+
c.trackables.isOLMDisabled = true
207213
c.startPollAndRestartIfResourceEnabled(olmGroupVersionResource)
208214
}
209215

pkg/console/operator/sync_v400.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ func (co *consoleOperator) SyncConfigMap(
376376
copiedCSVsDisabled bool
377377
ccdErr error
378378
)
379-
if !co.isOLMDisabled {
379+
if !co.trackables.isOLMDisabled {
380380
copiedCSVsDisabled, ccdErr = co.isCopiedCSVsDisabled(ctx)
381381
if ccdErr != nil {
382382
return nil, false, "FailedGetOLMConfig", ccdErr
@@ -461,12 +461,9 @@ func (co *consoleOperator) GetTelemetryConfiguration(ctx context.Context, operat
461461
return nil, err
462462
}
463463

464-
// If for any reason the getting the ORGANIZATION_ID fails,
465-
// log the error and set ORGANIZATION_ID to empty string.
466-
organizationID, err := telemetry.GetOrganizationID(clusterID, accessToken)
467-
if err != nil {
468-
klog.Errorf("telemetry config error: %s", err)
469-
}
464+
organizationID := telemetry.GetOrganizationID(telemetryConfig, co.trackables.organizationID, clusterID, accessToken)
465+
// cache ORGANIZATION_ID
466+
co.trackables.organizationID = organizationID
470467
telemetryConfig["ORGANIZATION_ID"] = organizationID
471468

472469
return telemetryConfig, nil

pkg/console/telemetry/telemetry.go

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"k8s.io/apimachinery/pkg/api/errors"
1313
appsv1listers "k8s.io/client-go/listers/apps/v1"
1414
v1 "k8s.io/client-go/listers/core/v1"
15+
"k8s.io/klog/v2"
1516

1617
"github.com/openshift/console-operator/pkg/api"
1718
deploymentsub "github.com/openshift/console-operator/pkg/console/subresource/deployment"
@@ -79,6 +80,27 @@ func GetAccessToken(secretsLister v1.SecretLister) (string, error) {
7980
return authsBytes.Auth, nil
8081
}
8182

83+
// check if:
84+
// 1. custom ORGANIZATION_ID is awailable as telemetry annotation on console-operator config or in telemetry-config configmap
85+
// 2. cached ORGANIZATION_ID is available on the operator controller instance
86+
// else fetch the ORGANIZATION_ID from OCM
87+
func GetOrganizationID(telemetryConfig map[string]string, cachedOrganizationID, clusterID, accessToken string) string {
88+
if customOrganizationID, isCustomOrgIDSet := telemetryConfig["ORGANIZATION_ID"]; isCustomOrgIDSet {
89+
return customOrganizationID
90+
}
91+
92+
if cachedOrganizationID != "" {
93+
return cachedOrganizationID
94+
}
95+
96+
fetchedOrganizationID, err := FetchOrganizationID(clusterID, accessToken)
97+
klog.V(4).Infoln("Fetching ORGANIZATION_ID from OCM")
98+
if err != nil {
99+
klog.Errorf("telemetry config error: %s", err)
100+
}
101+
return fetchedOrganizationID
102+
}
103+
82104
// Needed to create our own types for OCM Subscriptions since their types and client are useless
83105
// https://github.com/openshift-online/ocm-sdk-go/blob/main/accountsmgmt/v1/subscription_client.go - everything private
84106
// https://github.com/openshift-online/ocm-sdk-go/blob/main/accountsmgmt/v1/subscriptions_client.go#L38-L41 - useless client
@@ -89,8 +111,8 @@ type Organization struct {
89111
OrganizationID string `json:"organization_id"`
90112
}
91113

92-
// GetOrganizationID fetches the organization ID using the cluster ID and access token
93-
func GetOrganizationID(clusterID, accessToken string) (string, error) {
114+
// FetchOrganizationID fetches the organization ID using the cluster ID and access token
115+
func FetchOrganizationID(clusterID, accessToken string) (string, error) {
94116
u, err := buildURL(clusterID)
95117
if err != nil {
96118
return "", err // more contextual error handling can be added here if needed

0 commit comments

Comments
 (0)