From f5473bf8e76d5735a77cc0ae8326f0a147a9b9fc Mon Sep 17 00:00:00 2001 From: petervo Date: Tue, 19 Sep 2017 23:02:47 -0700 Subject: [PATCH] kubernetes: Fix hangs when connecting If a kube config has no user data for a context kubectl prompts. Unfortunately there is no way to stop this. So only run kubectl version when when know we have a user with a auth-provider section. Make sure this is the default user by using --minify to get the current user only. Closes #7707 Fixes #7688 Reviewed-by: Stef Walter --- pkg/kubernetes/scripts/kube-client-cockpit.js | 41 +++++++++++++++---- 1 file changed, 33 insertions(+), 8 deletions(-) diff --git a/pkg/kubernetes/scripts/kube-client-cockpit.js b/pkg/kubernetes/scripts/kube-client-cockpit.js index ab6c0f44ba2..733326c7b14 100644 --- a/pkg/kubernetes/scripts/kube-client-cockpit.js +++ b/pkg/kubernetes/scripts/kube-client-cockpit.js @@ -897,14 +897,39 @@ function read() { var cmd = ["kubectl", "config", "view", "--output=json", "--raw"]; - /* Call kubectl version first incase we have a auth-provider - * that needs to be populated */ - return runCommand(["kubectl", "version"]) - .then(function() { - return runCommand(cmd); - }, function () { - return runCommand(cmd); - }); + /* Call kubectl minified config view. That only outputs + * the objects that would be used by a connection */ + return runCommand(["kubectl", "config", "view", "--minify", "--output=json"]) + .then(function (data) { + var p; + var auth_provider; + var user; + + /* If the default data has a 'auth-provider' + * section then call kubectl version to try to + * get it to fill in any token data. + */ + try { + user = JSON.parse(data)["users"][0]; + if (user && user['user']) + auth_provider = user['user']['auth-provider']; + } catch(ex) { + console.warn("received invalid kubectl config", ex); + } + + if (auth_provider) { + p = runCommand(["kubectl", "version"]) + .then(function () { + return runCommand(cmd); + }, function () { + return runCommand(cmd); + }); + } else { + p = runCommand(cmd); + } + + return p; + }); } return {