external-dns icon indicating copy to clipboard operation
external-dns copied to clipboard

fix(pihole): crash when multiple targets are present with pihole API V6

Open codeeno opened this issue 6 months ago • 11 comments

Description

Hello,

While trying to set up external-dns (v0.17.0) with Pi-hole (v6.0.6) in my homelab I've come across the following error, placing my pod into CrashLoopBackOff state:

time="2025-05-18T20:57:16Z" level=debug msg="Endpoints generated from ingress: networking/pihole: [pihole.home 0 IN A  10.0.1.10;10.0.1.11 []]"
time="2025-05-18T20:57:16Z" level=info msg="PUT pihole.home IN A -> 10.0.1.10"
time="2025-05-18T20:57:16Z" level=debug msg="Error on request http://pihole-web.networking.svc.cluster.local/api/config/dns/hosts/10.0.1.10%3B10.0.1.11%20pihole.home"

This was with:

env:
  - name: EXTERNAL_DNS_PIHOLE_SERVER
    value: http://pihole-web.networking.svc.cluster.local
  - name: EXTERNAL_DNS_PIHOLE_API_VERSION
    value: "6"

The error happens because all targets are being passed to the generateApiUrl function, even though Pi-hole only supports one target per record and one record per domain. In my case, the targets [10.0.1.10 10.0.1.11] resulted in the API url ending in config/dns/hosts/10.0.1.10%3B10.0.1.11%20pihole.home which the V6 API does not accept.

In the previous implementation, we seemingly have only passed the first of multiple targets. This PR restores that implementation for V6.

Checklist

  • [x] Unit tests updated
  • [ ] End user documentation updated

codeeno avatar May 18 '25 21:05 codeeno

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: Once this PR has been reviewed and has the lgtm label, please assign mloiseleur for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment Approvers can cancel approval by writing /approve cancel in a comment

k8s-ci-robot avatar May 18 '25 21:05 k8s-ci-robot

Welcome @codeeno!

It looks like this is your first PR to kubernetes-sigs/external-dns 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes-sigs/external-dns has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. :smiley:

k8s-ci-robot avatar May 18 '25 21:05 k8s-ci-robot

Hi @codeeno. Thanks for your PR.

I'm waiting for a kubernetes-sigs member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

k8s-ci-robot avatar May 18 '25 21:05 k8s-ci-robot

/ok-to-test

ivankatliarchuk avatar May 19 '25 07:05 ivankatliarchuk

@tJouve Do you think you can review this PR ? Wdyt of this approach ?

mloiseleur avatar May 19 '25 19:05 mloiseleur

Hi, Not sure if useful but it looks like I have a related issue : pihole-external-dns-96bdfcc8b-2m7s4_logs.txt Looks like external-dns is trying to push both IPv6 addresses of my loadbalancer on only one record. That sends my pod into a CrashLoopBackoff state.

Pi-hole only supports one target per record and one record per domain.

I think pi-hole does allow multiple records per domains. At least I am able to do configure that by hand in the web interface.

Nic0w avatar May 19 '25 19:05 Nic0w

This config multiple/single target should be discoverable

ivankatliarchuk avatar May 19 '25 19:05 ivankatliarchuk

Hello,

@codeeno Your fix look good and fix a bug.

