Skip to content

Commit

Permalink
Implement other resources into SDK
Browse files Browse the repository at this point in the history
Signed-off-by: Nic Jackson <[email protected]>
  • Loading branch information
nicholasjackson committed Jul 7, 2021
1 parent 02de259 commit 2239fad
Show file tree
Hide file tree
Showing 9 changed files with 533 additions and 172 deletions.
141 changes: 6 additions & 135 deletions sdk/v1Alpha.go
Original file line number Diff line number Diff line change
@@ -1,63 +1,12 @@
package sdk

import (
"context"

"github.com/go-logr/logr"
accessv1alpha3 "github.com/servicemeshinterface/smi-controller-sdk/apis/access/v1alpha3"
splitv1alpha4 "github.com/servicemeshinterface/smi-controller-sdk/apis/split/v1alpha4"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
)

// UpsertTrafficTarget defines a callback function for updating or
// inserting a new TrafficTarget
type UpsertTrafficTarget interface {
UpsertTrafficTarget(
ctx context.Context,
r client.Client,
l logr.Logger,
tt *accessv1alpha3.TrafficTarget) (ctrl.Result, error)
}

// DeleteTrafficTarget defines a callback function for deleting
// a new TrafficTarget
type DeleteTrafficTarget interface {
DeleteTrafficTarget(
ctx context.Context,
r client.Client,
l logr.Logger,
tt *accessv1alpha3.TrafficTarget) (ctrl.Result, error)
}

// UpsertTrafficSplit defines a callback function for updating or
// inserting a new TrafficSplit
type UpsertTrafficSplit interface {
UpsertTrafficSplit(
ctx context.Context,
r client.Client,
l logr.Logger,
tt *splitv1alpha4.TrafficSplit) (ctrl.Result, error)
}

// DeleteTrafficSplit defines a callback function for deleting
// a new TrafficSplit
type DeleteTrafficSplit interface {
DeleteTrafficSplit(
ctx context.Context,
r client.Client,
l logr.Logger,
tt *splitv1alpha4.TrafficSplit) (ctrl.Result, error)
}

// V1Alpha defines an interface containing callback methods for the v1alpha2 API
// V1Alpha defines an interface containing callback methods for all the specs
// We define the methods as individual interfaces as we want to enable the user to
// implement only the callbacks they need
type V1Alpha interface {
UpsertTrafficTarget
DeleteTrafficTarget
UpsertTrafficSplit
DeleteTrafficSplit
v1AlphaAccess
v1AlphaSplit
v1AlphaSpecs
}

// v1Alpha2Impl is a concrete implementation of the V1Alpha2 interface
Expand All @@ -66,86 +15,8 @@ type v1AlphaImpl struct {
}

// RegisterV1Alpha2 registers user defined callback functions to the api
// This is a loose registration rather than a direct interface to allow optional
// implementation of the interface methods
func (a *v1AlphaImpl) RegisterV1Alpha(i interface{}) {
a.userV1alpha = i
}

// UpsertTrafficTarget will call the user defined UpsertTrafficTarget callback
// when defined
func (a *v1AlphaImpl) UpsertTrafficTarget(
ctx context.Context,
r client.Client,
l logr.Logger,
tt *accessv1alpha3.TrafficTarget,
) (ctrl.Result, error) {

// does the user api have this callback?
v, ok := a.userV1alpha.(UpsertTrafficTarget)

if !ok {
l.Info("Client code does not implement UpsertTrafficTarget")
return ctrl.Result{}, nil
}

// call the interface method
return v.UpsertTrafficTarget(ctx, r, l, tt)
}

func (a *v1AlphaImpl) DeleteTrafficTarget(
ctx context.Context,
r client.Client,
l logr.Logger,
tt *accessv1alpha3.TrafficTarget,
) (ctrl.Result, error) {

// does the user api have this callback?
v, ok := a.userV1alpha.(DeleteTrafficTarget)

if !ok {
l.Info("Client code does not implement DeleteTrafficTarget")
return ctrl.Result{}, nil
}

// call the interface method
return v.DeleteTrafficTarget(ctx, r, l, tt)
}

// UpsertTrafficSplit will call the user defined UpsertTrafficSplit callback
// when defined
func (a *v1AlphaImpl) UpsertTrafficSplit(
ctx context.Context,
r client.Client,
l logr.Logger,
tt *splitv1alpha4.TrafficSplit,
) (ctrl.Result, error) {

// does the user api have this callback?
v, ok := a.userV1alpha.(UpsertTrafficSplit)

if !ok {
l.Info("Client code does not implement UpsertTrafficSplit")
return ctrl.Result{}, nil
}

// call the interface method
return v.UpsertTrafficSplit(ctx, r, l, tt)
}

func (a *v1AlphaImpl) DeleteTrafficSplit(
ctx context.Context,
r client.Client,
l logr.Logger,
tt *splitv1alpha4.TrafficSplit,
) (ctrl.Result, error) {

// does the user api have this callback?
v, ok := a.userV1alpha.(DeleteTrafficSplit)

if !ok {
l.Info("Client code does not implement DeleteTrafficSplit")
return ctrl.Result{}, nil
}

// call the interface method
return v.DeleteTrafficSplit(ctx, r, l, tt)
}
69 changes: 68 additions & 1 deletion sdk/v1AlphaMock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/go-logr/logr"
accessv1alpha3 "github.com/servicemeshinterface/smi-controller-sdk/apis/access/v1alpha3"
specsv1alpha4 "github.com/servicemeshinterface/smi-controller-sdk/apis/specs/v1alpha4"
splitv1alpha4 "github.com/servicemeshinterface/smi-controller-sdk/apis/split/v1alpha4"
"github.com/stretchr/testify/mock"
ctrl "sigs.k8s.io/controller-runtime"
Expand All @@ -26,7 +27,7 @@ func (v *V1AlphaMock) UpsertTrafficTarget(
return ctrl.Result{}, nil
}

