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

metrics: Add map pressure metric for auth map #28357

Merged
merged 1 commit into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions pkg/auth/authmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type authMap interface {
DeleteIf(predicate func(key authKey, info authInfo) bool) error
Get(key authKey) (authInfo, error)
All() (map[authKey]authInfo, error)
MaxEntries() uint32
}

type authMapCacher interface {
Expand Down
34 changes: 28 additions & 6 deletions pkg/auth/authmap_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,29 @@ import (
"golang.org/x/exp/maps"

"github.com/cilium/cilium/pkg/lock"
"github.com/cilium/cilium/pkg/maps/authmap"
"github.com/cilium/cilium/pkg/metrics"
)

type authMapCache struct {
logger logrus.FieldLogger
authmap authMap
cacheEntries map[authKey]authInfoCache
cacheEntriesMutex lock.RWMutex
pressureGauge *metrics.GaugeWithThreshold
}

func newAuthMapCache(logger logrus.FieldLogger, authmap authMap) *authMapCache {
func newAuthMapCache(logger logrus.FieldLogger, authMap authMap) *authMapCache {
var pressureGauge *metrics.GaugeWithThreshold

if metrics.BPFMapPressure {
pressureGauge = metrics.NewBPFMapPressureGauge(authmap.MapName, 0)
}
return &authMapCache{
logger: logger,
authmap: authmap,
cacheEntries: map[authKey]authInfoCache{},
logger: logger,
authmap: authMap,
cacheEntries: map[authKey]authInfoCache{},
pressureGauge: pressureGauge,
}
}

Expand Down Expand Up @@ -69,6 +78,7 @@ func (r *authMapCache) Update(key authKey, info authInfo) error {
authInfo: info,
storedAt: time.Now(),
}
r.updatePressureMetric()

return nil
}
Expand All @@ -88,10 +98,14 @@ func (r *authMapCache) Delete(key authKey) error {
}

delete(r.cacheEntries, key)

r.updatePressureMetric()
return nil
}

func (r *authMapCache) MaxEntries() uint32 {
return r.authmap.MaxEntries()
}

func (r *authMapCache) DeleteIf(predicate func(key authKey, info authInfo) bool) error {
r.cacheEntriesMutex.Lock()
defer r.cacheEntriesMutex.Unlock()
Expand All @@ -111,7 +125,7 @@ func (r *authMapCache) DeleteIf(predicate func(key authKey, info authInfo) bool)
delete(r.cacheEntries, k)
}
}

r.updatePressureMetric()
return nil
}

Expand All @@ -129,8 +143,16 @@ func (r *authMapCache) restoreCache() error {
}
}

r.updatePressureMetric()
r.logger.
WithField("cached_entries", len(r.cacheEntries)).
Debug("Restored entries")
return nil
}

func (r *authMapCache) updatePressureMetric() {
if r.pressureGauge == nil {
return
}
r.pressureGauge.Set(float64(len(r.cacheEntries)) / float64(r.authmap.MaxEntries()))
}
4 changes: 4 additions & 0 deletions pkg/auth/authmap_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,7 @@ func (r *authMapWriter) DeleteIf(predicate func(key authKey, info authInfo) bool

return nil
}

func (r *authMapWriter) MaxEntries() uint32 {
return r.authMap.MaxEntries()
}
4 changes: 4 additions & 0 deletions pkg/auth/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,10 @@ func (r *fakeAuthMap) Update(key authKey, info authInfo) error {
return nil
}

func (r *fakeAuthMap) MaxEntries() uint32 {
return 1 << 8
}

func assertErrorString(errString string) assert.ErrorAssertionFunc {
return func(t assert.TestingT, err error, msgAndArgs ...interface{}) bool {
return assert.EqualError(t, err, errString, msgAndArgs)
Expand Down
7 changes: 7 additions & 0 deletions pkg/maps/authmap/auth_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ type Map interface {
// IterateWithCallback iterates through all the keys/values of an auth map,
// passing each key/value pair to the cb callback.
IterateWithCallback(cb IterateCallback) error

// MaxEntries returns the maximum number of entries the auth map can hold.
MaxEntries() uint32
}

type authMap struct {
Expand Down Expand Up @@ -113,6 +116,10 @@ func (m *authMap) IterateWithCallback(cb IterateCallback) error {
)
}

func (m *authMap) MaxEntries() uint32 {
return m.bpfMap.MaxEntries()
}

// AuthKey implements the bpf.MapKey interface.
//
// Must be in sync with struct auth_key in <bpf/lib/common.h>
Expand Down
4 changes: 4 additions & 0 deletions pkg/maps/authmap/fake/auth_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,7 @@ func (f fakeAuthMap) IterateWithCallback(cb authmap.IterateCallback) error {
}
return nil
}

func (f fakeAuthMap) MaxEntries() uint32 {
return 1 << 8
}