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

Use ID only for cache deletion. #1020

Merged
merged 1 commit into from
Dec 11, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions pkg/controller/assignmetadata/assignmetadata_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,18 +124,26 @@ func (r *AssignMetadataReconciler) Reconcile(request reconcile.Request) (reconci
}
deleted = deleted || !assignMetadata.GetDeletionTimestamp().IsZero()

if deleted {
id, err := mutation.MakeID(assignMetadata)
if err != nil {
log.Error(err, "Failed to get id out of metadata")
return ctrl.Result{}, nil
}

if err := r.system.Remove(id); err != nil {
log.Error(err, "Remove failed", "resource", request.NamespacedName)
}
return ctrl.Result{}, nil
}

mutator, err := mutation.MutatorForAssignMetadata(assignMetadata)
if err != nil {
log.Error(err, "Creating mutator for resource failed", "resource", request.NamespacedName)
}
if !deleted {
if err := r.system.Upsert(mutator); err != nil {
log.Error(err, "Insert failed", "resource", request.NamespacedName)
}
} else {
if err := r.system.Remove(mutator); err != nil {
log.Error(err, "Remove failed", "resource", request.NamespacedName)
}
if err := r.system.Upsert(mutator); err != nil {
log.Error(err, "Insert failed", "resource", request.NamespacedName)
}

return ctrl.Result{}, nil
}
48 changes: 24 additions & 24 deletions pkg/mutation/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ func (s *System) Upsert(m Mutator) error {
s.mutatorsMap[toAdd.ID()] = toAdd

i := sort.Search(len(s.orderedMutators), func(i int) bool {
return greaterOrEqual(s.orderedMutators[i], toAdd)
return greaterOrEqual(s.orderedMutators[i].ID(), toAdd.ID())
})

if i == len(s.orderedMutators) { // Adding to the bottom of the list
s.orderedMutators = append(s.orderedMutators, toAdd)
return nil
}

found := equal(s.orderedMutators[i], toAdd)
found := equal(s.orderedMutators[i].ID(), toAdd.ID())
if found {
s.orderedMutators[i] = toAdd
return nil
Expand Down Expand Up @@ -98,15 +98,15 @@ func (s *System) Mutate(obj *unstructured.Unstructured, ns *corev1.Namespace) er
}

// Remove removes the mutator from the mutation system
func (s *System) Remove(m Mutator) error {
func (s *System) Remove(id ID) error {
s.Lock()
defer s.Unlock()

if _, ok := s.mutatorsMap[m.ID()]; !ok {
if _, ok := s.mutatorsMap[id]; !ok {
return nil
}

err := s.schemaDB.Remove(m.ID())
err := s.schemaDB.Remove(id)
// TODO: get back on this once schemaDB implementation is done
// and understand how to recover from a failed remove.
// One option is to rebuild the schema from scratch using the cache content.
Expand All @@ -115,61 +115,61 @@ func (s *System) Remove(m Mutator) error {
}

i := sort.Search(len(s.orderedMutators), func(i int) bool {
res := equal(s.orderedMutators[i], m)
res := equal(s.orderedMutators[i].ID(), id)
if res {
return true
}
return greaterOrEqual(s.orderedMutators[i], m)
return greaterOrEqual(s.orderedMutators[i].ID(), id)
})

delete(s.mutatorsMap, m.ID())
delete(s.mutatorsMap, id)

found := equal(s.orderedMutators[i], m)
found := equal(s.orderedMutators[i].ID(), id)

// The map is expected to be in sync with the list, so if we don't find it
// we return an error.
if !found {
return fmt.Errorf("Failed to find mutator with ID %v on sorted list", m.ID())
return fmt.Errorf("Failed to find mutator with ID %v on sorted list", id)
}
copy(s.orderedMutators[i:], s.orderedMutators[i+1:])
s.orderedMutators[len(s.orderedMutators)-1] = nil
s.orderedMutators = s.orderedMutators[:len(s.orderedMutators)-1]
return nil
}

func greaterOrEqual(mutator1, mutator2 Mutator) bool {
if mutator1.ID().Group > mutator2.ID().Group {
func greaterOrEqual(id1, id2 ID) bool {
if id1.Group > id2.Group {
return true
}
if mutator1.ID().Group < mutator2.ID().Group {
if id1.Group < id2.Group {
return false
}
if mutator1.ID().Kind > mutator2.ID().Kind {
if id1.Kind > id2.Kind {
return true
}
if mutator1.ID().Kind < mutator2.ID().Kind {
if id1.Kind < id2.Kind {
return false
}
if mutator1.ID().Namespace > mutator2.ID().Namespace {
if id1.Namespace > id2.Namespace {
return true
}
if mutator1.ID().Namespace < mutator2.ID().Namespace {
if id1.Namespace < id2.Namespace {
return false
}
if mutator1.ID().Name > mutator2.ID().Name {
if id1.Name > id2.Name {
return true
}
if mutator1.ID().Name < mutator2.ID().Name {
if id1.Name < id2.Name {
return false
}
return true
}

func equal(mutator1, mutator2 Mutator) bool {
if mutator1.ID().Group == mutator2.ID().Group &&
mutator1.ID().Kind == mutator2.ID().Kind &&
mutator1.ID().Namespace == mutator2.ID().Namespace &&
mutator1.ID().Name == mutator2.ID().Name {
func equal(id1, id2 ID) bool {
if id1.Group == id2.Group &&
id1.Kind == id2.Kind &&
id1.Namespace == id2.Namespace &&
id1.Name == id2.Name {
return true
}
return false
Expand Down
2 changes: 1 addition & 1 deletion pkg/mutation/system_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func TestSorting(t *testing.T) {
&MockMutator{Mocked: ID{Group: "bbb", Kind: "aaa", Namespace: "aaa", Name: "aaa"}},
},
action: func(s *System) error {
return s.Remove(&MockMutator{Mocked: ID{Group: "aaa", Kind: "bbb", Namespace: "ccc", Name: "aaa"}})
return s.Remove(ID{Group: "aaa", Kind: "bbb", Namespace: "ccc", Name: "aaa"})
},
},
{
Expand Down