Skip to content

Commit 2a2d84d

Browse files
committed
Fix user check for jira ticketing
1 parent 7e72bda commit 2a2d84d

File tree

2 files changed

+45
-25
lines changed

2 files changed

+45
-25
lines changed

service_configuration_lib/spark_config.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@
7777

7878
SUPPORTED_CLUSTER_MANAGERS = ['kubernetes', 'local']
7979
DEFAULT_SPARK_RUN_CONFIG = '/nail/srv/configs/spark.yaml'
80+
USER_BATCH = 'batch' # used by batch servers
81+
USER_TRON = 'TRON' # used by Tron jobs, or other paasta CLI commands such as `paasta validate/mark-for-deployment`
82+
USER_UNSPECIFIED = 'UNSPECIFIED'
8083

8184
log = logging.Logger(__name__)
8285
log.setLevel(logging.WARN)
@@ -305,7 +308,7 @@ def _get_k8s_spark_env(
305308
service_account_name: Optional[str] = None,
306309
include_self_managed_configs: bool = True,
307310
k8s_server_address: Optional[str] = None,
308-
user: Optional[str] = None,
311+
user: Optional[str] = USER_UNSPECIFIED,
309312
jira_ticket: Optional[str] = None,
310313
) -> Dict[str, str]:
311314
# RFC 1123: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#dns-label-names
@@ -314,8 +317,6 @@ def _get_k8s_spark_env(
314317
_paasta_cluster = utils.get_k8s_resource_name_limit_size_with_hash(paasta_cluster)
315318
_paasta_service = utils.get_k8s_resource_name_limit_size_with_hash(paasta_service)
316319
_paasta_instance = utils.get_k8s_resource_name_limit_size_with_hash(paasta_instance)
317-
if not user:
318-
user = os.environ.get('USER', 'UNSPECIFIED')
319320

320321
spark_env = {
321322
'spark.master': f'k8s://https://k8s.{paasta_cluster}.paasta:6443',
@@ -1040,6 +1041,7 @@ def get_spark_conf(
10401041
:param service_account_name: The k8s service account to use for spark k8s authentication.
10411042
:param force_spark_resource_configs: skip the resource/instances recalculation.
10421043
This is strongly not recommended.
1044+
:param user: the user who is running the spark job.
10431045
:returns: spark opts in a dict.
10441046
"""
10451047
# Mesos deprecation
@@ -1051,8 +1053,11 @@ def get_spark_conf(
10511053
# is str type.
10521054
user_spark_opts = _convert_user_spark_opts_value_to_str(user_spark_opts)
10531055

1056+
# Get user from environment variables if it's not set
1057+
user = user or os.environ.get('USER', None)
1058+
10541059
if self.mandatory_default_spark_srv_conf.get('spark.yelp.jira_ticket.enabled') == 'true':
1055-
needs_jira_check = os.environ.get('USER', '') not in ['batch', 'TRON', '']
1060+
needs_jira_check = user not in [USER_BATCH, USER_TRON, None]
10561061
if needs_jira_check:
10571062
valid_ticket = self._get_valid_jira_ticket(jira_ticket)
10581063
if valid_ticket is None:

tests/spark_config_test.py

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,20 @@ def gpu_pool(self, tmpdir, monkeypatch):
623623
},
624624
False,
625625
),
626+
(
627+
'Local spark cluster',
628+
'local',
629+
{
630+
'spark.executor.cores': '2',
631+
'spark.executor.instances': '600',
632+
},
633+
{
634+
'spark.executor.memory': '28g',
635+
'spark.executor.cores': '4',
636+
'spark.executor.instances': '600',
637+
},
638+
False,
639+
),
626640
],
627641
)
628642
def test_adjust_spark_requested_resources(
@@ -1887,35 +1901,22 @@ def test_get_spark_conf_with_jira_validation_disabled(self, mock_spark_srv_conf_
18871901
assert 'spark.kubernetes.executor.label.spark.yelp.com/jira_ticket' not in result
18881902

18891903
@pytest.mark.parametrize(
1890-
'user_env,should_check', [
1904+
'user,should_check', [
18911905
('regular_user', True),
18921906
('batch', False),
18931907
('TRON', False),
1894-
('', False),
1908+
(None, False),
18951909
],
18961910
)
18971911
def test_jira_ticket_check_for_different_users(
1898-
self, user_env, should_check, mock_spark_srv_conf_file_with_jira_enabled, mock_log,
1912+
self, user, should_check, mock_spark_srv_conf_file_with_jira_enabled, mock_log,
18991913
):
19001914
"""Test that Jira ticket validation is skipped for certain users."""
1901-
with mock.patch.dict(os.environ, {'USER': user_env}):
1902-
spark_conf_builder = spark_config.SparkConfBuilder()
1915+
spark_conf_builder = spark_config.SparkConfBuilder()
19031916

1904-
if should_check:
1905-
# For regular users, validation should be enforced
1906-
with pytest.raises(RuntimeError):
1907-
spark_conf_builder.get_spark_conf(
1908-
cluster_manager='kubernetes',
1909-
spark_app_base_name='test-app',
1910-
user_spark_opts={},
1911-
paasta_cluster='test-cluster',
1912-
paasta_pool='test-pool',
1913-
paasta_service='test-service',
1914-
paasta_instance='test-instance',
1915-
docker_img='test-image',
1916-
)
1917-
else:
1918-
# For special users, validation should be skipped
1917+
if should_check:
1918+
# For regular users, validation should be enforced
1919+
with pytest.raises(RuntimeError):
19191920
spark_conf_builder.get_spark_conf(
19201921
cluster_manager='kubernetes',
19211922
spark_app_base_name='test-app',
@@ -1925,5 +1926,19 @@ def test_jira_ticket_check_for_different_users(
19251926
paasta_service='test-service',
19261927
paasta_instance='test-instance',
19271928
docker_img='test-image',
1929+
user=user,
19281930
)
1929-
mock_log.debug.assert_called_with('Jira ticket check not required for this job configuration.')
1931+
else:
1932+
# For special users, validation should be skipped
1933+
spark_conf_builder.get_spark_conf(
1934+
cluster_manager='kubernetes',
1935+
spark_app_base_name='test-app',
1936+
user_spark_opts={},
1937+
paasta_cluster='test-cluster',
1938+
paasta_pool='test-pool',
1939+
paasta_service='test-service',
1940+
paasta_instance='test-instance',
1941+
docker_img='test-image',
1942+
user=user,
1943+
)
1944+
mock_log.debug.assert_called_with('Jira ticket check not required for this job configuration.')

0 commit comments

Comments
 (0)