-
Notifications
You must be signed in to change notification settings - Fork 2
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
controller: reconciliation workflows #9
Merged
Merged
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
42bea9d
controller: new reconciler abstractions Dispatcher and Subscriber
guicassolato b9c52ef
Renames controller.Dispatcher to controller.Workflow
guicassolato 1b6c378
controller.newGatewayAPITopologyBuilder made private
guicassolato 16fd7a5
fixup: workflow postcondition
guicassolato 99c6532
rename controller/topology after its function: topology_builder
guicassolato 3cb5faf
controller: pass context to create/delete Security/AuthorizationPolicies
guicassolato 4299327
Public variables for the API resources
guicassolato d94e488
fixup: oops, ratelimitpolicies informer should not have been removed
guicassolato 3bbaa3c
fixup: RateLimitPolicyKind global var
guicassolato File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,9 @@ | ||
package controller | ||
|
||
import gwapiv1 "sigs.k8s.io/gateway-api/apis/v1" | ||
|
||
var ( | ||
GatewayClassesResource = gwapiv1.SchemeGroupVersion.WithResource("gatewayclasses") | ||
GatewaysResource = gwapiv1.SchemeGroupVersion.WithResource("gateways") | ||
HTTPRoutesResource = gwapiv1.SchemeGroupVersion.WithResource("httproutes") | ||
) |
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,44 @@ | ||
package controller | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/samber/lo" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
|
||
"github.com/kuadrant/policy-machinery/machinery" | ||
) | ||
|
||
type ResourceEventMatcher struct { | ||
Resource *schema.GroupVersionResource | ||
EventType *EventType | ||
ObjectNamespace string | ||
ObjectName string | ||
} | ||
|
||
type Subscription struct { | ||
ReconcileFunc CallbackFunc | ||
Events []ResourceEventMatcher | ||
} | ||
|
||
// Subscriber calls the reconciler function of the first subscription that matches the event | ||
type Subscriber []Subscription | ||
|
||
func (s Subscriber) Reconcile(ctx context.Context, resourceEvent ResourceEvent, topology *machinery.Topology) { | ||
subscription, found := lo.Find(s, func(subscription Subscription) bool { | ||
_, found := lo.Find(subscription.Events, func(m ResourceEventMatcher) bool { | ||
obj := resourceEvent.OldObject | ||
if obj == nil { | ||
obj = resourceEvent.NewObject | ||
} | ||
return (m.Resource == nil || *m.Resource == resourceEvent.Resource) && | ||
(m.EventType == nil || *m.EventType == resourceEvent.EventType) && | ||
(m.ObjectNamespace == "" || m.ObjectNamespace == obj.GetNamespace()) && | ||
(m.ObjectName == "" || m.ObjectName == obj.GetName()) | ||
}) | ||
return found | ||
}) | ||
if found && subscription.ReconcileFunc != nil { | ||
subscription.ReconcileFunc(ctx, resourceEvent, topology) | ||
} | ||
} |
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,40 @@ | ||
package controller | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
|
||
"github.com/kuadrant/policy-machinery/machinery" | ||
) | ||
|
||
// Workflow runs an optional precondition reconciliation function, then dispatches the reconciliation event to | ||
// a list of concurrent reconciliation tasks, and runs an optional postcondition reconciliation function. | ||
type Workflow struct { | ||
Precondition CallbackFunc | ||
Tasks []CallbackFunc | ||
Postcondition CallbackFunc | ||
} | ||
|
||
func (d *Workflow) Run(ctx context.Context, resourceEvent ResourceEvent, topology *machinery.Topology) { | ||
// run precondition reconcile function | ||
if d.Precondition != nil { | ||
d.Precondition(ctx, resourceEvent, topology) | ||
} | ||
|
||
// dispatch the event to concurrent tasks | ||
funcs := d.Tasks | ||
waitGroup := &sync.WaitGroup{} | ||
waitGroup.Add(len(funcs)) | ||
for _, f := range funcs { | ||
go func() { | ||
defer waitGroup.Done() | ||
f(ctx, resourceEvent, topology) | ||
}() | ||
} | ||
waitGroup.Wait() | ||
|
||
// run precondition reconcile function | ||
if d.Postcondition != nil { | ||
d.Postcondition(ctx, resourceEvent, topology) | ||
} | ||
} |
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
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
File renamed without changes.
File renamed without changes.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Time to start writing tests 😜