Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add status check for Harbor's read-only status #6917

Merged
merged 2 commits into from
Jun 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions harbor/datadog_checks/harbor/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ def _fetch_and_set_harbor_version(self):
self.harbor_version = [int(s) for s in version_str]
self.with_chartrepo = systeminfo.get('with_chartmuseum', False)

def read_only_status(self):
systeminfo = self._make_get_request(SYSTEM_INFO_URL)
return systeminfo.get('read_only', None)

def _make_paginated_get_request(self, url):
http_params = {'page_size': 100}
resp = self.http.get(self._resolve_url(url), params=http_params)
Expand Down
6 changes: 6 additions & 0 deletions harbor/datadog_checks/harbor/harbor.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ def _submit_disk_metrics(self, api, base_tags):
self.gauge('harbor.disk.free', disk_free, tags=base_tags)
self.gauge('harbor.disk.total', disk_total, tags=base_tags)

def _submit_read_only_status(self, api, base_tags):
read_only_status = api.read_only_status()
if read_only_status is not None:
self.gauge('harbor.registry.read_only', int(read_only_status), tags=base_tags)

def check(self, instance):
harbor_url = instance["url"]
tags = instance.get("tags", [])
Expand All @@ -106,6 +111,7 @@ def check(self, instance):
self._check_registries_health(api, tags)
self._submit_project_metrics(api, tags)
self._submit_disk_metrics(api, tags)
self._submit_read_only_status(api, tags)
except Exception:
self.log.exception("An error occured when collecting Harbor metrics")
self.service_check(CAN_CONNECT, AgentCheck.CRITICAL)
Expand Down
1 change: 1 addition & 0 deletions harbor/metadata.csv
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ metric_name,metric_type,interval,unit_name,per_unit_name,description,orientation
harbor.projects.count,gauge,,,,The total number of projects.,0,harbor,nb projects
harbor.disk.free,gauge,,byte,,The amount of storage space that is free.,0,harbor,disk free
harbor.disk.total,gauge,,byte,,The total amount of storage space.,0,harbor,disk total
harbor.registry.read_only,gauge,,,,The 'read_only' status of a registry.,0,harbor,registry read_only
7 changes: 6 additions & 1 deletion harbor/tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,18 @@

HARBOR_COMPONENTS = ['chartmuseum', 'registry', 'redis', 'jobservice', 'registryctl', 'portal', 'core', 'database']

HARBOR_VERSION = [int(i) for i in os.environ['HARBOR_VERSION'].split('.')]

HARBOR_METRICS = [
# Metric_name, requires admin privileges
('harbor.projects.count', False),
('harbor.disk.free', True),
('harbor.disk.total', True),
]
if HARBOR_VERSION >= VERSION_1_5:
HARBOR_METRICS.append(('harbor.registry.read_only', False))

HERE = os.path.dirname(os.path.abspath(__file__))
HARBOR_VERSION = [int(i) for i in os.environ['HARBOR_VERSION'].split('.')]
URL = 'http://{}'.format(get_docker_hostname())
INSTANCE = {'url': URL, 'username': 'NotAnAdmin', 'password': 'Str0ngPassw0rd'}
ADMIN_INSTANCE = {'url': URL, 'username': 'admin', 'password': 'Harbor12345'}
Expand Down Expand Up @@ -95,5 +98,7 @@
VOLUME_INFO_FIXTURE = {"storage": {"total": 1e6, "free": 5e5}}

SYSTEM_INFO_FIXTURE = {"harbor_version": "v{}-25bb24ca".format(os.environ['HARBOR_VERSION'])}
if HARBOR_VERSION >= VERSION_1_5:
SYSTEM_INFO_FIXTURE['read_only'] = False
if HARBOR_VERSION >= VERSION_1_6:
SYSTEM_INFO_FIXTURE['with_chartmuseum'] = True
10 changes: 9 additions & 1 deletion harbor/tests/test_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from datadog_checks.base import AgentCheck

from .common import HARBOR_COMPONENTS, VERSION_1_5, VERSION_1_6, VERSION_1_8
from .common import HARBOR_COMPONENTS, HARBOR_VERSION, VERSION_1_5, VERSION_1_6, VERSION_1_8
from .conftest import MockResponse


Expand Down Expand Up @@ -52,6 +52,14 @@ def test_submit_disk_metrics(aggregator, harbor_check, harbor_api):
aggregator.assert_metric('harbor.disk.total', 1e6, tags=tags)


@pytest.mark.usefixture("patch_requests")
@pytest.mark.skipif(HARBOR_VERSION < VERSION_1_5, reason="The registry.read_only metric is submitted for Harbor 1.5+")
def test_submit_read_only_status(aggregator, harbor_check, harbor_api):
tags = ['tag1:val1', 'tag2']
harbor_check._submit_read_only_status(harbor_api, tags)
aggregator.assert_metric('harbor.registry.read_only', False, tags=tags)


def test_api__make_get_request(harbor_api):
harbor_api.http = MagicMock()
harbor_api.http.get = MagicMock(return_value=MockResponse({"json": True}, 200))
Expand Down