Skip to content

Commit

Permalink
tests: add envtests unit test for HTTPRouteReconciler
Browse files Browse the repository at this point in the history
  • Loading branch information
pmalek committed Mar 22, 2023
1 parent 33959c1 commit 15959ec
Show file tree
Hide file tree
Showing 14 changed files with 643 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func startKongAdminAPIServiceReconciler(ctx context.Context, t *testing.T, clien

mgr, err := ctrl.NewManager(cfg, ctrl.Options{
Logger: logrusr.New(logrus.New()),
Scheme: scheme.Scheme,
Scheme: client.Scheme(),
SyncPeriod: lo.ToPtr(2 * time.Second),
MetricsBindAddress: "0",
})
Expand Down Expand Up @@ -106,7 +106,7 @@ func TestKongAdminAPIController(t *testing.T) {
// In tests below we use a deferred cancel to stop the manager and not wait
// for its timeout.

cfg := envtest.Setup(t)
cfg := envtest.Setup(t, scheme.Scheme)
client, err := ctrlclient.New(cfg, ctrlclient.Options{})
require.NoError(t, err)

Expand Down
11 changes: 11 additions & 0 deletions internal/controllers/controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package controllers

import (
"github.com/go-logr/logr"
ctrl "sigs.k8s.io/controller-runtime"
)

type Reconciler interface {
SetupWithManager(ctrl.Manager) error
SetLogger(logr.Logger)
}
25 changes: 25 additions & 0 deletions internal/controllers/gateway/dataplane_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package gateway

import (
"sigs.k8s.io/controller-runtime/pkg/client"

k8sobj "github.com/kong/kubernetes-ingress-controller/v2/internal/util/kubernetes/object"
)

// DataPlane is a common interface that is used by reconcilers to interact
// with the dataplane.
//
// TODO: This can probably be used in other reconcilers as well.
type DataPlane interface {
DataPlaneClient

AreKubernetesObjectReportsEnabled() bool
KubernetesObjectConfigurationStatus(obj client.Object) k8sobj.ConfigurationStatus
}

// DataPlaneClient is a common client interface that is used by reconcilers to interact
// with the dataplane to perform CRUD operations on provided objects.
type DataPlaneClient interface {
UpdateObject(obj client.Object) error
DeleteObject(obj client.Object) error
}
29 changes: 29 additions & 0 deletions internal/controllers/gateway/dataplane_mock_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package gateway

import (
"sigs.k8s.io/controller-runtime/pkg/client"

k8sobj "github.com/kong/kubernetes-ingress-controller/v2/internal/util/kubernetes/object"
)

type DataplaneMock struct {
KubernetesObjectReportsEnabled bool
// Mapping namespace to name to status
ObjectsStatuses map[string]map[string]k8sobj.ConfigurationStatus
}

func (d DataplaneMock) UpdateObject(_ client.Object) error {
return nil
}

func (d DataplaneMock) DeleteObject(_ client.Object) error {
return nil
}

func (d DataplaneMock) AreKubernetesObjectReportsEnabled() bool {
return d.KubernetesObjectReportsEnabled
}

func (d DataplaneMock) KubernetesObjectConfigurationStatus(obj client.Object) k8sobj.ConfigurationStatus {
return d.ObjectsStatuses[obj.GetNamespace()][obj.GetName()]
}
3 changes: 2 additions & 1 deletion internal/controllers/gateway/gateway_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ func isObjectUnmanaged(anns map[string]string) bool {
// isGatewayClassControlledAndUnmanaged returns boolean if the GatewayClass
// is controlled by this controller and is configured for unmanaged mode.
func isGatewayClassControlledAndUnmanaged(gatewayClass *GatewayClass) bool {
return gatewayClass.Spec.ControllerName == GetControllerName() && isObjectUnmanaged(gatewayClass.Annotations)
isUnamanaged := isObjectUnmanaged(gatewayClass.Annotations)
return gatewayClass.Spec.ControllerName == GetControllerName() && isUnamanaged
}

// getRefFromPublishService splits a publish service string in the format namespace/name into a types.NamespacedName
Expand Down
8 changes: 6 additions & 2 deletions internal/controllers/gateway/httproute_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/source"
gatewayv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1"

"github.com/kong/kubernetes-ingress-controller/v2/internal/dataplane"
"github.com/kong/kubernetes-ingress-controller/v2/internal/util"
k8sobj "github.com/kong/kubernetes-ingress-controller/v2/internal/util/kubernetes/object"
)
Expand All @@ -38,7 +37,7 @@ type HTTPRouteReconciler struct {

Log logr.Logger
Scheme *runtime.Scheme
DataplaneClient *dataplane.KongClient
DataplaneClient DataPlane
// If EnableReferenceGrant is true, we will check for ReferenceGrant if backend in another
// namespace is in backendRefs.
// If it is false, referencing backend in different namespace will be rejected.
Expand Down Expand Up @@ -692,3 +691,8 @@ func (r *HTTPRouteReconciler) getHTTPRouteRuleReason(ctx context.Context, httpRo
}
return gatewayv1beta1.RouteReasonResolvedRefs, nil
}

// SetLogger sets the logger.
func (r *HTTPRouteReconciler) SetLogger(l logr.Logger) {
r.Log = l
}

0 comments on commit 15959ec

Please sign in to comment.