Skip to content
This repository has been archived by the owner on Mar 19, 2024. It is now read-only.

Commit

Permalink
k8s: move Gateway API usage to v1beta1 or explicitly v1alpha2 for Rou…
Browse files Browse the repository at this point in the history
…tes and ReferenceGrant

gateway-api: add ReferencePolicy backwards compatibility support

mocks: regenerate

k8s/controller: add gwv1alpha2 to scheme

k8s/gatewayclient: add gwv1alpha2 to scheme for test helpers
  • Loading branch information
mikemorris committed Jun 20, 2022
1 parent e3f29b9 commit 40c4b64
Show file tree
Hide file tree
Showing 39 changed files with 1,210 additions and 1,182 deletions.
10 changes: 5 additions & 5 deletions internal/k8s/builder/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
gw "sigs.k8s.io/gateway-api/apis/v1alpha2"
gwv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1"

"github.com/hashicorp/consul-api-gateway/internal/k8s/utils"
"github.com/hashicorp/consul-api-gateway/pkg/apis/v1alpha1"
)

type GatewayServiceBuilder struct {
gateway *gw.Gateway
gateway *gwv1beta1.Gateway
gwConfig *v1alpha1.GatewayClassConfig
}

func NewGatewayService(gw *gw.Gateway) *GatewayServiceBuilder {
func NewGatewayService(gw *gwv1beta1.Gateway) *GatewayServiceBuilder {
return &GatewayServiceBuilder{gateway: gw}
}

Expand Down Expand Up @@ -80,15 +80,15 @@ func filterAnnotations(annotations map[string]string, allowed []string) map[stri
}

type GatewayDeploymentBuilder struct {
gateway *gw.Gateway
gateway *gwv1beta1.Gateway
gwConfig *v1alpha1.GatewayClassConfig
sdsHost string
sdsPort int
consulCAData string
consulGatewayNamespace string
}

func NewGatewayDeployment(gw *gw.Gateway) *GatewayDeploymentBuilder {
func NewGatewayDeployment(gw *gwv1beta1.Gateway) *GatewayDeploymentBuilder {
return &GatewayDeploymentBuilder{gateway: gw}
}

Expand Down
10 changes: 5 additions & 5 deletions internal/k8s/builder/gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/serializer/json"
"k8s.io/apimachinery/pkg/util/yaml"
gateway "sigs.k8s.io/gateway-api/apis/v1alpha2"
gwv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1"

"github.com/hashicorp/consul-api-gateway/pkg/apis/v1alpha1"
)
Expand All @@ -37,15 +37,15 @@ func init() {

type gatewayTestConfig struct {
gatewayClassConfig *v1alpha1.GatewayClassConfig
gatewayClass *gateway.GatewayClass
gateway *gateway.Gateway
gatewayClass *gwv1beta1.GatewayClass
gateway *gwv1beta1.Gateway
}

func newGatewayTestConfig() *gatewayTestConfig {
return &gatewayTestConfig{
gatewayClassConfig: &v1alpha1.GatewayClassConfig{},
gatewayClass: &gateway.GatewayClass{},
gateway: &gateway.Gateway{},
gatewayClass: &gwv1beta1.GatewayClass{},
gateway: &gwv1beta1.Gateway{},
}
}

Expand Down
6 changes: 4 additions & 2 deletions internal/k8s/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import (
klogv2 "k8s.io/klog/v2"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/healthz"
gw "sigs.k8s.io/gateway-api/apis/v1alpha2"
gwv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
gwv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1"

"github.com/hashicorp/consul/api"
"github.com/hashicorp/go-hclog"
Expand Down Expand Up @@ -41,7 +42,8 @@ const (

func init() {
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
utilruntime.Must(gw.AddToScheme(scheme))
utilruntime.Must(gwv1beta1.AddToScheme(scheme))
utilruntime.Must(gwv1alpha2.AddToScheme(scheme))
}

type ConsulNamespaceConfig struct {
Expand Down
16 changes: 8 additions & 8 deletions internal/k8s/controllers/gateway_class_config_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
gateway "sigs.k8s.io/gateway-api/apis/v1alpha2"
gwv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1"

"github.com/hashicorp/go-hclog"

Expand Down Expand Up @@ -106,23 +106,23 @@ func TestGatewayClassConfig(t *testing.T) {
err: errExpected,
expectationCB: func(client *mocks.MockClient, reconciler *reconcilerMocks.MockReconcileManager) {
client.EXPECT().GetGatewayClassConfig(gomock.Any(), classConfigName).Return(&apigwv1alpha1.GatewayClassConfig{}, nil)
client.EXPECT().GatewayClassesUsingConfig(gomock.Any(), gomock.Any()).Return(&gateway.GatewayClassList{}, nil)
client.EXPECT().GatewayClassesUsingConfig(gomock.Any(), gomock.Any()).Return(&gwv1beta1.GatewayClassList{}, nil)
client.EXPECT().EnsureFinalizer(gomock.Any(), gomock.Any(), gatewayClassConfigFinalizer).Return(false, errExpected)
},
}, {
name: "create",
expectationCB: func(client *mocks.MockClient, reconciler *reconcilerMocks.MockReconcileManager) {
client.EXPECT().GetGatewayClassConfig(gomock.Any(), classConfigName).Return(&apigwv1alpha1.GatewayClassConfig{}, nil)
client.EXPECT().GatewayClassesUsingConfig(gomock.Any(), gomock.Any()).Return(&gateway.GatewayClassList{}, nil)
client.EXPECT().GatewayClassesUsingConfig(gomock.Any(), gomock.Any()).Return(&gwv1beta1.GatewayClassList{}, nil)
client.EXPECT().EnsureFinalizer(gomock.Any(), gomock.Any(), gatewayClassConfigFinalizer).Return(true, nil)
},
}, {
name: "update-in-use",
expectationCB: func(client *mocks.MockClient, reconciler *reconcilerMocks.MockReconcileManager) {
gcUsing := gateway.GatewayClass{
gcUsing := gwv1beta1.GatewayClass{
ObjectMeta: meta.ObjectMeta{Name: "class"},
Spec: gateway.GatewayClassSpec{
ParametersRef: &gateway.ParametersReference{
Spec: gwv1beta1.GatewayClassSpec{
ParametersRef: &gwv1beta1.ParametersReference{
Group: apigwv1alpha1.Group,
Kind: apigwv1alpha1.GatewayClassConfigKind,
Name: "config",
Expand All @@ -131,8 +131,8 @@ func TestGatewayClassConfig(t *testing.T) {
}

client.EXPECT().GetGatewayClassConfig(gomock.Any(), classConfigName).Return(&apigwv1alpha1.GatewayClassConfig{}, nil)
client.EXPECT().GatewayClassesUsingConfig(gomock.Any(), gomock.Any()).Return(&gateway.GatewayClassList{
Items: []gateway.GatewayClass{gcUsing},
client.EXPECT().GatewayClassesUsingConfig(gomock.Any(), gomock.Any()).Return(&gwv1beta1.GatewayClassList{
Items: []gwv1beta1.GatewayClass{gcUsing},
}, nil)
reconciler.EXPECT().DeleteGatewayClass(gomock.Any(), gcUsing.Name).Return(nil)
client.EXPECT().EnsureFinalizer(gomock.Any(), gomock.Any(), gatewayClassConfigFinalizer).Return(true, nil)
Expand Down
4 changes: 2 additions & 2 deletions internal/k8s/controllers/gateway_class_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"time"

ctrl "sigs.k8s.io/controller-runtime"
gateway "sigs.k8s.io/gateway-api/apis/v1alpha2"
gwv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1"

"github.com/hashicorp/consul-api-gateway/internal/k8s/gatewayclient"
"github.com/hashicorp/consul-api-gateway/internal/k8s/reconciler"
Expand Down Expand Up @@ -92,6 +92,6 @@ func (r *GatewayClassReconciler) Reconcile(ctx context.Context, req ctrl.Request
// SetupWithManager sets up the controller with the Manager.
func (r *GatewayClassReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&gateway.GatewayClass{}).
For(&gwv1beta1.GatewayClass{}).
Complete(gatewayclient.NewRequeueingMiddleware(r.Log, r))
}
50 changes: 25 additions & 25 deletions internal/k8s/controllers/gateway_class_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
gateway "sigs.k8s.io/gateway-api/apis/v1alpha2"
gwv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1"

"github.com/hashicorp/consul-api-gateway/internal/k8s/gatewayclient/mocks"
reconcilerMocks "github.com/hashicorp/consul-api-gateway/internal/k8s/reconciler/mocks"
Expand Down Expand Up @@ -53,9 +53,9 @@ func TestGatewayClass(t *testing.T) {
}, {
name: "not-managed",
expectationCB: func(client *mocks.MockClient, reconciler *reconcilerMocks.MockReconcileManager) {
client.EXPECT().GetGatewayClass(gomock.Any(), className).Return(&gateway.GatewayClass{
Spec: gateway.GatewayClassSpec{
ControllerName: gateway.GatewayController("other"),
client.EXPECT().GetGatewayClass(gomock.Any(), className).Return(&gwv1beta1.GatewayClass{
Spec: gwv1beta1.GatewayClassSpec{
ControllerName: gwv1beta1.GatewayController("other"),
},
}, nil)
},
Expand All @@ -64,12 +64,12 @@ func TestGatewayClass(t *testing.T) {
err: errExpected,
expectationCB: func(client *mocks.MockClient, reconciler *reconcilerMocks.MockReconcileManager) {
now := meta.Now()
client.EXPECT().GetGatewayClass(gomock.Any(), className).Return(&gateway.GatewayClass{
client.EXPECT().GetGatewayClass(gomock.Any(), className).Return(&gwv1beta1.GatewayClass{
ObjectMeta: meta.ObjectMeta{
DeletionTimestamp: &now,
},
Spec: gateway.GatewayClassSpec{
ControllerName: gateway.GatewayController(mockControllerName),
Spec: gwv1beta1.GatewayClassSpec{
ControllerName: gwv1beta1.GatewayController(mockControllerName),
},
}, nil)
client.EXPECT().GatewayClassInUse(gomock.Any(), gomock.Any()).Return(false, errExpected)
Expand All @@ -79,12 +79,12 @@ func TestGatewayClass(t *testing.T) {
result: ctrl.Result{RequeueAfter: 10 * time.Second},
expectationCB: func(client *mocks.MockClient, reconciler *reconcilerMocks.MockReconcileManager) {
now := meta.Now()
client.EXPECT().GetGatewayClass(gomock.Any(), className).Return(&gateway.GatewayClass{
client.EXPECT().GetGatewayClass(gomock.Any(), className).Return(&gwv1beta1.GatewayClass{
ObjectMeta: meta.ObjectMeta{
DeletionTimestamp: &now,
},
Spec: gateway.GatewayClassSpec{
ControllerName: gateway.GatewayController(mockControllerName),
Spec: gwv1beta1.GatewayClassSpec{
ControllerName: gwv1beta1.GatewayController(mockControllerName),
},
}, nil)
client.EXPECT().GatewayClassInUse(gomock.Any(), gomock.Any()).Return(true, nil)
Expand All @@ -94,12 +94,12 @@ func TestGatewayClass(t *testing.T) {
err: errExpected,
expectationCB: func(client *mocks.MockClient, reconciler *reconcilerMocks.MockReconcileManager) {
now := meta.Now()
client.EXPECT().GetGatewayClass(gomock.Any(), className).Return(&gateway.GatewayClass{
client.EXPECT().GetGatewayClass(gomock.Any(), className).Return(&gwv1beta1.GatewayClass{
ObjectMeta: meta.ObjectMeta{
DeletionTimestamp: &now,
},
Spec: gateway.GatewayClassSpec{
ControllerName: gateway.GatewayController(mockControllerName),
Spec: gwv1beta1.GatewayClassSpec{
ControllerName: gwv1beta1.GatewayController(mockControllerName),
},
}, nil)
client.EXPECT().GatewayClassInUse(gomock.Any(), gomock.Any()).Return(false, nil)
Expand All @@ -109,12 +109,12 @@ func TestGatewayClass(t *testing.T) {
name: "deleting",
expectationCB: func(client *mocks.MockClient, reconciler *reconcilerMocks.MockReconcileManager) {
now := meta.Now()
client.EXPECT().GetGatewayClass(gomock.Any(), className).Return(&gateway.GatewayClass{
client.EXPECT().GetGatewayClass(gomock.Any(), className).Return(&gwv1beta1.GatewayClass{
ObjectMeta: meta.ObjectMeta{
DeletionTimestamp: &now,
},
Spec: gateway.GatewayClassSpec{
ControllerName: gateway.GatewayController(mockControllerName),
Spec: gwv1beta1.GatewayClassSpec{
ControllerName: gwv1beta1.GatewayController(mockControllerName),
},
}, nil)
client.EXPECT().GatewayClassInUse(gomock.Any(), gomock.Any()).Return(false, nil)
Expand All @@ -124,19 +124,19 @@ func TestGatewayClass(t *testing.T) {
name: "create-finalizer-error",
err: errExpected,
expectationCB: func(client *mocks.MockClient, reconciler *reconcilerMocks.MockReconcileManager) {
client.EXPECT().GetGatewayClass(gomock.Any(), className).Return(&gateway.GatewayClass{
Spec: gateway.GatewayClassSpec{
ControllerName: gateway.GatewayController(mockControllerName),
client.EXPECT().GetGatewayClass(gomock.Any(), className).Return(&gwv1beta1.GatewayClass{
Spec: gwv1beta1.GatewayClassSpec{
ControllerName: gwv1beta1.GatewayController(mockControllerName),
},
}, nil)
client.EXPECT().EnsureFinalizer(gomock.Any(), gomock.Any(), gatewayClassFinalizer).Return(false, errExpected)
},
}, {
name: "create-finalizer-updated",
expectationCB: func(client *mocks.MockClient, reconciler *reconcilerMocks.MockReconcileManager) {
client.EXPECT().GetGatewayClass(gomock.Any(), className).Return(&gateway.GatewayClass{
Spec: gateway.GatewayClassSpec{
ControllerName: gateway.GatewayController(mockControllerName),
client.EXPECT().GetGatewayClass(gomock.Any(), className).Return(&gwv1beta1.GatewayClass{
Spec: gwv1beta1.GatewayClassSpec{
ControllerName: gwv1beta1.GatewayController(mockControllerName),
},
}, nil)
client.EXPECT().EnsureFinalizer(gomock.Any(), gomock.Any(), gatewayClassFinalizer).Return(true, nil)
Expand All @@ -145,9 +145,9 @@ func TestGatewayClass(t *testing.T) {
name: "create-upsert-error",
err: errExpected,
expectationCB: func(client *mocks.MockClient, reconciler *reconcilerMocks.MockReconcileManager) {
client.EXPECT().GetGatewayClass(gomock.Any(), className).Return(&gateway.GatewayClass{
Spec: gateway.GatewayClassSpec{
ControllerName: gateway.GatewayController(mockControllerName),
client.EXPECT().GetGatewayClass(gomock.Any(), className).Return(&gwv1beta1.GatewayClass{
Spec: gwv1beta1.GatewayClassSpec{
ControllerName: gwv1beta1.GatewayController(mockControllerName),
},
}, nil)
client.EXPECT().EnsureFinalizer(gomock.Any(), gomock.Any(), gatewayClassFinalizer).Return(false, nil)
Expand Down
35 changes: 17 additions & 18 deletions internal/k8s/controllers/gateway_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import (
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"
gateway "sigs.k8s.io/gateway-api/apis/v1alpha2"
gwv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
gwv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1"

"github.com/hashicorp/go-hclog"

Expand Down Expand Up @@ -86,7 +87,7 @@ func (r *GatewayReconciler) SetupWithManager(mgr ctrl.Manager) error {
)

return ctrl.NewControllerManagedBy(mgr).
For(&gateway.Gateway{}).
For(&gwv1beta1.Gateway{}).
Owns(&appsv1.Deployment{}).
Owns(&corev1.Service{}).
Owns(&corev1.ServiceAccount{}).
Expand All @@ -96,14 +97,13 @@ func (r *GatewayReconciler) SetupWithManager(mgr ctrl.Manager) error {
builder.WithPredicates(predicate),
).
Watches(
&source.Kind{Type: &gateway.ReferenceGrant{}},
&source.Kind{Type: &gwv1alpha2.ReferenceGrant{}},
handler.EnqueueRequestsFromMapFunc(r.referenceGrantToGatewayRequests),
).
// TODO: this can't be enabled until the ReferencePolicy object is restored
// Watches(
// &source.Kind{Type: &gateway.ReferencePolicy{}},
// handler.EnqueueRequestsFromMapFunc(r.referencePolicyToGatewayRequests),
// ).
Watches(
&source.Kind{Type: &gwv1alpha2.ReferencePolicy{}},
handler.EnqueueRequestsFromMapFunc(r.referencePolicyToGatewayRequests),
).
Complete(gatewayclient.NewRequeueingMiddleware(r.Log, r))
}

Expand All @@ -122,17 +122,16 @@ func podToGatewayRequest(object client.Object) []reconcile.Request {
}

func (r *GatewayReconciler) referenceGrantToGatewayRequests(object client.Object) []reconcile.Request {
return r.getRequestsFromReferenceGrant(object.(*gateway.ReferenceGrant))
return r.getRequestsFromReferenceGrant(object.(*gwv1alpha2.ReferenceGrant))
}

// TODO: this can't be enabled until the ReferencePolicy object is restored
// func (r *GatewayReconciler) referencePolicyToGatewayRequests(object client.Object) []reconcile.Request {
// refPolicy := object.(*gateway.ReferencePolicy)
// // TODO: Convert to ReferenceGrant
// return r.getRequestsFromReferenceGrant(refGrant)
// }
func (r *GatewayReconciler) referencePolicyToGatewayRequests(object client.Object) []reconcile.Request {
refPolicy := object.(*gwv1alpha2.ReferencePolicy)
refGrant := gwv1alpha2.ReferenceGrant{Spec: refPolicy.Spec}
return r.getRequestsFromReferenceGrant(&refGrant)
}

func (r *GatewayReconciler) getRequestsFromReferenceGrant(refGrant *gateway.ReferenceGrant) []reconcile.Request {
func (r *GatewayReconciler) getRequestsFromReferenceGrant(refGrant *gwv1alpha2.ReferenceGrant) []reconcile.Request {
gateways := r.getGatewaysAffectedByReferenceGrant(refGrant)

requests := make([]reconcile.Request, 0, len(gateways))
Expand All @@ -152,8 +151,8 @@ func (r *GatewayReconciler) getRequestsFromReferenceGrant(refGrant *gateway.Refe
// getGatewaysAffectedByReferenceGrant retrieves all Gateways potentially impacted by the ReferenceGrant
// modification. Currently, this is unfiltered and so returns all Gateways in the namespace referenced by
// the ReferenceGrant.
func (r *GatewayReconciler) getGatewaysAffectedByReferenceGrant(refGrant *gateway.ReferenceGrant) []gateway.Gateway {
var matches []gateway.Gateway
func (r *GatewayReconciler) getGatewaysAffectedByReferenceGrant(refGrant *gwv1alpha2.ReferenceGrant) []gwv1beta1.Gateway {
var matches []gwv1beta1.Gateway

for _, from := range refGrant.Spec.From {
// TODO: search by from.Group and from.Kind instead of assuming this ReferenceGrant references a Gateway
Expand Down
Loading

0 comments on commit 40c4b64

Please sign in to comment.