diff --git a/CHANGES.rst b/CHANGES.rst index b66afabe..ea0305d0 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -12,6 +12,8 @@ Unreleased * Implemented a handler allowing changing the ``backendImage`` of ``grandCentral``. +* Added the Prometheus annotations to ``grandCentral`` to allow metrics scrapping on it. + 2.33.0 (2023-11-14) ------------------- diff --git a/crate/operator/constants.py b/crate/operator/constants.py index bb437997..c8142693 100644 --- a/crate/operator/constants.py +++ b/crate/operator/constants.py @@ -54,6 +54,7 @@ GRAND_CENTRAL_RESOURCE_PREFIX = "grand-central" GRAND_CENTRAL_BACKEND_API_PORT = 5050 +GRAND_CENTRAL_PROMETHEUS_PORT = 8000 class CloudProvider(str, enum.Enum): diff --git a/crate/operator/grand_central.py b/crate/operator/grand_central.py index 1cfa792c..27e9435d 100644 --- a/crate/operator/grand_central.py +++ b/crate/operator/grand_central.py @@ -66,6 +66,7 @@ from crate.operator.constants import ( GRAND_CENTRAL_BACKEND_API_PORT, + GRAND_CENTRAL_PROMETHEUS_PORT, GRAND_CENTRAL_RESOURCE_PREFIX, LABEL_NAME, SHARED_NODE_SELECTOR_KEY, @@ -116,10 +117,15 @@ def get_grand_central_deployment( ), ), ] + annotations = { + "prometheus.io/port": str(GRAND_CENTRAL_PROMETHEUS_PORT), + "prometheus.io/scrape": "true", + } return V1Deployment( metadata=V1ObjectMeta( name=f"{GRAND_CENTRAL_RESOURCE_PREFIX}-{name}", labels=labels, + annotations=annotations, owner_references=owner_references, ), spec=V1DeploymentSpec( @@ -145,7 +151,11 @@ def get_grand_central_deployment( V1ContainerPort( container_port=GRAND_CENTRAL_BACKEND_API_PORT, name="grand-central", - ) + ), + V1ContainerPort( + container_port=GRAND_CENTRAL_PROMETHEUS_PORT, + name="prometheus", + ), ], resources=V1ResourceRequirements( limits={ @@ -216,6 +226,11 @@ def get_grand_central_service( port=GRAND_CENTRAL_BACKEND_API_PORT, target_port=GRAND_CENTRAL_BACKEND_API_PORT, ), + V1ServicePort( + name="prometheus", + port=GRAND_CENTRAL_PROMETHEUS_PORT, + target_port=GRAND_CENTRAL_PROMETHEUS_PORT, + ), ], selector={LABEL_NAME: f"{GRAND_CENTRAL_RESOURCE_PREFIX}-{name}"}, ), diff --git a/tests/test_create.py b/tests/test_create.py index d69b54cb..a52772f1 100644 --- a/tests/test_create.py +++ b/tests/test_create.py @@ -37,6 +37,7 @@ from crate.operator.constants import ( API_GROUP, DATA_PVC_NAME_PREFIX, + GRAND_CENTRAL_PROMETHEUS_PORT, GRAND_CENTRAL_RESOURCE_PREFIX, LABEL_COMPONENT, LABEL_MANAGED_BY, @@ -1265,6 +1266,33 @@ async def test_create_with_grand_central_backend_enabled( == "my-crate-cluster.gc.aks1.eastus.azure.cratedb-dev.net" ) + # Test Prometheus + assert deploy.metadata.annotations["prometheus.io/scrape"] == "true" + assert deploy.metadata.annotations["prometheus.io/port"] == str( + GRAND_CENTRAL_PROMETHEUS_PORT + ) + app_prometheus_port = next( + ( + port.container_port + for port in deploy.spec.template.spec.containers[0].ports + if port.name == "prometheus" + ), + None, + ) + assert app_prometheus_port == GRAND_CENTRAL_PROMETHEUS_PORT + + service = await core.read_namespaced_service( + namespace=namespace.metadata.name, + name=f"{GRAND_CENTRAL_RESOURCE_PREFIX}-{name}", + ) + svc_prometheus_port = next( + (port for port in service.spec.ports if port.name == "prometheus"), + None, + ) + assert svc_prometheus_port + assert svc_prometheus_port.port == GRAND_CENTRAL_PROMETHEUS_PORT + assert svc_prometheus_port.target_port == GRAND_CENTRAL_PROMETHEUS_PORT + async def test_preserve_unknown_object_keys( self, faker, namespace, cratedb_crd, api_client ):