Skip to content

Commit

Permalink
regular kubectl commands must be run from a layer too (#216)
Browse files Browse the repository at this point in the history
  • Loading branch information
Franr authored Aug 30, 2023
1 parent c5494a6 commit 5cbbb4f
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 6 deletions.
6 changes: 3 additions & 3 deletions leverage/containers/kubectl.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def start_shell(self):

def configure(self):
# make sure we are on the cluster layer
self.check_for_layer_location()
self.check_for_cluster_layer()

logger.info("Retrieving k8s cluster information...")
# generate the command that will configure the new cluster
Expand All @@ -67,8 +67,8 @@ def _get_eks_kube_config(self) -> str:
aws_eks_cmd = next(op for op in output.split("\r\n") if op.startswith("aws eks update-kubeconfig"))
return aws_eks_cmd + f" --region {self.region}"

def check_for_layer_location(self):
super(KubeCtlContainer, self).check_for_layer_location()
def check_for_cluster_layer(self):
self.check_for_layer_location()
# assuming the "cluster" layer will contain the expected EKS outputs
if self.cwd.parts[-1] != "cluster":
logger.error("This command can only run at the [bold]cluster layer[/bold].")
Expand Down
1 change: 1 addition & 0 deletions leverage/modules/kubectl.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def kubectl(context, state, args):
"""Run Kubectl commands in a custom containerized environment."""
state.container = KubeCtlContainer(get_docker_client())
state.container.ensure_image()
state.container.check_for_layer_location()
_handle_subcommand(context=context, cli_container=state.container, args=args)


Expand Down
6 changes: 3 additions & 3 deletions tests/test_containers/test_kubectl.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ def test_get_eks_kube_config_tf_output_error(kubectl_container):
kubectl_container._get_eks_kube_config()


def test_check_for_layer_location(kubectl_container, propagate_logs, caplog):
def test_check_for_cluster_layer(kubectl_container, propagate_logs, caplog):
"""
Test that if we are not on a cluster layer, we raise an error.
"""
with patch.object(TerraformContainer, "check_for_layer_location"): # assume parent method is already tested
with pytest.raises(Exit):
kubectl_container.cwd = Path("/random")
kubectl_container.check_for_layer_location()
kubectl_container.check_for_cluster_layer()

assert caplog.messages[0] == "This command can only run at the [bold]cluster layer[/bold]."

Expand Down Expand Up @@ -80,7 +80,7 @@ def test_start_shell(kubectl_container):


# don't rely on the filesystem
@patch.object(KubeCtlContainer, "check_for_layer_location", Mock())
@patch.object(KubeCtlContainer, "check_for_cluster_layer", Mock())
# nor terraform
@patch.object(KubeCtlContainer, "_get_eks_kube_config", Mock(return_value=AWS_EKS_UPDATE_KUBECONFIG))
def test_configure(kubectl_container, fake_os_user):
Expand Down

0 comments on commit 5cbbb4f

Please sign in to comment.