Open msmaverick2018 opened 5 years ago
Hi - does this sample work correctly for you?
https://github.com/tintoy/dotnet-kube-client/tree/develop/samples/ConfigFromConfigMap
I know it's not ASP.NET Core, but it'll at least tell me whether config-map reloads work for you at all (and we can go from there).
BTW, if you want to see changes to config after the app is started, you need to inject IOptionsMonitor<MyOptions>
not IOptions<MyOptions>
(that one has caught me out before).
BTW, here's the smallest example I could make:
public class Startup
{
public Startup(IConfiguration configuration) => Configuration = configuration;
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddOptions();
services.Configure<MyOptions>(Configuration);
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.Use(next => async context =>
{
MyOptions options = context.RequestServices.GetRequiredService<IOptionsMonitor<MyOptions>>().CurrentValue;
await context.Response.WriteAsync(
$"Foo: '{options.Foo}', Bar: '{options.Bar}'"
);
});
}
}
public class MyOptions
{
public string Foo { get; set; }
public string Bar { get; set; }
}
public static class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration(config =>
{
KubeClientOptions clientOptions = K8sConfig.Load().ToKubeClientOptions(
defaultKubeNamespace: "default"
);
config.AddKubeConfigMap(clientOptions, "my-config-map", reloadOnChange: true);
})
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
Thanks for responding, I have tried it an ASP.NET Core application using IOptionsMonitor but i am still not seeing the update. I have read your blog too. Following is the git repo for my attempt: C# Code: https://github.com/msmaverick2018/knative-repo/tree/master/AspnetCoreApp Kubernetes stuff: https://github.com/msmaverick2018/knative-repo/tree/master/kubernetes
Not sure if i am missing something obvious. Thanks for your help in advance.
Just out of curiosity, if you try the example code I posted above, does that work for you?
Running into a similar issue where reloadOnChange appears to stop working after a while. I think it may be related to the fact that watches timeout after a while. Does the client retry on watch expiration?
https://github.com/kubernetes/kubernetes/issues/42552 https://github.com/kubernetes-client/java/issues/266
No - I don’t think we attempt to retry. I guess we could implement that if I can figure out exactly what a timeout response looks like though.
I tried setting it up in a demo asp.net core app, the configuration does not reload when the config map changes. Do you have any working samples for ASP.NET Core Web App