Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(gateway-api): Add custom backendRef and filters support for HTTP…
Browse files Browse the repository at this point in the history
…Route

Signed-off-by: kahirokunn <okinakahiro@gmail.com>
kahirokunn committed Dec 17, 2024
1 parent 64b5081 commit 4f3511b
Showing 23 changed files with 2,220 additions and 135 deletions.
439 changes: 428 additions & 11 deletions artifacts/flagger/crd.yaml

Large diffs are not rendered by default.

439 changes: 428 additions & 11 deletions charts/flagger/crds/crd.yaml

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions charts/flagger/templates/rbac.yaml
Original file line number Diff line number Diff line change
@@ -226,6 +226,8 @@ rules:
resources:
- httproutes
- httproutes/finalizers
- referencegrants
- referencegrants/finalizers
verbs:
- get
- list
439 changes: 428 additions & 11 deletions kustomize/base/flagger/crd.yaml

Large diffs are not rendered by default.

29 changes: 27 additions & 2 deletions pkg/apis/flagger/v1beta1/canary.go
Original file line number Diff line number Diff line change
@@ -20,6 +20,7 @@ import (
"fmt"
"time"

v1 "github.com/fluxcd/flagger/pkg/apis/gatewayapi/v1"
"github.com/fluxcd/flagger/pkg/apis/gatewayapi/v1beta1"
istiov1beta1 "github.com/fluxcd/flagger/pkg/apis/istio/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -213,11 +214,11 @@ type CanaryService struct {

// Primary is the metadata to add to the primary service
// +optional
Primary *CustomMetadata `json:"primary,omitempty"`
Primary *CustomBackend `json:"primary,omitempty"`

// Canary is the metadata to add to the canary service
// +optional
Canary *CustomMetadata `json:"canary,omitempty"`
Canary *CustomBackend `json:"canary,omitempty"`
}

// CanaryAnalysis is used to describe how the analysis should be done
@@ -496,6 +497,30 @@ type CustomMetadata struct {
Annotations map[string]string `json:"annotations,omitempty"`
}

// CustomBackend holds labels, annotations, and proxyRef to set on generated objects.
type CustomBackend struct {
CustomMetadata

// Ref references a Kubernetes object.
BackendObjectReference *v1.BackendObjectReference `json:"backendRef,omitempty"`

// Filters defined at this level should be executed if and only if the
// request is being forwarded to the backend defined here.
//
// Support: Implementation-specific (For broader support of filters, use the
// Filters field in HTTPRouteRule.)
//
// +optional
// +kubebuilder:validation:MaxItems=16
// +kubebuilder:validation:XValidation:message="May specify either httpRouteFilterRequestRedirect or httpRouteFilterRequestRewrite, but not both",rule="!(self.exists(f, f.type == 'RequestRedirect') && self.exists(f, f.type == 'URLRewrite'))"
// +kubebuilder:validation:XValidation:message="May specify either httpRouteFilterRequestRedirect or httpRouteFilterRequestRewrite, but not both",rule="!(self.exists(f, f.type == 'RequestRedirect') && self.exists(f, f.type == 'URLRewrite'))"
// +kubebuilder:validation:XValidation:message="RequestHeaderModifier filter cannot be repeated",rule="self.filter(f, f.type == 'RequestHeaderModifier').size() <= 1"
// +kubebuilder:validation:XValidation:message="ResponseHeaderModifier filter cannot be repeated",rule="self.filter(f, f.type == 'ResponseHeaderModifier').size() <= 1"
// +kubebuilder:validation:XValidation:message="RequestRedirect filter cannot be repeated",rule="self.filter(f, f.type == 'RequestRedirect').size() <= 1"
// +kubebuilder:validation:XValidation:message="URLRewrite filter cannot be repeated",rule="self.filter(f, f.type == 'URLRewrite').size() <= 1"
Filters []v1.HTTPRouteFilter `json:"filters,omitempty"`
}

