Skip to content

Commit a1c51d4

Browse files
committed
fix panic for uninitialized
1 parent 73a8e1e commit a1c51d4

File tree

3 files changed

+25
-10
lines changed

3 files changed

+25
-10
lines changed

pkg/console/operator/operator.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,13 @@ func NewConsoleOperator(
209209
Resource: api.OLMConfigResource,
210210
}
211211

212+
// set default values for trackables
213+
c.trackables = trackables{
214+
isOLMDisabled: false,
215+
organizationID: "",
216+
accountMail: "",
217+
}
218+
212219
if found, _ := isResourceEnabled(dynamicClient, olmGroupVersionResource); found {
213220
olmConfigInformer := dynamicInformers.ForResource(olmGroupVersionResource)
214221
informers = append(informers, olmConfigInformer.Informer())

pkg/console/operator/sync_v400.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ func (co *consoleOperator) SyncConfigMap(
438438
// 2. get telemetry annotation from console-operator config
439439
// 3. get default telemetry value from telemetry-config configmap
440440
// 4. get CLUSTER_ID from the cluster-version config
441-
// 5. get ORGANIZATION_ID from OCM, if ORGANIZATION_ID is not already set
441+
// 5. get ORGANIZATION_ID and ACCOUNT_MAIL from OCM, if they are not already set
442442
func (co *consoleOperator) GetTelemetryConfiguration(ctx context.Context, operatorConfig *operatorv1.Console) (map[string]string, error) {
443443
telemetryConfig := make(map[string]string)
444444

@@ -481,7 +481,7 @@ func (co *consoleOperator) GetTelemetryConfiguration(ctx context.Context, operat
481481
return nil, err
482482
}
483483
organizationID, accountMail, refreshCache := telemetry.GetOrganizationMeta(telemetryConfig, co.trackables.organizationID, co.trackables.accountMail, clusterID, accessToken)
484-
// cache fetched ORGANIZATION_ID
484+
// cache fetched ORGANIZATION_ID and ACCOUNT_MAIL
485485
if refreshCache {
486486
co.trackables.organizationID = organizationID
487487
co.trackables.accountMail = accountMail

pkg/console/telemetry/telemetry.go

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,16 @@ func GetAccessToken(secretsLister v1.SecretLister) (string, error) {
8181
}
8282

8383
// 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
84+
// 1. custom ORGANIZATION_ID and ACCOUNT_MAIL is awailable as telemetry annotation on console-operator config or in telemetry-config configmap
85+
// 2. cached ORGANIZATION_ID and ACCOUNT_MAIL is available on the operator controller instance
86+
// else fetch the ORGANIZATION_ID and ACCOUNT_MAIL from OCM
8787
func GetOrganizationMeta(telemetryConfig map[string]string, cachedOrganizationID, cachedAccountEmail, clusterID, accessToken string) (string, string, bool) {
8888
customOrganizationID, isCustomOrgIDSet := telemetryConfig["ORGANIZATION_ID"]
89-
if isCustomOrgIDSet {
90-
klog.V(4).Infoln("telemetry config: using custom organization ID")
91-
return customOrganizationID, "", false
89+
customAccountMail, isCustomAccountMailSet := telemetryConfig["ACCOUNT_MAIL"]
90+
91+
if isCustomOrgIDSet && isCustomAccountMailSet {
92+
klog.V(4).Infoln("telemetry config: using custom data")
93+
return customOrganizationID, customAccountMail, false
9294
}
9395

9496
if cachedOrganizationID != "" && cachedAccountEmail != "" {
@@ -99,7 +101,15 @@ func GetOrganizationMeta(telemetryConfig map[string]string, cachedOrganizationID
99101
fetchedOCMRespose, err := FetchSubscription(clusterID, accessToken)
100102
if err != nil {
101103
klog.Errorf("telemetry config error: %s", err)
104+
return "", "", false // Ensure safe return in case of error
105+
}
106+
107+
// Check if the fetched response is nil before accessing fields
108+
if fetchedOCMRespose == nil {
109+
klog.Errorf("telemetry config error: FetchSubscription returned nil response")
110+
return "", "", false
102111
}
112+
103113
return fetchedOCMRespose.Organization.ExternalId, fetchedOCMRespose.Creator.Email, true
104114
}
105115

@@ -160,8 +170,6 @@ func FetchSubscription(clusterID, accessToken string) (*Subscription, error) {
160170
if err = json.Unmarshal(body, &ocmResponse); err != nil {
161171
return nil, fmt.Errorf("error decoding JSON: %v", err)
162172
}
163-
klog.Infof("---> data: %#v\n", string(body))
164-
klog.Infof("---> ocmResponse: %#v\n", ocmResponse)
165173

166174
if len(ocmResponse.Items) == 0 {
167175
return nil, fmt.Errorf("empty OCM response")

0 commit comments

Comments
 (0)