Skip to content

Commit

Permalink
Add a root client to the Calculator struct to be used for querying th…
Browse files Browse the repository at this point in the history
…e root credential only

Signed-off-by: Feilian Xie <fxie@redhat.com>
  • Loading branch information
fxierh committed Jan 21, 2024
1 parent 4a537e4 commit 7c4f0b5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
13 changes: 9 additions & 4 deletions pkg/operator/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,10 @@ func Add(mgr, rootCredentialManager manager.Manager, kubeConfig string) error {
logger := log.WithField("controller", controllerName)

mc := &Calculator{
Client: utils.LiveClient(mgr),
Interval: 2 * time.Minute,
log: logger,
Client: mgr.GetClient(),
rootClient: rootCredentialManager.GetClient(),
Interval: 2 * time.Minute,
log: logger,
}
err := mgr.Add(mc)
if err != nil {
Expand All @@ -92,8 +93,12 @@ func Add(mgr, rootCredentialManager manager.Manager, kubeConfig string) error {
// This should be used for metrics which do not fit well into controller reconcile loops,
// things that are calculated globally rather than metrics related to specific reconciliations.
type Calculator struct {
// controller-runtime client used for querying anything except the root credential
Client client.Client

// controller-runtime client used for querying the root credential only
rootClient client.Client

// Interval is the length of time we sleep between metrics calculations.
Interval time.Duration

Expand Down Expand Up @@ -181,7 +186,7 @@ func (mc *Calculator) getCloudSecret() (*corev1.Secret, error) {
mc.log.WithField("cloud", platformType).Info("unsupported cloud for determining CCO mode")
return nil, nil
}
err = mc.Client.Get(context.TODO(), secretKey, secret)
err = mc.rootClient.Get(context.TODO(), secretKey, secret)
return secret, err
}

Expand Down
12 changes: 7 additions & 5 deletions pkg/operator/metrics/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ var (
)

func TestSecretGetter(t *testing.T) {
configv1.AddToScheme(scheme.Scheme)
err := configv1.Install(scheme.Scheme)
assert.NoError(t, err, "error installing configv1 types to scheme")

logger := log.WithField("controller", "metricscontrollertest")

Expand Down Expand Up @@ -112,11 +113,12 @@ func TestSecretGetter(t *testing.T) {

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {

fakeClient := fake.NewClientBuilder().WithRuntimeObjects(test.clusterInfra, test.cloudCredsSecret).Build()
fakeClient := fake.NewClientBuilder().WithRuntimeObjects(test.clusterInfra).Build()
fakeRootClient := fake.NewClientBuilder().WithRuntimeObjects(test.cloudCredsSecret).Build()
calc := &Calculator{
Client: fakeClient,
log: logger,
Client: fakeClient,
rootClient: fakeRootClient,
log: logger,
}

secret, err := calc.getCloudSecret()
Expand Down

0 comments on commit 7c4f0b5

Please sign in to comment.