Skip to content

Commit 539a5d6

Browse files
authored
Merge pull request #14309 from bergerhoffer/OSDOCS-34
OSDOCS-34: Updating with the latest examples
2 parents da41554 + 9033102 commit 539a5d6

4 files changed

+58
-40
lines changed

modules/osdk-monitoring-prometheus-metrics-helper-modifying-port.adoc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@
33
// * applications/operator_sdk/osdk-monitoring-prometheus.adoc
44

55
[id="osdk-monitoring-prometheus-metrics-helper-modifying-port-{context}"]
6-
= Modifying metrics port
6+
= Modifying the metrics port
77

88
Operator authors can modify the port that metrics are exposed on.
99

1010
.Prerequisites
1111

12-
- Go-based Operator generated using the Operator SDK
13-
- Kubernetes-based cluster with the Prometheus Operator deployed
12+
* Go-based Operator generated using the Operator SDK
13+
* Kubernetes-based cluster with the Prometheus Operator deployed
1414

1515
.Procedure
1616

17-
. In the generated Operator's `cmd/manager/main.go` file, change the `var
18-
metricsPort int32 = 8383` variable.
17+
* In the generated Operator's `cmd/manager/main.go` file, change the value of `metricsPort` in the line `var
18+
metricsPort int32 = 8383`.

modules/osdk-monitoring-prometheus-metrics-helper.adoc

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,15 @@
55
[id="osdk-monitoring-prometheus-metrics-helper-{context}"]
66
= Metrics helper
77

8-
In Go-based Operators generated using the Operator SDK, the following helper
8+
In Go-based Operators generated using the Operator SDK, the following
99
function exposes general metrics about the running program:
1010

1111
[source,go]
1212
----
1313
func ExposeMetricsPort(ctx context.Context, port int32) (*v1.Service, error)
1414
----
1515

16-
These metrics are inherited from the `controller-runtime` library API. The
17-
metrics are by default served on `:8383/metrics`.
16+
These metrics are inherited from the `controller-runtime` library API. By default, the metrics are served on `0.0.0.0:8383/metrics`.
1817

1918
A Service object is created with the metrics port exposed, which can be then
2019
accessed by Prometheus. The Service object is garbage collected when the leader
@@ -25,29 +24,34 @@ Operators generated using the Operator SDK:
2524

2625
[source,go]
2726
----
28-
import(
29-
"github.com/operator-framework/operator-sdk/pkg/metrics"
30-
"sigs.k8s.io/controller-runtime/pkg/manager"
31-
)
32-
33-
// Change the below variables to serve metrics on different host or port.
34-
var metricsHost = "0.0.0.0" <1>
35-
var metricsPort int32 = 8383 <2>
36-
27+
import(
28+
"github.com/operator-framework/operator-sdk/pkg/metrics"
29+
"sigs.k8s.io/controller-runtime/pkg/manager"
30+
)
31+
32+
var (
33+
// Change the below variables to serve metrics on a different host or port.
34+
metricsHost = "0.0.0.0" <1>
35+
metricsPort int32 = 8383 <2>
36+
)
37+
...
38+
func main() {
39+
...
3740
// Pass metrics address to controller-runtime manager
38-
mgr, err := manager.New(cfg, manager.Options{
39-
Namespace: namespace,
40-
MetricsBindAddress: fmt.Sprintf("%s:%d", metricsHost, metricsPort),
41-
})
41+
mgr, err := manager.New(cfg, manager.Options{
42+
Namespace: namespace,
43+
MetricsBindAddress: fmt.Sprintf("%s:%d", metricsHost, metricsPort),
44+
})
4245
4346
...
44-
45-
// Create Service object to expose the metrics port.
46-
_, err = metrics.ExposeMetricsPort(ctx, metricsPort)
47-
if err != nil {
47+
// Create Service object to expose the metrics port.
48+
_, err = metrics.ExposeMetricsPort(ctx, metricsPort)
49+
if err != nil {
4850
// handle error
49-
log.Info(err.Error())
50-
}
51+
log.Info(err.Error())
52+
}
53+
...
54+
}
5155
----
52-
<1> Host the metrics are exposed on.
53-
<2> Port the metrics are exposed on.
56+
<1> The host that the metrics are exposed on.
57+
<2> The port that the metrics are exposed on.

modules/osdk-monitoring-prometheus-servicemonitor-creating.adoc

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,36 @@ newly created Service.
1111

1212
.Prerequisites
1313

14-
- Go-based Operator generated using the Operator SDK
15-
- Kubernetes-based cluster with the Prometheus Operator deployed
14+
* Go-based Operator generated using the Operator SDK
15+
* Kubernetes-based cluster with the Prometheus Operator deployed
1616

1717
.Procedure
1818

19-
. Add the `metrics.CreateServiceMonitor()` helper function to your Operator code,
20-
creating only one ServiceMonitor per application and namespace:
19+
* Add the `metrics.CreateServiceMonitor()` helper function to your Operator code:
2120
+
2221
[source,go]
2322
----
24-
import "github.com/operator-framework/operator-sdk/pkg/metrics"
23+
import(
24+
"k8s.io/api/core/v1"
25+
"github.com/operator-framework/operator-sdk/pkg/metrics"
26+
"sigs.k8s.io/controller-runtime/pkg/client/config"
27+
)
28+
func main() {
2529
26-
serviceMonitors, err := metrics.CreateServiceMonitors(config *rest.Config, ns string, services []*v1.Service) <1>
30+
...
31+
// Populate below with the Service(s) for which you want to create ServiceMonitors.
32+
services := []*v1.Service{}
33+
// Create one ServiceMonitor per application per namespace.
34+
// Change the below value to name of the Namespace you want the ServiceMonitor to be created in.
35+
ns := "default"
36+
// restConfig is used for talking to the Kubernetes apiserver
37+
restConfig := config.GetConfig()
38+
39+
// Pass the Service(s) to the helper function, which in turn returns the array of ServiceMonitor objects.
40+
serviceMonitors, err := metrics.CreateServiceMonitors(restConfig, ns, services)
2741
if err != nil {
28-
<2>
42+
// Handle errors here.
2943
}
44+
...
45+
}
3046
----
31-
<1> Pass the Service(s) to the helper function, which in turn returns the ServiceMonitor object.
32-
<2> Handle errors here.

modules/osdk-monitoring-prometheus-servicemonitor.adoc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ generate a ServiceMonitor Custom Resource (CR) based on it.
1515

1616
.Additional resources
1717

18-
- See the
18+
* See the
1919
link:https://github.com/coreos/prometheus-operator/blob/7a25bf6b6bb2347dacb235659b73bc210117acc7/Documentation/design.md#servicemonitor[Prometheus Operator documentation]
20-
for more about the ServiceMonitor CRD.
20+
for more information about the ServiceMonitor CRD.

0 commit comments

Comments
 (0)