Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Self Service Configuration from Factory #207

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions pkg/api/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
type Factory interface {
GetAPI() (API, error)
GetAPIsFromNamespace(namespace string) (map[string]API, error)
GetAPIsFromFactory(resource interface{}) (map[string]API, error)
}

type apiFactory struct {
Expand Down Expand Up @@ -170,6 +171,10 @@
return apis, nil
}

func (f *apiFactory) GetAPIsFromFactory(resource interface{}) (map[string]API, error) {
return nil, fmt.Errorf("not implemented")

Check warning on line 175 in pkg/api/factory.go

View check run for this annotation

Codecov / codecov/patch

pkg/api/factory.go#L174-L175

Added lines #L174 - L175 were not covered by tests
}

func (f *apiFactory) getApiFromNamespace(namespace string) (API, error) {
cm, secret, err := f.getConfigMapAndSecret(namespace)
if err != nil {
Expand Down
36 changes: 30 additions & 6 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,18 @@
return ctrl
}

// NewControllerWithNamespaceSupport For self-service notification getting configuration from apiFactory
func NewControllerWithFactorySupport(
client dynamic.NamespaceableResourceInterface,
informer cache.SharedIndexInformer,
apiFactory api.Factory,
opts ...Opts,
) *notificationController {
ctrl := NewController(client, informer, apiFactory, opts...)
ctrl.factorySupport = true
return ctrl
}

type notificationController struct {
client dynamic.NamespaceableResourceInterface
informer cache.SharedIndexInformer
Expand All @@ -165,6 +177,7 @@
toUnstructured func(obj v1.Object) (*unstructured.Unstructured, error)
eventCallback func(eventSequence NotificationEventSequence)
namespaceSupport bool
factorySupport bool
}

func (c *notificationController) Run(threadiness int, stopCh <-chan struct{}) {
Expand Down Expand Up @@ -304,15 +317,17 @@
}
}

if !c.namespaceSupport {
api, err := c.apiFactory.GetAPI()
if c.factorySupport {
apisWithNamespace, err := c.apiFactory.GetAPIsFromFactory(resource)
if err != nil {
logEntry.Errorf("Failed to get api: %v", err)
logEntry.Errorf("Failed to get api with key: %v", err)

Check warning on line 323 in pkg/controller/controller.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/controller.go#L323

Added line #L323 was not covered by tests
eventSequence.addError(err)
return
}
c.processResource(api, resource, logEntry, &eventSequence)
} else {
for _, api := range apisWithNamespace {
c.processResource(api, resource, logEntry, &eventSequence)
}

} else if c.namespaceSupport {
apisWithNamespace, err := c.apiFactory.GetAPIsFromNamespace(resource.GetNamespace())
if err != nil {
logEntry.Errorf("Failed to get api with namespace: %v", err)
Expand All @@ -321,7 +336,16 @@
for _, api := range apisWithNamespace {
c.processResource(api, resource, logEntry, &eventSequence)
}
} else {
api, err := c.apiFactory.GetAPI()
if err != nil {
logEntry.Errorf("Failed to get api: %v", err)
eventSequence.addError(err)
return
}
c.processResource(api, resource, logEntry, &eventSequence)
}

logEntry.Info("Processing completed")

return
Expand Down
27 changes: 20 additions & 7 deletions pkg/controller/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func newResource(name string, modifiers ...func(app *unstructured.Unstructured))
return &app
}

func newController(t *testing.T, ctx context.Context, client dynamic.Interface, opts ...Opts) (*notificationController, *mocks.MockAPI, error) {
func newController(t *testing.T, ctx context.Context, client dynamic.Interface, factorySupport bool, opts ...Opts) (*notificationController, *mocks.MockAPI, error) {
mockCtrl := gomock.NewController(t)
go func() {
<-ctx.Done()
Expand All @@ -90,7 +90,12 @@ func newController(t *testing.T, ctx context.Context, client dynamic.Interface,

go informer.Run(ctx.Done())

c := NewControllerWithNamespaceSupport(resourceClient, informer, &mocks.FakeFactory{Api: mockAPI}, opts...)
var c *notificationController
if factorySupport {
c = NewControllerWithFactorySupport(resourceClient, informer, &mocks.FakeFactory{Api: mockAPI}, opts...)
} else {
c = NewControllerWithNamespaceSupport(resourceClient, informer, &mocks.FakeFactory{Api: mockAPI}, opts...)
}
if !cache.WaitForCacheSync(ctx.Done(), informer.HasSynced) {
return nil, nil, errors.New("failed to sync informers")
}
Expand All @@ -105,7 +110,7 @@ func TestSendsNotificationIfTriggered(t *testing.T) {
subscriptions.SubscribeAnnotationKey("my-trigger", "mock"): "recipient",
}))

ctrl, api, err := newController(t, ctx, newFakeClient(app))
ctrl, api, err := newController(t, ctx, newFakeClient(app), false)
assert.NoError(t, err)

receivedObj := map[string]interface{}{}
Expand Down Expand Up @@ -136,7 +141,7 @@ func TestDoesNotSendNotificationIfAnnotationPresent(t *testing.T) {
subscriptions.SubscribeAnnotationKey("my-trigger", "mock"): "recipient",
notifiedAnnotationKey: mustToJson(state),
}))
ctrl, api, err := newController(t, ctx, newFakeClient(app))
ctrl, api, err := newController(t, ctx, newFakeClient(app), false)
assert.NoError(t, err)

api.EXPECT().RunTrigger("my-trigger", gomock.Any()).Return([]triggers.ConditionResult{{Triggered: true, Templates: []string{"test"}}}, nil)
Expand All @@ -158,7 +163,7 @@ func TestRemovesAnnotationIfNoTrigger(t *testing.T) {
subscriptions.SubscribeAnnotationKey("my-trigger", "mock"): "recipient",
notifiedAnnotationKey: mustToJson(state),
}))
ctrl, api, err := newController(t, ctx, newFakeClient(app))
ctrl, api, err := newController(t, ctx, newFakeClient(app), false)
assert.NoError(t, err)

