-
Notifications
You must be signed in to change notification settings - Fork 305
Add support for informers #394
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
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
**/bin/ | ||
**/obj/ | ||
**/out/ | ||
**/layer/ | ||
**Dockerfile* | ||
*/*.md |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
|
||
namespace informers | ||
{ | ||
/// <summary> | ||
/// Starts all controllers registered in dependency injection container | ||
/// </summary> | ||
public class ControllerService : BackgroundService | ||
macsux marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
private readonly IServiceProvider _serviceProvider; | ||
|
||
public ControllerService(IServiceProvider serviceProvider) | ||
{ | ||
_serviceProvider = serviceProvider; | ||
} | ||
|
||
protected override async Task ExecuteAsync(CancellationToken stoppingToken) | ||
{ | ||
var controllers = _serviceProvider.GetServices<IController>(); | ||
await Task.WhenAll(controllers.Select(x => x.Initialize(stoppingToken))); | ||
} | ||
|
||
macsux marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reactive.Disposables; | ||
using System.Reactive.Linq; | ||
using System.Text; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using k8s; | ||
using k8s.Informers; | ||
using k8s.Informers.Notifications; | ||
using k8s.Models; | ||
using KellermanSoftware.CompareNetObjects; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace informers | ||
{ | ||
// this sample demos of informer in a basic controller context. | ||
// there are two loggers: | ||
// _informerLogger lets you see raw data coming out of informer stream | ||
|
||
// try creating and deleting some pods in "default" namespace and watch the output | ||
// current code is not production grade and lacks concurrency guards against modifying same resource | ||
public class DeltaChangesQueryingController : IController | ||
macsux marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
private readonly IKubernetesInformer<V1Pod> _podInformer; | ||
private readonly CompositeDisposable _subscription = new CompositeDisposable(); | ||
private readonly ILogger _informerLogger; | ||
private readonly CompareLogic _objectCompare = new CompareLogic(); | ||
|
||
public DeltaChangesQueryingController(IKubernetesInformer<V1Pod> podInformer, ILoggerFactory loggerFactory) | ||
{ | ||
_podInformer = podInformer; | ||
_informerLogger = loggerFactory.CreateLogger("Informer"); | ||
_objectCompare.Config.MaxDifferences = 100; | ||
} | ||
|
||
|
||
public Task Initialize(CancellationToken cancellationToken) | ||
{ | ||
_podInformer | ||
.GetResource(ResourceStreamType.ListWatch, KubernetesInformerOptions.Builder.NamespaceEquals("default").Build()) | ||
.Resync(TimeSpan.FromSeconds(10)) | ||
.Catch<ResourceEvent<V1Pod>, Exception>(e => | ||
{ | ||
_informerLogger.LogCritical(e, e.Message); | ||
return Observable.Throw<ResourceEvent<V1Pod>>(e); | ||
}) | ||
.Buffer(TimeSpan.FromSeconds(5)) | ||
.Where(x => x.Any()) | ||
.Do(x => | ||
{ | ||
var eventsPerResource = x.GroupBy(x => x.Value.Metadata.Name); | ||
foreach (var item in eventsPerResource) | ||
{ | ||
PrintChanges(item.ToList()); | ||
} | ||
}) | ||
.Subscribe() | ||
.DisposeWith(_subscription); | ||
return Task.CompletedTask; | ||
} | ||
|
||
private void PrintChanges(IList<ResourceEvent<V1Pod>> changes) | ||
{ | ||
// it's possible to do reconciliation here, but the current code is not production grade and lacks concurrency guards against modifying same resource | ||
var obj = changes.First().Value; | ||
var sb = new StringBuilder(); | ||
sb.AppendLine($"Received changes for object with ID {obj.Metadata.Name} with {changes.Count} items"); | ||
sb.AppendLine($"Last known state was {changes.Last().EventFlags}"); | ||
foreach (var item in changes) | ||
{ | ||
sb.AppendLine($"==={item.EventFlags}==="); | ||
sb.AppendLine($"Name: {item.Value.Metadata.Name}"); | ||
sb.AppendLine($"Version: {item.Value.Metadata.ResourceVersion}"); | ||
if (item.EventFlags.HasFlag(EventTypeFlags.Modify)) | ||
{ | ||
var updateDelta = _objectCompare.Compare(item.OldValue, item.Value); | ||
foreach (var difference in updateDelta.Differences) | ||
{ | ||
sb.AppendLine($"{difference.PropertyName}: {difference.Object1} -> {difference.Object2}"); | ||
} | ||
} | ||
} | ||
|
||
_informerLogger.LogInformation(sb.ToString()); | ||
|
||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace informers | ||
{ | ||
/// <summary> | ||
/// Base interface for implementing controllers | ||
/// </summary> | ||
public interface IController | ||
macsux marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
/// <summary> | ||
/// Signals that controller is done processing all the work and no more work will ever be processed. | ||
/// Mainly useful in testing | ||
/// </summary> | ||
public Task Initialize(CancellationToken cancellationToken); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using System; | ||
using System.Linq; | ||
using k8s; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace informers | ||
{ | ||
public class Program | ||
{ | ||
public static void Main(string[] args) | ||
{ | ||
CreateHostBuilder(args).Build().Run(); | ||
macsux marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
public static IHostBuilder CreateHostBuilder(string[] args) => | ||
Host.CreateDefaultBuilder(args) | ||
.ConfigureLogging(x => x.AddConsole()) | ||
.ConfigureServices((hostContext, services) => | ||
{ | ||
services.AddKubernetesClient(KubernetesClientConfiguration.BuildDefaultConfig); | ||
services.AddKubernetesInformers(); | ||
|
||
services.AddHostedService<ControllerService>(); | ||
services.AddSingleton<IController, DeltaChangesQueryingController>(); | ||
}); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"profiles": { | ||
"K8SControllerExample": { | ||
"commandName": "Project", | ||
"environmentVariables": { | ||
"DOTNET_ENVIRONMENT": "Development" | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft": "Warning", | ||
"Microsoft.Hosting.Lifetime": "Information" | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft": "Warning", | ||
"Microsoft.Hosting.Lifetime": "Information" | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Worker"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netcoreapp3.1</TargetFramework> | ||
<SignAssembly>true</SignAssembly> | ||
<AssemblyOriginatorKeyFile>..\..\src\KubernetesClient\kubernetes-client.snk</AssemblyOriginatorKeyFile> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="CompareNETObjects" Version="4.65.0" /> | ||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="3.1.2" /> | ||
<PackageReference Include="Microsoft.Extensions.Http" Version="3.1.2" /> | ||
<PackageReference Include="NMica" Version="2.0.2" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\KubernetesClient.DependencyInjection\KubernetesClient.DependencyInjection.csproj" /> | ||
</ItemGroup> | ||
</Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
src/KubernetesClient.DependencyInjection/KubernetesClient.DependencyInjection.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<Authors>The Kubernetes Project Authors</Authors> | ||
<Copyright>2017 The Kubernetes Project Authors</Copyright> | ||
<Description>Client library for the Kubernetes open source container orchestrator.</Description> | ||
|
||
<PackageLicenseUrl>https://www.apache.org/licenses/LICENSE-2.0</PackageLicenseUrl> | ||
<PackageProjectUrl>https://github.com/kubernetes-client/csharp</PackageProjectUrl> | ||
<PackageIconUrl>https://raw.githubusercontent.com/kubernetes/kubernetes/master/logo/logo.png</PackageIconUrl> | ||
<PackageTags>kubernetes;docker;containers;</PackageTags> | ||
<TargetFrameworks>netstandard2.0</TargetFrameworks> | ||
<RootNamespace>k8s</RootNamespace> | ||
<SignAssembly>true</SignAssembly> | ||
<AssemblyOriginatorKeyFile>..\KubernetesClient\kubernetes-client.snk</AssemblyOriginatorKeyFile> | ||
<GenerateDocumentationFile>true</GenerateDocumentationFile> | ||
<NoWarn>1701;1702;1591;1570;1572;1573;1574</NoWarn> | ||
|
||
<!-- Publish the repository URL in the built .nupkg (in the NuSpec <Repository> element) --> | ||
<PublishRepositoryUrl>true</PublishRepositoryUrl> | ||
|
||
<!-- Build symbol package (.snupkg) to distribute the PDB containing Source Link --> | ||
<IncludeSymbols>true</IncludeSymbols> | ||
<SymbolPackageFormat>snupkg</SymbolPackageFormat> | ||
<LangVersion>8</LangVersion> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Nerdbank.GitVersioning" Version="3.1.74" PrivateAssets="all" /> | ||
|
||
<ProjectReference Include="..\KubernetesClient.Informers\KubernetesClient.Informers.csproj" /> | ||
<PackageReference Include="Microsoft.Extensions.Http" Version="3.1.2" /> | ||
</ItemGroup> | ||
|
||
</Project> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.