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

Remove dependency on k8s.io code #4

Merged
merged 4 commits into from
Apr 4, 2024
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
36 changes: 26 additions & 10 deletions fuzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"testing"

"github.com/stretchr/testify/require"
"k8s.io/apimachinery/pkg/util/sets"

"github.com/cilium/statedb"
"github.com/cilium/statedb/index"
Expand Down Expand Up @@ -117,35 +116,52 @@ func (a *realActionLog) validate(db *statedb.DB, t *testing.T) {
defer a.Unlock()

// Collapse the log down to objects that are alive at the end.
alive := map[statedb.Table[fuzzObj]]sets.Set[uint64]{}
alive := map[statedb.Table[fuzzObj]]map[uint64]struct{}{}
for _, e := range a.log {
aliveThis, ok := alive[e.table]
if !ok {
aliveThis = sets.New[uint64]()
aliveThis = map[uint64]struct{}{}
alive[e.table] = aliveThis
}
switch e.act {
case actInsert:
aliveThis.Insert(e.id)
aliveThis[e.id] = struct{}{}
case actDelete:
aliveThis.Delete(e.id)
delete(aliveThis, e.id)
case actDeleteAll:
aliveThis.Clear()
clear(aliveThis)
}
}

for table, expected := range alive {
txn := db.ReadTxn()
iter, _ := table.All(txn)
actual := sets.New[uint64]()
actual := map[uint64]struct{}{}
for obj, _, ok := iter.Next(); ok; obj, _, ok = iter.Next() {
actual.Insert(obj.id)
actual[obj.id] = struct{}{}
}
require.True(t, expected.Equal(actual), "validate failed, mismatching ids: %v",
actual.SymmetricDifference(expected))
require.Equal(t, expected, actual, "validate failed, mismatching ids: %v",
setSymmetricDifference(actual, expected))
}
}

func setSymmetricDifference[T comparable, M map[T]struct{}](s1, s2 M) M {
counts := make(map[T]int, len(s1)+len(s2))
for k1 := range s1 {
counts[k1] = 1
}
for k2 := range s2 {
counts[k2]++
}
result := M{}
for k, count := range counts {
if count == 1 {
result[k] = struct{}{}
}
}
return result
}

type nopActionLog struct {
}

Expand Down
26 changes: 1 addition & 25 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,16 @@ require (
go.uber.org/goleak v1.3.0
golang.org/x/exp v0.0.0-20240119083558-1b970713d09a
golang.org/x/time v0.5.0
k8s.io/api v0.29.1
k8s.io/apimachinery v0.29.1
k8s.io/client-go v0.29.1
)

require (
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-openapi/jsonpointer v0.20.0 // indirect
github.com/go-openapi/swag v0.22.4 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
Expand All @@ -47,20 +33,10 @@ require (
github.com/subosito/gotenv v1.6.0 // indirect
go.uber.org/dig v1.17.1 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/net v0.20.0 // indirect
golang.org/x/oauth2 v0.15.0 // indirect
golang.org/x/sys v0.17.0 // indirect
golang.org/x/term v0.16.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/protobuf v1.32.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/klog/v2 v2.120.1 // indirect
k8s.io/utils v0.0.0-20230726121419-3b25d923346b // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
sigs.k8s.io/yaml v1.4.0 // indirect
)
119 changes: 3 additions & 116 deletions go.sum

Large diffs are not rendered by default.

12 changes: 0 additions & 12 deletions iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ package statedb
import (
"bytes"
"fmt"

"k8s.io/apimachinery/pkg/util/sets"
)

// Collect creates a slice of objects out of the iterator.
Expand All @@ -20,16 +18,6 @@ func Collect[Obj any](iter Iterator[Obj]) []Obj {
return objs
}

// CollectSet creates a set of objects out of the iterator.
// The iterator is consumed in the process.
func CollectSet[Obj comparable](iter Iterator[Obj]) sets.Set[Obj] {
objs := sets.New[Obj]()
for obj, _, ok := iter.Next(); ok; obj, _, ok = iter.Next() {
objs.Insert(obj)
}
return objs
}

// ProcessEach invokes the given function for each object provided by the iterator.
func ProcessEach[Obj any, It Iterator[Obj]](iter It, fn func(Obj, Revision) error) (err error) {
for obj, rev, ok := iter.Next(); ok; obj, rev, ok = iter.Next() {
Expand Down
5 changes: 2 additions & 3 deletions iterator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestFilter(t *testing.T) {
txn.Commit()

iter, _ := table.All(db.ReadTxn())
filtered := CollectSet(
filtered := Collect(
Map(
Filter(
iter,
Expand All @@ -51,6 +51,5 @@ func TestFilter(t *testing.T) {
),
)
assert.Len(t, filtered, 2)
assert.True(t, filtered.Has(2))
assert.True(t, filtered.Has(4))
assert.Equal(t, filtered, []int{2, 4})
}
12 changes: 5 additions & 7 deletions reconciler/example/ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import (
"os"
"path"

"k8s.io/apimachinery/pkg/util/sets"

"github.com/cilium/hive/cell"
"github.com/cilium/statedb"
"github.com/cilium/statedb/reconciler"
Expand Down Expand Up @@ -46,19 +44,19 @@ func (ops *MemoOps) Delete(ctx context.Context, txn statedb.ReadTxn, memo *Memo)

// Prune unexpected memos.
func (ops *MemoOps) Prune(ctx context.Context, txn statedb.ReadTxn, iter statedb.Iterator[*Memo]) error {
expected := sets.New[string]()
expected := map[string]struct{}{}
for memo, _, ok := iter.Next(); ok; memo, _, ok = iter.Next() {
expected.Insert(memo.Name)
expected[memo.Name] = struct{}{}
}

// Find unexpected files
unexpected := sets.New[string]()
unexpected := map[string]struct{}{}
if entries, err := os.ReadDir(ops.directory); err != nil {
return err
} else {
for _, entry := range entries {
if !expected.Has(entry.Name()) {
unexpected.Insert(entry.Name())
if _, ok := expected[entry.Name()]; !ok {
unexpected[entry.Name()] = struct{}{}
}
}
}
Expand Down
13 changes: 0 additions & 13 deletions reflector/common.go

This file was deleted.

5 changes: 0 additions & 5 deletions reflector/doc.go

This file was deleted.

Loading
Loading