Skip to content
This repository has been archived by the owner on Nov 20, 2023. It is now read-only.

Bug/1544 tye deploy namespace issues #1545

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
24 changes: 12 additions & 12 deletions src/Microsoft.Tye.Core/ValidateIngressStep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ public override async Task ExecuteAsync(OutputContext output, ApplicationBuilder
//
// For instance we don't support k8s 1.18.X IngressClass resources because that version
// isn't broadly available yet.
if (Force)
{
output.WriteDebugLine("Skipping ingress validation because force was specified.");
return;
}

var ingressClass = "nginx";
if (!IngressClasses.Add(ingressClass))
{
Expand All @@ -48,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 Expand Up @@ -89,12 +95,6 @@ public override async Task ExecuteAsync(OutputContext output, ApplicationBuilder
throw new CommandException("Unable connect to kubernetes.", ex);
}

if (Force)
{
output.WriteDebugLine("Skipping because force was specified.");
return;
}

if (!Interactive)
{
throw new CommandException(
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