Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Customizing CoreDNS ConfigMap with overrides and additional servers #4397

Merged
merged 1 commit into from
Nov 12, 2021

Conversation

iwilltry42
Copy link
Collaborator

@iwilltry42 iwilltry42 commented Nov 4, 2021

INFO: As per #4397 (comment), the PR was reduced to a single import of *.server overrides

Proposed Changes

Feature: Add CoreDNS Customization Options

Problem:
Before, to customize CoreDNS, one had to edit the default configmap,
which gets re-written on every K3s server restart.

Solution:
Mount an additional coredns-custom configmap into the CoreDNS container
and import overrides and additional server blocks from the included
files.

Types of Changes

New Feature (non-breaking change).

Verification

  • build K3s with new feature and use it e.g. via k3d
  • deploy a configmap coredns-custom with some modifications, e.g. an additional server:
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: coredns-custom
      namespace: kube-system
    data:
      example.server: |
        example.org {
          log
          whoami
        }
  • run some queries from within the cluster and check the response and coredns logs

Linked Issues

User-Facing Change

Allow optional customizations to CoreDNS via `coredns-custom` configmap

Further Comments

Contents of the coredns-custom configmap can include the following keys:

  • *.server: defines a new server block
  • *.override.start: defines a configuration override that will be imported at the start of the original Corefile (i.e. just before the kubernetes plugin)
  • *.override.fallthrough: defines a configuration override that will be imported at in the middle of the original Corefile to define fallthrough actions (i.e. just after consulting the NodeHosts hosts file and before forwarding to `/etc/resolv.conf)
  • *.override.end: defines a configuration override that will be imported at the end of the original Corefile (i.e. at the very end of the Corefile)

@iwilltry42 iwilltry42 requested a review from a team as a code owner November 4, 2021 12:37
@iwilltry42 iwilltry42 force-pushed the feature/coredns-config branch 2 times, most recently from 1a0763a to 35c5b7e Compare November 4, 2021 13:31
manifests/coredns.yaml Outdated Show resolved Hide resolved
Problem:
Before, to customize CoreDNS, one had to edit the default configmap,
which gets re-written on every K3s server restart.

Solution:
Mount an additional coredns-custom configmap into the CoreDNS container
and import overrides and additional server blocks from the included
files.

Signed-off-by: Thorsten Klein <iwilltry42@gmail.com>
@iwilltry42
Copy link
Collaborator Author

Rebased upon master to resolve conflict in zz_generated_bindata

@brandond brandond merged commit 41ff19d into k3s-io:master Nov 12, 2021
@brandond
Copy link
Contributor

@iwilltry42 would you like to backport to 1.22?

@Oats87
Copy link
Member

Oats87 commented Nov 12, 2021

Merging this PR appears to have broken master

@iwilltry42
Copy link
Collaborator Author

iwilltry42 commented Nov 12, 2021

@Oats87 , looks like it's (again) the make generate results that differ. I ran that stuff a dozen times but always get slightly different results than CI somehow 🤔
May be related to my Go version 🤔

@brandond that just needs a PR against the release-1.22 branch?

Update: Backport at #4479

@chris13524
Copy link

This PR describes support for *.override.* keys that inject configuration into an existing server block:

*.override.start: defines a configuration override that will be imported at the start of the original Corefile (i.e. just before the kubernetes plugin)

However, upon testing, this functionality does not appear to be present and the code only contains an import directive for the *.server keys.

@iwilltry42
Copy link
Collaborator Author

This PR describes support for *.override.* keys that inject configuration into an existing server block:

*.override.start: defines a configuration override that will be imported at the start of the original Corefile (i.e. just before the kubernetes plugin)

However, upon testing, this functionality does not appear to be present and the code only contains an import directive for the *.server keys.

@chris13524 as per this thread, the PR was reduced to a single import of *.server overrides

@lonwern
Copy link

lonwern commented Jan 10, 2022

Could someone backport this feature to 1.21? since 1.22 removed a lot of old APIs, some of our server have to stay at 1.21.

@ffatghub
Copy link

ffatghub commented Apr 8, 2022

Hi @iwilltry42

I need to add a simple rewrite rule before the kubernetes plugin, like this one in my Corefile

data:
  Corefile: |
    .:53 {
        errors
        health
        ready
        rewrite name foo.bar.test k3d-server-0
        kubernetes cluster.local in-addr.arpa ip6.arpa {
          pods insecure
          fallthrough in-addr.arpa ip6.arpa
        }

I guess coredns-custom is the right tool for the task. So I started extending your example:

apiVersion: v1
kind: ConfigMap
metadata:
  name: coredns-custom
  namespace: kube-system
data:
  test.server: |
    test.org {
      log
      whoami
    }

  test.override.start: |
    test.org {
      log
      whoami
    }

  test.override.fallthrough: |
    test.org {
      log
      whoami
    }

  test.override.end: |
    test.org {
      log
      whoami
    }

dig test.org shows the whoami plugin is invoked and a log entry is written by coredns

I would have expected multiple import statements in the coredns main config map, however only the .end (I guess?) is present

data:
  Corefile: |
    .:53 {
        errors
        health
        ready
        kubernetes cluster.local in-addr.arpa ip6.arpa {
          pods insecure
          fallthrough in-addr.arpa ip6.arpa
        }
        hosts /etc/coredns/NodeHosts {
          ttl 60
          reload 15s
          fallthrough
        }
        prometheus :9153
        forward . /etc/resolv.conf
        cache 30
        loop
        reload
        loadbalance
    }
    import /etc/coredns/custom/*.server

I thought *.override.start was the right "hook" but if I put a rewrite using *.override.start nothing happens (CoreDNS correcly reloads the custom map)

Am I missing something?

thanks a lot
fra

@iwilltry42
Copy link
Collaborator Author

Hi @ffatghub , please see #4397 (comment)

@andrevtg
Copy link

andrevtg commented Jun 14, 2024

Should this support wildcards? Not working for me in k3d.

apiVersion: v1
kind: ConfigMap
metadata:
  name: coredns-custom
  namespace: kube-system
data:
  localdomain.server: |
    localdomain {
        hosts {
            192.168.65.254 *.localdomain
            192.168.65.254 whoami.localdomain xxx.localdomain
            ttl 60
            fallthrough
        }
    }

In k3d I have managed to make it work with rewrite instead:

apiVersion: v1
kind: ConfigMap
metadata:
  name: coredns-custom
  namespace: kube-system
data:
  rewrite.override: |
    rewrite name regex (.*)\.localdomain host.k3d.internal

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants