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

feat: additional template functions #3949

Open
wants to merge 11 commits into
base: master
Choose a base branch
from

Conversation

matkam
Copy link

@matkam matkam commented Sep 21, 2023

Description

Adds a few useful text/template functions:

  • replaceAll: strings.replaceAll
  • isIPv6: exposes an existing function that returns a bool to let you know if a string is an IPv6 address
  • isIPv4: a new function that returns a bool to let you know if a string is an IPv4 address

N/A

Checklist

  • Unit tests updated
  • End user documentation updated

@k8s-ci-robot k8s-ci-robot added the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Sep 21, 2023
@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Sep 21, 2023

CLA Signed

The committers listed above are authorized under a signed CLA.

@k8s-ci-robot k8s-ci-robot added the cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. label Sep 21, 2023
@k8s-ci-robot
Copy link
Contributor

Welcome @matkam!

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 k8s-ci-robot added the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label Sep 21, 2023
@k8s-ci-robot
Copy link
Contributor

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

@k8s-ci-robot k8s-ci-robot added the size/S Denotes a PR that changes 10-29 lines, ignoring generated files. label Sep 21, 2023
@k8s-ci-robot k8s-ci-robot added size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. size/S Denotes a PR that changes 10-29 lines, ignoring generated files. and removed size/S Denotes a PR that changes 10-29 lines, ignoring generated files. size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Sep 21, 2023
@matkam matkam changed the title [WIP] Additional template functions Additional template functions Sep 22, 2023
@k8s-ci-robot k8s-ci-robot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Sep 22, 2023
@johngmyers
Copy link
Contributor

Please submit a CLA per the instructions above.

@matkam
Copy link
Author

matkam commented Sep 22, 2023

Please submit a CLA per the instructions above.

Yes 👍
I'm just waiting on my employer to click the button.

@k8s-ci-robot k8s-ci-robot added 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 Sep 26, 2023
@matkam
Copy link
Author

matkam commented Sep 26, 2023

@johngmyers we are good to go here 👍

source/source.go Outdated

func isIPv4String(input string) bool {
netIP := net.ParseIP(input)
return netIP != nil && netIP.To4() != nil
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this function intended to return true for IPv4-mapped IPv6 addresses?

Copy link
Author

Choose a reason for hiding this comment

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

It just returns true when the input string is an IPv4 address. It'll return false if its IPv6 or otherwise invalid IP address.

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Author

Choose a reason for hiding this comment

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

how about something like this?

func isIPv4String(input string) bool {
	netIP := net.ParseIP(input)
	return netIP != nil && netIP.To4() != nil && !strings.Contains(input, ":")
}

@johngmyers
Copy link
Contributor

Sprig has replace, which is similar. Perhaps we should follow their syntax?

@johngmyers
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 Sep 27, 2023
@matkam
Copy link
Author

matkam commented Sep 27, 2023

Sprig has replace, which is similar. Perhaps we should follow their syntax?

does replace work differently? I could also add strings.replace to the FuncMap.

@johngmyers
Copy link
Contributor

I believe replace has the arguments in a different order. See the documentation in https://masterminds.github.io/sprig/strings.html

@matkam
Copy link
Author

matkam commented Sep 27, 2023

I believe replace has the arguments in a different order. See the documentation in https://masterminds.github.io/sprig/strings.html

sprig looks like a nice package. how about just importing it and using hermeticTxtFuncMap like this:

func parseTemplate(fqdnTemplate string) (tmpl *template.Template, err error) {
	if fqdnTemplate == "" {
		return nil, nil
	}
	
	funcs := sprig.HermeticTxtFuncMap()
	funcs["isIPv6"] = isIPv6String
	funcs["isIPv4"] = isIPv4String

	return template.New("endpoint").Funcs(funcs).Parse(fqdnTemplate)
}

@matkam matkam requested a review from johngmyers October 9, 2023 23:18
@matkam
Copy link
Author

matkam commented Oct 27, 2023

@johngmyers let me know what you think. I've updated the template functions to include Sprig's hermetic text functions, and fixed the isIPv4String function so that it only returns true for valid IPv4 strings.

@mloiseleur
Copy link
Contributor

/retitle feat: additional template functions

@k8s-ci-robot k8s-ci-robot changed the title Additional template functions feat: additional template functions Mar 12, 2024
@simonostendorf
Copy link
Contributor

Hello, I would like to see this merged and released. What needs to be done?

@k8s-ci-robot k8s-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label May 29, 2024
@k8s-ci-robot k8s-ci-robot removed the lgtm "Looks good to me", indicates that a PR is ready to be merged. label May 29, 2024
@k8s-ci-robot
Copy link
Contributor

New changes are detected. LGTM label has been removed.

@k8s-ci-robot k8s-ci-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label May 29, 2024
source/source.go Outdated
@@ -29,6 +29,7 @@ import (
"time"
"unicode"

sprig "github.com/go-task/slim-sprig"
Copy link
Contributor

Choose a reason for hiding this comment

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

Please no import for a single func.

Copy link
Author

Choose a reason for hiding this comment

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

@szuecs it includes several useful go template functions: https://github.com/go-task/slim-sprig/blob/master/functions.go

Would you rather not include these functions, and only keeping isIPv4?

@k8s-triage-robot
Copy link

The Kubernetes project currently lacks enough contributors to adequately respond to all PRs.

This bot triages PRs according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the PR is closed

You can:

  • Mark this PR as fresh with /remove-lifecycle stale
  • Close this PR with /close
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle stale

@k8s-ci-robot k8s-ci-robot added lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. labels Nov 5, 2024
@k8s-ci-robot k8s-ci-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Nov 12, 2024
@matkam
Copy link
Author

matkam commented Nov 12, 2024

/remove-lifecycle stale

@k8s-ci-robot k8s-ci-robot removed the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Nov 12, 2024
@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 ask for approval from mloiseleur. 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 size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Jan 7, 2025
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