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

Second version of helper code for sorting map files. #19492

Merged
merged 4 commits into from
May 7, 2018

Conversation

ramr
Copy link
Contributor

@ramr ramr commented Apr 24, 2018

@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

@openshift-ci-robot openshift-ci-robot added the size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. label Apr 24, 2018
)

// Generate a regular expression to match route hosts (and paths if any).
func GenerateRouteRegexp(hostname, path string, wildcard bool) string {
Copy link
Contributor

Choose a reason for hiding this comment

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

godoc

Copy link
Contributor Author

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.

// build the correct subpath regex, depending on whether path ends with a segment separator
var pathRE, subpathRE string
switch {
case strings.TrimRight(path, "/") == "":
Copy link
Contributor

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

Copy link
Contributor Author

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.
Copy link
Contributor

Choose a reason for hiding this comment

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

why keeping this wrapper?

Copy link
Contributor Author

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)
Copy link
Contributor

Choose a reason for hiding this comment

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

same question as above

Copy link
Contributor Author

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)
Copy link
Contributor

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.
Copy link
Contributor

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok will do. thx

@ramr
Copy link
Contributor Author

ramr commented Apr 24, 2018

/test verify

@ramr
Copy link
Contributor Author

ramr commented Apr 25, 2018

/test gcp

@ramr
Copy link
Contributor Author

ramr commented Apr 25, 2018

Flake #19058
Flake #19505

@ramr
Copy link
Contributor Author

ramr commented Apr 25, 2018

/test gcp

Copy link
Contributor

@knobunc knobunc left a 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.

@knobunc knobunc self-assigned this Apr 30, 2018
@knobunc
Copy link
Contributor

knobunc commented Apr 30, 2018

@smarterclayton Is this the change you wanted?

@ramr
Copy link
Contributor Author

ramr commented May 1, 2018

flake #19500

@ramr
Copy link
Contributor Author

ramr commented May 1, 2018

/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" . -}}
Copy link
Contributor

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

Copy link
Contributor

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.

Copy link
Contributor

@smarterclayton smarterclayton May 1, 2018

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
}

Copy link
Contributor Author

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 {
Copy link
Contributor

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
Copy link
Contributor

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.

Copy link
Contributor Author

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.

@smarterclayton
Copy link
Contributor

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.
@ramr
Copy link
Contributor Author

ramr commented May 2, 2018

@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.

@knobunc
Copy link
Contributor

knobunc commented May 3, 2018

/approve
/retest

@openshift-ci-robot
Copy link

[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 /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@openshift-ci-robot openshift-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label May 3, 2018
@knobunc
Copy link
Contributor

knobunc commented May 7, 2018

/lgtm

@openshift-ci-robot openshift-ci-robot added the lgtm Indicates that a PR is ready to be merged. label May 7, 2018
@openshift-merge-robot openshift-merge-robot merged commit 1cdf917 into openshift:master May 7, 2018
@ramr ramr deleted the tmplsort-helpers branch May 7, 2018 19:29
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. lgtm Indicates that a PR is ready to be merged. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants