Skip to content

Commit abac23c

Browse files
djzagerIsh Shah
authored and
Ish Shah
committed
ansible: add custom resource metrics (#1723)
* Add e2e test for crd metrics * ansible: add custom resource metrics This PR enables custom resource metrics for ansible based operators. * Add metrics test to e2e ansible test * Add check for operator metrics * Verify metrics after CR creation In the Ansible Operator tests, we should verify that the metrics reflect that an instance of the memcached resource has been created. Also add the change to the Changelog.
1 parent 640171c commit abac23c

File tree

4 files changed

+83
-3
lines changed

4 files changed

+83
-3
lines changed

CHANGELOG.md

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

55
- The `operator-sdk olm-catalog gen-csv` command now produces indented JSON for the `alm-examples` annotation. ([#1793](https://github.com/operator-framework/operator-sdk/pull/1793))
66
- Added flag `--dep-manager` to command [`operator-sdk print-deps`](https://github.com/operator-framework/operator-sdk/blob/master/doc/sdk-cli-reference.md#print-deps) to specify the type of dependency manager file to print. The choice of dependency manager is inferred from top-level dependency manager files present if `--dep-manager` is not set. ([#1819](https://github.com/operator-framework/operator-sdk/pull/1819))
7+
- Ansible based operators now gather and serve metrics about each custom resource on port 8686 of the metrics service. ([#1723](https://github.com/operator-framework/operator-sdk/pull/1723))
8+
- Generating and serving info metrics about each custom resource. By default these metrics are exposed on port 8686. ([#1277](https://github.com/operator-framework/operator-sdk/pull/1277))
79

810
### Changed
911

ci/tests/e2e-ansible.sh

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,30 @@ test_operator() {
6060
exit 1
6161
fi
6262

63+
# verify that metrics service was created
64+
if ! timeout 20s bash -c -- "until kubectl get service/memcached-operator-metrics > /dev/null 2>&1; do sleep 1; done";
65+
then
66+
echo "Failed to get metrics service"
67+
kubectl logs deployment/memcached-operator
68+
exit 1
69+
fi
70+
71+
# verify that the metrics endpoint exists
72+
if ! timeout 1m bash -c -- "until kubectl run -it --rm --restart=Never test-metrics --image=registry.access.redhat.com/ubi7/ubi-minimal:latest -- curl -sfo /dev/null http://memcached-operator-metrics:8383/metrics; do sleep 1; done";
73+
then
74+
echo "Failed to verify that metrics endpoint exists"
75+
kubectl logs deployment/memcached-operator
76+
exit 1
77+
fi
78+
79+
# verify that the operator metrics endpoint exists
80+
if ! timeout 1m bash -c -- "until kubectl run -it --rm --restart=Never test-metrics --image=registry.access.redhat.com/ubi7/ubi-minimal:latest -- curl -sfo /dev/null http://memcached-operator-metrics:8686/metrics; do sleep 1; done";
81+
then
82+
echo "Failed to verify that metrics endpoint exists"
83+
kubectl logs deployment/memcached-operator
84+
exit 1
85+
fi
86+
6387
# create CR
6488
kubectl create -f deploy/crds/ansible_v1alpha1_memcached_cr.yaml
6589
if ! timeout 20s bash -c -- 'until kubectl get deployment -l app=memcached | grep memcached; do sleep 1; done';
@@ -70,6 +94,15 @@ test_operator() {
7094
kubectl logs deployment/memcached-operator -c ansible
7195
exit 1
7296
fi
97+
98+
# verify that metrics reflect cr creation
99+
if ! bash -c -- 'kubectl run -it --rm --restart=Never test-metrics --image=registry.access.redhat.com/ubi7/ubi-minimal:latest -- curl http://memcached-operator-metrics:8686/metrics | grep example-memcached';
100+
then
101+
echo "Failed to verify custom resource metrics"
102+
kubectl logs deployment/memcached-operator
103+
exit 1
104+
fi
105+
73106
memcached_deployment=$(kubectl get deployment -l app=memcached -o jsonpath="{..metadata.name}")
74107
if ! timeout 1m kubectl rollout status deployment/${memcached_deployment};
75108
then

hack/tests/e2e-ansible.sh

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,30 @@ test_operator() {
4141
exit 1
4242
fi
4343

44+
# verify that metrics service was created
45+
if ! timeout 20s bash -c -- "until kubectl get service/memcached-operator-metrics > /dev/null 2>&1; do sleep 1; done";
46+
then
47+
echo "Failed to get metrics service"
48+
kubectl logs deployment/memcached-operator
49+
exit 1
50+
fi
51+
52+
# verify that the metrics endpoint exists
53+
if ! timeout 1m bash -c -- "until kubectl run -it --rm --restart=Never test-metrics --image=registry.access.redhat.com/ubi7/ubi-minimal:latest -- curl -sfo /dev/null http://memcached-operator-metrics:8383/metrics; do sleep 1; done";
54+
then
55+
echo "Failed to verify that metrics endpoint exists"
56+
kubectl logs deployment/memcached-operator
57+
exit 1
58+
fi
59+
60+
# verify that the operator metrics endpoint exists
61+
if ! timeout 1m bash -c -- "until kubectl run -it --rm --restart=Never test-metrics --image=registry.access.redhat.com/ubi7/ubi-minimal:latest -- curl -sfo /dev/null http://memcached-operator-metrics:8686/metrics; do sleep 1; done";
62+
then
63+
echo "Failed to verify that metrics endpoint exists"
64+
kubectl logs deployment/memcached-operator
65+
exit 1
66+
fi
67+
4468
# create CR
4569
kubectl create -f deploy/crds/ansible_v1alpha1_memcached_cr.yaml
4670
if ! timeout 20s bash -c -- 'until kubectl get deployment -l app=memcached | grep memcached; do sleep 1; done';
@@ -50,6 +74,14 @@ test_operator() {
5074
kubectl logs deployment/memcached-operator -c ansible
5175
exit 1
5276
fi
77+
78+
# verify that metrics reflect cr creation
79+
if ! bash -c -- 'kubectl run -it --rm --restart=Never test-metrics --image=registry.access.redhat.com/ubi7/ubi-minimal:latest -- curl http://memcached-operator-metrics:8686/metrics | grep example-memcached';
80+
then
81+
echo "Failed to verify custom resource metrics"
82+
kubectl logs deployment/memcached-operator
83+
exit 1
84+
fi
5385
memcached_deployment=$(kubectl get deployment -l app=memcached -o jsonpath="{..metadata.name}")
5486
if ! timeout 1m kubectl rollout status deployment/${memcached_deployment};
5587
then

pkg/ansible/run.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"github.com/operator-framework/operator-sdk/pkg/ansible/runner"
3030
"github.com/operator-framework/operator-sdk/pkg/ansible/watches"
3131
"github.com/operator-framework/operator-sdk/pkg/k8sutil"
32+
kubemetrics "github.com/operator-framework/operator-sdk/pkg/kube-metrics"
3233
"github.com/operator-framework/operator-sdk/pkg/leader"
3334
"github.com/operator-framework/operator-sdk/pkg/metrics"
3435
"github.com/operator-framework/operator-sdk/pkg/restmapper"
@@ -45,8 +46,10 @@ import (
4546
)
4647

4748
var (
48-
log = logf.Log.WithName("cmd")
49-
metricsPort int32 = 8383
49+
metricsHost = "0.0.0.0"
50+
log = logf.Log.WithName("cmd")
51+
metricsPort int32 = 8383
52+
operatorMetricsPort int32 = 8686
5053
)
5154

5255
func printVersion() {
@@ -79,13 +82,14 @@ func Run(flags *aoflags.AnsibleOperatorFlags) error {
7982
mgr, err := manager.New(cfg, manager.Options{
8083
Namespace: namespace,
8184
MapperProvider: restmapper.NewDynamicRESTMapper,
82-
MetricsBindAddress: fmt.Sprintf("0.0.0.0:%d", metricsPort),
85+
MetricsBindAddress: fmt.Sprintf("%s:%d", metricsHost, metricsPort),
8386
})
8487
if err != nil {
8588
log.Error(err, "Failed to create a new manager.")
8689
return err
8790
}
8891

92+
var gvks []schema.GroupVersionKind
8993
cMap := controllermap.NewControllerMap()
9094
watches, err := watches.Load(flags.WatchesFile)
9195
if err != nil {
@@ -116,6 +120,7 @@ func Run(flags *aoflags.AnsibleOperatorFlags) error {
116120
OwnerWatchMap: controllermap.NewWatchMap(),
117121
AnnotationWatchMap: controllermap.NewWatchMap(),
118122
})
123+
gvks = append(gvks, w.GroupVersionKind)
119124
}
120125

121126
operatorName, err := k8sutil.GetOperatorName()
@@ -131,8 +136,16 @@ func Run(flags *aoflags.AnsibleOperatorFlags) error {
131136
return err
132137
}
133138

139+
// Generates operator specific metrics based on the GVKs.
140+
// It serves those metrics on "http://metricsHost:operatorMetricsPort".
141+
err = kubemetrics.GenerateAndServeCRMetrics(cfg, []string{namespace}, gvks, metricsHost, operatorMetricsPort)
142+
if err != nil {
143+
log.Info("Could not generate and serve custom resource metrics", "error", err.Error())
144+
}
145+
134146
// Add to the below struct any other metrics ports you want to expose.
135147
servicePorts := []v1.ServicePort{
148+
{Port: operatorMetricsPort, Name: metrics.CRPortName, Protocol: v1.ProtocolTCP, TargetPort: intstr.IntOrString{Type: intstr.Int, IntVal: operatorMetricsPort}},
136149
{Port: metricsPort, Name: metrics.OperatorPortName, Protocol: v1.ProtocolTCP, TargetPort: intstr.IntOrString{Type: intstr.Int, IntVal: metricsPort}},
137150
}
138151
// Create Service object to expose the metrics port(s).

0 commit comments

Comments
 (0)