Skip to content

Commit

Permalink
Add slice of callbacks to policy solver (projectcalico#9361)
Browse files Browse the repository at this point in the history
  • Loading branch information
mazdakn authored Oct 21, 2024
1 parent add788d commit 796b98d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
4 changes: 2 additions & 2 deletions felix/calc/calc_graph.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2016-2021 Tigera, Inc. All rights reserved.
// Copyright (c) 2016-2024 Tigera, Inc. All rights reserved.

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -366,7 +366,7 @@ func NewCalculationGraph(callbacks PipelineCallbacks, conf *config.Config, liveC
activeRulesCalc.PolicyMatchListener = polResolver
polResolver.RegisterWith(allUpdDispatcher, localEndpointDispatcher)
// And hook its output to the callbacks.
polResolver.Callbacks = callbacks
polResolver.RegisterCallback(callbacks)
cg.policyResolver = polResolver

// Register for host IP updates.
Expand Down
15 changes: 12 additions & 3 deletions felix/calc/policy_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ type PolicyResolver struct {
endpoints map[model.Key]interface{}
dirtyEndpoints set.Set[any] /* FIXME model.WorkloadEndpointKey or model.HostEndpointKey */
policySorter *PolicySorter
Callbacks PolicyResolverCallbacks
Callbacks []PolicyResolverCallbacks
InSync bool
}

Expand All @@ -73,6 +73,7 @@ func NewPolicyResolver() *PolicyResolver {
endpoints: make(map[model.Key]interface{}),
dirtyEndpoints: set.New[any](),
policySorter: NewPolicySorter(),
Callbacks: []PolicyResolverCallbacks{},
}
}

Expand All @@ -84,6 +85,10 @@ func (pr *PolicyResolver) RegisterWith(allUpdDispatcher, localEndpointDispatcher
localEndpointDispatcher.RegisterStatusHandler(pr.OnDatamodelStatus)
}

func (pr *PolicyResolver) RegisterCallback(cb PolicyResolverCallbacks) {
pr.Callbacks = append(pr.Callbacks, cb)
}

func (pr *PolicyResolver) OnUpdate(update api.Update) (filterOut bool) {
switch key := update.Key.(type) {
case model.WorkloadEndpointKey, model.HostEndpointKey:
Expand Down Expand Up @@ -178,7 +183,9 @@ func (pr *PolicyResolver) sendEndpointUpdate(endpointID interface{}) error {
endpoint, ok := pr.endpoints[endpointID.(model.Key)]
if !ok {
log.Debugf("Endpoint is unknown, sending nil update")
pr.Callbacks.OnEndpointTierUpdate(endpointID.(model.Key), nil, []TierInfo{})
for _, cb := range pr.Callbacks {
cb.OnEndpointTierUpdate(endpointID.(model.Key), nil, []TierInfo{})
}
return nil
}

Expand Down Expand Up @@ -210,6 +217,8 @@ func (pr *PolicyResolver) sendEndpointUpdate(endpointID interface{}) error {
}

log.Debugf("Endpoint tier update: %v -> %v", endpointID, applicableTiers)
pr.Callbacks.OnEndpointTierUpdate(endpointID.(model.Key), endpoint, applicableTiers)
for _, cb := range pr.Callbacks {
cb.OnEndpointTierUpdate(endpointID.(model.Key), endpoint, applicableTiers)
}
return nil
}
2 changes: 1 addition & 1 deletion felix/calc/policy_resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func TestPolicyResolver_OnUpdate(t *testing.T) {
func createPolicyResolver() (*PolicyResolver, *policyResolverRecorder) {
pr := NewPolicyResolver()
recorder := newPolicyResolverRecorder()
pr.Callbacks = recorder
pr.RegisterCallback(recorder)
return pr, recorder
}

Expand Down

0 comments on commit 796b98d

Please sign in to comment.