Skip to content

Commit 286cd72

Browse files
committed
OCPBUGS-29900:fix the Metric cco_credentials_mode issue
1 parent 8f1a631 commit 286cd72

File tree

2 files changed

+8
-56
lines changed

2 files changed

+8
-56
lines changed

pkg/operator/metrics/metrics.go

Lines changed: 8 additions & 54 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{
154155
mode: mode,
155156
rootSecret: cloudSecret,
156157
rootSecretNotFound: errors.IsNotFound(err),
157-
foundPodIdentityCredentials: accumulator.podIdentityCredentials > 0,
158+
foundPodIdentityCredentials: 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.
@@ -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-
}

pkg/operator/metrics/metrics_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,6 @@ func TestCredentialsRequests(t *testing.T) {
233233
},
234234
validate: func(t *testing.T, accumulator *credRequestAccumulator) {
235235
assert.Equal(t, 2, accumulator.crTotals["aws"])
236-
assert.Equal(t, 1, accumulator.podIdentityCredentials)
237236
},
238237
},
239238
{
@@ -253,7 +252,6 @@ func TestCredentialsRequests(t *testing.T) {
253252
},
254253
validate: func(t *testing.T, accumulator *credRequestAccumulator) {
255254
assert.Equal(t, 2, accumulator.crTotals["azure"])
256-
assert.Equal(t, 1, accumulator.podIdentityCredentials)
257255
},
258256
},
259257
}

0 commit comments

Comments
 (0)