Skip to content

Commit

Permalink
dotnet#1544 - forwarded namespace to kubectl detector
Browse files Browse the repository at this point in the history
  • Loading branch information
RSoeborg committed Feb 23, 2023
1 parent 2271dfa commit 134d727
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
}
Expand Down
12 changes: 6 additions & 6 deletions src/Microsoft.Tye.Core/ValidateIngressStep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
11 changes: 9 additions & 2 deletions src/shared/KubectlDetector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ internal static class KubectlDetector
{
private static Lazy<Task<Version?>> _kubectlInstalled = new Lazy<Task<Version?>>(GetKubectlVersion);
private static Lazy<Task<bool>> _kubectlConnectedToCluster = new Lazy<Task<bool>>(DetectKubectlConnectedToCluster);
private static string? _namespace = null;

public static Task<Version?> GetKubernetesServerVersion(OutputContext output)
{
Expand All @@ -22,10 +23,11 @@ internal static class KubectlDetector
return _kubectlInstalled.Value;
}

public static Task<bool> IsKubectlConnectedToClusterAsync(OutputContext output)
public static Task<bool> IsKubectlConnectedToClusterAsync(OutputContext output, string? @namespace = null)
{
if (!_kubectlConnectedToCluster.IsValueCreated)
{
_namespace = @namespace;
output.WriteInfoLine("Verifying kubectl connection to cluster...");
}
return _kubectlConnectedToCluster.Value;
Expand Down Expand Up @@ -72,7 +74,12 @@ private static async Task<bool> 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)
Expand Down
2 changes: 1 addition & 1 deletion src/tye/Program.DeployCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
}
Expand Down

0 comments on commit 134d727

Please sign in to comment.