Search code examples
asp.net-corekubernetesazure-aksasp.net-core-3.1configmap

configmap changes not picked up while deploying same image version in AKS(.NET)


I am using Azure DevOps to deploy the .NET 3.1 application to AKS. while deploying if there is only a config change and there is not really change in the code, we deploy the same image with only the config change but while doing it, even though the config map gets updated but the pods doesn't understand that there is a change and still reads from the previous config. I have to manually delete the pod and then AKS auto creates the pods and it picks the latest config.

From here https://medium.com/@fbeltrao/automatically-reload-configuration-changes-based-on-kubernetes-config-maps-in-a-net-d956f8c8399a and here https://github.com/dotnet/runtime/issues/36091 that there is an issue. I have followed the workaround https://github.com/dotnet/runtime/issues/36091#issuecomment-786931531 with symlink and it's not working with .NET core 3.1 or 6.0

.ConfigureAppConfiguration(c => c.AddSymLinkJsonFile("config/appsettings.json", optional: true, reloadOnChange: true));

namespace Microsoft.Extensions.Configuration
{
    internal static class JsonSymlinkConfigurationExtensions
    {
        internal static void AddSymLinkJsonFile(this IConfigurationBuilder c, string relativePath, bool optional, bool reloadOnChange)
        {
            var fileInfo = c.GetFileProvider().GetFileInfo(relativePath);

            if (TryGetSymLinkTarget(fileInfo.PhysicalPath, out string targetPath))
            {
                string targetDirectory = Path.GetDirectoryName(targetPath);

                if (TryGetSymLinkTarget(targetDirectory, out string symlinkDirectory))
                {
                    targetDirectory = symlinkDirectory;
                }

                c.AddJsonFile(new PhysicalFileProvider(targetDirectory), Path.GetFileName(targetPath), optional, reloadOnChange);
            }
            else
            {
                c.AddJsonFile(relativePath, optional, reloadOnChange);
            }
        }

        private static bool TryGetSymLinkTarget(string path, out string target, int maximumSymlinkDepth = 32)
        {
            target = null;

            int depth = 0;

            if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                var symbolicLinkInfo = new UnixSymbolicLinkInfo(path);
                while (symbolicLinkInfo.Exists && symbolicLinkInfo.IsSymbolicLink)
                {
                    target = symbolicLinkInfo.ContentsPath;

                    if (!Path.IsPathFullyQualified(target))
                    {
                        target = Path.GetFullPath(target, Path.GetDirectoryName(symbolicLinkInfo.FullName));
                    }

                    symbolicLinkInfo = new UnixSymbolicLinkInfo(target);
                    
                    if (depth++ > maximumSymlinkDepth)
                    {
                        throw new InvalidOperationException("Exceeded maximum symlink depth");
                    }
                }
            }
            return target != null;
        }
    }
}

Solution

  • I couldn't see any sample which is mentioned in the github link. Even after the upgrade to .NET6 I am facing the issue. so I have solved it to the kubernetes way. I have added restart command post deployment. so when the pods come back, it will come with the new config.

    kubectl rollout restart deployment ***service-deployment --namespace=${{ parameters.kubernetesNS }}