Support wildcard IPv4 and IPv6 addresses (#550)

Some tools can generate kubeconfig files which use wildcard IPv4 or IPv6 addresses. For example, using k3d with --api-server=https://0.0.0.0:6433/ would generate a kubeconfig file like this:

```
apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: (...)
    server: https://0.0.0.0:6433
  name: k3d-k3s-default
```

Standard Kubernetes tools (like kubectl or Helm) correctly parse the 0.0.0.0 IP address and transform it 127.0.0.1; 3rd party tools like curl or wget will do the same on Unix systems.

This is default behavior on Unix but not on Windows. As a result, the .NET Kubernetes client will fail to work with kubeconfig files like this and you'll get HTTP exceptions.

Go has explicit workarounds for this (see 1a0b1cca4c), and this PR attemps to replicate these workarounds in the .NET client.
This commit is contained in:
Frederik Carlier
2021-01-28 00:19:48 -08:00
committed by GitHub
parent 6f5706d753
commit 4e58609159
5 changed files with 107 additions and 0 deletions

View File

@@ -10,6 +10,7 @@ using System.Threading.Tasks;
using k8s.Authentication;
using k8s.Exceptions;
using k8s.KubeConfigModels;
using System.Net;
namespace k8s
{
@@ -273,6 +274,22 @@ namespace k8s
throw new KubeConfigException($"Bad server host URL `{Host}` (cannot be parsed)");
}
if (IPAddress.TryParse(uri.Host, out var ipAddress))
{
if (IPAddress.Equals(IPAddress.Any, ipAddress))
{
var builder = new UriBuilder(Host);
builder.Host = $"{IPAddress.Loopback}";
Host = builder.ToString();
}
else if (IPAddress.Equals(IPAddress.IPv6Any, ipAddress))
{
var builder = new UriBuilder(Host);
builder.Host = $"{IPAddress.IPv6Loopback}";
Host = builder.ToString();
}
}
if (uri.Scheme == "https")
{
if (!string.IsNullOrEmpty(clusterDetails.ClusterEndpoint.CertificateAuthorityData))