-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Second version of helper code for sorting map files. #19492
Conversation
pkg/router/template/util/util.go
Outdated
) | ||
|
||
// Generate a regular expression to match route hosts (and paths if any). | ||
func GenerateRouteRegexp(hostname, path string, wildcard bool) string { |
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.
godoc
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 was actually existing code that got moved. But I can add godoc to it.
pkg/router/template/util/util.go
Outdated
// build the correct subpath regex, depending on whether path ends with a segment separator | ||
var pathRE, subpathRE string | ||
switch { | ||
case strings.TrimRight(path, "/") == "": |
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 think we use len() == 0 convention to check empty strings
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.
ok - will do (its existing code that got moved to a new file).
@@ -99,49 +104,19 @@ func genSubdomainWildcardRegexp(hostname, path string, exactPath bool) string { | |||
return fmt.Sprintf(`^[^\.]*%s(|/.*)$`, expr) | |||
} | |||
|
|||
// generateRouteRegexp is now legacy and around for backward | |||
// compatibility and allows old templates to continue running. |
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.
why keeping this wrapper?
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.
For any existing custom haproxy templates to work, the helper function needs to exist. One scenario where this is needed is if you upgrade a router image but are still using the custom haproxy template file (based on an older template).
} | ||
|
||
return "^" + hostRE + portRE + pathRE + subpathRE + "$" | ||
return templateutil.GenerateRouteRegexp(hostname, path, wildcard) |
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 question as above
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 answer!! ;^)
} | ||
|
||
return hostname | ||
return templateutil.GenCertificateHostName(hostname, wildcard) |
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.
and here
if err != nil { | ||
glog.Errorf("Error reading map file %s: %v", name, err) | ||
return []string{} | ||
// Returns a haproxy backend config for a given service alias. |
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 would keep the godoc standard even if this is private 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.
ok will do. thx
/test verify |
/test gcp |
/test gcp |
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.
Looks good to me.
@smarterclayton Is this the change you wanted? |
flake #19500 |
/retest cmd |
{{ define "/var/lib/haproxy/conf/cert_config.map" -}} | ||
{{ range $idx, $data := sortedMapData "/var/lib/haproxy/conf/.tmp/cert_config.map" false -}} | ||
{{ $data }} | ||
{{ range $idx, $line := generateHAProxyMap "cert_config.map" . -}} |
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 like this a lot 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.
Although....
Can you get at the template name from within a template? If we did that we could avoid having the filename argument. I.e. when you create the template create a closure over the template name.
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.
Basically, in pkg/router/template/plugin.go line 122 (inside NewTemplatePlugin) when we iterate over the loop of templates, create a closure on the specific helpers that are filename specific and create a closure with:
for _, template := range masterTemplates.Templates() {
...
// copy helperFunctions into a new map
name := template.Name()
fns["generateHAProxyMap"] = func(State) {
// use name here instead of taking it as an argument
}
// do the same for others
template = template.Funcs(fns)
templates[template.Name()] = template
}
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.
ok will do.
} | ||
|
||
backendConfig := backendConfig(k, cfg, hascert) | ||
if entry := haproxyutil.GenerateMapEntry(certConfigMap, backendConfig); entry != 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.
It would be better if this was passed in as a closure from the template name, rather than it coming from a hardcoded function.
@@ -0,0 +1,71 @@ | |||
package util |
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.
Don't put methods into packages called util. Is there any reason this isn't in the haproxy package? Because this looks haproxy specific.
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.
Moved those out of the template_helper.go
code as they are common - used by both nginx and older haproxy templates (the new template would just call those functions directly instead of via the the template_helper code). All expect for the last one GenerateBackendNamePrefix
which I guess is sorta haproxy specific - but technically just a prefix name, so qualifies as an utility function.
i like that it's tested a lot. I'd like to remove the filename parameter to generateHAProxyMap completely and make it be a closure, see my example. |
generation function to be scoped to the template its operating under.
@smarterclayton ok made the requested change - had to do it slightly differently. It needed a clone before setting the functions (creating a new template) as the template functions are shared - last one in replaces the ones set before it. |
/approve |
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: knobunc, ramr 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 |
/lgtm |
@smarterclayton PTAL - this is based on your comments on #19219
It seems lot more work/code as opposed to a
grep -v | sort -r + grep | sort -r + cat
but on the flip side, there is more testing around the map generation code./cc @knobunc