Skip to content

Commit 8388ce2

Browse files
author
Paul Weil
committed
allocate route host on update if host is empty in spec
1 parent 6619616 commit 8388ce2

File tree

2 files changed

+77
-14
lines changed

2 files changed

+77
-14
lines changed

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

Lines changed: 48 additions & 2 deletions
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"
@@ -31,7 +32,7 @@ func (a *testAllocator) GenerateHostname(*api.Route, *api.RouterShard) string {
3132
return a.Hostname
3233
}
3334

34-
func newStorage(t *testing.T, allocator *testAllocator) (*REST, *etcdtesting.EtcdTestServer) {
35+
func newStorage(t *testing.T, allocator routetypes.RouteAllocator) (*REST, *etcdtesting.EtcdTestServer) {
3536
etcdStorage, server := registrytest.NewEtcdStorage(t, "")
3637
storage, _ := NewREST(etcdStorage, allocator)
3738
return storage, server
@@ -52,7 +53,7 @@ func validRoute() *api.Route {
5253
}
5354

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

pkg/route/registry/route/strategy.go

Lines changed: 29 additions & 12 deletions
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)