Skip to content

Commit

Permalink
Improve url join to not mutate the base url when proxying a call (#5416)
Browse files Browse the repository at this point in the history
  • Loading branch information
Charly Fontaine authored and ofek committed Jan 9, 2020
1 parent a45f8d3 commit 36337b8
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 4 deletions.
3 changes: 2 additions & 1 deletion kubelet/datadog_checks/kubelet/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from .__about__ import __version__
from .common import KubeletCredentials, PodListUtils, get_pod_by_uid, is_static_pending_pod
from .common import KubeletCredentials, PodListUtils, get_pod_by_uid, is_static_pending_pod, urljoin
from .kubelet import KubeletCheck

__all__ = [
'KubeletCheck',
'__version__',
'PodListUtils',
'KubeletCredentials',
'urljoin',
'get_pod_by_uid',
'is_static_pending_pod',
]
9 changes: 9 additions & 0 deletions kubelet/datadog_checks/kubelet/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ def get_pod_by_uid(uid, podlist):
return None


def urljoin(*args):
"""
Joins given arguments into an url. Trailing but not leading slashes are
stripped for each argument.
:return: string
"""
return '/'.join(arg.strip('/') for arg in args)


def is_static_pending_pod(pod):
"""
Return if the pod is a static pending pod
Expand Down
3 changes: 1 addition & 2 deletions kubelet/datadog_checks/kubelet/kubelet.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import requests
from kubeutil import get_connection_info
from six import iteritems
from six.moves.urllib.parse import urljoin

from datadog_checks.base.utils.date import UTC, parse_rfc3339
from datadog_checks.base.utils.tagging import tagger
Expand All @@ -22,7 +21,7 @@
from datadog_checks.errors import CheckException

from .cadvisor import CadvisorScraper
from .common import CADVISOR_DEFAULT_PORT, KubeletCredentials, PodListUtils, replace_container_rt_prefix
from .common import CADVISOR_DEFAULT_PORT, KubeletCredentials, PodListUtils, replace_container_rt_prefix, urljoin
from .prometheus import CadvisorPrometheusScraperMixin

try:
Expand Down
13 changes: 12 additions & 1 deletion kubelet/tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import pytest

from datadog_checks.checks.openmetrics import OpenMetricsBaseCheck
from datadog_checks.kubelet import KubeletCredentials, PodListUtils, get_pod_by_uid, is_static_pending_pod
from datadog_checks.kubelet import KubeletCredentials, PodListUtils, get_pod_by_uid, is_static_pending_pod, urljoin

from .test_kubelet import mock_from_file

Expand Down Expand Up @@ -100,6 +100,17 @@ def test_pod_by_uid():
assert pod is None


def test_url_join():
res = urljoin("https://10.100.0.1:443/api/fargate-XX.us-east-2.compute.internal/proxy", "/pods")
assert res == 'https://10.100.0.1:443/api/fargate-XX.us-east-2.compute.internal/proxy/pods'
res = urljoin("https://10.100.0.1:443/api/fargate-XX.us-east-2.compute.internal/proxy", "pods")
assert res == 'https://10.100.0.1:443/api/fargate-XX.us-east-2.compute.internal/proxy/pods'
res = urljoin("https://10.100.0.1:443/api/fargate-XX.us-east-2.compute.internal/proxy/", "pods")
assert res == 'https://10.100.0.1:443/api/fargate-XX.us-east-2.compute.internal/proxy/pods'
res = urljoin("https://10.100.0.1:443/api/fargate-XX.us-east-2.compute.internal/proxy/", "/pods")
assert res == 'https://10.100.0.1:443/api/fargate-XX.us-east-2.compute.internal/proxy/pods'


def test_is_static_pod():
podlist = json.loads(mock_from_file('pods.json'))

Expand Down

0 comments on commit 36337b8

Please sign in to comment.