Skip to content

Commit a4fe1bc

Browse files
authored
Merge pull request #41171 from adellape/osdk_ansible_insights
2 parents 0ac6d87 + 994ef9e commit a4fe1bc

File tree

5 files changed

+250
-9
lines changed

5 files changed

+250
-9
lines changed

modules/osdk-ansible-inside-operator-local.adoc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ You can customize the roles path by setting the environment variable `ANSIBLE_RO
1515

1616
.Prerequisites
1717

18-
- link:https://ansible-runner.readthedocs.io/en/latest/install.html[Ansible Runner] version v1.1.0+
19-
- link:https://github.com/ansible/ansible-runner-http[Ansible Runner HTTP Event Emitter plug-in] version v1.0.0+
18+
- link:https://ansible-runner.readthedocs.io/en/latest/install.html[Ansible Runner] v2.0.2+
19+
- link:https://github.com/ansible/ansible-runner-http[Ansible Runner HTTP Event Emitter plug-in] v1.0.0+
2020
- Performed the previous steps for testing the Kubernetes Collection locally
2121
2222
.Procedure

modules/osdk-ansible-metrics.adoc

Lines changed: 240 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,240 @@
1+
// Module included in the following assemblies:
2+
//
3+
// * operators/operator_sdk/osdk-monitoring-prometheus.adoc
4+
5+
:_content-type: PROCEDURE
6+
[id="osdk-ansible-metrics_{context}"]
7+
= Exposing custom metrics for Ansible-based Operators
8+
9+
As an Operator author creating Ansible-based Operators, you can use the Operator SDK's `osdk_metrics` module to expose custom Operator and Operand metrics, emit events, and support logging.
10+
11+
.Prerequisites
12+
13+
* Ansible-based Operator generated using the Operator SDK
14+
* Prometheus Operator, which is deployed by default on {product-title} clusters
15+
16+
.Procedure
17+
18+
. Generate an Ansible-based Operator. This example uses a `testmetrics.com` domain:
19+
+
20+
[source,terminal]
21+
----
22+
$ operator-sdk init \
23+
--plugins=ansible \
24+
--domain=testmetrics.com
25+
----
26+
27+
. Create a `metrics` API. This example uses a `kind` named `Testmetrics`:
28+
+
29+
[source,terminal]
30+
----
31+
$ operator-sdk create api \
32+
--group metrics \
33+
--version v1 \
34+
--kind Testmetrics \
35+
--generate-role
36+
----
37+
38+
. Edit the `roles/testmetrics/tasks/main.yml` file and use the `osdk_metrics` module to create custom metrics for your Operator project:
39+
+
40+
.Example `roles/testmetrics/tasks/main.yml` file
41+
[%collapsible]
42+
====
43+
[source,yaml]
44+
----
45+
---
46+
# tasks file for Memcached
47+
- name: start k8sstatus
48+
k8s:
49+
definition:
50+
kind: Deployment
51+
apiVersion: apps/v1
52+
metadata:
53+
name: '{{ ansible_operator_meta.name }}-memcached'
54+
namespace: '{{ ansible_operator_meta.namespace }}'
55+
spec:
56+
replicas: "{{size}}"
57+
selector:
58+
matchLabels:
59+
app: memcached
60+
template:
61+
metadata:
62+
labels:
63+
app: memcached
64+
spec:
65+
containers:
66+
- name: memcached
67+
command:
68+
- memcached
69+
- -m=64
70+
- -o
71+
- modern
72+
- -v
73+
image: "docker.io/memcached:1.4.36-alpine"
74+
ports:
75+
- containerPort: 11211
76+
77+
- osdk_metric:
78+
name: my_thing_counter
79+
description: This metric counts things
80+
counter: {}
81+
82+
- osdk_metric:
83+
name: my_counter_metric
84+
description: Add 3.14 to the counter
85+
counter:
86+
increment: yes
87+
88+
- osdk_metric:
89+
name: my_gauge_metric
90+
description: Create my gauge and set it to 2.
91+
gauge:
92+
set: 2
93+
94+
- osdk_metric:
95+
name: my_histogram_metric
96+
description: Observe my histogram
97+
histogram:
98+
observe: 2
99+
100+
- osdk_metric:
101+
name: my_summary_metric
102+
description: Observe my summary
103+
summary:
104+
observe: 2
105+
----
106+
====
107+
108+
.Verification
109+
110+
. Run your Operator on a cluster. For example, to use the "run as a deployment" method:
111+
112+
113+
.. Build the Operator image and push it to a registry:
114+
+
115+
[source,terminal]
116+
----
117+
$ make docker-build docker-push IMG=<registry>/<user>/<image_name>:<tag>
118+
----
119+
120+
.. Install the Operator on a cluster:
121+
+
122+
[source,terminal]
123+
----
124+
$ make install
125+
----
126+
127+
.. Deploy the Operator:
128+
+
129+
[source,terminal]
130+
----
131+
$ make deploy IMG=<registry>/<user>/<image_name>:<tag>
132+
----
133+
134+
. Create a `Testmetrics` custom resource (CR):
135+
136+
.. Define the CR spec:
137+
+
138+
.Example `config/samples/metrics_v1_testmetrics.yaml` file
139+
[%collapsible]
140+
====
141+
[source,yaml]
142+
----
143+
apiVersion: metrics.testmetrics.com/v1
144+
kind: Testmetrics
145+
metadata:
146+
name: testmetrics-sample
147+
spec:
148+
size: 1
149+
----
150+
====
151+
152+
.. Create the object:
153+
+
154+
[source,terminal]
155+
----
156+
$ oc create -f config/samples/metrics_v1_testmetrics.yaml
157+
----
158+
159+
. Get the pod details:
160+
+
161+
[source,terminal]
162+
----
163+
$ oc get pods
164+
----
165+
+
166+
.Example output
167+
[source,terminal]
168+
----
169+
NAME READY STATUS RESTARTS AGE
170+
ansiblemetrics-controller-manager-<id> 2/2 Running 0 149m
171+
testmetrics-sample-memcached-<id> 1/1 Running 0 147m
172+
----
173+
174+
. Get the endpoint details:
175+
+
176+
[source,terminal]
177+
----
178+
$ oc get ep
179+
----
180+
+
181+
.Example output
182+
[source,terminal]
183+
----
184+
NAME ENDPOINTS AGE
185+
ansiblemetrics-controller-manager-metrics-service 10.129.2.70:8443 150m
186+
----
187+
188+
. Get the custom metrics token:
189+
+
190+
[source,terminal]
191+
----
192+
$ token=`oc sa get-token prometheus-k8s -n openshift-monitoring`
193+
----
194+
195+
. Check the metrics values:
196+
197+
.. Check the `my_counter_metric` value:
198+
+
199+
[source,terminal]
200+
----
201+
$ oc exec ansiblemetrics-controller-manager-<id> -- curl -k -H "Authoriza
202+
tion: Bearer $token" 'https://10.129.2.70:8443/metrics' | grep my_counter
203+
----
204+
+
205+
.Example output
206+
[source,terminal]
207+
----
208+
HELP my_counter_metric Add 3.14 to the counter
209+
TYPE my_counter_metric counter
210+
my_counter_metric 2
211+
----
212+
213+
.. Check the `my_gauge_metric` value:
214+
+
215+
[source,terminal]
216+
----
217+
$ oc exec ansiblemetrics-controller-manager-<id> -- curl -k -H "Authoriza
218+
tion: Bearer $token" 'https://10.129.2.70:8443/metrics' | grep gauge
219+
----
220+
+
221+
.Example output
222+
[source,terminal]
223+
----
224+
HELP my_gauge_metric Create my gauge and set it to 2.
225+
----
226+
227+
.. Check the `my_histogram_metric` and `my_summary_metric` values:
228+
+
229+
[source,terminal]
230+
----
231+
$ oc exec ansiblemetrics-controller-manager-<id> -- curl -k -H "Authoriza
232+
tion: Bearer $token" 'https://10.129.2.70:8443/metrics' | grep Observe
233+
----
234+
+
235+
.Example output
236+
[source,terminal]
237+
----
238+
HELP my_histogram_metric Observe my histogram
239+
HELP my_summary_metric Observe my summary
240+
----

modules/osdk-common-prereqs.adoc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ ifdef::golang[]
2626
- link:https://www.mercurial-scm.org/downloads[Mercurial] v3.9+
2727
endif::[]
2828
ifdef::ansible[]
29-
- link:https://docs.ansible.com/ansible/latest/index.html[Ansible] version v2.9.0+
30-
- link:https://ansible-runner.readthedocs.io/en/latest/install.html[Ansible Runner] version v2.0.2+
31-
- link:https://github.com/ansible/ansible-runner-http[Ansible Runner HTTP Event Emitter plug-in] version v1.0.0+
32-
- link:https://pypi.org/project/openshift/[OpenShift Python client] version v0.12.0+
29+
- link:https://docs.ansible.com/ansible/latest/index.html[Ansible] v2.9.0+
30+
- link:https://ansible-runner.readthedocs.io/en/latest/install.html[Ansible Runner] v2.0.2+
31+
- link:https://github.com/ansible/ansible-runner-http[Ansible Runner HTTP Event Emitter plug-in] v1.0.0+
32+
- link:https://pypi.org/project/openshift/[OpenShift Python client] v0.11.2+
3333
endif::[]
3434
- Logged into an {product-title} {product-version} cluster with `oc` with an account that has `cluster-admin` permissions
3535
- To allow the cluster to pull the image, the repository where you push your image must be set as public, or you must configure an image pull secret

modules/osdk-monitoring-custom-metrics.adoc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44

55
:_content-type: PROCEDURE
66
[id="osdk-monitoring-custom-metrics_{context}"]
7-
= Exposing custom metrics
7+
= Exposing custom metrics for Go-based Operators
88

99
As an Operator author, you can publish custom metrics by using the global Prometheus registry from the `controller-runtime/pkg/metrics` library.
1010

1111
.Prerequisites
1212

1313
* Go-based Operator generated using the Operator SDK
14-
* Prometheus Operator (deployed by default on {product-title} clusters)
14+
* Prometheus Operator, which is deployed by default on {product-title} clusters
1515
1616
.Procedure
1717

operators/operator_sdk/osdk-monitoring-prometheus.adoc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ include::_attributes/common-attributes.adoc[]
66

77
toc::[]
88

9-
This guide describes the built-in monitoring support provided by the Operator SDK using the Prometheus Operator and details usage for Operator authors.
9+
This guide describes the built-in monitoring support provided by the Operator SDK using the Prometheus Operator and details usage for authors of Go-based and Ansible-based Operators.
1010

1111
include::modules/osdk-monitoring-prometheus-operator-support.adoc[leveloffset=+1]
1212

1313
include::modules/osdk-monitoring-custom-metrics.adoc[leveloffset=+1]
14+
include::modules/osdk-ansible-metrics.adoc[leveloffset=+1]

0 commit comments

Comments
 (0)