Watch Kubernetes events using C# library - c#

This is my first time playing with K8s api/client. I am trying to watch for events in all namespaces.
Below is the method, on running it prints:
Exception : Object reference not set to an instance of an object.
Closed
There aren't enough docs on how to use the C# client library. Can someone help me in understanding what I am doing wrong?
This is the method:
public async Task Temp() {
var config = KubernetesClientConfiguration.BuildConfigFromConfigFile();
IKubernetes client = new Kubernetes(config);
var resourceVersion = client.ListEventForAllNamespaces().ResourceVersion();
var path = $"api/v1/events";
// wait for events
while (true)
{
var eventsWatcher = await client.WatchObjectAsync<V1EventList>(
timeoutSeconds: int.MaxValue,
path: path,
resourceVersion: resourceVersion,
onEvent: new Action<WatchEventType, V1EventList>((x, y) =>
{
Console.WriteLine(x.ToString());
}),
onClosed: new Action(() =>
{
Console.WriteLine("Closed");
}),
onError: new Action<Exception>((e) =>
{
Console.WriteLine($"Exception : {e.Message}");
}));
Thread.Sleep(1000);
}
}
On a side note, I am able to watch locally using http://localhost:8080/api/v1/events?watch=1&resourceVersion=27958468 after kubectl proxy

Found the issue. I was using the wrong "path". Instead of api/v1/events it should be api/v1/watch/events. The NullReferenceException was happening within the library's watcher.cs line 149

Related

How can I catch an ExtendedSocketException?

This may sound stupid, but I just can't catch an ExtendedSocketException that is getting thrown by SocketTaskExtensions.ConnectAsync().
The full namespace is: System.Net.Internals.SocketExceptionFactory.ExtendedSocketException
The compiler complains with Cannot resolve symbol 'ExtendedSocketException'. Both my class library and test projects are targeting .Net Core 2.1.
Also there is no such reference or Nuget package that could be added. At least I coudn't find anything... Also there seems nothing on https://learn.microsoft.com...
What am I doing wrong here?
[Fact]
[Trait("Category", "UnitTest")]
public async Task Should_Throw_Exception_If_Port_Unreachable()
{
// Arrange
var client = new TcpConnector();
var nonListeningPort = 81;
var endpoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), nonListeningPort);
// Act & Assert
var connectTask = client.ConnectAsync(endpoint);
Func<Task> func = async () => { await connectTask; };
func.Should().Throw<ExtendedSocketException>();
}
ExtendedSocketException is a private class so you can't reference it outside of .NET internals. You can catch SocketException however and inspect the exception message.
Even though the ExtendedSocketException is meant to be private, it's definitely being exposed:
On the other hand, it inherits from SocketException, so we shouldn't need to care about it too much.
In terms of your test, I assume that you should be able to modify your assertion to something similar to:
var exception = Assert.Catch(func);
Assert.IsInstanceOf<SocketException>(exception);
Assert.That(exception.Message.Contains("actively refused"));
ExtendedSocketException is derived from SocketException (for .NET Core). This can still be handled by using a catch block catching SocketException. We are able to confirm this through System.Type.IsAssignableFrom(Type c):
catch (Exception e)
{
Debug.Write(typeof(SocketException).IsAssignableFrom(e.GetType()));
}
In xUnit, the assertion can be:
var client = new TcpClient();
Task result() => client.ConnectAsync(IPAddress.Loopback, 23000);
var ex = Record.ExceptionAsync(async () => await client.ConnectAsync(IPAddress.Loopback, 23000));
Assert.IsAssignableFrom<SocketException>(ex.Result);

Google Directory API - How to get the resulting deserialized objects of a BatchRequest

