-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
base: master
Are you sure you want to change the base?
Conversation
Welcome @matkam! |
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 Once the patch is verified, the new status will be reflected by the 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. |
Please submit a CLA per the instructions above. |
Yes 👍 |
@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 |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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, ":")
}
Sprig has |
/ok-to-test |
does |
I believe |
sprig looks like a nice package. how about just importing it and using
|
@johngmyers let me know what you think. I've updated the template functions to include Sprig's hermetic text functions, and fixed the |
source/source.go
Outdated
@@ -29,6 +29,7 @@ import ( | |||
"time" | |||
"unicode" | |||
|
|||
sprig "github.com/go-task/slim-sprig" |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
?
The Kubernetes project currently lacks enough contributors to adequately respond to all PRs. This bot triages PRs according to the following rules:
You can:
Please send feedback to sig-contributor-experience at kubernetes/community. /lifecycle stale |
Co-authored-by: Michel Loiseleur <97035654+mloiseleur@users.noreply.github.com>
/remove-lifecycle stale |
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @matkam. Overall looks ok, if you still have time and interest to add this to a master branch, I've shared few bits that worth addressing.
Could you also provide a set of manual test steps using manifests and kubectl commands? This will help in understanding better the change and verifying the implementation.
/assign
@@ -37,6 +37,7 @@ require ( | |||
github.com/ffledgling/pdns-go v0.0.0-20180219074714-524e7daccd99 | |||
github.com/go-gandi/go-gandi v0.7.0 | |||
github.com/go-logr/logr v1.4.2 | |||
github.com/go-task/slim-sprig/v3 v3.0.0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the only concern is that the libary had last release 2+ years ago in 2023. Could you justify/convince the library maintainer to
- refresh a release tag
- refresh go.mod with go.sum
- would be nice to sync
go
version as well, but not critical
have you checked this library for known vulnerabilities?
trivy repo --scanners vuln .
or with snyk open source checkers
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This actually quite tricky. For example github.com/Masterminds/sprig is heavier, but it has 4k stars and almost ~100 contributors.... So worth to share a comparison on why this library vs upstream one
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It doesn't make a difference for my use case. I only need the replace
function. Context here. Would you prefer importing github.com/Masterminds/sprig instead, or even creating a similar function here and avoiding the import altogether?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have no opinion. If you refactor logic as per review, and cover functions with tests, we will decide on a library at the very end. Both libraries have they advantages. Or as you mention, we may not need any libraries at this time, as only few functions required.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you only need one function - so no import required. Better to creat own function. It's more about compatibility, we should have tests for all the functions provided.
// isIPv6String reports whether the target string is an IPv6 address, | ||
// including IPv4-mapped IPv6 addresses. | ||
func isIPv6String(target string) bool { | ||
netIP, err := netip.ParseAddr(target) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could you share code coveragate for this functions?
Can we have specific tests just in source_test.go
I know it's a private function, but we may move them at some point to utils.go, this will help with maintenance
Worth to add BenchmarkTest... as well, and if you could, share results
} | ||
|
||
// isIPv4String reports whether the target string is an IPv4 address. | ||
func isIPv4String(target string) bool { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same as other function
@@ -111,7 +112,7 @@ spec: | |||
- --domain-filter=external-dns-test.my-org.com | |||
- --aws-zone-type=public | |||
- --registry=txt | |||
- --fqdn-template={{.Name}}.external-dns-test.my-org.com | |||
- --fqdn-template={{.Name | replace "." "-"}}.external-dns-test.my-org.com |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I never tried this fqdn-template
, could you create a page or a section with bit more information about this fqdn-template
and it's use cases and advantages?
I can see an example for raw kubernetes manifest. How this behave in helm chart? as the syntax is very similar. I would assume helm generation will fail, am I correct?
May I also ask to write tests similar to like helm does as well https://github.com/helm/helm/blob/0d66425d9a745d8a289b1a5ebb6ccc744436da95/pkg/engine/funcs_test.go#L27 We probably should have same structure for files as helm too, aka move this functions to |
@@ -6,6 +6,7 @@ Using nodes (`--source=node`) as source is possible to synchronize a DNS zone wi | |||
The node source adds an `A` record per each node `externalIP` (if not found, any IPv4 `internalIP` is used instead). | |||
It also adds an `AAAA` record per each node IPv6 `internalIP`. | |||
The TTL of the records can be set with the `external-dns.alpha.kubernetes.io/ttl` node annotation. | |||
The FQDN template provides more than 100+ functions, documented [here](https://go-task.github.io/slim-sprig/). For instance, it includes a function to replace all `.` with `-` in the node name, which can be useful with cloud providers that include dots in the node name. There are two additional functions available on top of the standard sprig functions: `isIPv4` and `isIPv6`. The functions can be used to test a string for being an IPv4 or IPv6 address. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could we also have a markdown table with all currently supported or added functions, description and short usage example?
"trimPrefix": strings.TrimPrefix, | ||
} | ||
|
||
funcs := sprig.HermeticTxtFuncMap() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
extra := template.FuncMap{
"isIPv6" : isIPv6String
"isIPv4" : isIPv4String
}
and add them to funcs
if it's possible
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this code most likely better to move to
func funcMap() template.FuncMap {}
so we could have an isolated code coverage for multiple functions
Hi @ivankatliarchuk, thanks for looking at this PR. I definitely do want to get it merged in. I will look for some time in the coming weeks to address your review items. |
/label tide/merge-method-squash |
Description
Adds a few useful text/template functions:
strings.replaceAll
N/A
Checklist