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

[TXT Registry] Fix handling of records produced by toNewTXTName in toEndpoint #3724

Merged
merged 12 commits into from Aug 10, 2023

Conversation

Sewci0
Copy link
Contributor

@Sewci0 Sewci0 commented Jun 23, 2023

The current implementation of the toEndpoint function is unable to handle records generated by the toNewTXTName function. As a result, domains created with a prefix such as %{record_type}foo. are not deleted after the associated CRD is removed.

This PR addresses this issue by improving the handling of records produced by the toNewTXTName function in the toEndpoint function. Additionally, it covers edge cases related to dashes or dots in the affix. A new test, TestToEndpointNameNewTXT, has been added to ensure the proper conversion between the output of toNewTXTName and its input using the toEndpoint function.

Additionally, this PR fixes the testTXTRegistryRecordsPrefixed test, which previously operated under the incorrect assumption that the output of toNewTXTName with the prefix txt. for dualstack.test-zone.example.org would be aaaa-txt.dualstack.test-zone.example.org. In reality when not using a templated record type, the type will be prepended to the domain rather than the affix.

DNSName := strings.SplitN(endpointDNSName, ".", 2)

if !pr.recordTypeInAffix() {
	DNSName[0] = recordT + DNSName[0]
}

Here are the results of the new test before the change:

Potentially fixes: #3007 #3186

Checklist

  • Unit tests updated

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Jun 23, 2023
@k8s-ci-robot k8s-ci-robot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Jun 23, 2023
@Sewci0 Sewci0 changed the title [TXT Registry] Fix handling of records produced by toNewTXTName function in toEndpoint [TXT Registry] Fix handling of records produced by toNewTXTName function in toEndpoint Jun 23, 2023
@Sewci0 Sewci0 changed the title [TXT Registry] Fix handling of records produced by toNewTXTName function in toEndpoint [TXT Registry] Fix handling of records produced by toNewTXTName in toEndpoint Jun 23, 2023
@szuecs
Copy link
Contributor

szuecs commented Jun 26, 2023

/ok-to-test

@k8s-ci-robot k8s-ci-robot added the ok-to-test Indicates a non-member PR verified by an org member that is safe to test. label Jun 26, 2023
@szuecs
Copy link
Contributor

szuecs commented Jun 26, 2023

@mloiseleur can you review this one? ty

@mloiseleur
Copy link
Contributor

@szuecs I'll check it this week.

@Sewci0
Copy link
Contributor Author

Sewci0 commented Jun 26, 2023

Thank you both for looking at this! As soon as this is merged it should also enable: #3735

registry/txt.go Outdated Show resolved Hide resolved
registry/txt_test.go Show resolved Hide resolved
registry/txt_test.go Outdated Show resolved Hide resolved
registry/txt.go Outdated Show resolved Hide resolved
registry/txt.go Show resolved Hide resolved
@johngmyers
Copy link
Contributor

It is unfortunate that this is as overengineered as it is. It is far more configurable than it needs to be and this leads to bugs such as the handling of %{record_type}. Why is %{record_type} even necessary in the first place?

Sewci0 and others added 3 commits June 27, 2023 08:57
Co-authored-by: John Gardiner Myers <jgmyers@proofpoint.com>
@Sewci0
Copy link
Contributor Author

Sewci0 commented Jun 27, 2023

It is unfortunate that this is as overengineered as it is. It is far more configurable than it needs to be and this leads to bugs such as the handling of %{record_type}. Why is %{record_type} even necessary in the first place?

It is currently the only option when working with APEX cname records, as mentioned here: #449

@johngmyers
Copy link
Contributor

It is currently the only option when working with APEX cname records, as mentioned here: #449

That would be solved with --txt-prefix without arbitrary templating of %{record_type}, would it not?

@Sewci0 Sewci0 closed this Jun 27, 2023
@Sewci0 Sewci0 reopened this Jun 27, 2023
@Sewci0
Copy link
Contributor Author

Sewci0 commented Jun 27, 2023

It is currently the only option when working with APEX cname records, as mentioned here: #449

That would be solved with --txt-prefix without arbitrary templating of %{record_type}, would it not?

It wouldn't as the record type gets prepended to the hostname not the affix.

if !pr.recordTypeInAffix() {
DNSName[0] = recordT + DNSName[0]
}

@johngmyers
Copy link
Contributor

It wouldn't as the record type gets prepended to the hostname not the affix.

The record type demonstrably gets prepended to the prefix.

@Sewci0
Copy link
Contributor Author

Sewci0 commented Jun 27, 2023

It wouldn't as the record type gets prepended to the hostname not the affix.

The record type demonstrably gets prepended to the prefix.

@johngmyers I would love to see that demonstration. Running just now against the master branch:

