-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Autogenerate route names if host is not specified - second version with plugin based allocator support #1291
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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) AllocateRouterShard(route *routeapi.Route) (*routeapi.RouterShard, error) { | ||
|
|
||
| glog.V(4).Infof("Allocating router shard for Route: %s [alias=%s]", | ||
| route.ServiceName, route.Host) | ||
|
|
||
| shard, err := c.Plugin.Allocate(route) | ||
|
|
||
| if err != nil { | ||
| glog.Errorf("unable to allocate router shard: %v", err) | ||
| return shard, err | ||
| } | ||
|
|
||
| glog.V(4).Infof("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 hidden or 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.AllocateRouterShard(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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,19 @@ | ||
| 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 | ||
| } | ||
|
|
||
| // RouteAllocator is the interface for the route allocation controller | ||
| // which handles requests for RouterShard allocation and name generation. | ||
| type RouteAllocator interface { | ||
| AllocateRouterShard(*api.Route) (*api.RouterShard, error) | ||
| GenerateHostname(*api.Route, *api.RouterShard) string | ||
| } |
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -11,18 +11,21 @@ import ( | |
| "github.com/GoogleCloudPlatform/kubernetes/pkg/runtime" | ||
| "github.com/GoogleCloudPlatform/kubernetes/pkg/watch" | ||
|
|
||
| "github.com/openshift/origin/pkg/route" | ||
| "github.com/openshift/origin/pkg/route/api" | ||
| "github.com/openshift/origin/pkg/route/api/validation" | ||
| ) | ||
|
|
||
| // REST is an implementation of RESTStorage for the api server. | ||
| type REST struct { | ||
| registry Registry | ||
| registry Registry | ||
| allocator route.RouteAllocator | ||
| } | ||
|
|
||
| func NewREST(registry Registry) *REST { | ||
| func NewREST(registry Registry, allocator route.RouteAllocator) *REST { | ||
| return &REST{ | ||
| registry: registry, | ||
| registry: registry, | ||
| allocator: allocator, | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -71,6 +74,15 @@ func (rs *REST) Create(ctx kapi.Context, obj runtime.Object) (runtime.Object, er | |
| return nil, errors.NewConflict("route", route.Namespace, fmt.Errorf("Route.Namespace does not match the provided context")) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The formatting here looks messed up
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, I thought I used gofmt to fix it. |
||
| } | ||
|
|
||
| shard, err := rs.allocator.AllocateRouterShard(route) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("allocation error: %s for route: %#v", err, obj) | ||
| } | ||
|
|
||
| if len(route.Host) == 0 { | ||
| route.Host = rs.allocator.GenerateHostname(route, shard) | ||
| } | ||
|
|
||
| if errs := validation.ValidateRoute(route); len(errs) > 0 { | ||
| return nil, errors.NewInvalid("route", route.Name, errs) | ||
| } | ||
|
|
@@ -82,7 +94,7 @@ func (rs *REST) Create(ctx kapi.Context, obj runtime.Object) (runtime.Object, er | |
|
|
||
| escapeNewLines(route.TLS) | ||
|
|
||
| err := rs.registry.CreateRoute(ctx, route) | ||
| err = rs.registry.CreateRoute(ctx, route) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Define json mappings