func (v *V1AlphaMock) DeleteTrafficTarget(
func (v *V1AlphaMock) DeleteTrafficTarge(
ctx context.Context,
r client.Client,
l logr.Logger,
Expand Down Expand Up @@ -58,3 +59,69 @@ func (v *V1AlphaMock) DeleteTrafficSplit(

return ctrl.Result{}, nil
}

func (v *V1AlphaMock) UpsertHTTPRouteGroup(
ctx context.Context,
r client.Client,
l logr.Logger,
tt *specsv1alpha4.HTTPRouteGroup) (ctrl.Result, error) {

v.Called(ctx, r, l, tt)

return ctrl.Result{}, nil
}

func (v *V1AlphaMock) DeleteHTTPRouteGroup(
ctx context.Context,
r client.Client,
l logr.Logger,
tt *specsv1alpha4.HTTPRouteGroup) (ctrl.Result, error) {

v.Called(ctx, r, l, tt)

return ctrl.Result{}, nil
}

func (v *V1AlphaMock) UpsertTCPRoute(
ctx context.Context,
r client.Client,
l logr.Logger,
tt *specsv1alpha4.TCPRoute) (ctrl.Result, error) {

v.Called(ctx, r, l, tt)

return ctrl.Result{}, nil
}

func (v *V1AlphaMock) DeleteTCPRoute(
ctx context.Context,
r client.Client,
l logr.Logger,
tt *specsv1alpha4.TCPRoute) (ctrl.Result, error) {

v.Called(ctx, r, l, tt)

return ctrl.Result{}, nil
}

func (v *V1AlphaMock) UpsertUDPRoute(
ctx context.Context,
r client.Client,
l logr.Logger,
tt *specsv1alpha4.UDPRoute) (ctrl.Result, error) {

v.Called(ctx, r, l, tt)

return ctrl.Result{}, nil
}

func (v *V1AlphaMock) DeleteUDPRoute(
ctx context.Context,
r client.Client,
l logr.Logger,
tt *specsv1alpha4.UDPRoute) (ctrl.Result, error) {

v.Called(ctx, r, l, tt)

return ctrl.Result{}, nil
}
77 changes: 77 additions & 0 deletions sdk/v1Alpha_access.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package sdk

import (
"context"

"github.com/go-logr/logr"
accessv1alpha3 "github.com/servicemeshinterface/smi-controller-sdk/apis/access/v1alpha3"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
)

// V1AlphaAccess defines an interface containing callback methods for the v1alpha4 API
// traffic access objects
type v1AlphaAccess interface {
UpsertTrafficTarget
DeleteTrafficTarget
}

// UpsertTrafficTarget defines a callback function for updating or
// inserting a new TrafficTarget
type UpsertTrafficTarget interface {
UpsertTrafficTarget(
ctx context.Context,
r client.Client,
l logr.Logger,
tt *accessv1alpha3.TrafficTarget) (ctrl.Result, error)
}

// DeleteTrafficTarget defines a callback function for deleting
// a new TrafficTarget
type DeleteTrafficTarget interface {
DeleteTrafficTarget(
ctx context.Context,
r client.Client,
l logr.Logger,
tt *accessv1alpha3.TrafficTarget) (ctrl.Result, error)
}

// UpsertTrafficTarget will call the user defined UpsertTrafficTarget callback
// when defined
func (a *v1AlphaImpl) UpsertTrafficTarget(
ctx context.Context,
r client.Client,
l logr.Logger,
tt *accessv1alpha3.TrafficTarget,
) (ctrl.Result, error) {

// does the user api have this callback?
v, ok := a.userV1alpha.(UpsertTrafficTarget)

if !ok {
l.Info("Client code does not implement UpsertTrafficTarget")
return ctrl.Result{}, nil
}

// call the interface method
return v.UpsertTrafficTarget(ctx, r, l, tt)
}

func (a *v1AlphaImpl) DeleteTrafficTarget(
ctx context.Context,
r client.Client,
l logr.Logger,
tt *accessv1alpha3.TrafficTarget,
) (ctrl.Result, error) {

// does the user api have this callback?
v, ok := a.userV1alpha.(DeleteTrafficTarget)

if !ok {
l.Info("Client code does not implement DeleteTrafficTarget")
return ctrl.Result{}, nil
}

// call the interface method
return v.DeleteTrafficTarget(ctx, r, l, tt)
}
25 changes: 25 additions & 0 deletions sdk/v1Alpha_access_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package sdk

import (
"testing"

accessv1alpha3 "github.com/servicemeshinterface/smi-controller-sdk/apis/access/v1alpha3"
"github.com/stretchr/testify/mock"
)

func TestCallsUserDefinedUpsertTrafficTargetWhenSet(t *testing.T) {
v1, a, l := setupSDKTests(t)

a.V1Alpha().UpsertTrafficTarget(nil, nil, l, &accessv1alpha3.TrafficTarget{})

v1.AssertCalled(t, "UpsertTrafficTarget", mock.Anything, mock.Anything, mock.Anything, mock.Anything)
}

func TestDoesNotCallsUserDefinedUpsertTrafficTargetWhenNotSet(t *testing.T) {
v1, a, l := setupSDKTests(t)
a.RegisterV1Alpha(nil)

a.V1Alpha().UpsertTrafficTarget(nil, nil, l, &accessv1alpha3.TrafficTarget{})

v1.AssertNotCalled(t, "UpsertTrafficTarget", mock.Anything, mock.Anything, mock.Anything, mock.Anything)
}
Loading

0 comments on commit 2239fad

Please sign in to comment.