Skip to content
This repository has been archived by the owner on Mar 16, 2024. It is now read-only.

Commit

Permalink
Merge pull request #113 from thedadams/fix-concurrent-map-ops
Browse files Browse the repository at this point in the history
fix: avoid concurrent map operations for valid owner changes
  • Loading branch information
thedadams committed Feb 5, 2024
2 parents 2a58ee7 + 32d3918 commit 9cd5fb2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
5 changes: 3 additions & 2 deletions pkg/apply/apply.go
Expand Up @@ -3,6 +3,7 @@ package apply
import (
"context"
"fmt"
"sync"

"k8s.io/apimachinery/pkg/runtime/schema"
kclient "sigs.k8s.io/controller-runtime/pkg/client"
Expand All @@ -17,11 +18,11 @@ var (
// values that are allowed to change. The key is in the format of
// "oldValue => newValue"
// subcontext and the value is the old subcontext name.
validOwnerChange = map[string]bool{}
validOwnerChange = sync.Map{}
)

func AddValidOwnerChange(oldSubcontext, newSubContext string) {
validOwnerChange[fmt.Sprintf("%s => %s", oldSubcontext, newSubContext)] = true
validOwnerChange.Store(fmt.Sprintf("%s => %s", oldSubcontext, newSubContext), struct{}{})
}

type Apply interface {
Expand Down
14 changes: 8 additions & 6 deletions pkg/apply/desiredset_process.go
Expand Up @@ -277,12 +277,14 @@ func (a *apply) process(debugID string, set labels.Selector, gvk schema.GroupVer
func isAllowOwnerTransition(existingObj, newObj kclient.Object) bool {
existingAnno := existingObj.GetAnnotations()
newAnno := newObj.GetAnnotations()
return newAnno[LabelSubContext] != "" &&
(existingAnno[LabelGVK] == "" || existingAnno[LabelGVK] == newAnno[LabelGVK]) &&
(existingAnno[LabelNamespace] == "" || existingAnno[LabelNamespace] == newAnno[LabelNamespace]) &&
(existingAnno[LabelName] == "" || existingAnno[LabelName] == newAnno[LabelName]) &&
validOwnerChange[fmt.Sprintf("%s => %s", existingAnno[LabelSubContext], newAnno[LabelSubContext])]

if newAnno[LabelSubContext] == "" ||
(existingAnno[LabelGVK] != "" && existingAnno[LabelGVK] != newAnno[LabelGVK]) ||
(existingAnno[LabelNamespace] != "" && existingAnno[LabelNamespace] != newAnno[LabelNamespace]) ||
(existingAnno[LabelName] != "" && existingAnno[LabelName] != newAnno[LabelName]) {
return false
}
_, ok := validOwnerChange.Load(fmt.Sprintf("%s => %s", existingAnno[LabelSubContext], newAnno[LabelSubContext]))
return ok
}

// isAssigningSubContext is checking to see if an existing managed object
Expand Down

0 comments on commit 9cd5fb2

Please sign in to comment.