Skip to content
This repository has been archived by the owner on Nov 1, 2022. It is now read-only.

fix concurrent map write panics #2926

Merged
merged 1 commit into from
Mar 25, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions pkg/cluster/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,9 @@ type Cluster struct {
syncErrors map[resource.ID]error
muSyncErrors sync.RWMutex

allowedNamespaces map[string]struct{}
loggedAllowedNS map[string]bool // to keep track of whether we've logged a problem with seeing an allowed namespace
allowedNamespaces map[string]struct{}
loggedAllowedNS map[string]bool // to keep track of whether we've logged a problem with seeing an allowed namespace
loggedAllowedNSLock sync.RWMutex

imageIncluder cluster.Includer
resourceExcludeList []string
Expand Down Expand Up @@ -317,13 +318,13 @@ func (c *Cluster) getAllowedAndExistingNamespaces(ctx context.Context) ([]string
ns, err := c.client.CoreV1().Namespaces().Get(name, meta_v1.GetOptions{})
switch {
case err == nil:
c.loggedAllowedNS[name] = false // reset, so if the namespace goes away we'll log it again
c.updateLoggedAllowedNS(name, false) // reset, so if the namespace goes away we'll log it again
nsList = append(nsList, ns.Name)
case apierrors.IsUnauthorized(err) || apierrors.IsForbidden(err) || apierrors.IsNotFound(err):
if !c.loggedAllowedNS[name] {
if !c.getLoggedAllowedNS(name) {
c.logger.Log("warning", "cannot access allowed namespace",
"namespace", name, "err", err)
c.loggedAllowedNS[name] = true
c.updateLoggedAllowedNS(name, true)
}
default:
return nil, err
Expand All @@ -338,6 +339,20 @@ func (c *Cluster) getAllowedAndExistingNamespaces(ctx context.Context) ([]string
return []string{meta_v1.NamespaceAll}, nil
}

func (c *Cluster) updateLoggedAllowedNS(key string, value bool) {
c.loggedAllowedNSLock.Lock()
defer c.loggedAllowedNSLock.Unlock()

c.loggedAllowedNS[key] = value
}

func (c *Cluster) getLoggedAllowedNS(key string) bool {
c.loggedAllowedNSLock.RLock()
defer c.loggedAllowedNSLock.RUnlock()

return c.loggedAllowedNS[key]
}

func (c *Cluster) IsAllowedResource(id resource.ID) bool {
if len(c.allowedNamespaces) == 0 {
// All resources are allowed when all namespaces are allowed
Expand Down