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

@@ -381,6 +381,33 @@ namespace k8s.Tests
var cfg = await KubernetesClientConfiguration.BuildConfigFromConfigFileAsync(new FileInfo(path)).ConfigureAwait(false);
}
[Fact]
public void ContextWithWildcardIPv4()
{
var path = Path.GetFullPath("assets/kubeconfig.wildcard-ipv4.yml");
var cfg = KubernetesClientConfiguration.BuildConfigFromConfigFile(path);
Assert.Equal("https://127.0.0.1:443/", cfg.Host);
}
[Fact]
public void ContextWithWildcardIPv6()
{
var path = Path.GetFullPath("assets/kubeconfig.wildcard-ipv6.yml");
var cfg = KubernetesClientConfiguration.BuildConfigFromConfigFile(path);
Assert.Equal("https://[::1]:443/", cfg.Host);
}
[Fact]
public void ContextWithWildcardIPv62()
{
var path = Path.GetFullPath("assets/kubeconfig.wildcard-ipv6.yml");
var cfg = KubernetesClientConfiguration.BuildConfigFromConfigFile(path);
Assert.Equal("https://[::1]:443/", cfg.Host);
}
/// <summary>
/// Ensures Kube config file is loaded from explicit file
/// </summary>

View File

@@ -0,0 +1,21 @@
# Sample file based on https://kubernetes.io/docs/tasks/access-application-cluster/authenticate-across-clusters-kubeconfig/
# WARNING: File includes minor fixes
---
current-context: federal-context
apiVersion: v1
clusters:
- cluster:
server: https://0.0.0.0:443
name: horse-cluster
contexts:
- context:
cluster: horse-cluster
namespace: chisel-ns
user: green-user
name: federal-context
kind: Config
users:
- name: green-user
user:
password: secret
username: admin

View File

@@ -0,0 +1,21 @@
# Sample file based on https://kubernetes.io/docs/tasks/access-application-cluster/authenticate-across-clusters-kubeconfig/
# WARNING: File includes minor fixes
---
current-context: federal-context
apiVersion: v1
clusters:
- cluster:
server: https://[::]:443
name: horse-cluster
contexts:
- context:
cluster: horse-cluster
namespace: chisel-ns
user: green-user
name: federal-context
kind: Config
users:
- name: green-user
user:
password: secret
username: admin

View File

@@ -0,0 +1,21 @@
# Sample file based on https://kubernetes.io/docs/tasks/access-application-cluster/authenticate-across-clusters-kubeconfig/
# WARNING: File includes minor fixes
---
current-context: federal-context
apiVersion: v1
clusters:
- cluster:
server: https://[0:0:0:0:0:0:0:0]:443
name: horse-cluster
contexts:
- context:
cluster: horse-cluster
namespace: chisel-ns
user: green-user
name: federal-context
kind: Config
users:
- name: green-user
user:
password: secret
username: admin