All Projects → fbeltrao → ConfigMapFileProvider

fbeltrao / ConfigMapFileProvider

Licence: MIT license
.NET Core configuration based on Kubernetes config maps with auto reload support

Programming Languages

C#
18002 projects
Dockerfile
14818 projects

Projects that are alternatives of or similar to ConfigMapFileProvider

authelia
Instructions and configuration files to deploy Authelia in Unraid OS using Docker + FreeIPA LDAP.
Stars: ✭ 116 (+157.78%)
Mutual labels:  configuration-files
config-file-provider-plugin
Jenkins plugin to administrate the maven settings (settings.xml).
Stars: ✭ 43 (-4.44%)
Mutual labels:  configuration-files
climatecontrol
Python library for loading settings and config data from files and environment variables
Stars: ✭ 20 (-55.56%)
Mutual labels:  configuration-files
vacuum
Vacuum is a system-wide configuration file collector
Stars: ✭ 25 (-44.44%)
Mutual labels:  configuration-files
covidtrackerapiwrapper
CovidSharp is a crossplatform C# API wrapper for the Coronavirus tracking API (https://github.com/ExpDev07/coronavirus-tracker-api)
Stars: ✭ 11 (-75.56%)
Mutual labels:  csharp-code
LiteGui
Immediate Mode GUI From Scratch
Stars: ✭ 15 (-66.67%)
Mutual labels:  csharp-code
dotfiles
📦 Configuration files for my Arch/KDE/i3-gaps system
Stars: ✭ 41 (-8.89%)
Mutual labels:  configuration-files
Pokedex
Pokedex is a robust Discord bot that mimics the iconic Pokedex from the Pokemon games and show. It's loaded with features to help players of all skill levels to learn and better enjoy Pokemon! The goal of Pokedex is to provide users with as much data about the Pokemon games as they desire conveniently and with minimal effort.
Stars: ✭ 18 (-60%)
Mutual labels:  configuration-files
kerrigan
基于Tornado实现的一套配置中心,可基于分项目、环境管理配置,语法高亮、对比历史版本、快速回滚等,并提供Restful风格的API
Stars: ✭ 57 (+26.67%)
Mutual labels:  configuration-files
AnyDiff
A CSharp (C#) diff library that allows you to diff two objects and get a list of the differences back.
Stars: ✭ 80 (+77.78%)
Mutual labels:  csharp-code
plasma-dotfiles
In this repository I intend to keep configuration files that I deem important, in addition to the theme customizations that I make to have a consistent working environment.
Stars: ✭ 58 (+28.89%)
Mutual labels:  configuration-files
ETWNetMonv3
ETWNetMonv3 is simple C# code for Monitoring TCP Network Connection via ETW & ETWProcessMon/2 is for Monitoring Process/Thread/Memory/Imageloads/TCPIP via ETW + Detection for Remote-Thread-Injection & Payload Detection by VirtualMemAlloc Events (in-memory) etc.
Stars: ✭ 32 (-28.89%)
Mutual labels:  csharp-code
initMyEnv
This repo is to quickly initialize the environment for Mac OSX and Linux, including some useful configurations and bash scripts.
Stars: ✭ 14 (-68.89%)
Mutual labels:  configuration-files
Cryptography
Some simple cryptographic examples on C# 6.0
Stars: ✭ 14 (-68.89%)
Mutual labels:  csharp-code
Virtual-Host
Modified Nuclei Templates Version to FUZZ Host Header
Stars: ✭ 38 (-15.56%)
Mutual labels:  configuration-files
csharp
📚 Recursos para aprender C#
Stars: ✭ 37 (-17.78%)
Mutual labels:  csharp-code
i3
Archivos de configuraciones de i3
Stars: ✭ 32 (-28.89%)
Mutual labels:  configuration-files
Unity3D-ReactiveScriptables
ScriptableObject based framework / scaffolding that facilitates loosely coupled communication and automatic update propagation between MonoBehaviour components.
Stars: ✭ 25 (-44.44%)
Mutual labels:  csharp-code
setup
My OS X development setup... dotfiles, config, preferences, apps. Everything 🐙
Stars: ✭ 25 (-44.44%)
Mutual labels:  configuration-files
swiss-army
Ansible-driven configuration management for maintaining a preferred environment (base system and app dotfiles / configurations)
Stars: ✭ 44 (-2.22%)
Mutual labels:  configuration-files

.NET Configuration in Kubernetes config maps with auto reload

Log level configuration in config map

Kubernetes config maps allows the injection of configuration into an application. The contents of a config map can be injected as environment variables or mounted files.

For instance, imagine you want to configure the log level in a separated file that will be mounted into your application.

The following config map limits the verbosity to errors:

apiVersion: v1
kind: ConfigMap
metadata:
  name: demo-config
data:
  appsettings.json: |-
    {
      "Logging": {
        "LogLevel": {
          "Default": "Error",
          "System": "Error",
          "Microsoft": "Error"
        }
      }
    }

The file below deploys an application, mounting the contents of the config map into the /app/config folder.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: demo-deployment
  labels:
    app: config-demo-app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: config-demo-app
  template:
    metadata:
      labels:
        app: config-demo-app
    spec:
      containers:
      - name: configmapfileprovidersample
        image: fbeltrao/configmapfileprovidersample:1.0
        ports:
        - containerPort: 80
        volumeMounts:
        - name: config-volume
          mountPath: /app/config
      volumes:
      - name: config-volume
        configMap:
          name: demo-config

In order to read configurations from the provided path (config/appsettings.json) the following code changes are required:

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
    WebHost.CreateDefaultBuilder(args)
        .ConfigureAppConfiguration(c =>
        {
           c.AddJsonFile("config/appsettings.json", optional: true, reloadOnChange: true);
        })
        .UseStartup<Startup>();

Deploy the application:

kubectl apply -f configmap.yaml
kubectl apply -f deployment.yaml

We can peek into the running pod in Kubernetes, looking at the files stored in the container:

kubectl exec -it <pod-name> -- bash
root@demo-deployment-844f6c6546-x786b:/app# cd config/
root@demo-deployment-844f6c6546-x786b:/app/config# ls -la

rwxrwxrwx 3 root root 4096 Sep 14 09:01 .
drwxr-xr-x 1 root root 4096 Sep 14 08:47 ..
drwxr-xr-x 2 root root 4096 Sep 14 09:01 ..2019_09_14_09_01_16.386067924
lrwxrwxrwx 1 root root   31 Sep 14 09:01 ..data -> ..2019_09_14_09_01_16.386067924
lrwxrwxrwx 1 root root   53 Sep 14 08:47 appsettings.json -> ..data/appsettings.json

As you can see, the config map content is mounted using a symlink.

Let's change the log verbosity to debug, making the following changes to the config map:

apiVersion: v1
kind: ConfigMap
metadata:
  name: demo-config
data:
  appsettings.json: |-
    {
      "Logging": {
        "LogLevel": {
          "Default": "Debug",
          "System": "Error",
          "Microsoft": "Error"
        }
      }
    }

and redeploying it

kubectl apply -f configmap.yaml

Eventually the changes will be applied to the mounted file inside the container, as you can see below:

root@demo-deployment-844f6c6546-gzc6j:/app/config# ls -la
total 12
drwxrwxrwx 3 root root 4096 Sep 14 09:05 .
drwxr-xr-x 1 root root 4096 Sep 14 08:47 ..
drwxr-xr-x 2 root root 4096 Sep 14 09:05 ..2019_09_14_09_05_02.797339427
lrwxrwxrwx 1 root root   31 Sep 14 09:05 ..data -> ..2019_09_14_09_05_02.797339427
lrwxrwxrwx 1 root root   53 Sep 14 08:47 appsettings.json -> ..data/appsettings.json

Notice that the appsettings.json last modified date does not change, only the referenced file actually gets updated.

Unfortunately, the build-in reload on changes in .NET core file provider does not work. The config map does not trigger the configuration reload as one would expect.

Based on my investigation, it seems that the .NET core change discovery relies on the file last modified date. Since the file we are monitoring did not change (the symlink reference did), no changes are detected.

Working on a solution

This problem is tracked here. Until a fix is available we can take advantage of the extensible configuration system in .NET Core and implement a file based configuration provider that detect changes based on file contents.

The setup looks like this:

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
    WebHost.CreateDefaultBuilder(args)
        .ConfigureAppConfiguration(c =>
        {
            c.AddJsonFile(ConfigMapFileProvider.FromRelativePath("config"), 
                "appsettings.json", 
                optional: true, 
                reloadOnChange: true);
        })
        .UseStartup<Startup>();

The provided implementation detect changes based on the hash of the content. Check the sample project files for more details.

Disclaimer: this is a quick implementation, not tested in different environments/configurations. Use at your own risk.

Testing the sample application

Clone this repository then deploy the application:

kubectl apply -f configmap.yaml
kubectl apply -f deployment.yaml

In a separated console window stream the container log:

kubectl logs -l app=config-demo-app -f

Open a tunnel to the application with kubectl port-forward

 kubectl port-forward <pod-name> 60000:80

Verify that the log is in error level, by opening a browser and navigating to http://localhost:60000/api/values. Look at the pod logs. You should see the following lines:

fail: ConfigMapFileProviderSample.Controllers.ValuesController[0]
      ERR log
crit: ConfigMapFileProviderSample.Controllers.ValuesController[0]
      CRI log

Change the config map: Replace "Default": "Error" to "Default": "Debug" in the configmap.yaml file, then redeploy the config map.

kubectl apply -f configmap.yaml

Verify that the log level changes to Debug (it can take a couple of minutes until the file change is detected) by issuing new requests to http://localhost:60000/api/values. The logs will change to this:

dbug: ConfigMapFileProviderSample.Controllers.ValuesController[0]
      DBG log
info: ConfigMapFileProviderSample.Controllers.ValuesController[0]
      INF log
warn: ConfigMapFileProviderSample.Controllers.ValuesController[0]
      WRN log
fail: ConfigMapFileProviderSample.Controllers.ValuesController[0]
      ERR log
crit: ConfigMapFileProviderSample.Controllers.ValuesController[0]
      CRI log
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].