Skip to content

Commit

Permalink
feat: bump ReferenceGrant to v1beta1
Browse files Browse the repository at this point in the history
Signed-off-by: Liang Deng <[email protected]>
  • Loading branch information
YTGhost committed Mar 28, 2024
1 parent 1eceba6 commit cfd09c9
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 28 deletions.
3 changes: 2 additions & 1 deletion pkg/i2gw/ingress2gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client/config"
gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
gatewayv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
gatewayv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1"
)

func ToGatewayAPIResources(ctx context.Context, namespace string, inputFile string, providers []string) ([]GatewayResources, error) {
Expand Down Expand Up @@ -150,7 +151,7 @@ func MergeGatewayResources(gatewayResources ...GatewayResources) (GatewayResourc
TLSRoutes: make(map[types.NamespacedName]gatewayv1alpha2.TLSRoute),
TCPRoutes: make(map[types.NamespacedName]gatewayv1alpha2.TCPRoute),
UDPRoutes: make(map[types.NamespacedName]gatewayv1alpha2.UDPRoute),
ReferenceGrants: make(map[types.NamespacedName]gatewayv1alpha2.ReferenceGrant),
ReferenceGrants: make(map[types.NamespacedName]gatewayv1beta1.ReferenceGrant),
}
var errs field.ErrorList
mergedGatewayResources.Gateways, errs = mergeGateways(gatewayResources)
Expand Down
3 changes: 2 additions & 1 deletion pkg/i2gw/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
gatewayv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
gatewayv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1"
)

// ProviderConstructorByName is a map of ProviderConstructor functions by a
Expand Down Expand Up @@ -100,7 +101,7 @@ type GatewayResources struct {
TCPRoutes map[types.NamespacedName]gatewayv1alpha2.TCPRoute
UDPRoutes map[types.NamespacedName]gatewayv1alpha2.UDPRoute

ReferenceGrants map[types.NamespacedName]gatewayv1alpha2.ReferenceGrant
ReferenceGrants map[types.NamespacedName]gatewayv1beta1.ReferenceGrant
}

// FeatureParser is a function that reads the InputResources, and applies
Expand Down
2 changes: 1 addition & 1 deletion pkg/i2gw/providers/common/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ var (

ReferenceGrantGVK = schema.GroupVersionKind{
Group: "gateway.networking.k8s.io",
Version: "v1alpha2",
Version: "v1beta1",
Kind: "ReferenceGrant",
}
)
Expand Down
23 changes: 12 additions & 11 deletions pkg/i2gw/providers/istio/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"k8s.io/klog/v2"
gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
gatewayv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
gatewayv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1"
)

type converter struct {
Expand All @@ -52,7 +53,7 @@ func (c *converter) convert(storage *storage) (i2gw.GatewayResources, field.Erro
HTTPRoutes: make(map[types.NamespacedName]gatewayv1.HTTPRoute),
TLSRoutes: make(map[types.NamespacedName]gatewayv1alpha2.TLSRoute),
TCPRoutes: make(map[types.NamespacedName]gatewayv1alpha2.TCPRoute),
ReferenceGrants: make(map[types.NamespacedName]gatewayv1alpha2.ReferenceGrant),
ReferenceGrants: make(map[types.NamespacedName]gatewayv1beta1.ReferenceGrant),
}

rootPath := field.NewPath(ProviderName)
Expand Down Expand Up @@ -919,10 +920,10 @@ func (c *converter) isVirtualServiceAllowedForGateway(gateway types.NamespacedNa

// Generate parentRefs and optionally ReferenceGrants for the given VirtualService and all required Gateways
// We consider fields: vs.Spec.Gateways; gateway.Server[i].Hosts
func (c *converter) generateReferences(vs *istioclientv1beta1.VirtualService, fieldPath *field.Path) ([]gatewayv1.ParentReference, []*gatewayv1alpha2.ReferenceGrant) {
func (c *converter) generateReferences(vs *istioclientv1beta1.VirtualService, fieldPath *field.Path) ([]gatewayv1.ParentReference, []*gatewayv1beta1.ReferenceGrant) {
var (
parentRefs []gatewayv1.ParentReference
referenceGrants []*gatewayv1alpha2.ReferenceGrant
referenceGrants []*gatewayv1beta1.ReferenceGrant
)

for _, allowedGateway := range vs.Spec.GetGateways() {
Expand Down Expand Up @@ -974,27 +975,27 @@ type generateReferenceGrantsParams struct {
forHTTPRoute, forTLSRoute, forTCPRoute bool
}

func (c *converter) generateReferenceGrant(params generateReferenceGrantsParams) *gatewayv1alpha2.ReferenceGrant {
var fromGrants []gatewayv1alpha2.ReferenceGrantFrom
func (c *converter) generateReferenceGrant(params generateReferenceGrantsParams) *gatewayv1beta1.ReferenceGrant {
var fromGrants []gatewayv1beta1.ReferenceGrantFrom

if params.forHTTPRoute {
fromGrants = append(fromGrants, gatewayv1alpha2.ReferenceGrantFrom{
fromGrants = append(fromGrants, gatewayv1beta1.ReferenceGrantFrom{
Group: gatewayv1.Group(common.HTTPRouteGVK.Group),
Kind: gatewayv1.Kind(common.HTTPRouteGVK.Kind),
Namespace: gatewayv1.Namespace(params.fromNamespace),
})
}

if params.forTLSRoute {
fromGrants = append(fromGrants, gatewayv1alpha2.ReferenceGrantFrom{
fromGrants = append(fromGrants, gatewayv1beta1.ReferenceGrantFrom{
Group: gatewayv1.Group(common.TLSRouteGVK.Group),
Kind: gatewayv1.Kind(common.TLSRouteGVK.Kind),
Namespace: gatewayv1.Namespace(params.fromNamespace),
})
}

if params.forTCPRoute {
fromGrants = append(fromGrants, gatewayv1alpha2.ReferenceGrantFrom{
fromGrants = append(fromGrants, gatewayv1beta1.ReferenceGrantFrom{
Group: gatewayv1.Group(common.TCPRouteGVK.Group),
Kind: gatewayv1.Kind(common.TCPRouteGVK.Kind),
Namespace: gatewayv1.Namespace(params.fromNamespace),
Expand All @@ -1003,7 +1004,7 @@ func (c *converter) generateReferenceGrant(params generateReferenceGrantsParams)

gwName := gatewayv1.ObjectName(params.gateway.Name)

return &gatewayv1alpha2.ReferenceGrant{
return &gatewayv1beta1.ReferenceGrant{
TypeMeta: metav1.TypeMeta{
APIVersion: common.ReferenceGrantGVK.GroupVersion().String(),
Kind: common.ReferenceGrantGVK.Kind,
Expand All @@ -1012,9 +1013,9 @@ func (c *converter) generateReferenceGrant(params generateReferenceGrantsParams)
Namespace: params.gateway.Namespace,
Name: fmt.Sprintf("generated-reference-grant-from-%v-to-%v", params.fromNamespace, params.gateway.Namespace),
},
Spec: gatewayv1alpha2.ReferenceGrantSpec{
Spec: gatewayv1beta1.ReferenceGrantSpec{
From: fromGrants,
To: []gatewayv1alpha2.ReferenceGrantTo{
To: []gatewayv1beta1.ReferenceGrantTo{
{
Group: gatewayv1.Group(common.GatewayGVK.Group),
Kind: gatewayv1.Kind(common.GatewayGVK.Kind),
Expand Down
23 changes: 12 additions & 11 deletions pkg/i2gw/providers/istio/converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"k8s.io/apimachinery/pkg/util/validation/field"
gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
gatewayv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
gatewayv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1"
)

func Test_converter_convertGateway(t *testing.T) {
Expand Down Expand Up @@ -1565,7 +1566,7 @@ func Test_converter_generateReferenceGrants(t *testing.T) {
tests := []struct {
name string
args args
want *gatewayv1alpha2.ReferenceGrant
want *gatewayv1beta1.ReferenceGrant
}{
{
name: "generate reference grant for HTTPRoute,TLSRoute,TCPRoute",
Expand All @@ -1581,7 +1582,7 @@ func Test_converter_generateReferenceGrants(t *testing.T) {
forTCPRoute: true,
},
},
want: &gatewayv1alpha2.ReferenceGrant{
want: &gatewayv1beta1.ReferenceGrant{
TypeMeta: metav1.TypeMeta{
APIVersion: common.ReferenceGrantGVK.GroupVersion().String(),
Kind: common.ReferenceGrantGVK.Kind,
Expand All @@ -1590,8 +1591,8 @@ func Test_converter_generateReferenceGrants(t *testing.T) {
Namespace: "test",
Name: "generated-reference-grant-from-ns1-to-test",
},
Spec: gatewayv1alpha2.ReferenceGrantSpec{
From: []gatewayv1alpha2.ReferenceGrantFrom{
Spec: gatewayv1beta1.ReferenceGrantSpec{
From: []gatewayv1beta1.ReferenceGrantFrom{
{
Group: gatewayv1.Group(common.HTTPRouteGVK.Group),
Kind: gatewayv1.Kind(common.HTTPRouteGVK.Kind),
Expand All @@ -1608,7 +1609,7 @@ func Test_converter_generateReferenceGrants(t *testing.T) {
Namespace: gatewayv1.Namespace("ns1"),
},
},
To: []gatewayv1alpha2.ReferenceGrantTo{
To: []gatewayv1beta1.ReferenceGrantTo{
{
Group: gatewayv1.Group(common.GatewayGVK.Group),
Kind: gatewayv1.Kind(common.GatewayGVK.Kind),
Expand Down Expand Up @@ -1825,7 +1826,7 @@ func Test_converter_generateReferences(t *testing.T) {
fields fields
args args
wantParentReferences []gatewayv1.ParentReference
wantReferenceGrants []*gatewayv1alpha2.ReferenceGrant
wantReferenceGrants []*gatewayv1beta1.ReferenceGrant
}{
{
name: "nothing is generated if Gateway is not listed in the VirtualService",
Expand Down Expand Up @@ -1882,12 +1883,12 @@ func Test_converter_generateReferences(t *testing.T) {
Name: "gateway",
},
},
wantReferenceGrants: []*gatewayv1alpha2.ReferenceGrant{
wantReferenceGrants: []*gatewayv1beta1.ReferenceGrant{
{
TypeMeta: metav1.TypeMeta{Kind: "ReferenceGrant", APIVersion: "gateway.networking.k8s.io/v1alpha2"},
TypeMeta: metav1.TypeMeta{Kind: "ReferenceGrant", APIVersion: "gateway.networking.k8s.io/v1beta1"},
ObjectMeta: metav1.ObjectMeta{Name: "generated-reference-grant-from-test-to-prod", Namespace: "prod"},
Spec: gatewayv1alpha2.ReferenceGrantSpec{
To: []gatewayv1alpha2.ReferenceGrantTo{
Spec: gatewayv1beta1.ReferenceGrantSpec{
To: []gatewayv1beta1.ReferenceGrantTo{
{
Group: "gateway.networking.k8s.io", Kind: "Gateway", Name: common.PtrTo[gatewayv1.ObjectName]("gateway"),
},
Expand Down Expand Up @@ -1926,7 +1927,7 @@ func Test_converter_generateReferences(t *testing.T) {
Name: "gateway",
},
},
wantReferenceGrants: []*gatewayv1alpha2.ReferenceGrant{},
wantReferenceGrants: []*gatewayv1beta1.ReferenceGrant{},
},
}
for _, tt := range tests {
Expand Down
5 changes: 3 additions & 2 deletions pkg/i2gw/providers/istio/e2e_file_converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"k8s.io/apimachinery/pkg/types"
gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
gatewayv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
gatewayv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1"
)

const fixturesDir = "./fixtures"
Expand Down Expand Up @@ -108,7 +109,7 @@ func readGatewayResourcesFromFile(t *testing.T, filename string) (*i2gw.GatewayR
HTTPRoutes: make(map[types.NamespacedName]gatewayv1.HTTPRoute),
TLSRoutes: make(map[types.NamespacedName]gatewayv1alpha2.TLSRoute),
TCPRoutes: make(map[types.NamespacedName]gatewayv1alpha2.TCPRoute),
ReferenceGrants: make(map[types.NamespacedName]gatewayv1alpha2.ReferenceGrant),
ReferenceGrants: make(map[types.NamespacedName]gatewayv1beta1.ReferenceGrant),
}

for _, obj := range unstructuredObjects {
Expand Down Expand Up @@ -153,7 +154,7 @@ func readGatewayResourcesFromFile(t *testing.T, filename string) (*i2gw.GatewayR
Name: tcpRoute.Name,
}] = tcpRoute
case "ReferenceGrant":
var referenceGrant gatewayv1alpha2.ReferenceGrant
var referenceGrant gatewayv1beta1.ReferenceGrant
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(obj.UnstructuredContent(), &referenceGrant); err != nil {
return nil, fmt.Errorf("failed to parse k8s gateway ReferenceGrant object: %w", err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ spec:
name: test
weight: 1
---
apiVersion: gateway.networking.k8s.io/v1alpha2
apiVersion: gateway.networking.k8s.io/v1beta1
kind: ReferenceGrant
metadata:
name: generated-reference-grant-from-custom-ns-to-prod
Expand Down

0 comments on commit cfd09c9

Please sign in to comment.