diff --git a/pkg/route/registry/route/strategy.go b/pkg/route/registry/route/strategy.go index 02284cbc30c4..54182c3291d0 100644 --- a/pkg/route/registry/route/strategy.go +++ b/pkg/route/registry/route/strategy.go @@ -60,6 +60,11 @@ func (s routeStrategy) PrepareForUpdate(obj, old runtime.Object) { // Limit to kind/name // TODO: convert to LocalObjectReference route.Spec.To = kapi.ObjectReference{Kind: route.Spec.To.Kind, Name: route.Spec.To.Name} + // Ignore attempts to clear the spec Host + // Prevents "immutable field" errors when applying the same route definition used to create + if len(route.Spec.Host) == 0 { + route.Spec.Host = oldRoute.Spec.Host + } } // allocateHost allocates a host name ONLY if the host name on the route is empty and an allocator diff --git a/pkg/route/registry/route/strategy_test.go b/pkg/route/registry/route/strategy_test.go new file mode 100644 index 000000000000..b98a32a04fa7 --- /dev/null +++ b/pkg/route/registry/route/strategy_test.go @@ -0,0 +1,48 @@ +package route + +import ( + "testing" + + "github.com/openshift/origin/pkg/route/api" + kapi "k8s.io/kubernetes/pkg/api" + "k8s.io/kubernetes/pkg/types" +) + +type testAllocator struct { +} + +func (t testAllocator) AllocateRouterShard(*api.Route) (*api.RouterShard, error) { + return &api.RouterShard{}, nil +} +func (t testAllocator) GenerateHostname(*api.Route, *api.RouterShard) string { + return "mygeneratedhost.com" +} + +func TestEmptyHostDefaulting(t *testing.T) { + strategy := NewStrategy(testAllocator{}) + + hostlessCreatedRoute := &api.Route{} + strategy.PrepareForCreate(hostlessCreatedRoute) + if hostlessCreatedRoute.Spec.Host != "mygeneratedhost.com" { + t.Fatalf("Expected host to be allocated, got %s", hostlessCreatedRoute.Spec.Host) + } + + persistedRoute := &api.Route{ + ObjectMeta: kapi.ObjectMeta{ + Namespace: "foo", + Name: "myroute", + UID: types.UID("abc"), + ResourceVersion: "1", + }, + Spec: api.RouteSpec{ + Host: "myhost.com", + }, + } + obj, _ := kapi.Scheme.DeepCopy(persistedRoute) + hostlessUpdatedRoute := obj.(*api.Route) + hostlessUpdatedRoute.Spec.Host = "" + strategy.PrepareForUpdate(hostlessUpdatedRoute, persistedRoute) + if hostlessUpdatedRoute.Spec.Host != "myhost.com" { + t.Fatalf("expected empty spec.host to default to existing spec.host, got %s", hostlessUpdatedRoute.Spec.Host) + } +}