-
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?
Changes from all commits
8d52be2
b37cb51
b5a6cbd
3f77b0a
5798069
db35373
ae43729
db55aa1
a6851a2
664ea01
20e5eb9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
|
||
Nodes marked as **Unschedulable** as per [core/v1/NodeSpec](https://pkg.go.dev/k8s.io/api@v0.31.1/core/v1#NodeSpec) are excluded. | ||
This avoid exposing Unhealthy, NotReady or SchedulingDisabled (cordon) nodes. | ||
|
@@ -40,7 +41,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 | ||
- --txt-owner-id=my-identifier | ||
- --policy=sync | ||
- --log-level=debug | ||
|
@@ -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 commentThe reason will be displayed to describe this comment to others. Learn more. I never tried this 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? |
||
- --txt-owner-id=my-identifier | ||
- --policy=sync | ||
- --log-level=debug | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 commentThe 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
have you checked this library for known vulnerabilities?
or with snyk open source checkers There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe 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. |
||
github.com/google/go-cmp v0.6.0 | ||
github.com/google/uuid v1.6.0 | ||
github.com/gophercloud/gophercloud v1.14.1 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,7 @@ import ( | |
"time" | ||
"unicode" | ||
|
||
sprig "github.com/go-task/slim-sprig/v3" | ||
log "github.com/sirupsen/logrus" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/labels" | ||
|
@@ -147,9 +148,11 @@ func parseTemplate(fqdnTemplate string) (tmpl *template.Template, err error) { | |
if fqdnTemplate == "" { | ||
return nil, nil | ||
} | ||
funcs := template.FuncMap{ | ||
"trimPrefix": strings.TrimPrefix, | ||
} | ||
|
||
funcs := sprig.HermeticTxtFuncMap() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. extra := template.FuncMap{ and add them to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this code most likely better to move to
so we could have an isolated code coverage for multiple functions |
||
funcs["isIPv6"] = isIPv6String | ||
funcs["isIPv4"] = isIPv4String | ||
|
||
return template.New("endpoint").Funcs(funcs).Parse(fqdnTemplate) | ||
} | ||
|
||
|
@@ -266,6 +269,25 @@ func suitableType(target string) string { | |
return endpoint.RecordTypeCNAME | ||
} | ||
|
||
// 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 commentThe 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 |
||
if err != nil { | ||
return false | ||
} | ||
return netIP.Is6() | ||
} | ||
|
||
// 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 commentThe reason will be displayed to describe this comment to others. Learn more. same as other function |
||
netIP, err := netip.ParseAddr(target) | ||
if err != nil { | ||
return false | ||
} | ||
return netIP.Is4() | ||
} | ||
|
||
// endpointsForHostname returns the endpoint objects for each host-target combination. | ||
func endpointsForHostname(hostname string, targets endpoint.Targets, ttl endpoint.TTL, providerSpecific endpoint.ProviderSpecific, setIdentifier string, resource string) []*endpoint.Endpoint { | ||
var endpoints []*endpoint.Endpoint | ||
|
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?