@@ -16,27 +16,28 @@ package sdk
16
16
17
17
import (
18
18
"context"
19
+ "fmt"
19
20
"net/http"
20
21
"strconv"
21
22
22
23
"github.com/operator-framework/operator-sdk/pkg/k8sutil"
24
+ "github.com/operator-framework/operator-sdk/pkg/sdk/log"
25
+
23
26
"github.com/prometheus/client_golang/prometheus/promhttp"
24
- "github.com/sirupsen/logrus"
25
27
v1 "k8s.io/api/core/v1"
26
28
"k8s.io/apimachinery/pkg/api/errors"
27
29
"sigs.k8s.io/controller-runtime/pkg/client"
28
30
"sigs.k8s.io/controller-runtime/pkg/client/config"
29
31
)
30
32
31
33
// ExposeMetricsPort generate a Kubernetes Service to expose metrics port
32
- func ExposeMetricsPort () * v1.Service {
34
+ func ExposeMetricsPort () ( * v1.Service , error ) {
33
35
http .Handle ("/" + k8sutil .PrometheusMetricsPortName , promhttp .Handler ())
34
36
go http .ListenAndServe (":" + strconv .Itoa (k8sutil .PrometheusMetricsPort ), nil )
35
37
36
38
service , err := k8sutil .InitOperatorService ()
37
39
if err != nil {
38
- logrus .Errorf ("failed to initialize service object for operator metrics: %v" , err )
39
- return nil
40
+ return nil , fmt .Errorf ("failed to initialize service object for operator metrics: %v" , err )
40
41
}
41
42
kubeconfig , err := config .GetConfig ()
42
43
if err != nil {
@@ -48,9 +49,9 @@ func ExposeMetricsPort() *v1.Service {
48
49
}
49
50
err = runtimeClient .Create (context .TODO (), service )
50
51
if err != nil && ! errors .IsAlreadyExists (err ) {
51
- logrus .Errorf ("failed to create service for operator metrics: %v" , err )
52
- return nil
52
+ return nil , fmt .Errorf ("failed to create service for operator metrics: %v" , err )
53
53
}
54
- logrus .Infof ("Metrics service %s created" , service .Name )
55
- return service
54
+
55
+ log .Info (fmt .Sprintf ("Metrics service %s created" , service .Name ), "ServiceName" , service .Name )
56
+ return service , nil
56
57
}
0 commit comments