Skip to content

Commit 2d5a49b

Browse files
authored
Merge pull request #13666 from adellape/osdk_metrics
Add monitoring to OSDK
2 parents 5d5f0bb + 784ff29 commit 2d5a49b

10 files changed

+169
-11
lines changed

_topic_map.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ Topics:
119119
File: osdk-ansible
120120
- Name: Creating Helm-based Operators
121121
File: osdk-helm
122+
- Name: Configuring built-in monitoring with Prometheus
123+
File: osdk-monitoring-prometheus
122124
- Name: Operator SDK CLI reference
123125
File: osdk-cli-reference
124126
- Name: Migrating to Operator SDK v0.1.0

applications/operator_sdk/osdk-getting-started.adoc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
:relfileprefix: ../
2-
[id='getting-started-osdk']
2+
[id='osdk-getting-started']
33
= Getting started with the Operator SDK
44
include::modules/common-attributes.adoc[]
55
:context: osdk-getting-started
@@ -14,7 +14,9 @@ accomplished using two centerpieces of the Operator Framework: the Operator SDK
1414
(the `operator-sdk` CLI tool and `controller-runtime` library API) and the
1515
Operator Lifecycle Manager (OLM).
1616

17-
include::modules/osdk-operator-sdk.adoc[leveloffset=+1]
17+
include::modules/osdk-architecture.adoc[leveloffset=+1]
18+
include::modules/osdk-monitoring-prometheus-operator-support.adoc[leveloffset=+2]
19+
1820
include::modules/osdk-installing-cli.adoc[leveloffset=+1]
1921
include::modules/building-memcached-operator-using-osdk.adoc[leveloffset=+1]
2022
include::modules/managing-memcached-operator-using-olm.adoc[leveloffset=+1]

applications/operator_sdk/osdk-migrating-to-v0-1-0.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ The recommended method for migrating your project is to:
1818
. Modify the new project as described for v0.1.0.
1919

2020
This guide uses the `memcached-operator`, the example project
21-
from xref:osdk-getting-started.adoc#getting-started-osdk[Getting started with the Operator SDK],
21+
from xref:osdk-getting-started.adoc#osdk-getting-started[Getting started with the Operator SDK],
2222
to illustrate the migration steps. See the
2323
link:https://github.com/operator-framework/operator-sdk-samples/tree/aa15bd278eec0959595e0a0a7282a26055d7f9d6/memcached-operator[v0.0.7 memcached-operator]
2424
and
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
:relfileprefix: ../
2+
[id='osdk-monitoring-prometheus']
3+
= Configuring built-in monitoring with Prometheus
4+
include::modules/common-attributes.adoc[]
5+
:context: osdk-monitoring-prometheus
6+
7+
toc::[]
8+
9+
include::modules/osdk-monitoring-prometheus-operator-support.adoc[leveloffset=+1]
10+
11+
include::modules/osdk-monitoring-prometheus-metrics-helper.adoc[leveloffset=+1]
12+
include::modules/osdk-monitoring-prometheus-metrics-helper-modifying-port.adoc[leveloffset=+2]
13+
14+
include::modules/osdk-monitoring-prometheus-servicemonitor.adoc[leveloffset=+1]
15+
include::modules/osdk-monitoring-prometheus-servicemonitor-creating.adoc[leveloffset=+2]

modules/osdk-operator-sdk.adoc renamed to modules/osdk-architecture.adoc

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// * applications/operator_sdk/osdk-getting-started.adoc
44

5-
[id='osdk-operator-sdk-{context}']
5+
[id='osdk-architecture-{context}']
66
= Architecture of the Operator SDK
77

88
The link:https://coreos.com/operators/[Operator Framework] is an open source
@@ -24,8 +24,8 @@ providing:
2424
- Tools for scaffolding and code generation to quickly bootstrap a new project
2525
- Extensions to cover common Operator use cases
2626

27-
[discrete]
28-
=== Operator SDK workflow
27+
[id='osdk-architecture-workflow-{context}']
28+
== Workflow
2929

3030
The SDK provides the following workflow to develop a new Operator:
3131

@@ -42,12 +42,12 @@ At a high level, an Operator using the SDK processes events for watched
4242
resources in a user-defined handler and takes actions to reconcile the state of
4343
the application.
4444

45-
[discrete]
46-
=== Manager
45+
[id='osdk-architecture-manager-{context}']
46+
== Manager file
4747

48-
The main program for the Operator is the manager `cmd/manager/main.go` file. The
49-
manager automatically registers the scheme for all Custom Resources (CRs) defined
50-
under `pkg/apis/` and runs all controllers under `pkg/controller/`.
48+
The main program for the Operator is the manager file at `cmd/manager/main.go`.
49+
The manager automatically registers the scheme for all Custom Resources (CRs)
50+
defined under `pkg/apis/` and runs all controllers under `pkg/controller/`.
5151

