diff --git a/src/Microsoft.Tye.Core/DeployApplicationKubernetesManifestStep.cs b/src/Microsoft.Tye.Core/DeployApplicationKubernetesManifestStep.cs index 8e8d51400..991b46f69 100644 --- a/src/Microsoft.Tye.Core/DeployApplicationKubernetesManifestStep.cs +++ b/src/Microsoft.Tye.Core/DeployApplicationKubernetesManifestStep.cs @@ -23,7 +23,7 @@ public override async Task ExecuteAsync(OutputContext output, ApplicationBuilder throw new CommandException($"Cannot apply manifests because kubectl is not installed."); } - if (!await KubectlDetector.IsKubectlConnectedToClusterAsync(output)) + if (!await KubectlDetector.IsKubectlConnectedToClusterAsync(output, application.Namespace)) { throw new CommandException($"Cannot apply manifests because kubectl is not connected to a cluster."); } diff --git a/src/Microsoft.Tye.Core/ValidateIngressStep.cs b/src/Microsoft.Tye.Core/ValidateIngressStep.cs index 357877b7a..cb628166d 100644 --- a/src/Microsoft.Tye.Core/ValidateIngressStep.cs +++ b/src/Microsoft.Tye.Core/ValidateIngressStep.cs @@ -54,17 +54,17 @@ public override async Task ExecuteAsync(OutputContext output, ApplicationBuilder throw new CommandException($"Cannot validate ingress because kubectl is not installed."); } - if (!await KubectlDetector.IsKubectlConnectedToClusterAsync(output)) - { - throw new CommandException($"Cannot validate ingress because kubectl is not connected to a cluster."); - } - output.WriteDebugLine($"Validating ingress class '{ingressClass}'."); var config = KubernetesClientConfiguration.BuildDefaultConfig(); - + // If namespace is null, set it to default config.Namespace ??= "default"; + if (!await KubectlDetector.IsKubectlConnectedToClusterAsync(output, config.Namespace)) + { + throw new CommandException($"Cannot validate ingress because kubectl is not connected to a cluster."); + } + var kubernetes = new Kubernetes(config); // Looking for a deployment using a standard label. diff --git a/src/shared/KubectlDetector.cs b/src/shared/KubectlDetector.cs index 782ca8aa8..120120f57 100644 --- a/src/shared/KubectlDetector.cs +++ b/src/shared/KubectlDetector.cs @@ -12,6 +12,7 @@ internal static class KubectlDetector { private static Lazy> _kubectlInstalled = new Lazy>(GetKubectlVersion); private static Lazy> _kubectlConnectedToCluster = new Lazy>(DetectKubectlConnectedToCluster); + private static string? _namespace = null; public static Task GetKubernetesServerVersion(OutputContext output) { @@ -22,10 +23,11 @@ internal static class KubectlDetector return _kubectlInstalled.Value; } - public static Task IsKubectlConnectedToClusterAsync(OutputContext output) + public static Task IsKubectlConnectedToClusterAsync(OutputContext output, string? @namespace = null) { if (!_kubectlConnectedToCluster.IsValueCreated) { + _namespace = @namespace; output.WriteInfoLine("Verifying kubectl connection to cluster..."); } return _kubectlConnectedToCluster.Value; @@ -72,7 +74,12 @@ private static async Task DetectKubectlConnectedToCluster() { try { - var result = await ProcessUtil.RunAsync("kubectl", "cluster-info", throwOnError: false); + String args = "cluster-info"; + if (_namespace != null) { + args += $" --namespace {_namespace}"; + } + + var result = await ProcessUtil.RunAsync("kubectl", args, throwOnError: false); return result.ExitCode == 0; } catch (Exception) diff --git a/src/tye/Program.DeployCommand.cs b/src/tye/Program.DeployCommand.cs index 85aea5e48..3bfa1011e 100644 --- a/src/tye/Program.DeployCommand.cs +++ b/src/tye/Program.DeployCommand.cs @@ -69,7 +69,7 @@ private static async Task ExecuteDeployAsync(OutputContext output, ApplicationBu throw new CommandException($"Cannot apply manifests because kubectl is not installed."); } - if (!await KubectlDetector.IsKubectlConnectedToClusterAsync(output)) + if (!await KubectlDetector.IsKubectlConnectedToClusterAsync(output, application.Namespace)) { throw new CommandException($"Cannot apply manifests because kubectl is not connected to a cluster."); }