Skip to content

Commit

Permalink
Support subdomain field when host is unset
Browse files Browse the repository at this point in the history
If a route has an empty spec.host and a nonempty spec.subdomain and the
router has a domain configured, use the route's subdomain and router's
domain as the route's host.

* pkg/cmd/infra/router/router.go (RouterSelection): Add RouterDomain field.
(Bind): Add a --router-domain flag and ROUTER_DOMAIN environment variable
to specify the router's domain.
(RouteUpdate): Set the route's spec.host using the route's subdomain and
router's domain if the host is empty and the subdomain and domain are
nonempty.
  • Loading branch information
Miciah committed Nov 3, 2021
1 parent ddca0a6 commit 6f730c7
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions pkg/cmd/infra/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type RouterSelection struct {
UpdateStatus bool

HostnameTemplate string
RouterDomain string
OverrideHostname bool
OverrideDomains []string
RedactedDomains sets.String
Expand Down Expand Up @@ -81,6 +82,7 @@ func (o *RouterSelection) Bind(flag *pflag.FlagSet) {
flag.BoolVar(&o.UpdateStatus, "update-status", isTrue(env("ROUTER_UPDATE_STATUS", "true")), "If true, the router will update admitted route status.")
flag.DurationVar(&o.ResyncInterval, "resync-interval", controllerfactory.DefaultResyncInterval, "The interval at which the route list should be fully refreshed")
flag.StringVar(&o.HostnameTemplate, "hostname-template", env("ROUTER_SUBDOMAIN", ""), "If specified, a template that should be used to generate the hostname for a route without spec.host (e.g. '${name}-${namespace}.myapps.mycompany.com')")
flag.StringVar(&o.RouterDomain, "router-domain", env("ROUTER_DOMAIN", ""), "If specified, a domain that should be used to generate the hostname for a route with spec.subdomain and without spec.host (e.g. 'apps.mycluster.com')")
flag.BoolVar(&o.OverrideHostname, "override-hostname", isTrue(env("ROUTER_OVERRIDE_HOSTNAME", "")), "Override the spec.host value for a route with --hostname-template")
flag.StringSliceVar(&o.OverrideDomains, "override-domains", envVarAsStrings("ROUTER_OVERRIDE_DOMAINS", "", ","), "List of comma separated domains to override if present in any routes. This overrides the spec.host value in any matching routes with --hostname-template")
flag.StringVar(&o.LabelSelector, "labels", env("ROUTE_LABELS", ""), "A label selector to apply to the routes to watch")
Expand All @@ -101,6 +103,11 @@ func (o *RouterSelection) Bind(flag *pflag.FlagSet) {

// RouteUpdate updates the route before it is seen by the cache.
func (o *RouterSelection) RouteUpdate(route *routev1.Route) {
// If the route specifies a subdomain and no host name and we a router
// domain, set the host field using the subdomain and domain.
if len(route.Spec.Host) == 0 && len(route.Spec.Subdomain) > 0 && len(o.RouterDomain) != 0 {
route.Spec.Host = fmt.Sprintf("%s.%s", route.Spec.Subdomain, o.RouterDomain)
}
if len(o.HostnameTemplate) == 0 {
return
}
Expand Down

0 comments on commit 6f730c7

Please sign in to comment.