Skip to content

Commit 067cc61

Browse files
committed
OCPBUGS-29900:fix the Metric cco_credentials_mode issue
1 parent f019679 commit 067cc61

File tree

1 file changed

+16
-62
lines changed

1 file changed

+16
-62
lines changed

pkg/operator/metrics/metrics.go

Lines changed: 16 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package metrics
22

33
import (
44
"context"
5-
"strings"
65
"time"
76

87
"github.com/prometheus/client_golang/prometheus"
@@ -21,7 +20,6 @@ import (
2120
operatorv1 "github.com/openshift/api/operator/v1"
2221

2322
credreqv1 "github.com/openshift/cloud-credential-operator/pkg/apis/cloudcredential/v1"
24-
"github.com/openshift/cloud-credential-operator/pkg/azure"
2523
"github.com/openshift/cloud-credential-operator/pkg/operator/constants"
2624
"github.com/openshift/cloud-credential-operator/pkg/operator/platform"
2725
"github.com/openshift/cloud-credential-operator/pkg/operator/utils"
@@ -150,11 +148,14 @@ func (mc *Calculator) metricsLoop() {
150148
mc.log.WithError(err).Error("failed to fetch cloud secret")
151149
return
152150
}
151+
152+
tokenCluster, err := utils.IsTimedTokenCluster(mc.Client, context.TODO(), mc.log)
153+
153154
setCredentialsMode(&clusterState{
154-
mode: mode,
155-
rootSecret: cloudSecret,
156-
rootSecretNotFound: errors.IsNotFound(err),
157-
foundPodIdentityCredentials: accumulator.podIdentityCredentials > 0,
155+
mode: mode,
156+
rootSecret: cloudSecret,
157+
rootSecretNotFound: errors.IsNotFound(err),
158+
isTokenCluster: tokenCluster,
158159
}, mc.log)
159160
}
160161

@@ -219,17 +220,14 @@ type credRequestAccumulator struct {
219220
crTotals map[string]int
220221
crConditions map[credreqv1.CredentialsRequestConditionType]int
221222
crMode map[constants.CredentialsMode]int
222-
223-
podIdentityCredentials int
224223
}
225224

226225
func newAccumulator(client client.Client, logger log.FieldLogger) *credRequestAccumulator {
227226
acc := &credRequestAccumulator{
228-
kubeClient: client,
229-
logger: logger,
230-
crTotals: map[string]int{},
231-
crConditions: map[credreqv1.CredentialsRequestConditionType]int{},
232-
podIdentityCredentials: 0,
227+
kubeClient: client,
228+
logger: logger,
229+
crTotals: map[string]int{},
230+
crConditions: map[credreqv1.CredentialsRequestConditionType]int{},
233231
}
234232

235233
// make entries with '0' so we make sure to send updated metrics for any
@@ -250,15 +248,6 @@ func (a *credRequestAccumulator) processCR(cr *credreqv1.CredentialsRequest, cco
250248
cloudKey := cloudProviderSpecToMetricsKey(cloudType)
251249
a.crTotals[cloudKey]++
252250

253-
isPodIdentity, err := credRequestIsPodIdentity(cr, cloudType, a.kubeClient)
254-
if err != nil {
255-
a.logger.WithError(err).Error("failed to determine whether CredentialsRequest is of type STS")
256-
}
257-
258-
if isPodIdentity {
259-
a.podIdentityCredentials++
260-
}
261-
262251
// Skip reporting conditions if CCO is disabled, as we shouldn't be alerting in that case, except for stale credentials.
263252
// condition. The stale credentials are removed by cleanup controller. But when CCO is disabled the only way to inform
264253
// users to remove these credentials is through alerts.
@@ -279,10 +268,10 @@ func (a *credRequestAccumulator) processCR(cr *credreqv1.CredentialsRequest, cco
279268
}
280269

281270
type clusterState struct {
282-
mode operatorv1.CloudCredentialsMode
283-
rootSecret *corev1.Secret
284-
rootSecretNotFound bool
285-
foundPodIdentityCredentials bool
271+
mode operatorv1.CloudCredentialsMode
272+
rootSecret *corev1.Secret
273+
rootSecretNotFound bool
274+
isTokenCluster bool
286275
}
287276

288277
func setCredentialsMode(state *clusterState, logger log.FieldLogger) {
@@ -317,7 +306,7 @@ func determineCredentialsMode(state *clusterState, logger log.FieldLogger) const
317306

318307
// if the accumulator found any Secrets with pod identity credentials data
319308
// then we'll report the PodIdentity submode of Manual mode
320-
if state.foundPodIdentityCredentials {
309+
if state.isTokenCluster {
321310
return constants.ModeManualPodIdentity
322311
}
323312

@@ -362,38 +351,3 @@ func (a *credRequestAccumulator) setMetrics() {
362351
metricCredentialsRequestConditions.WithLabelValues(string(k)).Set(float64(v))
363352
}
364353
}
365-
366-
func credRequestIsPodIdentity(cr *credreqv1.CredentialsRequest, cloudType string, kubeClient client.Client) (bool, error) {
367-
secretKey := types.NamespacedName{Name: cr.Spec.SecretRef.Name, Namespace: cr.Spec.SecretRef.Namespace}
368-
secret := &corev1.Secret{}
369-
370-
err := kubeClient.Get(context.TODO(), secretKey, secret)
371-
if errors.IsNotFound(err) {
372-
// Secret for CredReq doesn't exist so we can't query it
373-
return false, nil
374-
} else if err != nil {
375-
return false, err
376-
}
377-
378-
switch cloudType {
379-
case "AWSProviderSpec":
380-
secretData, ok := secret.Data[constants.AWSSecretDataCredentialsKey]
381-
if !ok {
382-
return false, nil
383-
}
384-
385-
// web_identity_token_file is a clear indicator that the credentials
386-
// are configured for pod identity / STS credentials
387-
if strings.Contains(string(secretData), "web_identity_token_file") {
388-
return true, nil
389-
}
390-
391-
return false, nil
392-
case "AzureProviderSpec":
393-
_, ok := secret.Data[azure.AzureFederatedTokenFile]
394-
return ok, nil
395-
default:
396-
return false, nil
397-
}
398-
399-
}

0 commit comments

Comments
 (0)