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

pkg/policy: index policies by k8s UID #28173

Merged
merged 1 commit into from
Sep 15, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 20 additions & 3 deletions pkg/policy/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ type Repository struct {
Mutex lock.RWMutex
rules ruleSlice

// rulesIndexByK8sUID indexes the rules by k8s UID.
rulesIndexByK8sUID map[string]*rule

// revision is the revision of the policy repository. It will be
// incremented whenever the policy repository is changed.
// Always positive (>0).
Expand Down Expand Up @@ -190,9 +193,10 @@ func NewStoppedPolicyRepository(
) *Repository {
selectorCache := NewSelectorCache(idAllocator, idCache)
repo := &Repository{
selectorCache: selectorCache,
certManager: certManager,
secretManager: secretManager,
rulesIndexByK8sUID: map[string]*rule{},
selectorCache: selectorCache,
certManager: certManager,
secretManager: secretManager,
}
repo.revision.Store(1)
repo.policyCache = NewPolicyCache(repo, true)
Expand Down Expand Up @@ -367,6 +371,13 @@ func (p *Repository) AllowsEgressRLocked(ctx *SearchContext) api.Decision {
func (p *Repository) SearchRLocked(lbls labels.LabelArray) api.Rules {
result := api.Rules{}

if uid := lbls.Get(labels.LabelSourceK8sKeyPrefix + k8sConst.PolicyLabelUID); uid != "" {
r, ok := p.rulesIndexByK8sUID[uid]
if ok {
result = append(result, &r.Rule)
}
return result
}
for _, r := range p.rules {
if r.Labels.Contains(lbls) {
result = append(result, &r.Rule)
Expand Down Expand Up @@ -406,6 +417,9 @@ func (p *Repository) AddListLocked(rules api.Rules) (ruleSlice, uint64) {
metadata: newRuleMetadata(),
}
newList[i] = newRule
if uid := rules[i].Labels.Get(labels.LabelSourceK8sKeyPrefix + k8sConst.PolicyLabelUID); uid != "" {
p.rulesIndexByK8sUID[uid] = newRule
}
}

p.rules = append(p.rules, newList...)
Expand Down Expand Up @@ -516,6 +530,9 @@ func (p *Repository) DeleteByLabelsLocked(lbls labels.LabelArray) (ruleSlice, ui
if deleted > 0 {
p.BumpRevision()
p.rules = new
if uid := lbls.Get(labels.LabelSourceK8sKeyPrefix + k8sConst.PolicyLabelUID); uid != "" {
delete(p.rulesIndexByK8sUID, uid)
}
metrics.Policy.Sub(float64(deleted))
}

Expand Down