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/identity: protect LabelsSHA256 against concurrent initializations #11872

Merged
merged 1 commit into from Jun 4, 2020
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
12 changes: 9 additions & 3 deletions pkg/identity/identity.go
Expand Up @@ -17,6 +17,7 @@ package identity
import (
"fmt"
"net"
"sync"

"github.com/cilium/cilium/pkg/labels"
)
Expand All @@ -28,6 +29,9 @@ type Identity struct {
ID NumericIdentity `json:"id"`
// Set of labels that belong to this Identity.
Labels labels.Labels `json:"labels"`

// onceLabelSHA256 makes sure LabelsSHA256 is only set once
onceLabelSHA256 sync.Once
// SHA256 of labels.
LabelsSHA256 string `json:"labelsSHA256"`

Expand Down Expand Up @@ -92,9 +96,11 @@ func (id *Identity) Sanitize() {
// GetLabelsSHA256 returns the SHA256 of the labels associated with the
// identity. The SHA is calculated if not already cached.
func (id *Identity) GetLabelsSHA256() string {
if id.LabelsSHA256 == "" {
id.LabelsSHA256 = id.Labels.SHA256Sum()
}
id.onceLabelSHA256.Do(func() {
if id.LabelsSHA256 == "" {
id.LabelsSHA256 = id.Labels.SHA256Sum()
}
})

return id.LabelsSHA256
}
Expand Down