Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

to support different auth providers with kubectl proxy #258

Merged
merged 15 commits into from
Jun 5, 2023
Merged
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
6 changes: 6 additions & 0 deletions build-pipelines-PR.yml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ jobs:
solution: src/client.sln
msbuildArgs: -p:Localize=true -p:ConsumeEndpointManager=false
configuration: ${{ parameters.BuildConfiguration }}
- task: CredScan@3
inputs:
outputFormat: 'pre'
- task: VSTest@2
displayName: Run Unit Tests
condition: and(succeeded(), eq(variables.RunTests, 'true'))
Expand Down Expand Up @@ -208,6 +211,9 @@ jobs:
nugetConfigPath: src/nuget.config
verbosityRestore: Normal
searchPatternPush: $(Build.ArtifactStagingDirectory)/*.nupkg
- task: CredScan@3
inputs:
outputFormat: 'pre'
- task: DotNetCoreCLI@2
displayName: publish EndpointManagerLauncher(Windows)
enabled: True
Expand Down
37 changes: 37 additions & 0 deletions src/common.tests/Kubernetes/K8sClientFactoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,19 @@
// --------------------------------------------------------------------------------------------

using FakeItEasy;
using k8s;
using k8s.KubeConfigModels;
using Microsoft.BridgeToKubernetes.Common.IO;
using Microsoft.BridgeToKubernetes.Common.Kubernetes;
using Microsoft.BridgeToKubernetes.TestHelpers;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Xunit;
using YamlDotNet.RepresentationModel;
using YamlDotNet.Serialization.NamingConventions;

namespace Microsoft.BridgeToKubernetes.Common.Tests.Kubernetes
{
Expand Down Expand Up @@ -50,5 +57,35 @@ public void CreateFromInClusterConfig()
Assert.Equal(2, config.SslCaCerts.Count);
}

[Fact]
public void CreateFromKubeConfigTestWithProxy()
{
var fakeEnv = A.Fake<IEnvironmentVariables>();
var expectedProxyUri = "http://localhost:8080/";
A.CallTo(() => fakeEnv.KubectlProxy).Returns(expectedProxyUri);
_autoFake.Provide(fakeEnv);
var kClientFactory = _autoFake.Resolve<K8sClientFactory>();
var config = kClientFactory.CreateFromKubeConfig(null);
Assert.NotNull(config);
Assert.Equal(expectedProxyUri, config.BaseUri.ToString());

}

[Fact]
public void CreateFromKubeConfigTestWithOutProxy()
{
string expectedUri = "https://sample-demo.hcp.eastus.azmk8s.io/";
IEnvironmentVariables fakeEnv = A.Fake<IEnvironmentVariables>();
A.CallTo(() => fakeEnv.KubectlProxy).Returns(null);
_autoFake.Provide(fakeEnv);
K8sClientFactory kClientFactory = _autoFake.Resolve<K8sClientFactory>();
var deserializer = new YamlDotNet.Serialization.DeserializerBuilder()
.WithNamingConvention(CamelCaseNamingConvention.Instance).Build();
K8SConfiguration fakeConfig = deserializer.Deserialize<K8SConfiguration>(File
.ReadAllText(Path.Combine("TestData", "simpleconfig.yml")));
IKubernetes config = kClientFactory.CreateFromKubeConfig(fakeConfig);
Assert.NotNull(config);
Assert.Equal(expectedUri, config.BaseUri.ToString());
}
}
}
19 changes: 11 additions & 8 deletions src/common.tests/Kubernetes/KubernetesClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ public async Task EnsureClientInvokeWrapperHandlesIOExceptions()
() => _fakeRestClient.CoreV1.ListNamespacedPodWithHttpMessagesAsync(
A<string>._, A<bool?>._, A<string>._, A<string>._,
A<string>._, A<int?>._, A<string>._, A<string>._,
A<int?>._, A<bool?>._, A<bool?>._, A<IReadOnlyDictionary<string, IReadOnlyList<String>>>._,
A<bool?>._, A<int?>._, A<bool?>._, A<bool?>._,
A<IReadOnlyDictionary<string, IReadOnlyList<String>>>._,
A<CancellationToken>._);
A.CallTo(listPodsInNamespaceExpression)
.Throws<IOException>()
Expand All @@ -77,7 +78,8 @@ public async Task EnsureClientInvokeWrapperHandlesNotFound()
() => _fakeRestClient.CoreV1.ListNamespacedPodWithHttpMessagesAsync(
A<string>._, A<bool?>._, A<string>._, A<string>._,
A<string>._, A<int?>._, A<string>._, A<string>._,
A<int?>._, A<bool?>._, A<bool?>._, A<IReadOnlyDictionary<string, IReadOnlyList<String>>>._,
A<bool?>._, A<int?>._, A<bool?>._, A<bool?>._,
A<IReadOnlyDictionary<string, IReadOnlyList<String>>>._,
A<CancellationToken>._);
var ex = new HttpOperationException();
ex.Response = new HttpResponseMessageWrapper(new HttpResponseMessage(HttpStatusCode.NotFound), string.Empty);
Expand All @@ -95,7 +97,8 @@ public async Task ListPodsInNamespaceTest()
() => _fakeRestClient.CoreV1.ListNamespacedPodWithHttpMessagesAsync(
A<string>._, A<bool?>._, A<string>._, A<string>._,
A<string>._, A<int?>._, A<string>._, A<string>._,
A<int?>._, A<bool?>._, A<bool?>._, A<IReadOnlyDictionary<string, IReadOnlyList<String>>>._,
A<bool?>._, A<int?>._, A<bool?>._, A<bool?>._,
hsubramanianaks marked this conversation as resolved.
Show resolved Hide resolved
A<IReadOnlyDictionary<string, IReadOnlyList<String>>>._,
A<CancellationToken>._);

A.CallTo(listAllPodsInNamespaceExpression)
Expand All @@ -122,13 +125,13 @@ public async Task ListServicesInNamespace()
Expression<Func<Task<HttpOperationResponse<V1ServiceList>>>> listServiceForAllNamespacesExpression =
() => _fakeRestClient.CoreV1.ListServiceForAllNamespacesWithHttpMessagesAsync(
A<bool?>._, A<string>._, A<string>._, A<string>._,
A<int?>._, A<bool?>._, A<string>._, A<string>._, A<int?>._, A<bool?>._,
A<int?>._, A<bool?>._, A<string>._, A<string>._, A<bool?>._, A<int?>._, A<bool?>._,
A<IReadOnlyDictionary<string, IReadOnlyList<String>>>._, A<CancellationToken>._);

Expression<Func<Task<HttpOperationResponse<V1ServiceList>>>> listNamespacedServiceExpression =
() => _fakeRestClient.CoreV1.ListNamespacedServiceWithHttpMessagesAsync(
A<string>._, A<bool?>._, A<string>._, A<string>._,
A<string>._, A<int?>._, A<string>._, A<string>._, A<int?>._,
A<string>._, A<int?>._, A<string>._, A<string>._, A<bool?>._, A<int?>._,
A<bool?>._, A<bool?>._, A<IReadOnlyDictionary<string, IReadOnlyList<String>>>._, A<CancellationToken>._);

A.CallTo(listNamespacedServiceExpression)
Expand Down Expand Up @@ -156,13 +159,13 @@ public async Task ListServicesInAllNamespaces()
Expression<Func<Task<HttpOperationResponse<V1ServiceList>>>> listServiceForAllNamespacesExpression =
() => _fakeRestClient.CoreV1.ListServiceForAllNamespacesWithHttpMessagesAsync(
A<bool?>._, A<string>._, A<string>._, A<string>._,
A<int?>._, A<bool?>._, A<string>._, A<string>._, A<int?>._, A<bool?>._,
A<int?>._, A<bool?>._, A<string>._, A<string>._, A<bool?>._, A<int?>._, A<bool?>._,
A<IReadOnlyDictionary<string, IReadOnlyList<String>>>._, A<CancellationToken>._);

Expression<Func<Task<HttpOperationResponse<V1ServiceList>>>> listNamespacedServiceExpression =
() => _fakeRestClient.CoreV1.ListNamespacedServiceWithHttpMessagesAsync(
A<string>._, A<bool?>._, A<string>._, A<string>._,
A<string>._, A<int?>._, A<string>._, A<string>._, A<int?>._,
A<string>._, A<int?>._, A<string>._, A<string>._, A<bool?>._, A<int?>._,
A<bool?>._, A<bool?>._, A<IReadOnlyDictionary<string, IReadOnlyList<String>>>._, A<CancellationToken>._);

A.CallTo(listServiceForAllNamespacesExpression)
Expand Down Expand Up @@ -195,7 +198,7 @@ private V1PodStatus GetPodStatus(string userContainerStatus, string initContaine
private V1ContainerStatus GetV1ContainerStatus(string containerName, string containerState)
{
bool ready = string.Equals(containerState, "running");
return new V1ContainerStatus($"{containerName}-imagename", $"{containerName}-imageId", containerName, ready, 0, $"{containerName}-ContainerId", state: GetV1ContainerState(containerState));
return new V1ContainerStatus($"{containerName}-imagename", $"{containerName}-imageId", containerName, ready, 0, null, $"{containerName}-ContainerId", state: GetV1ContainerState(containerState));
}

private V1ContainerState GetV1ContainerState(string status)
Expand Down
Loading