func TestToNewTXTName(t *testing.T) {
	tests := []struct {
		name                 string
		pr                   affixNameMapper
		endpointDNSName      string
		recordType           string
		expectedNewTXTName   string
	}{
		{
			name: "Test 1",
			pr: affixNameMapper{
				prefix:              "prefix",
				suffix:              "",
			},
			endpointDNSName:    "example.com",
			recordType:         "A",
			expectedNewTXTName: "prefixa-example.com",
		},
		{
			name: "Test 2",
			pr: affixNameMapper{
				prefix:              "prefix.",
				suffix:              "",
			},
			endpointDNSName:    "test.example.com",
			recordType:         "A",
			expectedNewTXTName: "prefix.a-test.example.com",
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			result := tt.pr.toNewTXTName(tt.endpointDNSName, tt.recordType)
			assert.Equal(t, tt.expectedNewTXTName, result)
		})
	}
}
=== RUN   TestToNewTXTName
=== RUN   TestToNewTXTName/Test_1
=== RUN   TestToNewTXTName/Test_2
--- PASS: TestToNewTXTName (0.00s)
    --- PASS: TestToNewTXTName/Test_1 (0.00s)
    --- PASS: TestToNewTXTName/Test_2 (0.00s)
PASS

@johngmyers
Copy link
Contributor

@Sewci0 would you be able to address my review comments?

@Sewci0
Copy link
Contributor Author

Sewci0 commented Jul 14, 2023

Hi @johngmyers, sorry it took me so long. I've been pretty busy. Thank you for your comments, I implemented all of your suggestions. Let me know your thoughts!

Copy link
Contributor

@johngmyers johngmyers left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for fixing this!
/lgtm
/assign @szuecs

@@ -35,6 +34,12 @@ const (
providerSpecificForceUpdate = "txt/force-update"
)

type endpointKey struct {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use endpoint.EndpointKey instead of redefining?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no such thing. Did you mean endpoint.Endpoint?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Sewci0 it was moved recently 4417ad4.

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jul 18, 2023
func (pr affixNameMapper) dropAffix(name string) string {
// dropAffixExtractType strips TXT record to find an endpoint name it manages
// it also returns the record type
func (pr affixNameMapper) dropAffixExtractType(name string) (baseName, recordType string) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no use of named return values, so we can also just do:
(baseName, recordType string) -> (string, string)

If there is a blank return then we would return "","" in the current version

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem with (string, string) is it makes it difficult to know what the strings contain. The names convey the semantics.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be also a comment instead of named return values, which actually define variables in scope of the func.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Named return values work better with IDEs than comments do.

@@ -371,20 +396,24 @@ func (pr affixNameMapper) isSuffix() bool {
return len(pr.prefix) == 0 && len(pr.suffix) > 0
}

func (pr affixNameMapper) toEndpointName(txtDNSName string) (endpointName string, isAAAA bool) {
lowerDNSName, recordType := extractRecordType(strings.ToLower(txtDNSName))
func (pr affixNameMapper) toEndpointName(txtDNSName string) (endpointName string, recordType string) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as in https://github.com/kubernetes-sigs/external-dns/pull/3724/files#r1290447277 , I would be in favor of dropping the "named return" in case we do not use it.

@szuecs
Copy link
Contributor

szuecs commented Aug 10, 2023

@johngmyers from my side we can merge as I only have some nitpicks that we could address ourselves. As you added lgtm, I guess we are good to go.

/approve

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: johngmyers, Sewci0, szuecs

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

The pull request process is described 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 approved Indicates a PR has been approved by an approver from all required OWNERS files. label Aug 10, 2023
@szuecs
Copy link
Contributor

szuecs commented Aug 10, 2023

hmm 2 jobs pull-external-dns-* are hanging. Any idea how to retrigger, @johngmyers ?

@mloiseleur
Copy link
Contributor

mloiseleur commented Aug 10, 2023

/retest

@k8s-ci-robot
Copy link
Contributor

@mloiseleur: The /retest command does not accept any targets.
The following commands are available to trigger required jobs:

  • /test pull-external-dns-lint
  • /test pull-external-dns-unit-test

Use /test all to run all jobs.

In response to this:

/retest pull-external-dns-lint

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/test-infra repository.

@johngmyers
Copy link
Contributor

/retest

@k8s-ci-robot k8s-ci-robot merged commit 380e8b6 into kubernetes-sigs:master Aug 10, 2023
14 checks passed
@johngmyers johngmyers mentioned this pull request Sep 13, 2023
2 tasks
@haslersn
Copy link

There was an issue, which got fixed by this PR: #3031

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Tries to create records in Route53 that already exist (v1.12.2)
7 participants