-
Notifications
You must be signed in to change notification settings - Fork 289
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reduce memory consumption for Kubernetes source configuration (#1425)
- Loading branch information
Showing
13 changed files
with
529 additions
and
241 deletions.
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,70 @@ | ||
package kubernetes | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
"time" | ||
|
||
"github.com/sirupsen/logrus" | ||
"golang.org/x/sync/errgroup" | ||
) | ||
|
||
// backgroundProcessor is responsible for running background processes. | ||
type backgroundProcessor struct { | ||
mu sync.RWMutex | ||
cancelCtxFn func() | ||
startTime time.Time | ||
|
||
errGroup *errgroup.Group | ||
} | ||
|
||
// newBackgroundProcessor creates new background processor. | ||
func newBackgroundProcessor() *backgroundProcessor { | ||
return &backgroundProcessor{} | ||
} | ||
|
||
// StartTime returns the start time of the background processor. | ||
func (b *backgroundProcessor) StartTime() time.Time { | ||
b.mu.RLock() | ||
defer b.mu.RUnlock() | ||
return b.startTime | ||
} | ||
|
||
// Run starts the background processes. | ||
func (b *backgroundProcessor) Run(parentCtx context.Context, fns []func(ctx context.Context)) { | ||
b.mu.Lock() | ||
defer b.mu.Unlock() | ||
|
||
b.startTime = time.Now() | ||
ctx, cancelFn := context.WithCancel(parentCtx) | ||
b.cancelCtxFn = cancelFn | ||
|
||
errGroup, errGroupCtx := errgroup.WithContext(ctx) | ||
b.errGroup = errGroup | ||
|
||
for _, fn := range fns { | ||
fn := fn | ||
errGroup.Go(func() error { | ||
fn(errGroupCtx) | ||
return nil | ||
}) | ||
} | ||
} | ||
|
||
// StopAndWait stops the background processes and waits for them to finish. | ||
func (b *backgroundProcessor) StopAndWait(log logrus.FieldLogger) error { | ||
b.mu.Lock() | ||
defer b.mu.Unlock() | ||
|
||
if b.cancelCtxFn != nil { | ||
log.Debug("Cancelling context of the background processor...") | ||
b.cancelCtxFn() | ||
} | ||
|
||
if b.errGroup == nil { | ||
return nil | ||
} | ||
|
||
log.Debug("Waiting for background processor to finish...") | ||
return b.errGroup.Wait() | ||
} |
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,93 @@ | ||
package kubernetes | ||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
|
||
"github.com/kubeshop/botkube/pkg/maputil" | ||
) | ||
|
||
// configurationStore stores all source configurations in a thread-safe way. | ||
type configurationStore struct { | ||
store map[string]SourceConfig | ||
storeByKubeconfig map[string]map[string]struct{} | ||
|
||
lock sync.RWMutex | ||
} | ||
|
||
// newConfigurations creates new empty configurationStore instance. | ||
func newConfigurations() *configurationStore { | ||
return &configurationStore{ | ||
store: make(map[string]SourceConfig), | ||
storeByKubeconfig: make(map[string]map[string]struct{}), | ||
} | ||
} | ||
|
||
// Store stores SourceConfig in a thread-safe way. | ||
func (c *configurationStore) Store(sourceName string, cfg SourceConfig) { | ||
c.lock.Lock() | ||
defer c.lock.Unlock() | ||
|
||
key := c.keyForStore(sourceName, cfg.isInteractivitySupported) | ||
|
||
c.store[key] = cfg | ||
|
||
kubeConfigKey := string(cfg.kubeConfig) | ||
if _, ok := c.storeByKubeconfig[kubeConfigKey]; !ok { | ||
c.storeByKubeconfig[kubeConfigKey] = make(map[string]struct{}) | ||
} | ||
c.storeByKubeconfig[kubeConfigKey][key] = struct{}{} | ||
} | ||
|
||
// Get returns SourceConfig by a key. | ||
func (c *configurationStore) Get(sourceKey string) (SourceConfig, bool) { | ||
c.lock.RLock() | ||
defer c.lock.RUnlock() | ||
val, ok := c.store[sourceKey] | ||
return val, ok | ||
} | ||
|
||
// GetSystemConfig returns system Source Config. | ||
// The system config is used for getting system (plugin-wide) logger and informer resync period. | ||
func (c *configurationStore) GetSystemConfig() (SourceConfig, bool) { | ||
c.lock.RLock() | ||
defer c.lock.RUnlock() | ||
|
||
sortedKeys := maputil.SortKeys(c.store) | ||
if len(sortedKeys) == 0 { | ||
return SourceConfig{}, false | ||
} | ||
|
||
return c.store[sortedKeys[0]], true | ||
} | ||
|
||
// Len returns number of stored SourceConfigs. | ||
func (c *configurationStore) Len() int { | ||
c.lock.RLock() | ||
defer c.lock.RUnlock() | ||
return len(c.store) | ||
} | ||
|
||
// CloneByKubeconfig returns a copy of the underlying map of source configurations grouped by kubeconfigs. | ||
func (c *configurationStore) CloneByKubeconfig() map[string]map[string]SourceConfig { | ||
c.lock.RLock() | ||
defer c.lock.RUnlock() | ||
|
||
var out = make(map[string]map[string]SourceConfig) | ||
for kubeConfig, srcIndex := range c.storeByKubeconfig { | ||
if out[kubeConfig] == nil { | ||
out[kubeConfig] = make(map[string]SourceConfig) | ||
} | ||
|
||
for srcKey := range srcIndex { | ||
out[kubeConfig][srcKey] = c.store[srcKey] | ||
} | ||
} | ||
|
||
return out | ||
} | ||
|
||
// keyForStore returns a key for storing configuration in the store. | ||
func (c *configurationStore) keyForStore(sourceName string, isInteractivitySupported bool) string { | ||
return fmt.Sprintf("%s/%t", sourceName, isInteractivitySupported) | ||
} |
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
Oops, something went wrong.