Skip to content

Commit

Permalink
fix: fresh objects for Synchronization executions
Browse files Browse the repository at this point in the history
- snapshots are updated without Event tasks until success Synchronization execution
- fix documentation for "Group" binding context
  • Loading branch information
diafour committed Mar 11, 2021
1 parent 81e5d89 commit b16bb3e
Show file tree
Hide file tree
Showing 17 changed files with 446 additions and 298 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ jobs:
fail-fast: true
matrix:
os: [ubuntu-latest]
k8s_version: [1.16, 1.17]
k8s_version: [1.19, 1.20]
runs-on: ${{ matrix.os }}
env:
ACTIONS_ALLOW_UNSECURE_COMMANDS: true
Expand Down
8 changes: 4 additions & 4 deletions HOOKS.md
Original file line number Diff line number Diff line change
Expand Up @@ -693,13 +693,13 @@ kubernetes:

#### "Synchronization" binding context for group

During startup, the hook will be executed with the "Synchronization" binding context with `snapshots` JSON object:
During startup, the hook will be executed with the "Group" binding context with `snapshots` JSON object that contains all objects from bindings in the group. Note there is no `objects` field as for "normal" binding:

```yaml
[
{
"binding": "pods",
"type": "Synchronization",
"type": "Group",
"snapshots": {
"monitor_pods": [
{
Expand Down Expand Up @@ -734,9 +734,9 @@ During startup, the hook will be executed with the "Synchronization" binding con
]
```

#### "Group" binding context
#### "Event" binding context for group

If pod `pod-dfbd12` is then added into the "default" namespace, then the hook will be executed with the "Group" binding context:
Hook is executed on event from every binding in group. So, if pod `pod-dfbd12` is added into the "default" namespace, then the hook will be executed with the "Group" binding context with `snapshots` JSON object that contains all objects from bindings in the group:

```yaml
[
Expand Down
6 changes: 4 additions & 2 deletions pkg/hook/binding_context/binding_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func (bc BindingContext) MapV1() map[string]interface{} {
return res
}

// Set "snapshots" field if needed.
if len(bc.Metadata.IncludeSnapshots) > 0 || bc.Metadata.IncludeAllSnapshots {
if len(bc.Snapshots) > 0 {
res["snapshots"] = bc.Snapshots
Expand All @@ -66,6 +67,7 @@ func (bc BindingContext) MapV1() map[string]interface{} {
}
}

// Handle validating and conversion before grouping.
if bc.Metadata.BindingType == KubernetesValidating {
res["type"] = "Validating"
res["review"] = bc.AdmissionReview
Expand All @@ -80,8 +82,8 @@ func (bc BindingContext) MapV1() map[string]interface{} {
return res
}

// KubernetesValidating uses 'group' only for snapshots.
if bc.Metadata.Group != "" && bc.Metadata.BindingType != KubernetesValidating {
// Group is always has "type: Group", even for Synchronization.
if bc.Metadata.Group != "" {
res["binding"] = bc.Metadata.Group
res["type"] = "Group"
return res
Expand Down
39 changes: 27 additions & 12 deletions pkg/hook/controller/hook_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ import (
)

type BindingExecutionInfo struct {
BindingContext []BindingContext
IncludeSnapshots []string
IncludeAllSnapshots bool
AllowFailure bool
QueueName string
Binding string
Group string
WaitForSynchronization bool
BindingContext []BindingContext
IncludeSnapshots []string
IncludeAllSnapshots bool
AllowFailure bool
QueueName string
Binding string
Group string

KubernetesBinding OnKubernetesEventConfig
}

// В каждый хук надо будет положить этот объект.
Expand All @@ -45,15 +46,16 @@ type HookController interface {
CanHandleValidatingEvent(event ValidatingEvent) bool
CanHandleConversionEvent(event conversion.Event, rule conversion.Rule) bool

// These method should call underlying BindingController to get binding context
// These method should call an underlying *Binding*Controller to get binding context
// and then add Snapshots to binding context
HandleEnableKubernetesBindings(createTasksFn func(BindingExecutionInfo)) error
HandleKubeEvent(event KubeEvent, createTasksFn func(BindingExecutionInfo))
HandleScheduleEvent(crontab string, createTasksFn func(BindingExecutionInfo))
HandleValidatingEvent(event ValidatingEvent, createTasksFn func(BindingExecutionInfo))
HandleConversionEvent(event conversion.Event, rule conversion.Rule, createTasksFn func(BindingExecutionInfo))

StartMonitors()
UnlockKubernetesEvents()
UnlockKubernetesEventsFor(monitorID string)
StopMonitors()

EnableScheduleBindings()
Expand Down Expand Up @@ -219,9 +221,15 @@ func (hc *hookController) HandleScheduleEvent(crontab string, createTasksFn func
}
}

func (hc *hookController) StartMonitors() {
func (hc *hookController) UnlockKubernetesEvents() {
if hc.KubernetesController != nil {
hc.KubernetesController.StartMonitors()
hc.KubernetesController.UnlockEvents()
}
}

func (hc *hookController) UnlockKubernetesEventsFor(monitorID string) {
if hc.KubernetesController != nil {
hc.KubernetesController.UnlockEventsFor(monitorID)
}
}

Expand Down Expand Up @@ -306,10 +314,17 @@ func (hc *hookController) UpdateSnapshots(context []BindingContext) []BindingCon
return context
}

// Turn on cached snapshots mode to retrieve snapshots only once. (Synchronization and self-include)
hc.KubernetesController.StartCachedSnapshotMode()
defer hc.KubernetesController.StopCachedSnapshotMode()

newContext := []BindingContext{}
for _, bc := range context {
newBc := bc
newBc.Snapshots = hc.KubernetesSnapshotsFor(bc.Metadata.BindingType, bc.Binding)
if newBc.Metadata.BindingType == OnKubernetesEvent && newBc.Type == TypeSynchronization {
newBc.Objects = hc.KubernetesController.SnapshotsFor(bc.Binding)
}
newContext = append(newContext, newBc)
}

Expand Down
Loading

0 comments on commit b16bb3e

Please sign in to comment.