Skip to content

Commit 4e0dad0

Browse files
author
Israel Blancas
committed
Apply changes requested in CR
Signed-off-by: Israel Blancas <[email protected]>
1 parent e5b5663 commit 4e0dad0

File tree

3 files changed

+40
-13
lines changed

3 files changed

+40
-13
lines changed

internal/operator-metrics/metrics.go

+3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"k8s.io/apimachinery/pkg/util/intstr"
2727
"k8s.io/client-go/rest"
2828
"sigs.k8s.io/controller-runtime/pkg/client"
29+
"sigs.k8s.io/controller-runtime/pkg/manager"
2930
)
3031

3132
var (
@@ -46,6 +47,8 @@ var (
4647
openshiftInClusterMonitoringNamespace = "openshift-monitoring"
4748
)
4849

50+
var _ manager.Runnable = &OperatorMetrics{}
51+
4952
type OperatorMetrics struct {
5053
kubeClient client.Client
5154
}

internal/operator-metrics/metrics_test.go

+26-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,11 @@ import (
2424
"github.com/stretchr/testify/assert"
2525
"github.com/stretchr/testify/require"
2626
corev1 "k8s.io/api/core/v1"
27+
apierrors "k8s.io/apimachinery/pkg/api/errors"
2728
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2829
"k8s.io/apimachinery/pkg/runtime"
30+
"k8s.io/apimachinery/pkg/types"
31+
"k8s.io/apimachinery/pkg/util/wait"
2932
"k8s.io/client-go/rest"
3033
"sigs.k8s.io/controller-runtime/pkg/client/fake"
3134
)
@@ -65,8 +68,29 @@ func TestOperatorMetrics_Start(t *testing.T) {
6568
errChan <- metrics.Start(ctx)
6669
}()
6770

68-
// Wait a bit to allow the Start method to run
69-
time.Sleep(100 * time.Millisecond)
71+
ctxTimeout, cancelTimeout := context.WithTimeout(ctx, time.Second*10)
72+
defer cancelTimeout()
73+
74+
// Wait until one service monitor is being created
75+
var serviceMonitor *monitoringv1.ServiceMonitor = &monitoringv1.ServiceMonitor{}
76+
err = wait.PollUntilContextTimeout(
77+
ctxTimeout,
78+
time.Millisecond*100,
79+
time.Second*100,
80+
true,
81+
func(ctx context.Context) (bool, error) {
82+
errGet := client.Get(ctx, types.NamespacedName{Name: "opentelemetry-operator-metrics-monitor", Namespace: "test-namespace"}, serviceMonitor)
83+
84+
if errGet != nil {
85+
if apierrors.IsNotFound(errGet) {
86+
return false, nil
87+
}
88+
return false, err
89+
}
90+
return true, nil
91+
},
92+
)
93+
require.NoError(t, err)
7094

7195
cancel()
7296
err = <-errChan

main.go

+11-11
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,17 @@ func main() {
422422
os.Exit(1)
423423
}
424424

425+
if cfg.PrometheusCRAvailability() == prometheus.Available {
426+
operatorMetrics, opError := operatormetrics.NewOperatorMetrics(mgr.GetConfig(), scheme)
427+
if opError != nil {
428+
setupLog.Error(opError, "Failed to create the operator metrics SM")
429+
}
430+
err = mgr.Add(operatorMetrics)
431+
if err != nil {
432+
setupLog.Error(err, "Failed to add the operator metrics SM")
433+
}
434+
}
435+
425436
if os.Getenv("ENABLE_WEBHOOKS") != "false" {
426437
var crdMetrics *otelv1beta1.Metrics
427438

@@ -435,17 +446,6 @@ func main() {
435446
if err != nil {
436447
setupLog.Error(err, "Error init CRD metrics")
437448
}
438-
439-
if cfg.PrometheusCRAvailability() == prometheus.Available {
440-
operatorMetrics, opError := operatormetrics.NewOperatorMetrics(mgr.GetConfig(), scheme)
441-
if opError != nil {
442-
setupLog.Error(opError, "Failed to create the operator metrics SM")
443-
}
444-
err = mgr.Add(operatorMetrics)
445-
if err != nil {
446-
setupLog.Error(err, "Failed to add the operator metrics SM")
447-
}
448-
}
449449
}
450450

451451
bv := func(collector otelv1beta1.OpenTelemetryCollector) admission.Warnings {

0 commit comments

Comments
 (0)