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
6 changes: 2 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ require (
github.com/testcontainers/testcontainers-go/modules/nats v0.44.0
github.com/testcontainers/testcontainers-go/modules/postgres v0.44.0
github.com/testcontainers/testcontainers-go/modules/redpanda v0.44.0
go.mongodb.org/mongo-driver v1.17.10
go.mongodb.org/mongo-driver/v2 v2.9.3
go.uber.org/zap v1.28.0
golang.org/x/crypto v0.58.0
golang.org/x/oauth2 v0.37.0
Expand Down Expand Up @@ -117,7 +117,6 @@ require (
github.com/go-task/slim-sprig/v3 v3.0.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/golang/groupcache v0.0.0-20241129210726-2c02b8208cf8 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/flatbuffers v23.5.26+incompatible // indirect
github.com/google/gnostic-models v0.7.0 // indirect
github.com/google/go-cmp v0.7.0 // indirect
Expand Down Expand Up @@ -153,7 +152,6 @@ require (
github.com/moby/term v0.5.2 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.3-0.20250322232337-35a7c28c31ee // indirect
github.com/montanaflynn/stats v0.7.1 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/nats-io/nkeys v0.4.16 // indirect
github.com/nats-io/nuid v1.0.1 // indirect
Expand All @@ -179,7 +177,7 @@ require (
github.com/x448/float16 v0.8.4 // indirect
github.com/xanzy/ssh-agent v0.3.3 // indirect
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
github.com/xdg-go/scram v1.1.2 // indirect
github.com/xdg-go/scram v1.2.0 // indirect
github.com/xdg-go/stringprep v1.0.4 // indirect
github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78 // indirect
github.com/yusufpapurcu/wmi v1.2.4 // indirect
Expand Down
12 changes: 4 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,6 @@ github.com/golang/groupcache v0.0.0-20241129210726-2c02b8208cf8 h1:f+oWsMOmNPc8J
github.com/golang/groupcache v0.0.0-20241129210726-2c02b8208cf8/go.mod h1:wcDNUvekVysuuOpQKo3191zZyTpiI6se1N1ULghS0sw=
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=
github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/google/cel-go v0.31.0 h1:H0bhpFTqOvmHrBGrWKp7ZlhBm5Hh8PYUEXnwxT1LL7A=
github.com/google/cel-go v0.31.0/go.mod h1:X0bD6iVNR8pkROSOoHVdgTkzmRcosof7WQqCD6wcMc8=
github.com/google/flatbuffers v23.5.26+incompatible h1:M9dgRyhJemaM4Sw8+66GHBu8ioaQmyPLg1b8VwK5WJg=
Expand Down Expand Up @@ -355,8 +353,6 @@ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJ
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/modern-go/reflect2 v1.0.3-0.20250322232337-35a7c28c31ee h1:W5t00kpgFdJifH4BDsTlE89Zl93FEloxaWZfGcifgq8=
github.com/modern-go/reflect2 v1.0.3-0.20250322232337-35a7c28c31ee/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/montanaflynn/stats v0.7.1 h1:etflOAAHORrCC44V+aR6Ftzort912ZU+YLiSTuV8eaE=
github.com/montanaflynn/stats v0.7.1/go.mod h1:etXPPgVO6n31NxCd9KQUMvCM+ve0ruNzt6R8Bnaayow=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
github.com/nats-io/nats.go v1.55.0 h1:Ng8QKHdvdHIWoaiq0+d/aH1BWkxvcJxWJ0Y/9BPKx3M=
Expand Down Expand Up @@ -473,8 +469,8 @@ github.com/xanzy/ssh-agent v0.3.3 h1:+/15pJfg/RsTxqYcX6fHqOXZwwMP+2VyYWJeWM2qQFM
github.com/xanzy/ssh-agent v0.3.3/go.mod h1:6dzNDKs0J9rVPHPhaGCukekBHKqfl+L3KghI1Bc68Uw=
github.com/xdg-go/pbkdf2 v1.0.0 h1:Su7DPu48wXMwC3bs7MCNG+z4FhcyEuz5dlvchbq0B0c=
github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI=
github.com/xdg-go/scram v1.1.2 h1:FHX5I5B4i4hKRVRBCFRxq1iQRej7WO3hhBuJf+UUySY=
github.com/xdg-go/scram v1.1.2/go.mod h1:RT/sEzTbU5y00aCK8UOx6R7YryM0iF1N2MOmC3kKLN4=
github.com/xdg-go/scram v1.2.0 h1:bYKF2AEwG5rqd1BumT4gAnvwU/M9nBp2pTSxeZw7Wvs=
github.com/xdg-go/scram v1.2.0/go.mod h1:3dlrS0iBaWKYVt2ZfA4cj48umJZ+cAEbR6/SjLA88I8=
github.com/xdg-go/stringprep v1.0.4 h1:XLI/Ng3O1Atzq0oBs3TWm+5ZVgkq2aqdlvP9JtoZ6c8=
github.com/xdg-go/stringprep v1.0.4/go.mod h1:mPGuuIYwz7CmR2bT9j4GbQqutWS1zV24gijq1dTyGkM=
github.com/xyproto/randomstring v1.0.5 h1:YtlWPoRdgMu3NZtP45drfy1GKoojuR7hmRcnhZqKjWU=
Expand All @@ -494,8 +490,8 @@ go.etcd.io/etcd/client/pkg/v3 v3.7.0 h1:sW9njJzS3vXKcAJjjLQ4nk+avNUJ12Bcijcx8ehU
go.etcd.io/etcd/client/pkg/v3 v3.7.0/go.mod h1:cnzZGIUzSfjEwLC6UBVsSXlEK1eepS/JUD7wE6PLRT0=
go.etcd.io/etcd/client/v3 v3.7.0 h1:5MHO37VbPB87VRPKUXEcicjeQWiTSjpPv3Ume8xPx20=
go.etcd.io/etcd/client/v3 v3.7.0/go.mod h1:DJ382WuwjmbowjPDyaaQ0idWXy4dh91XRhe4FOrb9vM=
go.mongodb.org/mongo-driver v1.17.10 h1:kdAgQvu8TROXZpSkJQd5wzfaNCCrMbpZyKFtQ6qkPCE=
go.mongodb.org/mongo-driver v1.17.10/go.mod h1:LlOhpH5NUEfhxcAwG0UEkMqwYcc4JU18gtCdGudk/tQ=
go.mongodb.org/mongo-driver/v2 v2.9.3 h1:wyiWaPGYaFqRmhjNOXwlK6Cu8Qf35zFH4euH2NBBj34=
go.mongodb.org/mongo-driver/v2 v2.9.3/go.mod h1:SHKN0IWkKmEVGHLjXnni6s4wPKX4v86FTgOeJJFuXcA=
go.opentelemetry.io/auto/sdk v1.2.1 h1:jXsnJ4Lmnqd11kwkBV2LgLoFMZKizbCi5fNZ/ipaZ64=
go.opentelemetry.io/auto/sdk v1.2.1/go.mod h1:KRTj+aOaElaLi+wW1kO/DZRXwkF4C5xPbEe3ZiIhN7Y=
go.opentelemetry.io/contrib/detectors/gcp v1.45.0 h1:9jR0ZPRok9ryaOQ2Wx8rg5F7Aon59mxrqbVI60/vlBk=
Expand Down
14 changes: 7 additions & 7 deletions internal/sink/mongodb/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (
"fmt"
"time"

"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/v2/bson"
"go.mongodb.org/mongo-driver/v2/mongo"
"go.mongodb.org/mongo-driver/v2/mongo/options"

kollectdevv1alpha1 "github.com/platformrelay/kollect/api/v1alpha1"
"github.com/platformrelay/kollect/internal/collect"
Expand Down Expand Up @@ -67,12 +67,12 @@ func (a mongoCollectionAdmin) EnsureUniqueIndex(ctx context.Context, keys bson.D
}

type deleteManyCollection interface {
DeleteMany(context.Context, interface{}, ...*options.DeleteOptions) (*mongo.DeleteResult, error)
DeleteMany(context.Context, interface{}, ...options.Lister[options.DeleteManyOptions]) (*mongo.DeleteResult, error)
}

type exportCollection interface {
ReplaceOne(context.Context, interface{}, interface{}, ...*options.ReplaceOptions) (*mongo.UpdateResult, error)
DeleteMany(context.Context, interface{}, ...*options.DeleteOptions) (*mongo.DeleteResult, error)
ReplaceOne(context.Context, interface{}, interface{}, ...options.Lister[options.ReplaceOptions]) (*mongo.UpdateResult, error)
DeleteMany(context.Context, interface{}, ...options.Lister[options.DeleteManyOptions]) (*mongo.DeleteResult, error)
}

// NewBackend constructs a MongoDB sink backend.
Expand All @@ -99,7 +99,7 @@ func NewBackend(
return nil, err
}

client, err := mongo.Connect(connectCtx, opts)
client, err := mongo.Connect(opts)
if err != nil {
return nil, fmt.Errorf("mongodb connect: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/sink/mongodb/backend_collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"strings"
"testing"

"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/v2/bson"
)

type fakeCollectionAdmin struct {
Expand Down
98 changes: 79 additions & 19 deletions internal/sink/mongodb/backend_mongo_admin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,41 +7,101 @@ import (
"context"
"testing"

"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo/integration/mtest"
"go.mongodb.org/mongo-driver/v2/bson"
"go.mongodb.org/mongo-driver/v2/mongo"
"go.mongodb.org/mongo-driver/v2/mongo/options"
"go.mongodb.org/mongo-driver/v2/x/mongo/driver/drivertest"
"go.mongodb.org/mongo-driver/v2/x/mongo/driver/xoptions"
)

// mockAdmin returns a mongoCollectionAdmin over a client whose deployment answers
// each command with the next of responses (OP_MSG), so the real driver code path of
// the admin methods runs without a server. mongo-driver v2 moved mtest under
// internal/; drivertest.MockDeployment is the public equivalent of its mock client.
func mockAdmin(t *testing.T, responses ...bson.D) mongoCollectionAdmin {
t.Helper()

opts := options.Client()
if err := xoptions.SetInternalClientOptions(opts, "deployment", drivertest.NewMockDeployment(responses...)); err != nil {
t.Fatalf("set mock deployment: %v", err)
}

client, err := mongo.Connect(opts)
if err != nil {
t.Fatalf("mongo.Connect: %v", err)
}
t.Cleanup(func() { _ = client.Disconnect(context.Background()) })

db := client.Database("db")

return mongoCollectionAdmin{db: db, coll: db.Collection("items")}
}

func cursorResponse(ns string, batch ...bson.D) bson.D {
docs := make(bson.A, 0, len(batch))
for _, doc := range batch {
docs = append(docs, doc)
}

return bson.D{
{Key: "ok", Value: 1},
{Key: "cursor", Value: bson.D{
{Key: "id", Value: int64(0)},
{Key: "ns", Value: ns},
{Key: "firstBatch", Value: docs},
}},
}
}

func okResponse() bson.D {
return bson.D{{Key: "ok", Value: 1}}
}

func TestMongoCollectionAdmin_listCreateIndex(t *testing.T) {
t.Parallel()

mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
mt.Run("list names", func(mt *mtest.T) {
mt.AddMockResponses(
mtest.CreateCursorResponse(1, "db.$cmd.listCollections", mtest.FirstBatch, bson.D{{Key: "name", Value: "items"}}),
)
admin := mongoCollectionAdmin{db: mt.Client.Database("db"), coll: mt.Client.Database("db").Collection("items")}
t.Run("list names", func(t *testing.T) {
t.Parallel()

admin := mockAdmin(t, cursorResponse("db.$cmd.listCollections", bson.D{{Key: "name", Value: "items"}}))
names, err := admin.ListCollectionNames(context.Background(), bson.M{"name": "items"})
if err != nil {
mt.Fatalf("ListCollectionNames: %v", err)
t.Fatalf("ListCollectionNames: %v", err)
}
if len(names) != 1 {
mt.Fatalf("names = %v", names)
if len(names) != 1 || names[0] != "items" {
t.Fatalf("names = %v, want [items]", names)
}
})

mt.Run("create collection", func(mt *mtest.T) {
mt.AddMockResponses(mtest.CreateSuccessResponse())
admin := mongoCollectionAdmin{db: mt.Client.Database("db"), coll: mt.Client.Database("db").Collection("items")}
t.Run("create collection", func(t *testing.T) {
t.Parallel()

admin := mockAdmin(t, okResponse())
if err := admin.CreateCollection(context.Background(), "items"); err != nil {
mt.Fatalf("CreateCollection: %v", err)
t.Fatalf("CreateCollection: %v", err)
}
})

mt.Run("ensure index", func(mt *mtest.T) {
mt.AddMockResponses(mtest.CreateSuccessResponse())
admin := mongoCollectionAdmin{db: mt.Client.Database("db"), coll: mt.Coll}
t.Run("ensure index", func(t *testing.T) {
t.Parallel()

admin := mockAdmin(t, okResponse())
if err := admin.EnsureUniqueIndex(context.Background(), bson.D{{Key: "source_uid", Value: 1}}); err != nil {
mt.Fatalf("EnsureUniqueIndex: %v", err)
t.Fatalf("EnsureUniqueIndex: %v", err)
}
})

t.Run("server error surfaces", func(t *testing.T) {
t.Parallel()

admin := mockAdmin(t, bson.D{
{Key: "ok", Value: 0},
{Key: "code", Value: 13},
{Key: "errmsg", Value: "not authorized"},
{Key: "codeName", Value: "Unauthorized"},
})
if err := admin.CreateCollection(context.Background(), "items"); err == nil {
t.Fatal("CreateCollection: want the server's error, got nil")
}
})
}
8 changes: 4 additions & 4 deletions internal/sink/mongodb/backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (
"testing"
"time"

"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/v2/bson"
"go.mongodb.org/mongo-driver/v2/mongo"
"go.mongodb.org/mongo-driver/v2/mongo/options"

kollectdevv1alpha1 "github.com/platformrelay/kollect/api/v1alpha1"
"github.com/platformrelay/kollect/internal/collect"
Expand Down Expand Up @@ -100,7 +100,7 @@ type fakeDeleteManyCollection struct {
func (f *fakeDeleteManyCollection) DeleteMany(
_ context.Context,
filter interface{},
_ ...*options.DeleteOptions,
_ ...options.Lister[options.DeleteManyOptions],
) (*mongo.DeleteResult, error) {
f.gotFilter = filter
if f.err != nil {
Expand Down
2 changes: 1 addition & 1 deletion internal/sink/mongodb/ca_trust.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"net/url"
"strings"

"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/v2/mongo/options"

kollecterrors "github.com/platformrelay/kollect/internal/errors"
"github.com/platformrelay/kollect/internal/sink/netguard"
Expand Down
2 changes: 1 addition & 1 deletion internal/sink/mongodb/ca_trust_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"testing"
"time"

"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/v2/bson"

"github.com/platformrelay/kollect/internal/collect"
"github.com/platformrelay/kollect/internal/sink/sinktls/sinktlstest"
Expand Down
6 changes: 3 additions & 3 deletions internal/sink/mongodb/ca_trust_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import (
"testing"
"time"

"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/x/mongo/driver/connstring"
"go.mongodb.org/mongo-driver/x/mongo/driver/dns"
"go.mongodb.org/mongo-driver/v2/mongo/options"
"go.mongodb.org/mongo-driver/v2/x/mongo/driver/connstring"
"go.mongodb.org/mongo-driver/v2/x/mongo/driver/dns"

kollectdevv1alpha1 "github.com/platformrelay/kollect/api/v1alpha1"
kollecterrors "github.com/platformrelay/kollect/internal/errors"
Expand Down
4 changes: 2 additions & 2 deletions internal/sink/mongodb/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"fmt"
"time"

"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/v2/mongo"

kollectdevv1alpha1 "github.com/platformrelay/kollect/api/v1alpha1"
)
Expand All @@ -35,7 +35,7 @@ func TestConnection(
return err
}

client, err := mongo.Connect(probeCtx, opts)
client, err := mongo.Connect(opts)
if err != nil {
return fmt.Errorf("mongodb connect: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/sink/mongodb/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package mongodb
import (
"errors"

"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/v2/mongo"

kollecterrors "github.com/platformrelay/kollect/internal/errors"
)
Expand Down
2 changes: 1 addition & 1 deletion internal/sink/mongodb/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"fmt"
"testing"

"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/v2/mongo"

kollecterrors "github.com/platformrelay/kollect/internal/errors"
)
Expand Down
26 changes: 13 additions & 13 deletions internal/sink/mongodb/export_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import (

"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/v2/bson"
"go.mongodb.org/mongo-driver/v2/mongo"
"go.mongodb.org/mongo-driver/v2/mongo/options"

kollectdevv1alpha1 "github.com/platformrelay/kollect/api/v1alpha1"
"github.com/platformrelay/kollect/internal/collect"
Expand Down Expand Up @@ -335,7 +335,7 @@ func TestExportMongoDB_existingProvisioningModeSuccess(t *testing.T) {
collection = "preexisting"
)

client, err := mongo.Connect(ctx, options.Client().ApplyURI(uri))
client, err := mongo.Connect(options.Client().ApplyURI(uri))
if err != nil {
t.Fatalf("connect: %v", err)
}
Expand Down Expand Up @@ -764,21 +764,21 @@ func documentAttribute(
}
filter["source_uid"] = sourceUID

var doc struct {
Payload map[string]any `bson:"payload"`
}
if err := coll.FindOne(ctx, filter).Decode(&doc); err != nil {
// Read the stored BSON directly: driver v2 decodes nested documents in an
// `any` to bson.D, so a map[string]any assertion would depend on the decoder.
raw, err := coll.FindOne(ctx, filter).Raw()
if err != nil {
return 0, err
}

attrs, ok := doc.Payload["attributes"].(map[string]any)
if !ok {
return 0, fmt.Errorf("payload attributes missing for uid %s", sourceUID)
if _, err := raw.LookupErr("payload", "attributes"); err != nil {
return 0, fmt.Errorf("payload attributes missing for uid %s: %w", sourceUID, err)
}

value, ok := attrs[attr].(float64)
field := raw.Lookup("payload", "attributes", attr)
value, ok := field.DoubleOK()
if !ok {
return 0, fmt.Errorf("attribute %q type = %T, want float64", attr, attrs[attr])
return 0, fmt.Errorf("attribute %q BSON type = %s, want double", attr, field.Type)
}

return value, nil
Expand Down
2 changes: 1 addition & 1 deletion internal/sink/mongodb/export_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
package mongodb

import (
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/v2/bson"

"github.com/platformrelay/kollect/internal/collect"
"github.com/platformrelay/kollect/internal/pathvalidate"
Expand Down
Loading
Loading