Skip to content

Commit

Permalink
Merge pull request #156 from tintoy/develop
Browse files Browse the repository at this point in the history
Add DI registration methods for named KubeClientOptions from pod service account
  • Loading branch information
tintoy authored Jan 16, 2024
2 parents 117c4f8 + 486ed3b commit 159057c
Show file tree
Hide file tree
Showing 6 changed files with 260 additions and 64 deletions.
7 changes: 3 additions & 4 deletions GitVersion.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ branches:
track-merge-target: false
tracks-release-branches: false
is-release-branch: false
is-mainline: true
source-branches: [ 'develop' ]

develop:
regex: develop
Expand All @@ -25,8 +27,7 @@ branches:
track-merge-target: true
tracks-release-branches: true
is-release-branch: false
is-source-branch-for:
- master
source-branches: [ 'feature' ]

feature:
mode: ContinuousDeployment
Expand All @@ -35,8 +36,6 @@ branches:
increment: Minor
prevent-increment-of-merged-branch-version: false
track-merge-target: false
is-source-branch-for:
- develop

pull-request:
regex: (pull|pull\-request|pr)[/-]
Expand Down
99 changes: 99 additions & 0 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
trigger:
branches:
include:
- master
- develop
- release/*

tags:
include:
- 'v*'

pr:
branches:
include:
- master

pool:
vmImage: ubuntu-latest

variables:
buildConfiguration: 'Release'

steps:
- task: gitversion/setup@0
displayName: 'Install GitVersion'

inputs:
versionSpec: 5.x

- task: GitVersion/execute@0
displayName: 'Determine build version'
name: GitVersion

- script: |
BUILD_SEMVER="$(GitVersion.FullSemVer)"
echo "##vso[task.setvariable variable=BUILD_SEMVER]$BUILD_SEMVER"
echo "BUILD_SEMVER='$BUILD_SEMVER'"
displayName: Set environment variables from build version

- task: DotNetCoreCLI@2
displayName: 'Restore packages'

inputs:
command: 'restore'
projects: '$(Build.SourcesDirectory)/KubeClient.sln'
restoreArguments: '/p:VersionPrefix="$(GitVersion.MajorMinorPatch)" /p:VersionSuffix="$(GitVersion.PreReleaseTag)"'

- task: DotNetCoreCLI@2
displayName: 'Build solution'

inputs:
command: 'build'
projects: '$(Build.SourcesDirectory)/KubeClient.sln'
arguments: '--configuration "$(buildConfiguration)" /p:VersionPrefix="$(GitVersion.MajorMinorPatch)" /p:VersionSuffix="$(GitVersion.PreReleaseTag)"'

- task: DotNetCoreCLI@2
displayName: 'Run tests'

inputs:
command: 'test'
projects: '$(Build.SourcesDirectory)/test/KubeClient.*.Tests/*.csproj'
arguments: '--configuration "$(buildConfiguration)" /p:VersionPrefix="$(GitVersion.MajorMinorPatch)" /p:VersionSuffix="$(GitVersion.PreReleaseTag)"'

- task: DotNetCoreCLI@2
displayName: 'Create packages'

inputs:
command: 'pack'
projects: '$(Build.SourcesDirectory)/KubeClient.sln'

arguments: '--configuration "$(buildConfiguration)"'
outputDir: '$(Build.ArtifactStagingDirectory)/packages'
versioningScheme: byEnvVar
versionEnvVar: BUILD_SEMVER

- task: PublishBuildArtifacts@1
displayName: 'Publish packages as artifact'

inputs:
PathtoPublish: '$(Build.ArtifactStagingDirectory)/packages'
ArtifactName: 'packages'
publishLocation: 'Container'

- task: GitHubRelease@1
displayName: 'Create GitHub release from tag'

condition: contains(variables['Build.SourceBranch'], 'refs/tags/v')

inputs:
gitHubConnection: 'tintoy'
repositoryName: '$(Build.Repository.Name)'
action: 'create'
target: '$(Build.SourceVersion)'
tagSource: 'gitTag'
tagPattern: '^v\d+\.\d+.\d+(-[A-Za-z0-9%\.]+)?$'
addChangeLog: true
assets: '$(Build.ArtifactStagingDirectory)/packages/*.nupkg'
assetUploadMode: replace
Original file line number Diff line number Diff line change
Expand Up @@ -154,41 +154,6 @@ public static IServiceCollection AddKubeClient(this IServiceCollection services,
return services;
}

/// <summary>
/// Add named <see cref="KubeClientOptions"/> to the service collection.
/// </summary>
/// <param name="services">
/// The service collection to configure.
/// </param>
/// <param name="name">
/// A name used to resolve the options.
/// </param>
/// <param name="configure">
/// A delegate that performs required configuration of the <see cref="KubeClientOptions"/>.
/// </param>
/// <returns>
/// The configured service collection.
/// </returns>
public static IServiceCollection AddKubeClientOptions(this IServiceCollection services, string name, Action<KubeClientOptions> configure)
{
if (services == null)
throw new ArgumentNullException(nameof(services));

if (String.IsNullOrWhiteSpace(name))
throw new ArgumentException("Argument cannot be null, empty, or entirely composed of whitespace: 'name'.", nameof(name));

if (configure == null)
throw new ArgumentNullException(nameof(configure));

services.Configure<KubeClientOptions>(name, options =>
{
configure(options);
options.EnsureValid();
});

return services;
}

/// <summary>
/// Add support for named Kubernetes client instances.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,70 @@ namespace KubeClient
/// </summary>
public static class KubeClientOptionsRegistrationExtensions
{
/// <summary>
/// Add <see cref="KubeClientOptions"/> to the service collection.
/// </summary>
/// <param name="services">
/// The service collection to configure.
/// </param>
/// <param name="configure">
/// A delegate that performs required configuration of the <see cref="KubeClientOptions"/>.
/// </param>
/// <returns>
/// The configured service collection.
/// </returns>
public static IServiceCollection AddKubeClientOptions(this IServiceCollection services, Action<KubeClientOptions> configure)
{
if (services == null)
throw new ArgumentNullException(nameof(services));

if (configure == null)
throw new ArgumentNullException(nameof(configure));

services.Configure<KubeClientOptions>(options =>
{
configure(options);
options.EnsureValid();
});

return services;
}

/// <summary>
/// Add named <see cref="KubeClientOptions"/> to the service collection.
/// </summary>
/// <param name="services">
/// The service collection to configure.
/// </param>
/// <param name="name">
/// A name used to resolve the options.
/// </param>
/// <param name="configure">
/// A delegate that performs required configuration of the <see cref="KubeClientOptions"/>.
/// </param>
/// <returns>
/// The configured service collection.
/// </returns>
public static IServiceCollection AddKubeClientOptions(this IServiceCollection services, string name, Action<KubeClientOptions> configure)
{
if (services == null)
throw new ArgumentNullException(nameof(services));

if (String.IsNullOrWhiteSpace(name))
throw new ArgumentException("Argument cannot be null, empty, or entirely composed of whitespace: 'name'.", nameof(name));

if (configure == null)
throw new ArgumentNullException(nameof(configure));

services.Configure<KubeClientOptions>(name, options =>
{
configure(options);
options.EnsureValid();
});

return services;
}

/// <summary>
/// Add <see cref="KubeClientOptions"/> from local Kubernetes client configuration.
/// </summary>
Expand Down Expand Up @@ -210,5 +274,59 @@ public static IServiceCollection AddKubeClientOptionsFromKubeConfig(this IServic

return services;
}

/// <summary>
/// Add <see cref="KubeClientOptions"/> from the pod service account.
/// </summary>
/// <param name="services">
/// The service collection to configure.
/// </param>
/// <returns>
/// The configured service collection.
/// </returns>
public static IServiceCollection AddKubeClientOptionsFromPodServiceAccount(this IServiceCollection services)
{
if (services == null)
throw new ArgumentNullException(nameof(services));

services.AddKubeClientOptions(kubeClientOptions =>
{
KubeClientOptions fromPodServiceAccount = KubeClientOptions.FromPodServiceAccount();

fromPodServiceAccount.CopyTo(kubeClientOptions);
});

return services;
}

/// <summary>
/// Add named <see cref="KubeClientOptions"/> from the pod service account.
/// </summary>
/// <param name="services">
/// The service collection to configure.
/// </param>
/// <param name="name">
/// The name used to resolve these options.
/// </param>
/// <returns>
/// The configured service collection.
/// </returns>
public static IServiceCollection AddKubeClientOptionsFromPodServiceAccount(this IServiceCollection services, string name)
{
if (services == null)
throw new ArgumentNullException(nameof(services));

if (String.IsNullOrWhiteSpace(name))
throw new ArgumentException($"Argument cannot be null, empty, or entirely composed of whitespace: {nameof(name)}.", nameof(name));

services.AddKubeClientOptions(name, kubeClientOptions =>
{
KubeClientOptions fromPodServiceAccount = KubeClientOptions.FromPodServiceAccount();

fromPodServiceAccount.CopyTo(kubeClientOptions);
});

return services;
}
}
}
63 changes: 39 additions & 24 deletions src/KubeClient/KubeClientOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,36 +134,51 @@ public KubeClientOptions(string apiEndPoint)
public Dictionary<string, string> EnvironmentVariables { get; set; }

/// <summary>
/// Create a copy of the <see cref="KubeClientOptions"/>.
/// Create a copy of the <see cref="KubeClientOptions"/>.
/// </summary>
/// <returns>The new <see cref="KubeClientOptions"/>.</returns>
/// <returns>
/// The new <see cref="KubeClientOptions"/>.
/// </returns>
public KubeClientOptions Clone()
{
var clonedOptions = new KubeClientOptions
{
AccessToken = AccessToken,
AccessTokenCommand = AccessTokenCommand,
AccessTokenCommandArguments = AccessTokenCommandArguments,
AccessTokenExpirySelector = AccessTokenExpirySelector,
AccessTokenSelector = AccessTokenSelector,
AllowInsecure = AllowInsecure,
ApiEndPoint = ApiEndPoint,
AuthStrategy = AuthStrategy,
CertificationAuthorityCertificate = CertificationAuthorityCertificate,
ClientCertificate = ClientCertificate,
InitialAccessToken = InitialAccessToken,
InitialTokenExpiryUtc = InitialTokenExpiryUtc,
KubeNamespace = KubeNamespace,
LoggerFactory = LoggerFactory,
LogHeaders = LogHeaders,
LogPayloads = LogPayloads,
EnvironmentVariables = EnvironmentVariables
};
clonedOptions.ModelTypeAssemblies.AddRange(ModelTypeAssemblies);

var clonedOptions = new KubeClientOptions();

CopyTo(clonedOptions);

return clonedOptions;
}

/// <summary>
/// Copy all properties from the <see cref="KubeClientOptions"/> to other <see cref="KubeClientOptions"/>.
/// </summary>
/// <param name="toOptions">
/// The target <see cref="KubeClientOptions"/>.
/// </param>
public void CopyTo(KubeClientOptions toOptions)
{
if (toOptions == null)
throw new ArgumentNullException(nameof(toOptions));

toOptions.AccessToken = AccessToken;
toOptions.AccessTokenCommand = AccessTokenCommand;
toOptions.AccessTokenCommandArguments = AccessTokenCommandArguments;
toOptions.AccessTokenExpirySelector = AccessTokenExpirySelector;
toOptions.AccessTokenSelector = AccessTokenSelector;
toOptions.AllowInsecure = AllowInsecure;
toOptions.ApiEndPoint = ApiEndPoint;
toOptions.AuthStrategy = AuthStrategy;
toOptions.CertificationAuthorityCertificate = CertificationAuthorityCertificate;
toOptions.ClientCertificate = ClientCertificate;
toOptions.InitialAccessToken = InitialAccessToken;
toOptions.InitialTokenExpiryUtc = InitialTokenExpiryUtc;
toOptions.KubeNamespace = KubeNamespace;
toOptions.LoggerFactory = LoggerFactory;
toOptions.LogHeaders = LogHeaders;
toOptions.LogPayloads = LogPayloads;
toOptions.EnvironmentVariables = EnvironmentVariables;
toOptions.ModelTypeAssemblies.AddRange(ModelTypeAssemblies);
}

/// <summary>
/// Ensure that the <see cref="KubeClientOptions"/> are valid.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion test/KubeClient.Extensions.WebSockets.Tests/TestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ protected TestBase(ITestOutputHelper testOutput)
Assert.True(CurrentTest != null, "Cannot retrieve current test from ITestOutputHelper.");

Disposal.Add(
Log.BeginScope("CurrentTest", CurrentTest.DisplayName)
Log.BeginScope("CurrentTest {CurrentTest}", CurrentTest.DisplayName)
);
}

Expand Down

0 comments on commit 159057c

Please sign in to comment.