Skip to content

Commit

Permalink
feat(annotations): add annotation management for host and path in Ing…
Browse files Browse the repository at this point in the history
…ress Object
  • Loading branch information
SQuent committed Nov 6, 2024
1 parent 9a61a16 commit b43974c
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 4 deletions.
18 changes: 16 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,25 @@ These annotations apply to both Kubernetes Ingress and Traefik Ingressroutes res
- **Default:** `null` (no headers)
- **Example:** `uptime-kuma.autodiscovery.probe.headers: {"Authorization": "Bearer token"}`

6. **`uptime-kuma.autodiscovery.probe.port`**
6. **`uptime-kuma.autodiscovery.probe.host`**
- force the host for the probe. **WARNING: Be carefull with this paramter if you are using multiple hosts in a same Ingress object**
- **Type:** String
- **Default:** `null` (host grep from ingress)
- **Example:** `uptime-kuma.autodiscovery.probe.host: example.com`

7. **`uptime-kuma.autodiscovery.probe.path`**
- url sub path for the probe.
- **Type:** String
- **Default:** `null`
- **Example:** `uptime-kuma.autodiscovery.probe.path: /tintin`

8. **`uptime-kuma.autodiscovery.probe.port`**
- Port to use for the probe.
- **Type:** Integer
- **Default:** `null` (default port for the protocol)
- **Example:** `uptime-kuma.autodiscovery.probe.port: 8080`

7. **`uptime-kuma.autodiscovery.probe.method`**
9. **`uptime-kuma.autodiscovery.probe.method`**
- HTTP method to use for the probe (GET, POST, etc.).
- **Type:** String
- **Default:** `GET`
Expand All @@ -93,6 +105,8 @@ metadata:
uptime-kuma.autodiscovery.probe.enabled: true
uptime-kuma.autodiscovery.probe.type: http
uptime-kuma.autodiscovery.probe.headers: '{"Authorization": "Bearer token"}'
uptime-kuma.autodiscovery.probe.host: 'example.com'
uptime-kuma.autodiscovery.probe.path: '/tintin'
uptime-kuma.autodiscovery.probe.port: 8080
uptime-kuma.autodiscovery.probe.method: GET
spec:
Expand Down
10 changes: 8 additions & 2 deletions kuma_ingress_watcher/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,24 +137,30 @@ def process_routing_object(item, type_obj):
probe_type = annotations.get('uptime-kuma.autodiscovery.probe.type', 'http')
headers = annotations.get('uptime-kuma.autodiscovery.probe.headers')
port = annotations.get('uptime-kuma.autodiscovery.probe.port')
path = annotations.get('uptime-kuma.autodiscovery.probe.path')
hard_host = annotations.get('uptime-kuma.autodiscovery.probe.host')
method = annotations.get('uptime-kuma.autodiscovery.probe.method', 'GET')

if not enabled:
logger.info(f"Monitoring for {name} is disabled via annotations.")
delete_monitor(monitor_name)
return

process_routes(monitor_name, routes_or_rules, interval, probe_type, headers, port, method, type_obj)
process_routes(monitor_name, routes_or_rules, interval, probe_type, headers, port, path, hard_host, method, type_obj)


def process_routes(monitor_name, routes_or_rules, interval, probe_type, headers, port, method, type_obj):
def process_routes(monitor_name, routes_or_rules, interval, probe_type, headers, port, path, hard_host, method, type_obj):
index = 1
for route_or_rule in routes_or_rules:
hosts = extract_hosts(route_or_rule, type_obj)

if hosts:
for host in hosts:
url = f"https://{host}"
if hard_host:
url = f"https://{hard_host}"
if path:
url = f"{url}{path}"
if port:
url = f"{url}:{port}"

