Skip to content

Commit 1e92fe5

Browse files
author
Paul Weil
committed
allocate route host on update if host is empty in spec
1 parent de2a8bc commit 1e92fe5

File tree

2 files changed

+77
-14
lines changed

2 files changed

+77
-14
lines changed

pkg/route/registry/route/etcd/etcd_test.go

+48-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"k8s.io/kubernetes/pkg/runtime"
1111
etcdtesting "k8s.io/kubernetes/pkg/storage/etcd/testing"
1212

13+
routetypes "github.com/openshift/origin/pkg/route"
1314
"github.com/openshift/origin/pkg/route/api"
1415
_ "github.com/openshift/origin/pkg/route/api/install"
1516
"github.com/openshift/origin/pkg/route/registry/route"
@@ -32,7 +33,7 @@ func (a *testAllocator) GenerateHostname(*api.Route, *api.RouterShard) string {
3233
return a.Hostname
3334
}
3435

35-
func newStorage(t *testing.T, allocator *testAllocator) (*REST, *etcdtesting.EtcdTestServer) {
36+
func newStorage(t *testing.T, allocator routetypes.RouteAllocator) (*REST, *etcdtesting.EtcdTestServer) {
3637
etcdStorage, server := registrytest.NewEtcdStorage(t, "")
3738
storage, _, err := NewREST(restoptions.NewSimpleGetter(etcdStorage), allocator)
3839
if err != nil {
@@ -56,7 +57,7 @@ func validRoute() *api.Route {
5657
}
5758

5859
func TestCreate(t *testing.T) {
59-
storage, server := newStorage(t, &testAllocator{})
60+
storage, server := newStorage(t, nil)
6061
defer server.Terminate(t)
6162
test := registrytest.New(t, storage.Etcd)
6263
test.TestCreate(
@@ -114,6 +115,51 @@ func TestUpdate(t *testing.T) {
114115
},
115116
)
116117
}
118+
119+
func TestUpdateWithAllocation(t *testing.T) {
120+
allocator := &testAllocator{Hostname: "bar"}
121+
storage, server := newStorage(t, allocator)
122+
defer server.Terminate(t)
123+
124+
// create a route with a populated host
125+
originalRoute := validRoute()
126+
originalRoute.Spec.Host = "foo"
127+
created, err := storage.Create(kapi.NewDefaultContext(), originalRoute)
128+
if err != nil {
129+
t.Fatalf("error creating valid route to test allocations: %v", err)
130+
}
131+
132+
createdRoute := created.(*api.Route)
133+
if createdRoute.Spec.Host != "foo" {
134+
t.Fatalf("unexpected host on createdRoute: %#v", createdRoute)
135+
}
136+
if _, ok := createdRoute.Annotations[route.HostGeneratedAnnotationKey]; ok {
137+
t.Fatalf("created route should not have the generated host annotation")
138+
}
139+
140+
// update the route to set the host to empty
141+
createdRoute.Spec.Host = ""
142+
updated, _, err := storage.Update(kapi.NewDefaultContext(), createdRoute)
143+
if err != nil {
144+
t.Fatalf("error updating route to test allocations: %v", err)
145+
}
146+
147+
// route should now have the allocated host of bar and the generated host annotation
148+
updatedRoute := updated.(*api.Route)
149+
if updatedRoute == nil {
150+
t.Fatalf("expected updatedRoute to not be nil")
151+
}
152+
if updatedRoute.Spec.Host != "bar" {
153+
t.Fatalf("unexpected route: %#v", updatedRoute)
154+
}
155+
if v, ok := updatedRoute.Annotations[route.HostGeneratedAnnotationKey]; !ok || v != "true" {
156+
t.Fatalf("unexpected route: %#v", updatedRoute)
157+
}
158+
if !allocator.Allocate || !allocator.Generate {
159+
t.Fatalf("unexpected allocator: %#v", allocator)
160+
}
161+
}
162+
117163
func TestList(t *testing.T) {
118164
storage, server := newStorage(t, nil)
119165
defer server.Terminate(t)

pkg/route/registry/route/strategy.go

+29-12
Original file line numberDiff line numberDiff line change
@@ -46,29 +46,46 @@ func (s routeStrategy) PrepareForCreate(obj runtime.Object) {
4646
// Limit to kind/name
4747
// TODO: convert to LocalObjectReference
4848
route.Spec.To = kapi.ObjectReference{Kind: route.Spec.To.Kind, Name: route.Spec.To.Name}
49+
err := s.allocateHost(route)
50+
if err != nil {
51+
// TODO: this will be changed when moved to a controller
52+
utilruntime.HandleError(errors.NewInternalError(fmt.Errorf("allocation error: %v for route: %#v", err, obj)))
53+
}
54+
}
55+
56+
func (s routeStrategy) PrepareForUpdate(obj, old runtime.Object) {
57+
route := obj.(*api.Route)
58+
oldRoute := old.(*api.Route)
59+
route.Status = oldRoute.Status
60+
// Limit to kind/name
61+
// TODO: convert to LocalObjectReference
62+
route.Spec.To = kapi.ObjectReference{Kind: route.Spec.To.Kind, Name: route.Spec.To.Name}
63+
64+
// if the route host has been updated to empty we should allocate the host
65+
err := s.allocateHost(route)
66+
if err != nil {
67+
// TODO: this will be changed when moved to a controller
68+
utilruntime.HandleError(errors.NewInternalError(fmt.Errorf("allocation error: %v for route: %#v", err, obj)))
69+
}
70+
}
71+
72+
// allocateHost allocates a host name ONLY if the host name on the route is empty and an allocator
73+
// is configured. It must first allocate the shard and may return an error if shard allocation
74+
// fails.
75+
func (s routeStrategy) allocateHost(route *api.Route) error {
4976
if len(route.Spec.Host) == 0 && s.RouteAllocator != nil {
5077
// TODO: this does not belong here, and should be removed
5178
shard, err := s.RouteAllocator.AllocateRouterShard(route)
5279
if err != nil {
53-
// TODO: this will be changed when moved to a controller
54-
utilruntime.HandleError(errors.NewInternalError(fmt.Errorf("allocation error: %v for route: %#v", err, obj)))
55-
return
80+
return errors.NewInternalError(fmt.Errorf("allocation error: %v for route: %#v", err, route))
5681
}
5782
route.Spec.Host = s.RouteAllocator.GenerateHostname(route, shard)
5883
if route.Annotations == nil {
5984
route.Annotations = map[string]string{}
6085
}
6186
route.Annotations[HostGeneratedAnnotationKey] = "true"
6287
}
63-
}
64-
65-
func (routeStrategy) PrepareForUpdate(obj, old runtime.Object) {
66-
route := obj.(*api.Route)
67-
oldRoute := old.(*api.Route)
68-
route.Status = oldRoute.Status
69-
// Limit to kind/name
70-
// TODO: convert to LocalObjectReference
71-
route.Spec.To = kapi.ObjectReference{Kind: route.Spec.To.Kind, Name: route.Spec.To.Name}
88+
return nil
7289
}
7390

7491
func (routeStrategy) Validate(ctx kapi.Context, obj runtime.Object) field.ErrorList {

0 commit comments

Comments
 (0)