forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add simple shard allocator plugin to autogenerate host names for routes
based on service and namespace and hook it into the route processing [GOFM].
- Loading branch information
Showing
17 changed files
with
561 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package allocation | ||
|
||
import ( | ||
"github.com/golang/glog" | ||
|
||
"github.com/openshift/origin/pkg/route" | ||
routeapi "github.com/openshift/origin/pkg/route/api" | ||
) | ||
|
||
// RouteAllocationController abstracts the details of how routes are | ||
// allocated to router shards. | ||
type RouteAllocationController struct { | ||
Plugin route.AllocationPlugin | ||
} | ||
|
||
// Allocate a router shard for the given route. | ||
func (c *RouteAllocationController) Allocate(route *routeapi.Route) (*routeapi.RouterShard, error) { | ||
|
||
glog.V(4).Infof("RoutingAllocationController: Allocating shard for Route: %s [alias=%s]", | ||
route.ServiceName, route.Host) | ||
|
||
shard, err := c.Plugin.Allocate(route) | ||
|
||
if err != nil { | ||
glog.Errorf("RoutingAllocationController: Unable to allocate router shard: %v", err) | ||
return shard, err | ||
} | ||
|
||
glog.V(4).Infof("RoutingAllocationController: Route %s allocated to shard %s [suffix=%s]", | ||
route.ServiceName, shard.ShardName, shard.DNSSuffix) | ||
|
||
return shard, err | ||
} | ||
|
||
// Generate a host name for the given route and router shard combination. | ||
func (c *RouteAllocationController) GenerateHostname(route *routeapi.Route, shard *routeapi.RouterShard) string { | ||
glog.V(4).Infof("Generating host name for Route: %s", | ||
route.ServiceName) | ||
|
||
s := c.Plugin.GenerateHostname(route, shard) | ||
|
||
glog.V(4).Infof("Route: %s, generated host name/alias=%s", | ||
route.ServiceName, s) | ||
|
||
return s | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package allocation | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
kapi "github.com/GoogleCloudPlatform/kubernetes/pkg/api" | ||
routeapi "github.com/openshift/origin/pkg/route/api" | ||
) | ||
|
||
type TestAllocationPlugin struct { | ||
Name string | ||
} | ||
|
||
func (p *TestAllocationPlugin) Allocate(route *routeapi.Route) (*routeapi.RouterShard, error) { | ||
|
||
return &routeapi.RouterShard{ShardName: "test", DNSSuffix: "openshift.test"}, nil | ||
} | ||
|
||
func (p *TestAllocationPlugin) GenerateHostname(route *routeapi.Route, shard *routeapi.RouterShard) string { | ||
if len(route.ServiceName) > 0 && len(route.Namespace) > 0 { | ||
return fmt.Sprintf("%s-%s.%s", route.ServiceName, route.Namespace, shard.DNSSuffix) | ||
} | ||
|
||
return "test-test-test.openshift.test" | ||
} | ||
|
||
func TestRouteAllocationController(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
route *routeapi.Route | ||
}{ | ||
{ | ||
name: "No Name", | ||
route: &routeapi.Route{ | ||
ObjectMeta: kapi.ObjectMeta{ | ||
Namespace: "namespace", | ||
}, | ||
ServiceName: "service", | ||
}, | ||
}, | ||
{ | ||
name: "No namespace", | ||
route: &routeapi.Route{ | ||
ObjectMeta: kapi.ObjectMeta{ | ||
Name: "name", | ||
}, | ||
ServiceName: "nonamespace", | ||
}, | ||
}, | ||
{ | ||
name: "No service name", | ||
route: &routeapi.Route{ | ||
ObjectMeta: kapi.ObjectMeta{ | ||
Name: "name", | ||
Namespace: "foo", | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "Valid route", | ||
route: &routeapi.Route{ | ||
ObjectMeta: kapi.ObjectMeta{ | ||
Name: "name", | ||
Namespace: "foo", | ||
}, | ||
Host: "www.example.org", | ||
ServiceName: "serviceName", | ||
}, | ||
}, | ||
} | ||
|
||
plugin := &TestAllocationPlugin{Name: "test allocation plugin"} | ||
fac := &RouteAllocationControllerFactory{nil, nil} | ||
allocator := fac.Create(plugin) | ||
for _, tc := range tests { | ||
shard, err := allocator.Allocate(tc.route) | ||
if err != nil { | ||
t.Errorf("Test case %s got an error %s", tc.name, err) | ||
continue | ||
} | ||
name := allocator.GenerateHostname(tc.route, shard) | ||
if len(name) <= 0 { | ||
t.Errorf("Test case %s got %d length name", tc.name, len(name)) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// Package allocation contains all the route allocation controllers. | ||
package allocation |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package allocation | ||
|
||
import ( | ||
kclient "github.com/GoogleCloudPlatform/kubernetes/pkg/client" | ||
|
||
osclient "github.com/openshift/origin/pkg/client" | ||
"github.com/openshift/origin/pkg/route" | ||
) | ||
|
||
// RouteAllocationControllerFactory creates a RouteAllocationController | ||
// that allocates router shards to specific routes. | ||
type RouteAllocationControllerFactory struct { | ||
// Client is is an OpenShift client. | ||
OSClient osclient.Interface | ||
|
||
// KubeClient is a Kubernetes client. | ||
KubeClient kclient.Interface | ||
} | ||
|
||
// Create a RouteAllocationController instance. | ||
func (factory *RouteAllocationControllerFactory) Create(plugin route.AllocationPlugin) *RouteAllocationController { | ||
return &RouteAllocationController{Plugin: plugin} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package test | ||
|
||
import ( | ||
"fmt" | ||
|
||
routeapi "github.com/openshift/origin/pkg/route/api" | ||
"github.com/openshift/origin/pkg/route/controller/allocation" | ||
) | ||
|
||
type TestAllocationPlugin struct { | ||
Name string | ||
} | ||
|
||
func (p *TestAllocationPlugin) Allocate(route *routeapi.Route) (*routeapi.RouterShard, error) { | ||
|
||
return &routeapi.RouterShard{ShardName: "test", DNSSuffix: "openshift.test"}, nil | ||
} | ||
|
||
func (p *TestAllocationPlugin) GenerateHostname(route *routeapi.Route, shard *routeapi.RouterShard) string { | ||
if len(route.ServiceName) > 0 && len(route.Namespace) > 0 { | ||
return fmt.Sprintf("%s-%s.%s", route.ServiceName, route.Namespace, shard.DNSSuffix) | ||
} | ||
|
||
return "test-test-test.openshift.test" | ||
} | ||
|
||
func NewTestRouteAllocationController() *allocation.RouteAllocationController { | ||
plugin := &TestAllocationPlugin{"test route allocation plugin"} | ||
return &allocation.RouteAllocationController{Plugin: plugin} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// Package controller contains all the route handling controllers. | ||
package controller |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package route | ||
|
||
import ( | ||
api "github.com/openshift/origin/pkg/route/api" | ||
) | ||
|
||
// AllocationPlugin is the interface the route controller dispatches | ||
// requests for RouterShard allocation and name generation. | ||
type AllocationPlugin interface { | ||
Allocate(*api.Route) (*api.RouterShard, error) | ||
GenerateHostname(*api.Route, *api.RouterShard) string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.