Skip to content

Commit

Permalink
Consider swapped container's ports when choosing proxy image
Browse files Browse the repository at this point in the history
Fixes #983
  • Loading branch information
Abhay Saxena committed Apr 12, 2019
1 parent e251939 commit 6cc2016
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 21 deletions.
1 change: 1 addition & 0 deletions newsfragments/983.bugfix
@@ -0,0 +1 @@
Telepresence correctly forwards privileged ports when using swap-deployment.
36 changes: 15 additions & 21 deletions telepresence/proxy/deployment.py
Expand Up @@ -137,13 +137,6 @@ def _get_container_name(container, deployment_json):
return container


def _merge_expose_ports(expose, container_json):
expose.merge_automatic_ports([
port["containerPort"] for port in container_json.get("ports", [])
if port["protocol"] == "TCP"
])


def supplant_deployment(
runner: Runner, deployment_arg: str, expose: PortMapping,
add_custom_nameserver: bool
Expand Down Expand Up @@ -172,11 +165,11 @@ def supplant_deployment(
)
container = _get_container_name(container, deployment_json)

new_deployment_json, orig_container_json = new_swapped_deployment(
new_deployment_json = new_swapped_deployment(
deployment_json,
container,
run_id,
get_image_name(expose),
expose,
add_custom_nameserver,
)

Expand Down Expand Up @@ -230,8 +223,6 @@ def delete_new_deployment(check):
)
resize_original(0)

_merge_expose_ports(expose, orig_container_json)

span.end()
return new_deployment_name, run_id

Expand All @@ -240,9 +231,9 @@ def new_swapped_deployment(
old_deployment: Dict,
container_to_update: str,
run_id: str,
telepresence_image: str,
expose: PortMapping,
add_custom_nameserver: bool,
) -> Tuple[Dict, Dict]:
) -> Dict:
"""
Create a new Deployment that uses telepresence-k8s image.
Expand All @@ -257,8 +248,8 @@ def new_swapped_deployment(
7. Adds TELEPRESENCE_CONTAINER_NAMESPACE env variable so the forwarder does
not have to access the k8s API from within the pod.
Returns dictionary that can be encoded to JSON and used with kubectl apply,
and contents of swapped out container.
Returns dictionary that can be encoded to JSON and used with kubectl apply.
Mutates the passed-in PortMapping to include container ports.
"""
new_deployment_json = deepcopy(old_deployment)
new_deployment_json["spec"]["replicas"] = 1
Expand All @@ -272,7 +263,12 @@ def new_swapped_deployment(
old_deployment["spec"]["template"]["spec"]["containers"],
):
if container["name"] == container_to_update:
container["image"] = telepresence_image
# Merge container ports into the expose list
expose.merge_automatic_ports([
port["containerPort"] for port in container.get("ports", [])
if port["protocol"] == "TCP"
])
container["image"] = get_image_name(expose)
# Not strictly necessary for real use, but tests break without this
# since we don't upload test images to Docker Hub:
container["imagePullPolicy"] = "IfNotPresent"
Expand Down Expand Up @@ -306,7 +302,7 @@ def new_swapped_deployment(
}
}
})
return new_deployment_json, old_container
return new_deployment_json

raise RuntimeError(
"Couldn't find container {} in the Deployment.".
Expand Down Expand Up @@ -377,16 +373,14 @@ def apply_json(json_config):

container = _get_container_name(container, dc_json)

new_dc_json, orig_container_json = new_swapped_deployment(
new_dc_json = new_swapped_deployment(
dc_json,
container,
run_id,
get_image_name(expose),
expose,
add_custom_nameserver,
)

apply_json(new_dc_json)

_merge_expose_ports(expose, orig_container_json)

return deployment, run_id

0 comments on commit 6cc2016

Please sign in to comment.