i am trying to speed up some google directory api calls in the .net client library with BatchRequests
lets say i have the following batchRequest (which consists only of one
request for simplicity):
static async Task BatchRequesting()
{
var batchReq = new BatchRequest(_dirservices[0]);
var r = _dirservices[0].Users.Get("user#domain.com");
batchReq.Queue<UsersResource.GetRequest>(r,
(contentReq, error, j, message) =>
{
... what to do here?
});
await batchReq.ExecuteAsync();
}
how do i get the resulting deserialized response object in the callback (which would be a User object in my case)
Do i have to handle the message.Content object (HttpContent) myself with all the json deserializing?
I found the solution. I used the wrong generic parameter. My Code example has to be like this:
static async Task BatchRequesting()
{
var batchReq = new BatchRequest(_directoryService);
var request = _directoryService.Users.Get("user#domain.com");
batchReq.Queue<User>(request,
(returnedUser, error, j, message) =>
{
if (error != null)
{
Console.WriteLine(error.Message);
}
else
{
... work with returnedUser
}
});
await batchReq.ExecuteAsync();
}

ReactiveUI: Why do I have to specify the scheduler explicitly in "...Throttle..."when using the TestScheduler

I'm new to ReactiveUI. I have the following simple setup: a path to a csv can be specified and the containing datapoints will be displayed to the user (using oxyplot).
Now I'm trying to test the following subscription:
public GraphViewModel(IScreen hostScreen)
{
HostScreen = hostScreen;
setupGraphFormatting();
// Data Loading if path is valid
this.WhenAnyValue(viewModel => viewModel.PathToDataCsv)
.ObserveOn(RxApp.MainThreadScheduler)
.Throttle(TimeSpan.FromMilliseconds(500), RxApp.TaskpoolScheduler)
.Select(csvPath => csvPath?.Trim('"'))
.Where(csvPath => !string.IsNullOrEmpty(csvPath) && File.Exists(csvPath))
.Subscribe(csvPath =>
{
csvPath = csvPath?.Trim('"');
updatePlotModel(csvPath);
}, exception => {});
/* additional Code*/
}
And that's the corresponding UnitTest:
[Test]
public void If_PathToDataCsv_has_a_valid_value()
{
new TestScheduler().With(scheduler =>
{
string pathToValidCsvFile = "data.log";
var viewModel = new GraphViewModel(null);
scheduler.AdvanceByMs(1000);
viewModel.PathToDataCsv = pathToValidCsvFile;
scheduler.AdvanceByMs(1000);
viewModel.PlotModel.Series.Count.Should().Be(6);
});
}
My first implementation of WhenAnyValue didn't set any of the Schedulers specifically ( in Throttle and lacking any ObserverOn ):
public GraphViewModel(IScreen hostScreen)
{
HostScreen = hostScreen;
setupGraphFormatting();
// Data Loading if path is valid
this.WhenAnyValue(viewModel => viewModel.PathToDataCsv)
.Throttle(TimeSpan.FromMilliseconds(500))
.Select(csvPath => csvPath?.Trim('"'))
.Where(csvPath => !string.IsNullOrEmpty(csvPath) && File.Exists(csvPath))
.Subscribe(csvPath =>
{
csvPath = csvPath?.Trim('"');
updatePlotModel(csvPath);
}, exception => {});
/* additional Code*/
}
But then my Unittest failed. My assumption was that TestScheduler was being used for Throttle behind the scenes and I didn't have to do anything. Am I doing something wrong or is this the right way: If I want to use TestScheduler/TimeTravelâ„¢ I have to specify the schedulers the way I did?
Edit in response to Glenn Watsons answer:
Ok, now it's clear: The methods in question (Throttle, ObserverOn) of course do not use ReactiveUI's Schedulers, because these are methods from the Reactive Extensions Framework. So they can't be replaced implicitly by ReactiveUI in case of a UnitTest except I tell the methods to use the RxApp Schedulers...
RxApp provides the ThreadPoolScheduler when you are in release mode, and the testing scheduler when you are in unit test mode.
By default the reactive extensions (separate to ReactiveUI) will use their own default schedulers which are unaware of unit tests.

Set Service Start Parameter using Topshelf

