Skip to content

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
wants to merge 2 commits into from
Closed
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 .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
**/bin/
**/obj/
**/out/
**/layer/
**Dockerfile*
*/*.md
4 changes: 4 additions & 0 deletions .github/workflows/nuget.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,9 @@ jobs:
run: dotnet test
- name: Pack
run: dotnet pack --configuration Release src/KubernetesClient -o pkg
- name: Pack
run: dotnet pack --configuration Release src/KubernetesClient.Informers -o pkg
- name: Pack
run: dotnet pack --configuration Release src/KubernetesClient.DependencyInjection -o pkg
- name: Push
run: dotnet nuget push pkg\*.nupkg -s https://www.nuget.org/ -k ${{ secrets.nuget_api_key }}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
obj/
bin/
**/TestResults
pkg/**

# User-specific VS files
*.suo
Expand All @@ -13,3 +14,4 @@ bin/
# JetBrains Rider
.idea/
*.sln.iml
examples/informers/Dockerfile
29 changes: 29 additions & 0 deletions examples/informers/ControllerService.cs
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
{
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)));
}

}
}
90 changes: 90 additions & 0 deletions examples/informers/DeltaChangesQueryingController.cs
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
{
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());

}
}
}
17 changes: 17 additions & 0 deletions examples/informers/IController.cs
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
{
/// <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);
}
}
29 changes: 29 additions & 0 deletions examples/informers/Program.cs
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();
}

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>();
});
}
}
10 changes: 10 additions & 0 deletions examples/informers/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"profiles": {
"K8SControllerExample": {
"commandName": "Project",
"environmentVariables": {
"DOTNET_ENVIRONMENT": "Development"
}
}
}
}
9 changes: 9 additions & 0 deletions examples/informers/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}
9 changes: 9 additions & 0 deletions examples/informers/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}
19 changes: 19 additions & 0 deletions examples/informers/informers.csproj
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>
60 changes: 60 additions & 0 deletions kubernetes-client.sln
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "patch", "examples\patch\pat
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "httpClientFactory", "examples\httpClientFactory\httpClientFactory.csproj", "{A07314A0-02E8-4F36-B233-726D59D28F08}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "informers", "examples\informers\informers.csproj", "{A24E81F3-EFB1-4AFE-8C87-BDF2D3A0C0C2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "KubernetesClient.Informers", "src\KubernetesClient.Informers\KubernetesClient.Informers.csproj", "{7C1B2872-A0E3-4437-9532-0227F419399C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "KubernetesClient.Informers.Tests", "tests\KubernetesClient.Informers.Tests\KubernetesClient.Informers.Tests.csproj", "{5116C7E7-66A8-4523-AE5A-4CF00391C399}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "KubernetesClient.DependencyInjection", "src\KubernetesClient.DependencyInjection\KubernetesClient.DependencyInjection.csproj", "{5832A440-3DD9-47A4-8D3C-1FB049978297}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -189,6 +197,54 @@ Global
{A07314A0-02E8-4F36-B233-726D59D28F08}.Release|x64.Build.0 = Release|Any CPU
{A07314A0-02E8-4F36-B233-726D59D28F08}.Release|x86.ActiveCfg = Release|Any CPU
{A07314A0-02E8-4F36-B233-726D59D28F08}.Release|x86.Build.0 = Release|Any CPU
{A24E81F3-EFB1-4AFE-8C87-BDF2D3A0C0C2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A24E81F3-EFB1-4AFE-8C87-BDF2D3A0C0C2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A24E81F3-EFB1-4AFE-8C87-BDF2D3A0C0C2}.Debug|x64.ActiveCfg = Debug|Any CPU
{A24E81F3-EFB1-4AFE-8C87-BDF2D3A0C0C2}.Debug|x64.Build.0 = Debug|Any CPU
{A24E81F3-EFB1-4AFE-8C87-BDF2D3A0C0C2}.Debug|x86.ActiveCfg = Debug|Any CPU
{A24E81F3-EFB1-4AFE-8C87-BDF2D3A0C0C2}.Debug|x86.Build.0 = Debug|Any CPU
{A24E81F3-EFB1-4AFE-8C87-BDF2D3A0C0C2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A24E81F3-EFB1-4AFE-8C87-BDF2D3A0C0C2}.Release|Any CPU.Build.0 = Release|Any CPU
{A24E81F3-EFB1-4AFE-8C87-BDF2D3A0C0C2}.Release|x64.ActiveCfg = Release|Any CPU
{A24E81F3-EFB1-4AFE-8C87-BDF2D3A0C0C2}.Release|x64.Build.0 = Release|Any CPU
{A24E81F3-EFB1-4AFE-8C87-BDF2D3A0C0C2}.Release|x86.ActiveCfg = Release|Any CPU
{A24E81F3-EFB1-4AFE-8C87-BDF2D3A0C0C2}.Release|x86.Build.0 = Release|Any CPU
{7C1B2872-A0E3-4437-9532-0227F419399C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7C1B2872-A0E3-4437-9532-0227F419399C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7C1B2872-A0E3-4437-9532-0227F419399C}.Debug|x64.ActiveCfg = Debug|Any CPU
{7C1B2872-A0E3-4437-9532-0227F419399C}.Debug|x64.Build.0 = Debug|Any CPU
{7C1B2872-A0E3-4437-9532-0227F419399C}.Debug|x86.ActiveCfg = Debug|Any CPU
{7C1B2872-A0E3-4437-9532-0227F419399C}.Debug|x86.Build.0 = Debug|Any CPU
{7C1B2872-A0E3-4437-9532-0227F419399C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7C1B2872-A0E3-4437-9532-0227F419399C}.Release|Any CPU.Build.0 = Release|Any CPU
{7C1B2872-A0E3-4437-9532-0227F419399C}.Release|x64.ActiveCfg = Release|Any CPU
{7C1B2872-A0E3-4437-9532-0227F419399C}.Release|x64.Build.0 = Release|Any CPU
{7C1B2872-A0E3-4437-9532-0227F419399C}.Release|x86.ActiveCfg = Release|Any CPU
{7C1B2872-A0E3-4437-9532-0227F419399C}.Release|x86.Build.0 = Release|Any CPU
{5116C7E7-66A8-4523-AE5A-4CF00391C399}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5116C7E7-66A8-4523-AE5A-4CF00391C399}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5116C7E7-66A8-4523-AE5A-4CF00391C399}.Debug|x64.ActiveCfg = Debug|Any CPU
{5116C7E7-66A8-4523-AE5A-4CF00391C399}.Debug|x64.Build.0 = Debug|Any CPU
{5116C7E7-66A8-4523-AE5A-4CF00391C399}.Debug|x86.ActiveCfg = Debug|Any CPU
{5116C7E7-66A8-4523-AE5A-4CF00391C399}.Debug|x86.Build.0 = Debug|Any CPU
{5116C7E7-66A8-4523-AE5A-4CF00391C399}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5116C7E7-66A8-4523-AE5A-4CF00391C399}.Release|Any CPU.Build.0 = Release|Any CPU
{5116C7E7-66A8-4523-AE5A-4CF00391C399}.Release|x64.ActiveCfg = Release|Any CPU
{5116C7E7-66A8-4523-AE5A-4CF00391C399}.Release|x64.Build.0 = Release|Any CPU
{5116C7E7-66A8-4523-AE5A-4CF00391C399}.Release|x86.ActiveCfg = Release|Any CPU
{5116C7E7-66A8-4523-AE5A-4CF00391C399}.Release|x86.Build.0 = Release|Any CPU
{5832A440-3DD9-47A4-8D3C-1FB049978297}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5832A440-3DD9-47A4-8D3C-1FB049978297}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5832A440-3DD9-47A4-8D3C-1FB049978297}.Debug|x64.ActiveCfg = Debug|Any CPU
{5832A440-3DD9-47A4-8D3C-1FB049978297}.Debug|x64.Build.0 = Debug|Any CPU
{5832A440-3DD9-47A4-8D3C-1FB049978297}.Debug|x86.ActiveCfg = Debug|Any CPU
{5832A440-3DD9-47A4-8D3C-1FB049978297}.Debug|x86.Build.0 = Debug|Any CPU
{5832A440-3DD9-47A4-8D3C-1FB049978297}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5832A440-3DD9-47A4-8D3C-1FB049978297}.Release|Any CPU.Build.0 = Release|Any CPU
{5832A440-3DD9-47A4-8D3C-1FB049978297}.Release|x64.ActiveCfg = Release|Any CPU
{5832A440-3DD9-47A4-8D3C-1FB049978297}.Release|x64.Build.0 = Release|Any CPU
{5832A440-3DD9-47A4-8D3C-1FB049978297}.Release|x86.ActiveCfg = Release|Any CPU
{5832A440-3DD9-47A4-8D3C-1FB049978297}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -206,6 +262,10 @@ Global
{542DC30E-FDF7-4A35-B026-6C21F435E8B1} = {879F8787-C3BB-43F3-A92D-6D4C7D3A5285}
{04DE2C84-117D-4E21-8B45-B7AE627697BD} = {B70AFB57-57C9-46DC-84BE-11B7DDD34B40}
{A07314A0-02E8-4F36-B233-726D59D28F08} = {B70AFB57-57C9-46DC-84BE-11B7DDD34B40}
{A24E81F3-EFB1-4AFE-8C87-BDF2D3A0C0C2} = {B70AFB57-57C9-46DC-84BE-11B7DDD34B40}
{7C1B2872-A0E3-4437-9532-0227F419399C} = {3D1864AA-1FFC-4512-BB13-46055E410F73}
{5116C7E7-66A8-4523-AE5A-4CF00391C399} = {8AF4A5C2-F0CE-47D5-A4C5-FE4AB83CA509}
{5832A440-3DD9-47A4-8D3C-1FB049978297} = {3D1864AA-1FFC-4512-BB13-46055E410F73}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {049A763A-C891-4E8D-80CF-89DD3E22ADC7}
Expand Down
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>
Loading