dotnet-kube-client icon indicating copy to clipboard operation
dotnet-kube-client copied to clipboard

reload on configmap change not working

Open msmaverick2018 opened this issue 6 years ago • 7 comments

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

msmaverick2018 avatar May 20 '19 14:05 msmaverick2018

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).

tintoy avatar May 20 '19 21:05 tintoy

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).

tintoy avatar May 20 '19 21:05 tintoy

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>();
            });
}

tintoy avatar May 20 '19 22:05 tintoy

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.

msmaverick2018 avatar May 21 '19 21:05 msmaverick2018

Just out of curiosity, if you try the example code I posted above, does that work for you?

tintoy avatar May 21 '19 22:05 tintoy

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

https://github.com/tintoy/dotnet-kube-client/blob/05e7bfe63d5c5648431cf008d2a5c4e6b4fe85ab/src/KubeClient/ResourceClients/KubeResourceClient.cs#L501

jkdey avatar Nov 11 '19 23:11 jkdey

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.

tintoy avatar Nov 12 '19 00:11 tintoy