I will just suggest to change the code to look like ( add Info message and use target := ep.Targets[0] )

        if len(ep.Targets) > 1 {
		log.Infof("Skipping : more than one target, only the first one is keep :  %s %s %s -> %s", action, ep.DNSName, ep.RecordType, ep.targets)
	}
	//
	target := ep.Targets[0]

	if p.cfg.DryRun {
		log.Infof("DRY RUN: %s %s IN %s -> %s", action, ep.DNSName, ep.RecordType, target)
		return nil
	}

	log.Infof("%s %s IN %s -> %s", action, ep.DNSName, ep.RecordType, target)

	// Get the current record
	if strings.Contains(ep.DNSName, "*") {
		return provider.NewSoftError(errors.New("UNSUPPORTED: Pihole DNS names cannot return wildcard"))
	}

	switch ep.RecordType {
	case endpoint.RecordTypeA, endpoint.RecordTypeAAAA:
		apiUrl = p.generateApiUrl(apiUrl, fmt.Sprintf("%s %s", target, ep.DNSName))
	case endpoint.RecordTypeCNAME:
		if ep.RecordTTL.IsConfigured() {
			apiUrl = p.generateApiUrl(apiUrl, fmt.Sprintf("%s,%s,%d", ep.DNSName, target, ep.RecordTTL))
		} else {
			apiUrl = p.generateApiUrl(apiUrl, fmt.Sprintf("%s,%s", ep.DNSName, target))
		}
	}

But in fact it is possible to specify multiples IP for the same A/AAA record . This statement Pi-hole only supports one target per record and one record per domain is not correct.

This config is legit image And is resolved like that image

The definition with the api in one call is not possible, but it will work with 2 different calls image image Result in

{
  "config": {
    "dns": {
      "hosts": [
        "192.168.253.253 duplicate.example.net",
        "192.168.253.254 duplicate.example.net"
      ]
    }
  },
  "took": 0.000047206878662109375
}

Deletion also work (one by one): image

Maybe we can support this feature by doing something like that : Iterate over the Targets list then create / delete in separate query to the API.

if len(ep.Targets) == 0 {
		log.Infof("Skipping : missing targets  %s %s %s", action, ep.DNSName, ep.RecordType)
		return nil
	}
	// Get the current record
	if strings.Contains(ep.DNSName, "*") {
		return provider.NewSoftError(errors.New("UNSUPPORTED: Pihole DNS names cannot return wildcard"))
	}

	for _, target := range ep.Targets {
		if p.cfg.DryRun {
			log.Infof("DRY RUN: %s %s IN %s -> %s", action, ep.DNSName, ep.RecordType, target)
			return nil
		}

		log.Infof("%s %s IN %s -> %s", action, ep.DNSName, ep.RecordType, target)

		switch ep.RecordType {
		case endpoint.RecordTypeA, endpoint.RecordTypeAAAA:
			apiUrl = p.generateApiUrl(apiUrl, fmt.Sprintf("%s %s", target, ep.DNSName))
		case endpoint.RecordTypeCNAME:
			if ep.RecordTTL.IsConfigured() {
				apiUrl = p.generateApiUrl(apiUrl, fmt.Sprintf("%s,%s,%d", ep.DNSName, target, ep.RecordTTL))
			} else {
				apiUrl = p.generateApiUrl(apiUrl, fmt.Sprintf("%s,%s", ep.DNSName, target))
			}
		}

		req, err := http.NewRequestWithContext(ctx, action, apiUrl, nil)
		if err != nil {
			return err
		}

		_, err = p.do(req)
		if err != nil {
			return err
		}
	}

What do you think about this implementation ?

tJouve avatar May 19 '25 20:05 tJouve

I'm running into this too, and I'd prefer to see the implementation that creates A records for each IP address, rather than only create a single record for the first IP address in the list.

hornet-dev avatar Jun 05 '25 18:06 hornet-dev

Hi @codeeno. Do you think you could address review suggestions?

ivankatliarchuk avatar Jun 18 '25 09:06 ivankatliarchuk

PR needs rebase.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

k8s-ci-robot avatar Jul 11 '25 19:07 k8s-ci-robot

Resolved https://github.com/kubernetes-sigs/external-dns/pull/5584

If something is missing, feel free to reopen PR

/close

ivankatliarchuk avatar Jul 11 '25 20:07 ivankatliarchuk

@ivankatliarchuk: Closed this PR.

In response to this:

Resolved https://github.com/kubernetes-sigs/external-dns/pull/5584

If something is missing, feel free to reopen PR

/close

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

k8s-ci-robot avatar Jul 11 '25 20:07 k8s-ci-robot