-
Notifications
You must be signed in to change notification settings - Fork 241
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
PAASTA-18479: Add list-namespaces tool #3997
Open
jfongatyelp
wants to merge
2
commits into
master
Choose a base branch
from
jfong/PAASTA-18479-list-namespaces
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+233
−7
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
#!/usr/bin/env python | ||
# Copyright 2015-2016 Yelp Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
from paasta_tools.cli.utils import get_instance_configs_for_service | ||
from paasta_tools.cli.utils import lazy_choices_completer | ||
from paasta_tools.cli.utils import validate_service_name | ||
from paasta_tools.spark_tools import SPARK_EXECUTOR_NAMESPACE | ||
from paasta_tools.utils import DEFAULT_SOA_DIR | ||
from paasta_tools.utils import list_clusters | ||
from paasta_tools.utils import list_services | ||
|
||
|
||
def add_subparser(subparsers) -> None: | ||
list_parser = subparsers.add_parser( | ||
"list-namespaces", | ||
help="Lists all k8s namespaces used by instances of a service", | ||
) | ||
list_parser.add_argument( | ||
"-s", | ||
"--service", | ||
help="Name of the service which you want to list the namespaces for.", | ||
required=True, | ||
).completer = lazy_choices_completer(list_services) | ||
# Most services likely don't need to filter by cluster/instance, and can add namespaces from all instances | ||
list_parser.add_argument( | ||
"-i", | ||
"--instance", | ||
help="Instance of the service that you want to list namespaces for. Like 'main' or 'canary'.", | ||
required=False, | ||
) | ||
list_parser.add_argument( | ||
"-c", | ||
"--cluster", | ||
help="Clusters that you want to list namespaces for. Like 'pnw-prod' or 'norcal-stagef'.", | ||
required=False, | ||
).completer = lazy_choices_completer(list_clusters) | ||
list_parser.add_argument( | ||
"-y", | ||
"-d", | ||
"--soa-dir", | ||
dest="soa_dir", | ||
default=DEFAULT_SOA_DIR, | ||
required=False, | ||
help="define a different soa config directory", | ||
) | ||
list_parser.set_defaults(command=paasta_list_namespaces) | ||
|
||
|
||
def paasta_list_namespaces(args): | ||
service = args.service | ||
soa_dir = args.soa_dir | ||
validate_service_name(service, soa_dir) | ||
|
||
namespaces = set() | ||
instance_configs = get_instance_configs_for_service( | ||
service=service, soa_dir=soa_dir, instances=args.instance, clusters=args.cluster | ||
) | ||
for instance in instance_configs: | ||
# We skip non-k8s instance types | ||
if instance.get_instance_type() in ("paasta-native", "adhoc"): | ||
continue | ||
namespaces.add(instance.get_namespace()) | ||
# Tron instances are TronActionConfigs | ||
if ( | ||
instance.get_instance_type() == "tron" | ||
and instance.get_executor() == "spark" | ||
): | ||
# We also need paasta-spark for spark executors | ||
namespaces.add(SPARK_EXECUTOR_NAMESPACE) | ||
|
||
# Print in list format to be used in iam_roles | ||
print(list(namespaces)) | ||
return 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
# tests/cli/cmds/test_list_namespaces.py | ||
from mock import MagicMock | ||
from mock import patch | ||
|
||
from paasta_tools.cli.cmds.list_namespaces import paasta_list_namespaces | ||
from paasta_tools.spark_tools import SPARK_EXECUTOR_NAMESPACE | ||
|
||
|
||
def test_list_namespaces_no_instances(capfd): | ||
mock_args = MagicMock( | ||
service="fake_service", | ||
instance=None, | ||
cluster=None, | ||
soa_dir="/fake/soa/dir", | ||
) | ||
with patch( | ||
"paasta_tools.cli.cmds.list_namespaces.get_instance_configs_for_service", | ||
return_value=[], | ||
autospec=True, | ||
), patch( | ||
"paasta_tools.cli.cmds.list_namespaces.validate_service_name", | ||
autospec=True, | ||
): | ||
assert paasta_list_namespaces(mock_args) == 0 | ||
stdout, _ = capfd.readouterr() | ||
assert stdout.strip() == "[]" | ||
|
||
|
||
def create_mock_instance_config(instance_type, namespace): | ||
""" | ||
Creates a mock InstanceConfig with specified instance_type and namespace. | ||
|
||
:param instance_type: The type of the instance (e.g., "kubernetes", "paasta-native"). | ||
:param namespace: The namespace associated with the instance. | ||
:return: A mock InstanceConfig object. | ||
""" | ||
mock_instance_config = MagicMock() | ||
mock_instance_config.get_instance_type.return_value = instance_type | ||
mock_instance_config.get_namespace.return_value = namespace | ||
return mock_instance_config | ||
|
||
|
||
def test_list_namespaces_with_instances_dupe_ns(capfd): | ||
mock_args = MagicMock( | ||
service="fake_service", | ||
instance=None, | ||
cluster=None, | ||
soa_dir="/fake/soa/dir", | ||
) | ||
|
||
mock_instance_configs = [ | ||
create_mock_instance_config("kubernetes", "k8s_namespace"), | ||
create_mock_instance_config("kubernetes", "k8s_namespace"), | ||
] | ||
|
||
with patch( | ||
"paasta_tools.cli.cmds.list_namespaces.get_instance_configs_for_service", | ||
return_value=mock_instance_configs, | ||
autospec=True, | ||
), patch( | ||
"paasta_tools.cli.cmds.list_namespaces.validate_service_name", | ||
autospec=True, | ||
): | ||
assert paasta_list_namespaces(mock_args) == 0 | ||
stdout, _ = capfd.readouterr() | ||
assert stdout.strip() == "['k8s_namespace']" | ||
|
||
|
||
def test_list_namespaces_tron(capfd): | ||
mock_args = MagicMock( | ||
service="fake_service", | ||
instance=None, | ||
cluster=None, | ||
soa_dir="/fake/soa/dir", | ||
) | ||
mock_tron_instance = create_mock_instance_config("tron", "tron") | ||
mock_tron_instance.get_executor.return_value = "paasta" | ||
|
||
with patch( | ||
"paasta_tools.cli.cmds.list_namespaces.get_instance_configs_for_service", | ||
return_value=[mock_tron_instance], | ||
autospec=True, | ||
), patch( | ||
"paasta_tools.cli.cmds.list_namespaces.validate_service_name", | ||
autospec=True, | ||
): | ||
assert paasta_list_namespaces(mock_args) == 0 | ||
stdout, _ = capfd.readouterr() | ||
assert "['tron']" | ||
|
||
|
||
def test_list_namespaces_spark(capfd): | ||
mock_args = MagicMock( | ||
service="fake_service", | ||
instance=None, | ||
cluster=None, | ||
soa_dir="/fake/soa/dir", | ||
) | ||
mock_spark_instance = create_mock_instance_config("tron", "tron") | ||
mock_spark_instance.get_executor.return_value = "spark" | ||
|
||
mock_instance_configs = [ | ||
create_mock_instance_config("kubernetes", "k8s_namespace"), | ||
mock_spark_instance, | ||
] | ||
|
||
with patch( | ||
"paasta_tools.cli.cmds.list_namespaces.get_instance_configs_for_service", | ||
return_value=mock_instance_configs, | ||
autospec=True, | ||
), patch( | ||
"paasta_tools.cli.cmds.list_namespaces.validate_service_name", | ||
autospec=True, | ||
): | ||
assert paasta_list_namespaces(mock_args) == 0 | ||
stdout, _ = capfd.readouterr() | ||
assert f"'{SPARK_EXECUTOR_NAMESPACE}'" in stdout | ||
assert "'tron'" in stdout | ||
assert "'k8s_namespace'" in stdout | ||
|
||
|
||
def test_list_namespaces_skips_non_k8s_instances(capfd): | ||
mock_args = MagicMock( | ||
service="fake_service", | ||
instance=None, | ||
cluster=None, | ||
soa_dir="/fake/soa/dir", | ||
) | ||
|
||
mock_k8s_instance_config = create_mock_instance_config("eks", "k8s_namespace") | ||
mock_adhoc_instance_config = create_mock_instance_config("adhoc", None) | ||
|
||
with patch( | ||
"paasta_tools.cli.cmds.list_namespaces.get_instance_configs_for_service", | ||
return_value=[mock_k8s_instance_config, mock_adhoc_instance_config], | ||
autospec=True, | ||
), patch( | ||
"paasta_tools.cli.cmds.list_namespaces.validate_service_name", | ||
autospec=True, | ||
): | ||
assert paasta_list_namespaces(mock_args) == 0 | ||
stdout, _ = capfd.readouterr() | ||
assert stdout.strip() == "['k8s_namespace']" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think it might be possible for batch-box-launched executors to use pod identity, but imo it's fine to leave them out u!less this becomes a problem (especially since things are actively migrating away from batch boxes)