Skip to content

Commit a187f61

Browse files
Merge pull request #550 from rikatz/route-bump-generation-on-updates
OCPBUGS-61228: Bump Route generation when spec is updated
2 parents 6301ea5 + 2407878 commit a187f61

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

pkg/route/apiserver/registry/route/strategy.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66

77
authorizationapi "k8s.io/api/authorization/v1"
8+
apiequality "k8s.io/apimachinery/pkg/api/equality"
89
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
910
"k8s.io/apimachinery/pkg/runtime"
1011
"k8s.io/apimachinery/pkg/util/validation/field"
@@ -66,6 +67,7 @@ func (s routeStrategy) routeValidationOptions() routecommon.RouteValidationOptio
6667
func (s routeStrategy) PrepareForCreate(ctx context.Context, obj runtime.Object) {
6768
route := obj.(*routeapi.Route)
6869
route.Status = routeapi.RouteStatus{}
70+
route.Generation = 1
6971
stripEmptyDestinationCACertificate(route)
7072

7173
// In kube APIs, disabled fields are stripped from inbound objects.
@@ -96,6 +98,15 @@ func (s routeStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Ob
9698
route.Spec.TLS.ExternalCertificate = nil
9799
}
98100
}
101+
102+
// Any changes to the spec increment the generation number.
103+
// Changes to status, or to any metadata field (eg.: labels and annotations)
104+
// should not impact on the generation
105+
// Preserve existing generation unless the Spec changes.
106+
route.Generation = oldRoute.Generation
107+
if !apiequality.Semantic.DeepEqual(oldRoute.Spec, route.Spec) {
108+
route.Generation = oldRoute.Generation + 1
109+
}
99110
}
100111

101112
func (s routeStrategy) Validate(ctx context.Context, obj runtime.Object) field.ErrorList {

pkg/route/apiserver/registry/route/strategy_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ func TestEmptyDefaultCACertificate(t *testing.T) {
120120
Name: "myroute",
121121
UID: types.UID("abc"),
122122
ResourceVersion: "1",
123+
Generation: 1,
123124
},
124125
Spec: routeapi.RouteSpec{
125126
Host: "myhost.com",
@@ -1112,3 +1113,45 @@ func TestExternalCertRemoval(t *testing.T) {
11121113
}
11131114
}
11141115
}
1116+
1117+
func TestRouteGenerationManagement(t *testing.T) {
1118+
ctx := apirequest.NewContext()
1119+
strategy := NewStrategy(testAllocator{}, &testSAR{allow: true}, &testSecretGetter{}, true)
1120+
1121+
simpleRoute := &routeapi.Route{}
1122+
strategy.Validate(ctx, simpleRoute)
1123+
if simpleRoute.Spec.Host != "mygeneratedhost.com" {
1124+
t.Fatalf("Expected host to be allocated, got %s", simpleRoute.Spec.Host)
1125+
}
1126+
1127+
if simpleRoute.Generation > 0 {
1128+
t.Fatalf("Expected generation to not be allocated yet, got %d", simpleRoute.Generation)
1129+
}
1130+
1131+
// PrepareForCreate should set a generation 1
1132+
strategy.PrepareForCreate(ctx, simpleRoute)
1133+
if simpleRoute.Generation != 1 {
1134+
t.Fatalf("Expected generation after create to be 1, got %d", simpleRoute.Generation)
1135+
}
1136+
1137+
newRoute := simpleRoute.DeepCopy()
1138+
// Changing annotations and labels should not bump the generation
1139+
newRoute.Annotations = map[string]string{
1140+
"someannotation": "novalue",
1141+
}
1142+
newRoute.Labels = map[string]string{
1143+
"somelabel": "novalue",
1144+
}
1145+
1146+
strategy.PrepareForUpdate(ctx, newRoute, simpleRoute)
1147+
if newRoute.Generation != 1 {
1148+
t.Fatalf("Expected generation after metadata update to still be 1, got %d", newRoute.Generation)
1149+
}
1150+
1151+
// Updating the spec should bump the generation
1152+
newRoute.Spec.Path = "/xpto"
1153+
strategy.PrepareForUpdate(ctx, newRoute, simpleRoute)
1154+
if newRoute.Generation != 2 {
1155+
t.Fatalf("Expected generation after spec update to be 2, got %d", newRoute.Generation)
1156+
}
1157+
}

0 commit comments

Comments
 (0)