Skip to content

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

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

codeeno
Copy link

@codeeno codeeno commented May 18, 2025

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

  • Unit tests updated
  • End user documentation updated

Copy link

linux-foundation-easycla bot commented May 18, 2025

CLA Signed


The committers listed above are authorized under a signed CLA.

@k8s-ci-robot
Copy link
Contributor

[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 k8s-ci-robot added the cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. label May 18, 2025
@k8s-ci-robot k8s-ci-robot requested a review from szuecs May 18, 2025 21:28
@k8s-ci-robot
Copy link
Contributor

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. 😃

@k8s-ci-robot
Copy link
Contributor

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 k8s-ci-robot added needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. and removed cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. labels May 18, 2025
@ivankatliarchuk
Copy link
Contributor

/ok-to-test

@k8s-ci-robot k8s-ci-robot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels May 19, 2025
@mloiseleur
Copy link
Collaborator

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

@Nic0w
Copy link

Nic0w commented May 19, 2025

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.

@ivankatliarchuk
Copy link
Contributor

This config multiple/single target should be discoverable

@tJouve
Copy link
Contributor

tJouve commented May 19, 2025

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 ?

@hornet-dev
Copy link

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.

@ivankatliarchuk
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants