Skip to content

Commit d269ed5

Browse files
committed
Allow customising the HTTPRoute name
1 parent d29c60d commit d269ed5

File tree

4 files changed

+83
-23
lines changed

4 files changed

+83
-23
lines changed

pkg/apis/flagger/v1beta1/canary.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,11 @@ type CanaryService struct {
225225
// +optional
226226
Backends []string `json:"backends,omitempty"`
227227

228+
// HTTPRouteName is the name of the HTTPRoute generated by Flagger
229+
// for the Gateway API provider. Defaults to the apex service name.
230+
// +optional
231+
HTTPRouteName string `json:"httpRouteName,omitempty"`
232+
228233
// Apex is metadata to add to the apex service
229234
// +optional
230235
Apex *CustomMetadata `json:"apex,omitempty"`
@@ -610,6 +615,16 @@ func (c *Canary) GetServiceNames() (apexName, primaryName, canaryName string) {
610615
return
611616
}
612617

618+
// GetHTTPRouteName returns the name to use for the HTTPRoute resource.
619+
// Defaults to the apex service name if not explicitly set.
620+
func (c *Canary) GetHTTPRouteName() string {
621+
if c.Spec.Service.HTTPRouteName != "" {
622+
return c.Spec.Service.HTTPRouteName
623+
}
624+
apexName, _, _ := c.GetServiceNames()
625+
return apexName
626+
}
627+
613628
// GetProgressDeadlineSeconds returns the progress deadline (default 600s)
614629
func (c *Canary) GetProgressDeadlineSeconds() int {
615630
if c.Spec.ProgressDeadlineSeconds != nil {

pkg/router/gateway_api.go

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ func (gwr *GatewayAPIRouter) Reconcile(canary *flaggerv1.Canary) error {
6767
return fmt.Errorf("GatewayRefs must be specified when using Gateway API as a provider.")
6868
}
6969

70-
apexSvcName, primarySvcName, canarySvcName := canary.GetServiceNames()
70+
_, primarySvcName, canarySvcName := canary.GetServiceNames()
71+
routeName := canary.GetHTTPRouteName()
7172

7273
hrNamespace := canary.Namespace
7374

@@ -138,7 +139,7 @@ func (gwr *GatewayAPIRouter) Reconcile(canary *flaggerv1.Canary) error {
138139
}
139140

140141
httpRoute, err := gwr.gatewayAPIClient.GatewayapiV1().HTTPRoutes(hrNamespace).Get(
141-
context.TODO(), apexSvcName, metav1.GetOptions{},
142+
context.TODO(), routeName, metav1.GetOptions{},
142143
)
143144

144145
newMetadata := canary.Spec.Service.Apex
@@ -156,7 +157,7 @@ func (gwr *GatewayAPIRouter) Reconcile(canary *flaggerv1.Canary) error {
156157
if errors.IsNotFound(err) {
157158
route := &v1.HTTPRoute{
158159
ObjectMeta: metav1.ObjectMeta{
159-
Name: apexSvcName,
160+
Name: routeName,
160161
Namespace: hrNamespace,
161162
Labels: newMetadata.Labels,
162163
Annotations: newMetadata.Annotations,
@@ -178,13 +179,13 @@ func (gwr *GatewayAPIRouter) Reconcile(canary *flaggerv1.Canary) error {
178179
Create(context.TODO(), route, metav1.CreateOptions{})
179180

180181
if err != nil {
181-
return fmt.Errorf("HTTPRoute %s.%s create error: %w", apexSvcName, hrNamespace, err)
182+
return fmt.Errorf("HTTPRoute %s.%s create error: %w", routeName, hrNamespace, err)
182183
}
183184
gwr.logger.With("canary", fmt.Sprintf("%s.%s", canary.Name, canary.Namespace)).
184185
Infof("HTTPRoute %s.%s created", route.GetName(), hrNamespace)
185186
return nil
186187
} else if err != nil {
187-
return fmt.Errorf("HTTPRoute %s.%s get error: %w", apexSvcName, hrNamespace, err)
188+
return fmt.Errorf("HTTPRoute %s.%s get error: %w", routeName, hrNamespace, err)
188189
}
189190

190191
ignoreCmpOptions := []cmp.Option{
@@ -266,11 +267,12 @@ func (gwr *GatewayAPIRouter) GetRoutes(canary *flaggerv1.Canary) (
266267
mirrored bool,
267268
err error,
268269
) {
269-
apexSvcName, primarySvcName, canarySvcName := canary.GetServiceNames()
270+
_, primarySvcName, canarySvcName := canary.GetServiceNames()
271+
routeName := canary.GetHTTPRouteName()
270272
hrNamespace := canary.Namespace
271-
httpRoute, err := gwr.gatewayAPIClient.GatewayapiV1().HTTPRoutes(hrNamespace).Get(context.TODO(), apexSvcName, metav1.GetOptions{})
273+
httpRoute, err := gwr.gatewayAPIClient.GatewayapiV1().HTTPRoutes(hrNamespace).Get(context.TODO(), routeName, metav1.GetOptions{})
272274
if err != nil {
273-
err = fmt.Errorf("HTTPRoute %s.%s get error: %w", apexSvcName, hrNamespace, err)
275+
err = fmt.Errorf("HTTPRoute %s.%s get error: %w", routeName, hrNamespace, err)
274276
return
275277
}
276278

@@ -285,7 +287,7 @@ func (gwr *GatewayAPIRouter) GetRoutes(canary *flaggerv1.Canary) (
285287
if condition.Type == string(v1.RouteConditionAccepted) && (condition.Status != metav1.ConditionTrue || condition.ObservedGeneration < currentGeneration) {
286288
err = fmt.Errorf(
287289
"HTTPRoute %s.%s parent %s is not ready (status: %s, observed generation: %d, current generation: %d)",
288-
apexSvcName, hrNamespace, parentRef.Name, string(condition.Status), condition.ObservedGeneration, currentGeneration,
290+
routeName, hrNamespace, parentRef.Name, string(condition.Status), condition.ObservedGeneration, currentGeneration,
289291
)
290292
return 0, 0, false, err
291293
}
@@ -346,11 +348,12 @@ func (gwr *GatewayAPIRouter) SetRoutes(
346348
) error {
347349
pWeight := int32(primaryWeight)
348350
cWeight := int32(canaryWeight)
349-
apexSvcName, primarySvcName, canarySvcName := canary.GetServiceNames()
351+
_, primarySvcName, canarySvcName := canary.GetServiceNames()
352+
routeName := canary.GetHTTPRouteName()
350353
hrNamespace := canary.Namespace
351-
httpRoute, err := gwr.gatewayAPIClient.GatewayapiV1().HTTPRoutes(hrNamespace).Get(context.TODO(), apexSvcName, metav1.GetOptions{})
354+
httpRoute, err := gwr.gatewayAPIClient.GatewayapiV1().HTTPRoutes(hrNamespace).Get(context.TODO(), routeName, metav1.GetOptions{})
352355
if err != nil {
353-
return fmt.Errorf("HTTPRoute %s.%s get error: %w", apexSvcName, hrNamespace, err)
356+
return fmt.Errorf("HTTPRoute %s.%s get error: %w", routeName, hrNamespace, err)
354357
}
355358
hrClone := httpRoute.DeepCopy()
356359
hostNames := []v1.Hostname{}

pkg/router/gateway_api_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,45 @@ func TestGatewayAPIRouter_Reconcile(t *testing.T) {
7474
assert.Equal(t, httpRoute.Annotations["foo"], "bar")
7575
}
7676

77+
func TestGatewayAPIRouter_ReconcileCustomHTTPRouteName(t *testing.T) {
78+
canary := newTestGatewayAPICanary()
79+
canary.Spec.Service.HTTPRouteName = "my-custom-route"
80+
mocks := newFixture(canary)
81+
router := &GatewayAPIRouter{
82+
gatewayAPIClient: mocks.meshClient,
83+
kubeClient: mocks.kubeClient,
84+
logger: mocks.logger,
85+
}
86+
87+
err := router.Reconcile(canary)
88+
require.NoError(t, err)
89+
90+
// HTTPRoute should be created with the custom name
91+
httpRoute, err := router.gatewayAPIClient.GatewayapiV1().HTTPRoutes("default").Get(context.TODO(), "my-custom-route", metav1.GetOptions{})
92+
require.NoError(t, err)
93+
assert.Equal(t, "my-custom-route", httpRoute.Name)
94+
95+
// BackendRefs should still point at the primary and canary services
96+
backendRefs := httpRoute.Spec.Rules[0].BackendRefs
97+
require.Equal(t, 2, len(backendRefs))
98+
assert.Equal(t, v1.ObjectName("podinfo-primary"), backendRefs[0].Name)
99+
assert.Equal(t, v1.ObjectName("podinfo-canary"), backendRefs[1].Name)
100+
101+
// GetRoutes and SetRoutes should work with the custom name
102+
primaryWeight, canaryWeight, _, err := router.GetRoutes(canary)
103+
require.NoError(t, err)
104+
assert.Equal(t, 100, primaryWeight)
105+
assert.Equal(t, 0, canaryWeight)
106+
107+
err = router.SetRoutes(canary, 50, 50, false)
108+
require.NoError(t, err)
109+
110+
primaryWeight, canaryWeight, _, err = router.GetRoutes(canary)
111+
require.NoError(t, err)
112+
assert.Equal(t, 50, primaryWeight)
113+
assert.Equal(t, 50, canaryWeight)
114+
}
115+
77116
func TestGatewayAPIRouter_Routes(t *testing.T) {
78117
canary := newTestGatewayAPICanary()
79118
mocks := newFixture(canary)

pkg/router/gateway_api_v1beta1.go

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ func (gwr *GatewayAPIV1Beta1Router) Reconcile(canary *flaggerv1.Canary) error {
6060
return fmt.Errorf("GatewayRefs must be specified when using Gateway API as a provider.")
6161
}
6262

63-
apexSvcName, primarySvcName, canarySvcName := canary.GetServiceNames()
63+
_, primarySvcName, canarySvcName := canary.GetServiceNames()
64+
routeName := canary.GetHTTPRouteName()
6465

6566
hrNamespace := canary.Namespace
6667

@@ -119,7 +120,7 @@ func (gwr *GatewayAPIV1Beta1Router) Reconcile(canary *flaggerv1.Canary) error {
119120
}
120121

121122
httpRoute, err := gwr.gatewayAPIClient.GatewayapiV1beta1().HTTPRoutes(hrNamespace).Get(
122-
context.TODO(), apexSvcName, metav1.GetOptions{},
123+
context.TODO(), routeName, metav1.GetOptions{},
123124
)
124125

125126
newMetadata := canary.Spec.Service.Apex
@@ -137,7 +138,7 @@ func (gwr *GatewayAPIV1Beta1Router) Reconcile(canary *flaggerv1.Canary) error {
137138
if errors.IsNotFound(err) {
138139
route := &v1beta1.HTTPRoute{
139140
ObjectMeta: metav1.ObjectMeta{
140-
Name: apexSvcName,
141+
Name: routeName,
141142
Namespace: hrNamespace,
142143
Labels: newMetadata.Labels,
143144
Annotations: newMetadata.Annotations,
@@ -159,13 +160,13 @@ func (gwr *GatewayAPIV1Beta1Router) Reconcile(canary *flaggerv1.Canary) error {
159160
Create(context.TODO(), route, metav1.CreateOptions{})
160161

161162
if err != nil {
162-
return fmt.Errorf("HTTPRoute %s.%s create error: %w", apexSvcName, hrNamespace, err)
163+
return fmt.Errorf("HTTPRoute %s.%s create error: %w", routeName, hrNamespace, err)
163164
}
164165
gwr.logger.With("canary", fmt.Sprintf("%s.%s", canary.Name, canary.Namespace)).
165166
Infof("HTTPRoute %s.%s created", route.GetName(), hrNamespace)
166167
return nil
167168
} else if err != nil {
168-
return fmt.Errorf("HTTPRoute %s.%s get error: %w", apexSvcName, hrNamespace, err)
169+
return fmt.Errorf("HTTPRoute %s.%s get error: %w", routeName, hrNamespace, err)
169170
}
170171

171172
ignoreCmpOptions := []cmp.Option{
@@ -241,11 +242,12 @@ func (gwr *GatewayAPIV1Beta1Router) GetRoutes(canary *flaggerv1.Canary) (
241242
mirrored bool,
242243
err error,
243244
) {
244-
apexSvcName, primarySvcName, canarySvcName := canary.GetServiceNames()
245+
_, primarySvcName, canarySvcName := canary.GetServiceNames()
246+
routeName := canary.GetHTTPRouteName()
245247
hrNamespace := canary.Namespace
246-
httpRoute, err := gwr.gatewayAPIClient.GatewayapiV1beta1().HTTPRoutes(hrNamespace).Get(context.TODO(), apexSvcName, metav1.GetOptions{})
248+
httpRoute, err := gwr.gatewayAPIClient.GatewayapiV1beta1().HTTPRoutes(hrNamespace).Get(context.TODO(), routeName, metav1.GetOptions{})
247249
if err != nil {
248-
err = fmt.Errorf("HTTPRoute %s.%s get error: %w", apexSvcName, hrNamespace, err)
250+
err = fmt.Errorf("HTTPRoute %s.%s get error: %w", routeName, hrNamespace, err)
249251
return
250252
}
251253
var weightedRule *v1beta1.HTTPRouteRule
@@ -301,11 +303,12 @@ func (gwr *GatewayAPIV1Beta1Router) SetRoutes(
301303
) error {
302304
pWeight := int32(primaryWeight)
303305
cWeight := int32(canaryWeight)
304-
apexSvcName, primarySvcName, canarySvcName := canary.GetServiceNames()
306+
_, primarySvcName, canarySvcName := canary.GetServiceNames()
307+
routeName := canary.GetHTTPRouteName()
305308
hrNamespace := canary.Namespace
306-
httpRoute, err := gwr.gatewayAPIClient.GatewayapiV1beta1().HTTPRoutes(hrNamespace).Get(context.TODO(), apexSvcName, metav1.GetOptions{})
309+
httpRoute, err := gwr.gatewayAPIClient.GatewayapiV1beta1().HTTPRoutes(hrNamespace).Get(context.TODO(), routeName, metav1.GetOptions{})
307310
if err != nil {
308-
return fmt.Errorf("HTTPRoute %s.%s get error: %w", apexSvcName, hrNamespace, err)
311+
return fmt.Errorf("HTTPRoute %s.%s get error: %w", routeName, hrNamespace, err)
309312
}
310313
hrClone := httpRoute.DeepCopy()
311314
hostNames := []v1beta1.Hostname{}

0 commit comments

Comments
 (0)