5252
The manager can restrict the namespace that all controllers watch for resources:
5353

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Module included in the following assemblies:
2+
//
3+
// * applications/operator_sdk/osdk-monitoring-prometheus.adoc
4+
5+
[id='osdk-monitoring-prometheus-metrics-helper-modifying-port-{context}']
6+
= Modifying metrics port
7+
8+
Operator authors can modify the port that metrics are exposed on.
9+
10+
.Prerequisites
11+
12+
- Go-based Operator generated using the Operator SDK
13+
- Kubernetes-based cluster with the Prometheus Operator deployed
14+
15+
.Procedure
16+
17+
. In the generated Operator's `cmd/manager/main.go` file, change the `var
18+
metricsPort int32 = 8383` variable.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Module included in the following assemblies:
2+
//
3+
// * applications/operator_sdk/osdk-monitoring-prometheus.adoc
4+
5+
[id='osdk-monitoring-prometheus-metrics-helper-{context}']
6+
= Metrics helper
7+
8+
In Go-based Operators generated using the Operator SDK, the following helper
9+
function exposes general metrics about the running program:
10+
11+
[source,go]
12+
----
13+
func ExposeMetricsPort(ctx context.Context, port int32) (*v1.Service, error)
14+
----
15+
16+
These metrics are inherited from the `controller-runtime` library API. The
17+
metrics are by default served on `:8383/metrics`.
18+
19+
A Service object is created with the metrics port exposed, which can be then
20+
accessed by Prometheus. The Service object is garbage collected when the leader
21+
Pod's root owner is deleted.
22+
23+
The following example is present in the `cmd/manager/main.go` file in all
24+
Operators generated using the Operator SDK:
25+
26+
[source,go]
27+
----
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+
37+
// 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+
})
42+
43+
...
44+
45+
// Create Service object to expose the metrics port.
46+
_, err = metrics.ExposeMetricsPort(ctx, metricsPort)
47+
if err != nil {
48+
// handle error
49+
log.Info(err.Error())
50+
}
51+
----
52+
<1> Host the metrics are exposed on.
53+
<2> Port the metrics are exposed on.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Module included in the following assemblies:
2+
//
3+
// * applications/operator_sdk/osdk-getting-started.adoc
4+
// * applications/operator_sdk/osdk-monitoring-prometheus.adoc
5+
6+
[id='osdk-monitoring-prometheus-operator-support-{context}']
7+
= Prometheus Operator support in the Operator SDK
8+
9+
link:https://prometheus.io/[Prometheus] is an open-source systems monitoring and
10+
alerting toolkit. The Prometheus Operator creates, configures, and manages
11+
Prometheus clusters running on Kubernetes-based clusters, such as
12+
{product-title}.
13+
14+
Helper functions exist in the Operator SDK by default to automatically set up
15+
metrics in any generated Go-based Operator for use on clusters where the
16+
Prometheus Operator is deployed.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Module included in the following assemblies:
2+
//
3+
// * applications/operator_sdk/osdk-monitoring-prometheus.adoc
4+
5+
[id='osdk-monitoring-prometheus-servicemonitor-creating-{context}']
6+
= Creating ServiceMonitor resources
7+
8+
Operator authors can add Service target discovery of created monitoring Services
9+
using the `metrics.CreateServiceMonitor()` helper function, which accepts the
10+
newly created Service.
11+
12+
.Prerequisites
13+
14+
- Go-based Operator generated using the Operator SDK
15+
- Kubernetes-based cluster with the Prometheus Operator deployed
16+
17+
.Procedure
18+
19+
. Add the `metrics.CreateServiceMonitor()` helper function to your Operator code,
20+
creating only one `ServiceMonitor` per application and namespace:
21+
+
22+
[source,go]
23+
----
24+
import "github.com/operator-framework/operator-sdk/pkg/metrics"
25+
26+
serviceMonitors, err := metrics.CreateServiceMonitors(config *rest.Config, ns string, services []*v1.Service) <1>
27+
if err != nil {
28+
<2>
29+
}
30+
----
31+
<1> Pass the Service(s) to the helper function, which in turn returns the ServiceMonitor object.
32+
<2> Handle errors here.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Module included in the following assemblies:
2+
//
3+
// * applications/operator_sdk/osdk-monitoring-prometheus.adoc
4+
5+
[id='osdk-monitoring-prometheus-servicemonitor-{context}']
6+
= ServiceMonitor resources
7+
8+
A ServiceMonitor is a Custom Resource Definition (CRD) provided by the
9+
Prometheus Operator that discovers the `Endpoints` in Service objects and
10+
configures Prometheus to monitor those Pods.
11+
12+
In Go-based Operators generated using the Operator SDK, the
13+
`GenerateServiceMonitor()` helper function can take a Service object and
14+
generate a ServiceMonitor Custom Resource (CR) based on it.
15+
16+
.Additional resources
17+
18+
- See the
19+
link:https://github.com/coreos/prometheus-operator/blob/7a25bf6b6bb2347dacb235659b73bc210117acc7/Documentation/design.md#servicemonitor[Prometheus Operator documentation]
20+
for more about the ServiceMonitor CRD.

0 commit comments

Comments
 (0)