Skip to content

Commit 1dc3a51

Browse files
Prompts user to install gke auth plugin if missing
1 parent 05d95c9 commit 1dc3a51

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

keras_remote/cli/prerequisites_check.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,25 @@ def check_docker():
4141
)
4242

4343

44+
def check_gke_auth_plugin():
45+
"""Verify gke-gcloud-auth-plugin is installed."""
46+
if not shutil.which("gke-gcloud-auth-plugin"):
47+
warning("gke-gcloud-auth-plugin not found.")
48+
if click.confirm(
49+
"Install it via `gcloud components install gke-gcloud-auth-plugin`?",
50+
default=True,
51+
):
52+
subprocess.run(
53+
["gcloud", "components", "install", "gke-gcloud-auth-plugin"],
54+
check=True,
55+
)
56+
else:
57+
raise click.ClickException(
58+
"gke-gcloud-auth-plugin is required. "
59+
"Install with: gcloud components install gke-gcloud-auth-plugin"
60+
)
61+
62+
4463
def check_gcloud_auth():
4564
"""Check if gcloud Application Default Credentials are configured."""
4665
result = subprocess.run(
@@ -61,5 +80,6 @@ def check_all():
6180
check_gcloud()
6281
check_pulumi()
6382
check_kubectl()
83+
check_gke_auth_plugin()
6484
check_docker()
6585
check_gcloud_auth()

keras_remote/cli/prerequisites_check_test.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
check_docker,
1010
check_gcloud,
1111
check_gcloud_auth,
12+
check_gke_auth_plugin,
1213
check_kubectl,
1314
check_pulumi,
1415
)
@@ -71,6 +72,40 @@ def test_missing(self, check_fn, error_match):
7172
check_fn()
7273

7374

75+
class TestCheckGkeAuthPlugin(absltest.TestCase):
76+
def test_present(self):
77+
with mock.patch(
78+
"shutil.which", return_value="/usr/bin/gke-gcloud-auth-plugin"
79+
):
80+
check_gke_auth_plugin()
81+
82+
def test_missing_user_accepts_install(self):
83+
with (
84+
mock.patch("shutil.which", return_value=None),
85+
mock.patch("keras_remote.cli.prerequisites_check.warning"),
86+
mock.patch("click.confirm", return_value=True),
87+
mock.patch(
88+
"keras_remote.cli.prerequisites_check.subprocess.run",
89+
) as mock_run,
90+
):
91+
check_gke_auth_plugin()
92+
mock_run.assert_called_once_with(
93+
["gcloud", "components", "install", "gke-gcloud-auth-plugin"],
94+
check=True,
95+
)
96+
97+
def test_missing_user_declines_install(self):
98+
with (
99+
mock.patch("shutil.which", return_value=None),
100+
mock.patch("keras_remote.cli.prerequisites_check.warning"),
101+
mock.patch("click.confirm", return_value=False),
102+
self.assertRaisesRegex(
103+
click.ClickException, "gke-gcloud-auth-plugin is required"
104+
),
105+
):
106+
check_gke_auth_plugin()
107+
108+
74109
class TestCheckGcloudAuth(absltest.TestCase):
75110
def test_token_success(self):
76111
"""When print-access-token succeeds, no login is triggered."""

0 commit comments

Comments
 (0)