Expand Down
114 changes: 114 additions & 0 deletions tests/test_process_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ def test_process_routes_single_route(self, mock_extract_hosts, mock_create_or_up
interval=60,
probe_type='http',
headers=None,
hard_host=None,
path=None,
port='8080',
method='GET',
type_obj='IngressRoute'
Expand All @@ -40,6 +42,8 @@ def test_process_routes_multiple_routes(self, mock_extract_hosts, mock_create_or
interval=60,
probe_type='http',
headers=None,
hard_host=None,
path=None,
port='8080',
method='GET',
type_obj='IngressRoute'
Expand All @@ -63,6 +67,8 @@ def test_process_routes_no_hosts(self, mock_extract_hosts, mock_create_or_update
interval=60,
probe_type='http',
headers=None,
hard_host=None,
path=None,
port='8080',
method='GET',
type_obj='IngressRoute'
Expand All @@ -81,6 +87,8 @@ def test_process_routes_with_empty_port(self, mock_extract_hosts, mock_create_or
interval=60,
probe_type='http',
headers=None,
hard_host=None,
path=None,
port=None,
method='GET',
type_obj='IngressRoute'
Expand All @@ -95,6 +103,112 @@ def test_process_routes_with_empty_port(self, mock_extract_hosts, mock_create_or
'GET'
)

@patch('kuma_ingress_watcher.controller.create_or_update_monitor')
@patch('kuma_ingress_watcher.controller.extract_hosts')
def test_process_routes_with_path(self, mock_extract_hosts, mock_create_or_update_monitor):
mock_extract_hosts.return_value = ['example.com']

process_routes(
monitor_name='test-monitor',
routes_or_rules=[{'match': 'Host(`example.com`)'}],
interval=60,
probe_type='http',
headers=None,
hard_host=None,
path='/milou',
port=None,
method='GET',
type_obj='IngressRoute'
)

mock_create_or_update_monitor.assert_called_once_with(
'test-monitor',
'https://example.com/milou',
60,
'http',
None,
'GET'
)

@patch('kuma_ingress_watcher.controller.create_or_update_monitor')
@patch('kuma_ingress_watcher.controller.extract_hosts')
def test_process_routes_with_hard_host_and_path(self, mock_extract_hosts, mock_create_or_update_monitor):
mock_extract_hosts.return_value = ['example.com']

process_routes(
monitor_name='test-monitor',
routes_or_rules=[{'match': 'Host(`example.com`)'}],
interval=60,
probe_type='http',
headers=None,
hard_host='tintin',
path='/milou',
port=None,
method='GET',
type_obj='IngressRoute'
)

mock_create_or_update_monitor.assert_called_once_with(
'test-monitor',
'https://tintin/milou',
60,
'http',
None,
'GET'
)

@patch('kuma_ingress_watcher.controller.create_or_update_monitor')
@patch('kuma_ingress_watcher.controller.extract_hosts')
def test_process_routes_with_hard_host_and_path_and_port(self, mock_extract_hosts, mock_create_or_update_monitor):
mock_extract_hosts.return_value = ['example.com']

process_routes(
monitor_name='test-monitor',
routes_or_rules=[{'match': 'Host(`example.com`)'}],
interval=60,
probe_type='http',
headers=None,
hard_host='tintin',
path='/milou',
port=8080,
method='GET',
type_obj='IngressRoute'
)

mock_create_or_update_monitor.assert_called_once_with(
'test-monitor',
'https://tintin/milou:8080',
60,
'http',
None,
'GET'
)

@patch('kuma_ingress_watcher.controller.create_or_update_monitor')
@patch('kuma_ingress_watcher.controller.extract_hosts')
def test_process_routes_multiple_routes_with_hard_host_and_path_and_port(self, mock_extract_hosts, mock_create_or_update_monitor):
mock_extract_hosts.side_effect = [['example.com'], ['example.org']]

process_routes(
monitor_name='test-monitor',
routes_or_rules=[{'match': 'Host(`example.com`)'}, {'match': 'Host(`example.org`)'}],
interval=60,
probe_type='http',
headers=None,
hard_host='tintin',
path='/milou',
port=8080,
method='GET',
type_obj='IngressRoute'
)

self.assertEqual(mock_create_or_update_monitor.call_count, 2)
calls = [
unittest.mock.call('test-monitor-1', 'https://tintin/milou:8080', 60, 'http', None, 'GET'),
unittest.mock.call('test-monitor-2', 'https://tintin/milou:8080', 60, 'http', None, 'GET')
]
mock_create_or_update_monitor.assert_has_calls(calls)


if __name__ == '__main__':
unittest.main()

0 comments on commit b43974c

Please sign in to comment.