forked from servicemeshinterface/smi-controller-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Nic Jackson <[email protected]>
- Loading branch information
1 parent
02de259
commit 2239fad
Showing
9 changed files
with
533 additions
and
172 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Oops, something went wrong.