Browse code

Change to use helper function and fix up helper function expectations and tests and add missing generated files.

ramr authored on 2016/10/26 09:37:16
Showing 6 changed files
... ...
@@ -27432,7 +27432,7 @@
27432 27432
      },
27433 27433
      "wildcardPolicy": {
27434 27434
       "type": "string",
27435
-      "description": "Wildcard policy if any for the route."
27435
+      "description": "Wildcard policy if any for the route. Currently only 'Subdomain' or 'None' is allowed."
27436 27436
      }
27437 27437
     }
27438 27438
    },
... ...
@@ -51353,7 +51353,7 @@
51353 51353
       "$ref": "#/definitions/v1.RouteTargetReference"
51354 51354
      },
51355 51355
      "wildcardPolicy": {
51356
-      "description": "Wildcard policy if any for the route.",
51356
+      "description": "Wildcard policy if any for the route. Currently only 'Subdomain' or 'None' is allowed.",
51357 51357
       "type": "string"
51358 51358
      }
51359 51359
     }
... ...
@@ -36,19 +36,12 @@ func RouteLessThan(route1, route2 *Route) bool {
36 36
 	return false
37 37
 }
38 38
 
39
-// GetSubdomainForHost returns the subdomain for the specified host.
40
-// This handles top-level domain names as well.
41
-// Example: acme.test and www.acme.test both return acme.test
42
-//          and www1.edge.acme.test returns edge.acme.test
43
-func GetSubdomainForHost(host string) string {
44
-	if len(host) == 0 {
45
-		return host
39
+// GetDomainForHost returns the domain for the specified host.
40
+// Note for top level domains, this will return an empty string.
41
+func GetDomainForHost(host string) string {
42
+	if idx := strings.IndexRune(host, '.'); idx > -1 {
43
+		return host[idx+1:]
46 44
 	}
47 45
 
48
-	parts := strings.SplitAfterN(host, ".", 3)
49
-	if len(parts) > 2 {
50
-		return parts[1] + parts[2]
51
-	}
52
-
53
-	return host
46
+	return ""
54 47
 }
... ...
@@ -89,7 +89,7 @@ func TestRouteLessThan(t *testing.T) {
89 89
 	}
90 90
 }
91 91
 
92
-func TestGetSubdomainForHost(t *testing.T) {
92
+func TestGetDomainForHost(t *testing.T) {
93 93
 	tests := []struct {
94 94
 		name        string
95 95
 		host        string
... ...
@@ -101,24 +101,24 @@ func TestGetSubdomainForHost(t *testing.T) {
101 101
 			expectation: "host.test",
102 102
 		},
103 103
 		{
104
-			name:        "plain2 aceswild",
104
+			name:        "aceswild",
105 105
 			host:        "www777.aceswild.test",
106 106
 			expectation: "aceswild.test",
107 107
 		},
108 108
 		{
109 109
 			name:        "subdomain1",
110 110
 			host:        "one.test",
111
-			expectation: "one.test",
111
+			expectation: "test",
112 112
 		},
113 113
 		{
114 114
 			name:        "subdomain2",
115 115
 			host:        "two.test",
116
-			expectation: "two.test",
116
+			expectation: "test",
117 117
 		},
118 118
 		{
119 119
 			name:        "subdomain3",
120 120
 			host:        "three.org",
121
-			expectation: "three.org",
121
+			expectation: "org",
122 122
 		},
123 123
 		{
124 124
 			name:        "nested subdomain",
... ...
@@ -143,17 +143,12 @@ func TestGetSubdomainForHost(t *testing.T) {
143 143
 		{
144 144
 			name:        "tld1",
145 145
 			host:        "test",
146
-			expectation: "test",
146
+			expectation: "",
147 147
 		},
148 148
 		{
149 149
 			name:        "tld2",
150 150
 			host:        "org",
151
-			expectation: "org",
152
-		},
153
-		{
154
-			name:        "tld3",
155
-			host:        "com",
156
-			expectation: "com",
151
+			expectation: "",
157 152
 		},
158 153
 		{
159 154
 			name:        "semi-longish host",
... ...
@@ -163,7 +158,7 @@ func TestGetSubdomainForHost(t *testing.T) {
163 163
 	}
164 164
 
165 165
 	for _, tc := range tests {
166
-		subdomain := GetSubdomainForHost(tc.host)
166
+		subdomain := GetDomainForHost(tc.host)
167 167
 
168 168
 		if subdomain != tc.expectation {
169 169
 			t.Errorf("Test case %s expected %v got %v", tc.name, tc.expectation, subdomain)
... ...
@@ -2,7 +2,6 @@ package controller
2 2
 
3 3
 import (
4 4
 	"fmt"
5
-	"strings"
6 5
 
7 6
 	"github.com/golang/glog"
8 7
 	kapi "k8s.io/kubernetes/pkg/api"
... ...
@@ -147,7 +146,7 @@ func (p *HostAdmitter) HandleRoute(eventType watch.EventType, route *routeapi.Ro
147 147
 
148 148
 		case watch.Deleted:
149 149
 			p.claimedHosts.RemoveRoute(route.Spec.Host, route)
150
-			wildcardKey := getDomain(route.Spec.Host)
150
+			wildcardKey := routeapi.GetDomainForHost(route.Spec.Host)
151 151
 			p.claimedWildcards.RemoveRoute(wildcardKey, route)
152 152
 			p.blockedWildcards.RemoveRoute(wildcardKey, route)
153 153
 		}
... ...
@@ -166,14 +165,6 @@ func (p *HostAdmitter) SetLastSyncProcessed(processed bool) error {
166 166
 	return p.plugin.SetLastSyncProcessed(processed)
167 167
 }
168 168
 
169
-func getDomain(hostname string) string {
170
-	index := strings.IndexRune(hostname, '.')
171
-	if index == -1 {
172
-		return ""
173
-	}
174
-	return hostname[index+1:]
175
-}
176
-
177 169
 // addRoute admits routes based on subdomain ownership - returns errors if the route is not admitted.
178 170
 func (p *HostAdmitter) addRoute(route *routeapi.Route) error {
179 171
 	// Find displaced routes (or error if an existing route displaces us)
... ...
@@ -186,7 +177,7 @@ func (p *HostAdmitter) addRoute(route *routeapi.Route) error {
186 186
 
187 187
 	// Remove displaced routes
188 188
 	for _, displacedRoute := range displacedRoutes {
189
-		wildcardKey := getDomain(displacedRoute.Spec.Host)
189
+		wildcardKey := routeapi.GetDomainForHost(displacedRoute.Spec.Host)
190 190
 		p.claimedHosts.RemoveRoute(displacedRoute.Spec.Host, displacedRoute)
191 191
 		p.blockedWildcards.RemoveRoute(wildcardKey, displacedRoute)
192 192
 		p.claimedWildcards.RemoveRoute(wildcardKey, displacedRoute)
... ...
@@ -197,7 +188,7 @@ func (p *HostAdmitter) addRoute(route *routeapi.Route) error {
197 197
 	}
198 198
 
199 199
 	// Add the new route
200
-	wildcardKey := getDomain(route.Spec.Host)
200
+	wildcardKey := routeapi.GetDomainForHost(route.Spec.Host)
201 201
 
202 202
 	switch route.Spec.WildcardPolicy {
203 203
 	case routeapi.WildcardPolicyNone:
... ...
@@ -233,7 +224,7 @@ func (p *HostAdmitter) displacedRoutes(newRoute *routeapi.Route) ([]*routeapi.Ro
233 233
 		displaced = append(displaced, p.claimedHosts[newRoute.Spec.Host][i])
234 234
 	}
235 235
 
236
-	wildcardKey := getDomain(newRoute.Spec.Host)
236
+	wildcardKey := routeapi.GetDomainForHost(newRoute.Spec.Host)
237 237
 
238 238
 	// See if any existing wildcard routes block our domain, or if we displace them
239 239
 	for i, route := range p.claimedWildcards[wildcardKey] {
... ...
@@ -210,7 +210,7 @@ func matchPattern(pattern, s string) bool {
210 210
 // Generate a regular expression to match wildcard hosts (and paths if any)
211 211
 // for a [sub]domain.
212 212
 func genSubdomainWildcardRegexp(hostname, path string, exactPath bool) string {
213
-	subdomain := routeapi.GetSubdomainForHost(hostname)
213
+	subdomain := routeapi.GetDomainForHost(hostname)
214 214
 	if len(subdomain) == 0 {
215 215
 		glog.Warningf("Generating subdomain wildcard regexp - invalid host name %s", hostname)
216 216
 		return fmt.Sprintf("%s%s", hostname, path)