// HTTPRewrite holds information about how to modify a request URI during
// forwarding.
type HTTPRewrite struct {
34 changes: 32 additions & 2 deletions pkg/apis/flagger/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

145 changes: 145 additions & 0 deletions pkg/apis/gatewayapi/v1beta1/referencegrant_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
/*
Copyright 2021 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1beta1

import metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

// +genclient
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// +kubebuilder:object:root=true
// +kubebuilder:resource:categories=gateway-api,shortName=refgrant
// +kubebuilder:printcolumn:name="Age",type=date,JSONPath=`.metadata.creationTimestamp`
// +kubebuilder:storageversion

// ReferenceGrant identifies kinds of resources in other namespaces that are
// trusted to reference the specified kinds of resources in the same namespace
// as the policy.
//
// Each ReferenceGrant can be used to represent a unique trust relationship.
// Additional Reference Grants can be used to add to the set of trusted
// sources of inbound references for the namespace they are defined within.
//
// All cross-namespace references in Gateway API (with the exception of cross-namespace
// Gateway-route attachment) require a ReferenceGrant.
//
// ReferenceGrant is a form of runtime verification allowing users to assert
// which cross-namespace object references are permitted. Implementations that
// support ReferenceGrant MUST NOT permit cross-namespace references which have
// no grant, and MUST respond to the removal of a grant by revoking the access
// that the grant allowed.
type ReferenceGrant struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

// Spec defines the desired state of ReferenceGrant.
Spec ReferenceGrantSpec `json:"spec,omitempty"`

// Note that `Status` sub-resource has been excluded at the
// moment as it was difficult to work out the design.
// `Status` sub-resource may be added in future.
}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// +kubebuilder:object:root=true
// ReferenceGrantList contains a list of ReferenceGrant.
type ReferenceGrantList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []ReferenceGrant `json:"items"`
}

// ReferenceGrantSpec identifies a cross namespace relationship that is trusted
// for Gateway API.
type ReferenceGrantSpec struct {
// From describes the trusted namespaces and kinds that can reference the
// resources described in "To". Each entry in this list MUST be considered
// to be an additional place that references can be valid from, or to put
// this another way, entries MUST be combined using OR.
//
// Support: Core
//
// +kubebuilder:validation:MinItems=1
// +kubebuilder:validation:MaxItems=16
From []ReferenceGrantFrom `json:"from"`

// To describes the resources that may be referenced by the resources
// described in "From". Each entry in this list MUST be considered to be an
// additional place that references can be valid to, or to put this another
// way, entries MUST be combined using OR.
//
// Support: Core
//
// +kubebuilder:validation:MinItems=1
// +kubebuilder:validation:MaxItems=16
To []ReferenceGrantTo `json:"to"`
}

// ReferenceGrantFrom describes trusted namespaces and kinds.
type ReferenceGrantFrom struct {
// Group is the group of the referent.
// When empty, the Kubernetes core API group is inferred.
//
// Support: Core
Group Group `json:"group"`

// Kind is the kind of the referent. Although implementations may support
// additional resources, the following types are part of the "Core"
// support level for this field.
//
// When used to permit a SecretObjectReference:
//
// * Gateway
//
// When used to permit a BackendObjectReference:
//
// * GRPCRoute
// * HTTPRoute
// * TCPRoute
// * TLSRoute
// * UDPRoute
Kind Kind `json:"kind"`

// Namespace is the namespace of the referent.
//
// Support: Core
Namespace Namespace `json:"namespace"`
}

// ReferenceGrantTo describes what Kinds are allowed as targets of the
// references.
type ReferenceGrantTo struct {
// Group is the group of the referent.
// When empty, the Kubernetes core API group is inferred.
//
// Support: Core
Group Group `json:"group"`

// Kind is the kind of the referent. Although implementations may support
// additional resources, the following types are part of the "Core"
// support level for this field:
//
// * Secret when used to permit a SecretObjectReference
// * Service when used to permit a BackendObjectReference
Kind Kind `json:"kind"`

// Name is the name of the referent. When unspecified, this policy
// refers to all resources of the specified Group and Kind in the local
// namespace.
//
// +optional
Name *ObjectName `json:"name,omitempty"`
}
2 changes: 2 additions & 0 deletions pkg/apis/gatewayapi/v1beta1/register.go
Original file line number Diff line number Diff line change
@@ -33,6 +33,8 @@ func addKnownTypes(scheme *runtime.Scheme) error {
scheme.AddKnownTypes(SchemeGroupVersion,
&HTTPRoute{},
&HTTPRouteList{},
&ReferenceGrant{},
&ReferenceGrantList{},
)
metav1.AddToGroupVersion(scheme, SchemeGroupVersion)
return nil
125 changes: 125 additions & 0 deletions pkg/apis/gatewayapi/v1beta1/zz_generated.deepcopy.go
2 changes: 2 additions & 0 deletions pkg/client/informers/externalversions/generic.go
8 changes: 8 additions & 0 deletions pkg/client/listers/gatewayapi/v1beta1/expansion_generated.go
70 changes: 70 additions & 0 deletions pkg/client/listers/gatewayapi/v1beta1/referencegrant.go
225 changes: 140 additions & 85 deletions pkg/router/gateway_api.go
Original file line number Diff line number Diff line change
@@ -97,12 +97,8 @@ func (gwr *GatewayAPIRouter) Reconcile(canary *flaggerv1.Canary) error {
Matches: matches,
Filters: gwr.makeFilters(canary),
BackendRefs: []v1.HTTPBackendRef{
{
BackendRef: gwr.makeBackendRef(primarySvcName, initialPrimaryWeight, canary.Spec.Service.Port),
},
{
BackendRef: gwr.makeBackendRef(canarySvcName, initialCanaryWeight, canary.Spec.Service.Port),
},
gwr.makeHTTPBackendRef(primarySvcName, initialPrimaryWeight, canary.Spec.Service.Port, canary.Spec.Service.Primary),
gwr.makeHTTPBackendRef(canarySvcName, initialCanaryWeight, canary.Spec.Service.Port, canary.Spec.Service.Canary),
},
},
},
@@ -123,9 +119,7 @@ func (gwr *GatewayAPIRouter) Reconcile(canary *flaggerv1.Canary) error {
Matches: matches,
Filters: gwr.makeFilters(canary),
BackendRefs: []v1.HTTPBackendRef{
{
BackendRef: gwr.makeBackendRef(primarySvcName, initialPrimaryWeight, canary.Spec.Service.Port),
},
gwr.makeHTTPBackendRef(primarySvcName, initialPrimaryWeight, canary.Spec.Service.Port, canary.Spec.Service.Primary),
},
})
if canary.Spec.Service.Timeout != "" {
@@ -153,6 +147,7 @@ func (gwr *GatewayAPIRouter) Reconcile(canary *flaggerv1.Canary) error {
newMetadata.Annotations = filterMetadata(newMetadata.Annotations)

if errors.IsNotFound(err) {
// create http route
route := &v1.HTTPRoute{
ObjectMeta: metav1.ObjectMeta{
Name: apexSvcName,
@@ -181,72 +176,130 @@ func (gwr *GatewayAPIRouter) Reconcile(canary *flaggerv1.Canary) error {
}
gwr.logger.With("canary", fmt.Sprintf("%s.%s", canary.Name, canary.Namespace)).
Infof("HTTPRoute %s.%s created", route.GetName(), hrNamespace)
return nil
} else if err != nil {
return fmt.Errorf("HTTPRoute %s.%s get error: %w", apexSvcName, hrNamespace, err)
}

ignoreCmpOptions := []cmp.Option{
cmpopts.IgnoreFields(v1.BackendRef{}, "Weight"),
cmpopts.EquateEmpty(),
}
if canary.Spec.Analysis.SessionAffinity != nil {
ignoreRoute := cmpopts.IgnoreSliceElements(func(r v1.HTTPRouteRule) bool {
// Ignore the rule that does sticky routing, i.e. matches against the `Cookie` header.
for _, match := range r.Matches {
for _, headerMatch := range match.Headers {
if *headerMatch.Type == headerMatchRegex && headerMatch.Name == cookieHeader &&
strings.Contains(headerMatch.Value, canary.Spec.Analysis.SessionAffinity.CookieName) {
return true
} else {
// update http route
ignoreCmpOptions := []cmp.Option{
cmpopts.IgnoreFields(v1.BackendRef{}, "Weight"),
cmpopts.EquateEmpty(),
}
if canary.Spec.Analysis.SessionAffinity != nil {
ignoreRoute := cmpopts.IgnoreSliceElements(func(r v1.HTTPRouteRule) bool {
// Ignore the rule that does sticky routing, i.e. matches against the `Cookie` header.
for _, match := range r.Matches {
for _, headerMatch := range match.Headers {
if *headerMatch.Type == headerMatchRegex && headerMatch.Name == cookieHeader &&
strings.Contains(headerMatch.Value, canary.Spec.Analysis.SessionAffinity.CookieName) {
return true
}
}
}
return false
})
ignoreCmpOptions = append(ignoreCmpOptions, ignoreRoute)
// Ignore backend specific filters, since we use that to insert the `Set-Cookie` header in responses.
ignoreCmpOptions = append(ignoreCmpOptions, cmpopts.IgnoreFields(v1.HTTPBackendRef{}, "Filters"))
}

if canary.GetAnalysis().Mirror {
// If a Canary run is in progress, the HTTPRoute rule will have an extra filter of type RequestMirror
// which needs to be ignored so that the requests are mirrored to the canary deployment.
inProgress := canary.Status.Phase == flaggerv1.CanaryPhaseWaiting || canary.Status.Phase == flaggerv1.CanaryPhaseProgressing ||
canary.Status.Phase == flaggerv1.CanaryPhaseWaitingPromotion
if inProgress {
ignoreCmpOptions = append(ignoreCmpOptions, cmpopts.IgnoreFields(v1.HTTPRouteRule{}, "Filters"))
}
return false
})
ignoreCmpOptions = append(ignoreCmpOptions, ignoreRoute)
// Ignore backend specific filters, since we use that to insert the `Set-Cookie` header in responses.
ignoreCmpOptions = append(ignoreCmpOptions, cmpopts.IgnoreFields(v1.HTTPBackendRef{}, "Filters"))
}
}

if canary.GetAnalysis().Mirror {
// If a Canary run is in progress, the HTTPRoute rule will have an extra filter of type RequestMirror
// which needs to be ignored so that the requests are mirrored to the canary deployment.
inProgress := canary.Status.Phase == flaggerv1.CanaryPhaseWaiting || canary.Status.Phase == flaggerv1.CanaryPhaseProgressing ||
canary.Status.Phase == flaggerv1.CanaryPhaseWaitingPromotion
if inProgress {
ignoreCmpOptions = append(ignoreCmpOptions, cmpopts.IgnoreFields(v1.HTTPRouteRule{}, "Filters"))
if httpRoute != nil {
// Preserve the existing annotations added by other controllers such as AWS Gateway API Controller.
mergedAnnotations := newMetadata.Annotations
for key, val := range httpRoute.Annotations {
if _, ok := mergedAnnotations[key]; !ok {
mergedAnnotations[key] = val
}
}
// Compare the existing HTTPRoute spec and metadata with the desired state.
// If there are differences, update the HTTPRoute object.

specDiff := cmp.Diff(
httpRoute.Spec, httpRouteSpec,
ignoreCmpOptions...,
)
labelsDiff := cmp.Diff(newMetadata.Labels, httpRoute.Labels, cmpopts.EquateEmpty())
annotationsDiff := cmp.Diff(mergedAnnotations, httpRoute.Annotations, cmpopts.EquateEmpty())
if (specDiff != "" && httpRoute.Name != "") || labelsDiff != "" || annotationsDiff != "" {
hrClone := httpRoute.DeepCopy()
hrClone.Spec = httpRouteSpec
hrClone.ObjectMeta.Annotations = mergedAnnotations
hrClone.ObjectMeta.Labels = newMetadata.Labels
_, err := gwr.gatewayAPIClient.GatewayapiV1().HTTPRoutes(hrNamespace).
Update(context.TODO(), hrClone, metav1.UpdateOptions{})
if err != nil {
return fmt.Errorf("HTTPRoute %s.%s update error: %w while reconciling", hrClone.GetName(), hrNamespace, err)
}
gwr.logger.With("canary", fmt.Sprintf("%s.%s", canary.Name, canary.Namespace)).
Infof("HTTPRoute %s.%s updated", hrClone.GetName(), hrNamespace)
}
}
}

if httpRoute != nil {
// Preserve the existing annotations added by other controllers such as AWS Gateway API Controller.
mergedAnnotations := newMetadata.Annotations
for key, val := range httpRoute.Annotations {
if _, ok := mergedAnnotations[key]; !ok {
mergedAnnotations[key] = val
// create reference grants
referenceGrants := []*v1beta1.ReferenceGrant{}

for _, rule := range httpRouteSpec.Rules {
for _, backendRef := range rule.BackendRefs {
if backendRef.Namespace != nil {
svcNamespace := string(*backendRef.Namespace)
if svcNamespace != hrNamespace {
group := v1beta1.Group("")
kind := v1beta1.Kind("Service")
if backendRef.Group != nil {
group = v1beta1.Group(*backendRef.Group)
}
if backendRef.Kind != nil {
kind = v1beta1.Kind(*backendRef.Kind)
}
name := (*v1beta1.ObjectName)(&backendRef.Name)

rg := &v1beta1.ReferenceGrant{
ObjectMeta: metav1.ObjectMeta{
Name: canary.Name,
Namespace: svcNamespace,
},
Spec: v1beta1.ReferenceGrantSpec{
From: []v1beta1.ReferenceGrantFrom{
{
Group: "gateway.networking.k8s.io",
Kind: "HTTPRoute",
Namespace: v1beta1.Namespace(hrNamespace),
},
},
To: []v1beta1.ReferenceGrantTo{
{
Group: group,
Kind: kind,
Name: name,
},
},
},
}
referenceGrants = append(referenceGrants, rg)
}
}
}
}

// Compare the existing HTTPRoute spec and metadata with the desired state.
// If there are differences, update the HTTPRoute object.
specDiff := cmp.Diff(
httpRoute.Spec, httpRouteSpec,
ignoreCmpOptions...,
)
labelsDiff := cmp.Diff(newMetadata.Labels, httpRoute.Labels, cmpopts.EquateEmpty())
annotationsDiff := cmp.Diff(mergedAnnotations, httpRoute.Annotations, cmpopts.EquateEmpty())
if (specDiff != "" && httpRoute.Name != "") || labelsDiff != "" || annotationsDiff != "" {
hrClone := httpRoute.DeepCopy()
hrClone.Spec = httpRouteSpec
hrClone.ObjectMeta.Annotations = mergedAnnotations
hrClone.ObjectMeta.Labels = newMetadata.Labels
_, err := gwr.gatewayAPIClient.GatewayapiV1().HTTPRoutes(hrNamespace).
Update(context.TODO(), hrClone, metav1.UpdateOptions{})
if err != nil {
return fmt.Errorf("HTTPRoute %s.%s update error: %w while reconciling", hrClone.GetName(), hrNamespace, err)
for _, rg := range referenceGrants {
_, err := gwr.gatewayAPIClient.GatewayapiV1beta1().ReferenceGrants(rg.Namespace).Get(context.TODO(), rg.Name, metav1.GetOptions{})
if errors.IsNotFound(err) {
_, err = gwr.gatewayAPIClient.GatewayapiV1beta1().ReferenceGrants(rg.Namespace).Create(context.TODO(), rg, metav1.CreateOptions{})
if err == nil {
gwr.logger.Infof("ReferenceGrant %s.%s has been created", rg.Name, rg.Namespace)
} else if !errors.IsAlreadyExists(err) {
return fmt.Errorf("ReferenceGrant %s.%s creation error: %w", rg.Name, rg.Namespace, err)
}
gwr.logger.With("canary", fmt.Sprintf("%s.%s", canary.Name, canary.Namespace)).
Infof("HTTPRoute %s.%s updated", hrClone.GetName(), hrNamespace)
}
}

@@ -351,12 +404,8 @@ func (gwr *GatewayAPIRouter) SetRoutes(
Matches: matches,
Filters: gwr.makeFilters(canary),
BackendRefs: []v1.HTTPBackendRef{
{
BackendRef: gwr.makeBackendRef(primarySvcName, pWeight, canary.Spec.Service.Port),
},
{
BackendRef: gwr.makeBackendRef(canarySvcName, cWeight, canary.Spec.Service.Port),
},
gwr.makeHTTPBackendRef(primarySvcName, pWeight, canary.Spec.Service.Port, canary.Spec.Service.Primary),
gwr.makeHTTPBackendRef(canarySvcName, cWeight, canary.Spec.Service.Port, canary.Spec.Service.Canary),
},
}
if canary.Spec.Service.Timeout != "" {
@@ -410,9 +459,7 @@ func (gwr *GatewayAPIRouter) SetRoutes(
Matches: matches,
Filters: gwr.makeFilters(canary),
BackendRefs: []v1.HTTPBackendRef{
{
BackendRef: gwr.makeBackendRef(primarySvcName, initialPrimaryWeight, canary.Spec.Service.Port),
},
gwr.makeHTTPBackendRef(primarySvcName, initialPrimaryWeight, canary.Spec.Service.Port, canary.Spec.Service.Primary),
},
Timeouts: &v1.HTTPRouteTimeouts{
Request: &timeout,
@@ -495,12 +542,8 @@ func (gwr *GatewayAPIRouter) getSessionAffinityRouteRules(canary *flaggerv1.Cana
mergedMatches := gwr.mergeMatchConditions([]v1.HTTPRouteMatch{cookieMatch}, svcMatches)
stickyRouteRule.Matches = mergedMatches
stickyRouteRule.BackendRefs = []v1.HTTPBackendRef{
{
BackendRef: gwr.makeBackendRef(primarySvcName, 0, canary.Spec.Service.Port),
},
{
BackendRef: gwr.makeBackendRef(canarySvcName, 100, canary.Spec.Service.Port),
},
gwr.makeHTTPBackendRef(primarySvcName, 0, canary.Spec.Service.Port, canary.Spec.Service.Primary),
gwr.makeHTTPBackendRef(canarySvcName, 100, canary.Spec.Service.Port, canary.Spec.Service.Canary),
}
} else {
// If canary weight is 0 and SessionAffinityCookie is non-blank, then it belongs to a previous canary run.
@@ -623,16 +666,28 @@ func (gwr *GatewayAPIRouter) mapRouteMatches(requestMatches []istiov1beta1.HTTPM
return matches, nil
}

func (gwr *GatewayAPIRouter) makeBackendRef(svcName string, weight, port int32) v1.BackendRef {
return v1.BackendRef{
BackendObjectReference: v1.BackendObjectReference{
Group: (*v1.Group)(&backendRefGroup),
Kind: (*v1.Kind)(&backendRefKind),
Name: v1.ObjectName(svcName),
Port: (*v1.PortNumber)(&port),
func (gwr *GatewayAPIRouter) makeHTTPBackendRef(svcName string, weight, port int32, customBackend *flaggerv1.CustomBackend) v1.HTTPBackendRef {
httpBackendRef := v1.HTTPBackendRef{
BackendRef: v1.BackendRef{
BackendObjectReference: v1.BackendObjectReference{
Group: (*v1.Group)(&backendRefGroup),
Kind: (*v1.Kind)(&backendRefKind),
Name: v1.ObjectName(svcName),
Port: (*v1.PortNumber)(&port),
},
Weight: &weight,
},
Weight: &weight,
}
if customBackend != nil {
if customBackend.BackendObjectReference != nil {
httpBackendRef.BackendObjectReference = *customBackend.BackendObjectReference
}
if customBackend.Filters != nil {
httpBackendRef.Filters = customBackend.Filters
}
}

return httpBackendRef
}

func (gwr *GatewayAPIRouter) mergeMatchConditions(analysis, service []v1.HTTPRouteMatch) []v1.HTTPRouteMatch {
65 changes: 59 additions & 6 deletions pkg/router/gateway_api_test.go
Original file line number Diff line number Diff line change
@@ -287,6 +287,63 @@ func TestGatewayAPIRouter_Routes(t *testing.T) {
assert.Len(t, hr.Spec.Rules, 1)
assert.Len(t, hr.Spec.Rules[0].Filters, 0)
})

t.Run("custom backend filters", func(t *testing.T) {
canary := mocks.canary.DeepCopy()
primaryHostName := v1.PreciseHostname("primary.example.com")
canary.Spec.Service.Primary = &flaggerv1.CustomBackend{
Filters: []v1.HTTPRouteFilter{
{
Type: v1.HTTPRouteFilterURLRewrite,
URLRewrite: &v1.HTTPURLRewriteFilter{
Hostname: &primaryHostName,
},
},
},
}

name := v1.ObjectName("canary")
unmanagedSvcNamespace := "kube-system"
namespace := v1.Namespace(unmanagedSvcNamespace)
port := v1.PortNumber(30080)
objRef := v1.BackendObjectReference{
Name: name,
Namespace: &namespace,
Port: &port,
}

canary.Spec.Service.Canary = &flaggerv1.CustomBackend{
BackendObjectReference: &objRef,
}
err = router.SetRoutes(canary, 50, 50, false)
require.NoError(t, err)

httpRoute, err := router.gatewayAPIClient.GatewayapiV1().HTTPRoutes("default").Get(context.TODO(), "podinfo", metav1.GetOptions{})
require.NoError(t, err)

primary := httpRoute.Spec.Rules[0].BackendRefs[0]
assert.Equal(t, int32(50), *primary.Weight)

canaryBackend := httpRoute.Spec.Rules[0].BackendRefs[1]
assert.Equal(t, canaryBackend.Name, name)
assert.Equal(t, canaryBackend.Namespace, &namespace)
assert.Equal(t, canaryBackend.Port, &port)

primaryBackend := httpRoute.Spec.Rules[0].BackendRefs[0].Filters[0].URLRewrite
assert.Equal(t, primaryBackend.Hostname, &primaryHostName)

err = router.Reconcile(canary)
require.NoError(t, err)

referenceGrant, err := router.gatewayAPIClient.GatewayapiV1beta1().ReferenceGrants(unmanagedSvcNamespace).Get(context.TODO(), canary.Name, metav1.GetOptions{})
require.NoError(t, err)
assert.Equal(t, unmanagedSvcNamespace, string(referenceGrant.Namespace))
assert.Equal(t, "HTTPRoute", string(referenceGrant.Spec.From[0].Kind))
assert.Equal(t, canary.Namespace, string(referenceGrant.Spec.From[0].Namespace))
assert.Equal(t, "Service", string(referenceGrant.Spec.To[0].Kind))
assert.Equal(t, "", string(referenceGrant.Spec.To[0].Group))
assert.Equal(t, string(name), string(*referenceGrant.Spec.To[0].Name))
})
}

func TestGatewayAPIRouter_getSessionAffinityRouteRules(t *testing.T) {
@@ -306,12 +363,8 @@ func TestGatewayAPIRouter_getSessionAffinityRouteRules(t *testing.T) {
_, pSvcName, cSvcName := canary.GetServiceNames()
weightedRouteRule := &v1.HTTPRouteRule{
BackendRefs: []v1.HTTPBackendRef{
{
BackendRef: router.makeBackendRef(pSvcName, initialPrimaryWeight, canary.Spec.Service.Port),
},
{
BackendRef: router.makeBackendRef(cSvcName, initialCanaryWeight, canary.Spec.Service.Port),
},
router.makeHTTPBackendRef(pSvcName, initialPrimaryWeight, canary.Spec.Service.Port, canary.Spec.Service.Primary),
router.makeHTTPBackendRef(cSvcName, initialCanaryWeight, canary.Spec.Service.Port, canary.Spec.Service.Canary),
},
}
rules, err := router.getSessionAffinityRouteRules(canary, 10, weightedRouteRule)
12 changes: 10 additions & 2 deletions pkg/router/kubernetes_default.go
Original file line number Diff line number Diff line change
@@ -50,13 +50,21 @@ func (c *KubernetesDefaultRouter) Initialize(canary *flaggerv1.Canary) error {
_, primaryName, canaryName := canary.GetServiceNames()

// canary svc
err := c.reconcileService(canary, canaryName, c.labelValue, canary.Spec.Service.Canary)
canaryCustomMetadata := &flaggerv1.CustomMetadata{}
if canary.Spec.Service.Canary != nil {
canaryCustomMetadata = &canary.Spec.Service.Canary.CustomMetadata
}
err := c.reconcileService(canary, canaryName, c.labelValue, canaryCustomMetadata)
if err != nil {
return fmt.Errorf("reconcileService failed: %w", err)
}

// primary svc
err = c.reconcileService(canary, primaryName, fmt.Sprintf("%s-primary", c.labelValue), canary.Spec.Service.Primary)
primaryCustomMetadata := &flaggerv1.CustomMetadata{}
if canary.Spec.Service.Primary != nil {
primaryCustomMetadata = &canary.Spec.Service.Primary.CustomMetadata
}
err = c.reconcileService(canary, primaryName, fmt.Sprintf("%s-primary", c.labelValue), primaryCustomMetadata)
if err != nil {
return fmt.Errorf("reconcileService failed: %w", err)
}
10 changes: 5 additions & 5 deletions pkg/router/kubernetes_default_test.go
Original file line number Diff line number Diff line change
@@ -375,13 +375,13 @@ func TestServiceRouter_InitializeMetadata(t *testing.T) {
labelSelector: "app",
}

metadata := &flaggerv1.CustomMetadata{
Labels: map[string]string{"test": "test"},
Annotations: map[string]string{"test": "test"},
mocks.canary.Spec.Service.Canary = &flaggerv1.CustomBackend{
CustomMetadata: flaggerv1.CustomMetadata{
Labels: map[string]string{"test": "test"},
Annotations: map[string]string{"test": "test"},
},
}

mocks.canary.Spec.Service.Canary = metadata

err := router.Initialize(mocks.canary)
require.NoError(t, err)

0 comments on commit 4f3511b

Please sign in to comment.