api.EXPECT().RunTrigger("my-trigger", gomock.Any()).Return([]triggers.ConditionResult{{Triggered: false}}, nil)
Expand All @@ -173,6 +178,14 @@ func TestRemovesAnnotationIfNoTrigger(t *testing.T) {
}

func TestUpdatedAnnotationsSavedAsPatch(t *testing.T) {
controllerRunAndVerifyResult(t, false)
}

func TestSendsNotificationUsingAPIFromFactory(t *testing.T) {
controllerRunAndVerifyResult(t, true)
}

func controllerRunAndVerifyResult(t *testing.T, factorySupport bool) {
ctx, cancel := context.WithCancel(context.TODO())
defer cancel()

Expand All @@ -191,7 +204,7 @@ func TestUpdatedAnnotationsSavedAsPatch(t *testing.T) {
patchCh <- action.(kubetesting.PatchAction).GetPatch()
return true, nil, nil
})
ctrl, api, err := newController(t, ctx, client)
ctrl, api, err := newController(t, ctx, client, factorySupport)
assert.NoError(t, err)
api.EXPECT().RunTrigger("my-trigger", gomock.Any()).Return([]triggers.ConditionResult{{Triggered: false}}, nil)

Expand Down Expand Up @@ -322,7 +335,7 @@ func TestWithEventCallback(t *testing.T) {
subscriptions.SubscribeAnnotationKey("my-trigger", "mock"): "recipient",
}))

ctrl, api, err := newController(t, ctx, newFakeClient(app), WithEventCallback(mockEventCallback))
ctrl, api, err := newController(t, ctx, newFakeClient(app), false, WithEventCallback(mockEventCallback))
ctrl.namespaceSupport = false
assert.NoError(t, err)
ctrl.apiFactory = &mocks.FakeFactory{Api: api, Err: tc.apiErr}
Expand Down
6 changes: 6 additions & 0 deletions pkg/mocks/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,9 @@ func (f *FakeFactory) GetAPIsFromNamespace(namespace string) (map[string]api.API
apiMap[namespace] = f.Api
return apiMap, f.Err
}

func (f *FakeFactory) GetAPIsFromFactory(resource interface{}) (map[string]api.API, error) {
apiMap := make(map[string]api.API)
apiMap["key1"] = f.Api
return apiMap, f.Err
}