tintoy / dotnet-kube-client

A Kubernetes API client for .NET Standard / .NET Core
MIT License
192 stars 33 forks source link

reload on configmap change not working #82

Open msmaverick2018 opened 5 years ago

msmaverick2018 commented 5 years ago

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

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

tintoy commented 5 years ago

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 commented 5 years ago

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>();
            });
}
msmaverick2018 commented 5 years ago

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.

tintoy commented 5 years ago

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

jkdey commented 5 years ago

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

tintoy commented 5 years ago

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.