I have a service with multiple instances with different parameters for each instance, at the moment I'm setting these parameters manually (in another code to be exact) to Image Path of the service in Registry (e.g. HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\MyService$i00). so our service installation is done in two steps.
I'm really interested to merge these steps in Topshelf installation for example like
MyService.exe install -instance "i00" -config "C:\i00Config.json"
First Try
I tried AddCommandLineDefinition from TopShelf but it seems it only works during installation and running through console not the service itself (will not add anything to service Image Path).
Second Try
I tried to see if its possible to do this with AfterInstall from Topshelf without any luck. here is a test code to see if it going to work or not (but unfortunately Topshelf overwrites the registry after AfterInstall call).
HostFactory.Run(x =>
{
x.UseNLog();
x.Service<MyService>(sc =>
{
sc.ConstructUsing(hs => new MyService(hs));
sc.WhenStarted((s, h) => s.Start(h));
sc.WhenStopped((s, h) => s.Stop(h));
});
x.AfterInstall(s =>
{
using (var system = Registry.LocalMachine.OpenSubKey("SYSTEM"))
using (var controlSet = system.OpenSubKey("CurrentControlSet"))
using (var services = controlSet.OpenSubKey("services"))
using (var service = services.OpenSubKey(string.IsNullOrEmpty(s.InstanceName)
? s.ServiceName
: s.ServiceName + "$" + s.InstanceName, true))
{
if (service == null)
return;
var imagePath = service.GetValue("ImagePath") as string;
if (string.IsNullOrEmpty(imagePath))
return;
var appendix = string.Format(" -{0} \"{1}\"", "config", "C:\i00config.json"); //only a test to see if it is possible at all or not
imagePath = imagePath + appendix;
service.SetValue("ImagePath", imagePath);
}
});
x.SetServiceName("MyService");
x.SetDisplayName("My Service");
x.SetDescription("My Service Sample");
x.StartAutomatically();
x.RunAsLocalSystem();
x.EnableServiceRecovery(r =>
{
r.OnCrashOnly();
r.RestartService(1); //first
r.RestartService(1); //second
r.RestartService(1); //subsequents
r.SetResetPeriod(0);
});
});
I couldn't find any relevant information about how it can be done using TopShelf so the question is, is it possible to do this with TopShelf?
Ok, as Travis mentioned, It seems there is no built-in feature or simple workaround for this problem. so I wrote a little extension for Topshelf based on a Custom Environment Builder (most of the code is borrowed form Topshelf project itself).
I posted the code on Github, in case others may find it useful, here is the Topshelf.StartParameters extension.
based on the extension my code would be like:
HostFactory.Run(x =>
{
x.EnableStartParameters();
x.UseNLog();
x.Service<MyService>(sc =>
{
sc.ConstructUsing(hs => new MyService(hs));
sc.WhenStarted((s, h) => s.Start(h));
sc.WhenStopped((s, h) => s.Stop(h));
});
x.WithStartParameter("config",a =>{/*we can use parameter here*/});
x.SetServiceName("MyService");
x.SetDisplayName("My Service");
x.SetDescription("My Service Sample");
x.StartAutomatically();
x.RunAsLocalSystem();
x.EnableServiceRecovery(r =>
{
r.OnCrashOnly();
r.RestartService(1); //first
r.RestartService(1); //second
r.RestartService(1); //subsequents
r.SetResetPeriod(0);
});
});
and I can simply set it with:
MyService.exe install -instance "i00" -config "C:\i00Config.json"
To answer you question, no this isn't possible with Topshelf. I am excited you figured out how to manage the ImagePath. But that's the crux of the problem, there's been some discussion on the mailing list (https://groups.google.com/d/msg/topshelf-discuss/Xu4XR6wGWxw/8mAtyJFATq8J) on this topic and issues about it in the past.
The big problem is that managing expectations of behavior when applying custom arguments to the ImagePath will be unintuitive. For example, what happens when you call start with custom command line arguments? I'm open to implementing this or accepting a PR if we get something that doesn't confuse me just thinking about it, let alone trying to use. Right now, I strongly encourage you to use configuration, not command line arguments, to manage this, even if it means duplicating code on disk.
The following work-around is nothing more than a registry update. The update operation expects the privileges the installer requires in order to write our extended arguments.
Basically, we're responding to the AfterInstall() event. As of Topshelf v4.0.3, calling the AppendImageArgs() work-around from within the event will cause your args to appear before the TS args. If the call is deferred, your args will appear after the TS args.
The work-around
private static void AppendImageArgs(string serviceName, IEnumerable<Tuple<string, object>> args)
{
try
{
using (var service = Registry.LocalMachine.OpenSubKey($#"System\CurrentControlSet\Services\{serviceName}", true))
{
const string imagePath = "ImagePath";
var value = service?.GetValue(imagePath) as string;
if (value == null)
return;
foreach (var arg in args)
if (arg.Item2 == null)
value += $" -{arg.Item1}";
else
value += $" -{arg.Item1} \"{arg.Item2}\"";
service.SetValue(imagePath, value);
}
}
catch (Exception e)
{
Log.Error(e);
}
}
An example call
private static void AppendImageArgs(string serviceName)
{
var args = new[]
{
new Tuple<string, object>("param1", "Hello"),
new Tuple<string, object>("param2", 1),
new Tuple<string, object>("Color", ConsoleColor.Cyan),
};
AppendImageArgs(serviceName, args);
}
And the resulting args that would appear in the ImagePath:
-displayname "MyService Display Name" -servicename "MyServiceName" -param1 "Hello" -param2 "1" -Color "Cyan"
Notice the args appeared after the TS args, -displayname & -servicename. In this example, the AppendImageArgs() call was invoked after TS finished its installation business.
Command line args can be specified normally using Topshelf methods such as AddCommandLineDefinition(). To force processing of the args, call ApplyCommandLine().

WorkFlow Foundation 4 WorkflowApplication Completed not triggered

I have the following code
AutoResetEvent instanceUnloaded = new AutoResetEvent(false);
WFStepsActivity workflow = WorkflowFactory.Current.BuildWorkflow(workflowinstance, requestinstance.RequestInstanceID);
WorkflowApplication wf = new WorkflowApplication(workflow);
wf.InstanceStore = WFInstanceStore.GetInstanceStore();
WFStepsPersistenceParticipant persist = new WFStepsPersistenceParticipant();
wf.Extensions.Add(persist);
wf.PersistableIdle = (arg) =>
{
return PersistableIdleAction.Unload;
};
wf.Unloaded = (arg) =>
{
log.Info("WFName_" + wfsetting.Name + "_Unloaded and return.");
instanceUnloaded.Set();
};
wf.OnUnhandledException = (arg) =>
{
return UnhandledExceptionAction.Abort;
};
wf.Completed = (arg) =>
{
if (arg.CompletionState == ActivityInstanceState.Closed)
{
persist.WorklflowCompleted = true;
}
};
wf.Run();
instanceUnloaded.WaitOne();
return persist.WorklflowCompleted;
where the WFStepsActivity is just the Acitivity structure that we build from settings.
The workflow currently ends when a bookmark is created.
context.CreateBookmark(context.WorkflowInstanceId.ToString(), new System.Activities.BookmarkCallback(OnResumeBookmark));
The issue is after the bookmark is created the wf.Completed is never triggered.
Would like to check if the Complete is triggered if Bookmark is created.
As I know Complete event will be trigger if u Cancel, Terminate
Isn't it supposed that once the workflow stop of bookmarked, this event will be triggered?
Bookmarks induce the workflow to become Idle, meaning that you must purposefully resume at the bookmark to continue and complete the workflow. First things first: Grok bookmarks.
I'm not sure you have supplied enough code to allow anyone to properly diagnose the precise issue (I can't see your bookmark creation, nor your supporting classes etc).
It is also possible that your workflow is throwing an exception, the details of which are being swallowed. If you want to catch and log any unhandled exceptions I would ammend your code thusly:
Exception error = null;
wf.OnUnhandledException = (arg) =>
{
error = arg.UnhandledException;
return UnhandledExceptionAction.Abort;
};
// and then after you've waited for completion...
if (error != null) {
throw error; // or handle it another way
}
Also useful: Configuring Tracking for a Workflow

Categories