Skip to content
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
38 changes: 29 additions & 9 deletions pkg/controller/runtime/internal/reduced/reduced.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,42 @@ package reduced

import "github.com/cosi-project/runtime/pkg/resource"

// Metadata is _comparable_, so that it can be a map key.
// Metadata reduces resource metadata for deduplication.
//
// It consists of two parts:
// - a comparable Key which is used for deduplication.
// - a Value which is reduced for duplicate keys to the last observed value.
type Metadata struct {
Namespace resource.Namespace
Typ resource.Type
ID resource.ID
Key
Value
}

// Key is a comparable representation of deduplication entry.
type Key struct {
Namespace resource.Namespace
Typ resource.Type
ID resource.ID
}

// Value is a reduced representation of resource metadata.
type Value struct {
Labels *resource.Labels
Phase resource.Phase
FinalizersEmpty bool
}

// NewMetadata creates a new reduced Metadata from a resource.Metadata.
func NewMetadata(md *resource.Metadata) Metadata {
return Metadata{
Namespace: md.Namespace(),
Typ: md.Type(),
ID: md.ID(),
Phase: md.Phase(),
FinalizersEmpty: md.Finalizers().Empty(),
Key: Key{
Namespace: md.Namespace(),
Typ: md.Type(),
ID: md.ID(),
},
Value: Value{
Phase: md.Phase(),
FinalizersEmpty: md.Finalizers().Empty(),
Labels: md.Labels(),
},
}
}
12 changes: 9 additions & 3 deletions pkg/controller/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,13 +266,18 @@ func (runtime *Runtime) watch(resourceNamespace resource.Namespace, resourceType
return runtime.state.WatchKindAggregated(runtime.runCtx, kind, runtime.watchCh, state.WithBootstrapBookmark(true))
}

type dedup map[reduced.Metadata]struct{}
type dedup map[reduced.Key]reduced.Value

func (d dedup) takeOne() reduced.Metadata {
for k := range d {
md := reduced.Metadata{
Key: k,
Value: d[k],
}

delete(d, k)

return k
return md
}

panic("dedup is empty")
Expand Down Expand Up @@ -353,7 +358,8 @@ eventLoop:
}
}

m[reduced.NewMetadata(e.Resource.Metadata())] = struct{}{}
reducedMD := reduced.NewMetadata(e.Resource.Metadata())
m[reducedMD.Key] = reducedMD.Value
}

return true
Expand Down