Skip to content

Commit 33d5d8b

Browse files
committed
to support factory based self service
1 parent cb43164 commit 33d5d8b

File tree

2 files changed

+34
-17
lines changed

2 files changed

+34
-17
lines changed

pkg/api/factory.go

+5
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ type Settings struct {
3333
type Factory interface {
3434
GetAPI() (API, error)
3535
GetAPIsFromNamespace(namespace string) (map[string]API, error)
36+
GetAPIsFromFactory(resource interface{}) (map[string]API, error)
3637
}
3738

3839
type apiFactory struct {
@@ -170,6 +171,10 @@ func (f *apiFactory) GetAPIsFromNamespace(namespace string) (map[string]API, err
170171
return apis, nil
171172
}
172173

174+
func (f *apiFactory) GetAPIsFromFactory(resource interface{}) (map[string]API, error) {
175+
return nil, fmt.Errorf("not implemented")
176+
}
177+
173178
func (f *apiFactory) getApiFromNamespace(namespace string) (API, error) {
174179
cm, secret, err := f.getConfigMapAndSecret(namespace)
175180
if err != nil {

pkg/controller/controller.go

+29-17
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,18 @@ func NewControllerWithNamespaceSupport(
154154
return ctrl
155155
}
156156

157+
// NewControllerWithNamespaceSupport For self-service notification
158+
func NewControllerWithFactorySupport(
159+
client dynamic.NamespaceableResourceInterface,
160+
informer cache.SharedIndexInformer,
161+
apiFactory api.Factory,
162+
opts ...Opts,
163+
) *notificationController {
164+
ctrl := NewController(client, informer, apiFactory, opts...)
165+
ctrl.factorySupport = true
166+
return ctrl
167+
}
168+
157169
type notificationController struct {
158170
client dynamic.NamespaceableResourceInterface
159171
informer cache.SharedIndexInformer
@@ -165,6 +177,7 @@ type notificationController struct {
165177
toUnstructured func(obj v1.Object) (*unstructured.Unstructured, error)
166178
eventCallback func(eventSequence NotificationEventSequence)
167179
namespaceSupport bool
180+
factorySupport bool
168181
}
169182

170183
func (c *notificationController) Run(threadiness int, stopCh <-chan struct{}) {
@@ -304,36 +317,35 @@ func (c *notificationController) processQueueItem() (processNext bool) {
304317
}
305318
}
306319

307-
if !c.namespaceSupport {
308-
api, err := c.apiFactory.GetAPI()
320+
if c.factorySupport {
321+
apisWithNamespace, err := c.apiFactory.GetAPIsFromFactory(resource)
309322
if err != nil {
310-
logEntry.Errorf("Failed to get api: %v", err)
323+
logEntry.Errorf("Failed to get api with namespace: %v", err)
311324
eventSequence.addError(err)
312-
return
313325
}
314-
c.processResource(api, resource, logEntry, &eventSequence)
315-
} else {
316-
// this is for cluster support
317-
annotations := resource.GetAnnotations()
318-
notificationNS := annotations["notification-namespace"]
319-
notificationCluster := annotations["notification-cluster"]
320-
log.Infof("%s", notificationNS)
321-
log.Infof("%s", notificationCluster)
322-
323-
resourceNameSpace := resource.GetNamespace()
324-
if notificationNS != "" && notificationCluster != "" {
325-
resourceNameSpace = notificationCluster + "#" + notificationNS
326+
for _, api := range apisWithNamespace {
327+
c.processResource(api, resource, logEntry, &eventSequence)
326328
}
327329

328-
apisWithNamespace, err := c.apiFactory.GetAPIsFromNamespace(resourceNameSpace)
330+
} else if c.namespaceSupport {
331+
apisWithNamespace, err := c.apiFactory.GetAPIsFromNamespace(resource.GetNamespace())
329332
if err != nil {
330333
logEntry.Errorf("Failed to get api with namespace: %v", err)
331334
eventSequence.addError(err)
332335
}
333336
for _, api := range apisWithNamespace {
334337
c.processResource(api, resource, logEntry, &eventSequence)
335338
}
339+
} else {
340+
api, err := c.apiFactory.GetAPI()
341+
if err != nil {
342+
logEntry.Errorf("Failed to get api: %v", err)
343+
eventSequence.addError(err)
344+
return
345+
}
346+
c.processResource(api, resource, logEntry, &eventSequence)
336347
}
348+
337349
logEntry.Info("Processing completed")
338350

339351
return

0 commit comments

Comments
 (0)