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 a6a8f0e
Show file tree
Hide file tree
Showing 12 changed files with 509 additions and 10 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
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
3 changes: 1 addition & 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

0 comments on commit a6a8f0e

Please sign in to comment.