Skip to content

V2 Migration POC #1609

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

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
V2 Migration POC
  • Loading branch information
prestonvasquez committed Aug 20, 2024
commit b5ce6d69c7b551a56d7ad489f43517e8ceb8d53f
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ require (
github.com/stretchr/objx v0.5.2
github.com/stretchr/testify v1.9.0
github.com/xdg/stringprep v1.0.3
go.mongodb.org/mongo-driver v1.16.0

go.uber.org/zap v1.27.0
gopkg.in/natefinch/lumberjack.v2 v2.2.1
k8s.io/api v0.27.12
Expand Down Expand Up @@ -64,6 +64,7 @@ require (
github.com/xdg-go/scram v1.1.2 // indirect
github.com/xdg-go/stringprep v1.0.4 // indirect
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d // indirect
go.mongodb.org/mongo-driver/v2 v2.0.0-beta1 // indirect
go.uber.org/multierr v1.10.0 // indirect
golang.org/x/crypto v0.25.0 // indirect
golang.org/x/net v0.27.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9dec
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
go.mongodb.org/mongo-driver v1.16.0 h1:tpRsfBJMROVHKpdGyc1BBEzzjDUWjItxbVSZ8Ls4BQ4=
go.mongodb.org/mongo-driver v1.16.0/go.mod h1:oB6AhJQvFQL4LEHyXi6aJzQJtBiTQHiAd83l0GdFaiw=
go.mongodb.org/mongo-driver/v2 v2.0.0-beta1 h1:vwKMYa9FCX1OW7efPaH0FUaD6o+WC0kiC7VtHtNX7UU=
go.mongodb.org/mongo-driver/v2 v2.0.0-beta1/go.mod h1:pfndQmffp38kKjbwVfoavadsdC0Nsg/qb+INK01PNaM=
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

mdbv1 "github.com/mongodb/mongodb-kubernetes-operator/api/v1"
. "github.com/mongodb/mongodb-kubernetes-operator/test/e2e/util/mongotester"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/v2/bson"

e2eutil "github.com/mongodb/mongodb-kubernetes-operator/test/e2e"
"github.com/mongodb/mongodb-kubernetes-operator/test/e2e/mongodbtests"
Expand Down Expand Up @@ -76,7 +76,7 @@ func testConfigAuthentication(ctx context.Context, mdb mdbv1.MongoDBCommunity, u
}
t.Logf("Config: use Sha256: %t (use label: %t), use Sha1: %t", pickedOpts.sha256, pickedOpts.useLabelForSha256, pickedOpts.sha1)

enabledMechanisms := primitive.A{"SCRAM-SHA-256"}
enabledMechanisms := bson.A{"SCRAM-SHA-256"}
var acceptedModes []mdbv1.AuthMode
if pickedOpts.sha256 {
if pickedOpts.useLabelForSha256 {
Expand All @@ -88,9 +88,9 @@ func testConfigAuthentication(ctx context.Context, mdb mdbv1.MongoDBCommunity, u
if pickedOpts.sha1 {
acceptedModes = append(acceptedModes, "SCRAM-SHA-1")
if pickedOpts.sha256 {
enabledMechanisms = primitive.A{"SCRAM-SHA-256", "SCRAM-SHA-1"}
enabledMechanisms = bson.A{"SCRAM-SHA-256", "SCRAM-SHA-1"}
} else {
enabledMechanisms = primitive.A{"SCRAM-SHA-1"}
enabledMechanisms = bson.A{"SCRAM-SHA-1"}
}
}

Expand Down
44 changes: 27 additions & 17 deletions test/e2e/util/mongotester/mongotester.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,15 @@
"time"

"github.com/stretchr/objx"
"go.mongodb.org/mongo-driver/bson/primitive"

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

mdbv1 "github.com/mongodb/mongodb-kubernetes-operator/api/v1"
"github.com/mongodb/mongodb-kubernetes-operator/pkg/automationconfig"
e2eutil "github.com/mongodb/mongodb-kubernetes-operator/test/e2e"
"github.com/stretchr/testify/assert"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/v2/bson"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/wait"
Expand Down Expand Up @@ -124,7 +123,7 @@
t.Skip()
}

if err := m.ensureClient(ctx, clientOpts...); err == nil {
if err := m.ensureClient(clientOpts...); err == nil {
t.Fatalf("No error, but it should have failed")
}
}
Expand All @@ -139,10 +138,10 @@
}

func (m *Tester) ScramIsConfigured(tries int, opts ...OptionApplier) func(t *testing.T) {
return m.hasAdminParameter("authenticationMechanisms", primitive.A{"SCRAM-SHA-256"}, tries, opts...)
return m.hasAdminParameter("authenticationMechanisms", bson.A{"SCRAM-SHA-256"}, tries, opts...)
}

func (m *Tester) ScramWithAuthIsConfigured(tries int, enabledMechanisms primitive.A, opts ...OptionApplier) func(t *testing.T) {
func (m *Tester) ScramWithAuthIsConfigured(tries int, enabledMechanisms bson.A, opts ...OptionApplier) func(t *testing.T) {
return m.hasAdminParameter("authenticationMechanisms", enabledMechanisms, tries, opts...)
}

Expand All @@ -153,7 +152,7 @@
}
}

func (m *Tester) EnsureAuthenticationWithAuthIsConfigured(tries int, enabledMechanisms primitive.A, opts ...OptionApplier) func(t *testing.T) {
func (m *Tester) EnsureAuthenticationWithAuthIsConfigured(tries int, enabledMechanisms bson.A, opts ...OptionApplier) func(t *testing.T) {
return func(t *testing.T) {
t.Run("Ensure keyFile authentication is configured", m.HasKeyfileAuth(tries, opts...))
t.Run(fmt.Sprintf("%q is configured", enabledMechanisms), m.ScramWithAuthIsConfigured(tries, enabledMechanisms, opts...))
Expand Down Expand Up @@ -197,7 +196,7 @@
}

return func(t *testing.T) {
if err := m.ensureClient(m.ctx, clientOpts...); err != nil {
if err := m.ensureClient(clientOpts...); err != nil {
t.Fatal(err)
}

Expand Down Expand Up @@ -249,7 +248,7 @@
ctx, cancel := context.WithTimeout(m.ctx, connectivityOpts.ContextTimeout)
defer cancel()

if err := m.ensureClient(ctx, clientOpts...); err != nil {
if err := m.ensureClient(clientOpts...); err != nil {
t.Fatal(err)
}

Expand Down Expand Up @@ -294,7 +293,7 @@
return nil
}

if err := m.ensureClient(m.ctx, &options.ClientOptions{TLSConfig: tls}); err != nil {
if err := m.ensureClient(&options.ClientOptions{TLSConfig: tls}); err != nil {
t.Fatal(err)
}

Expand Down Expand Up @@ -336,7 +335,7 @@
var result bson.M
err := m.mongoClient.
Database("admin").
RunCommand(m.ctx, bson.D{primitive.E{Key: "getCmdLineOpts", Value: 1}}).
RunCommand(m.ctx, bson.D{bson.E{Key: "getCmdLineOpts", Value: 1}}).
Decode(&result)

return result, err
Expand Down Expand Up @@ -384,10 +383,20 @@

// ensureClient establishes a mongo client connection applying any addition
// client options on top of what were provided at construction.
func (t *Tester) ensureClient(ctx context.Context, opts ...*options.ClientOptions) error {
func (t *Tester) ensureClient(opts ...*options.ClientOptions) error {
allOpts := t.clientOpts
allOpts = append(allOpts, opts...)
mongoClient, err := mongo.Connect(ctx, allOpts...)

clientOptions := options.Client()
for _, opts := range allOpts {
clientOptions.Opts = append(clientOptions.Opts, func(co *options.ClientOptions) error {

Check failure on line 392 in test/e2e/util/mongotester/mongotester.go

View workflow job for this annotation

GitHub Actions / Golangci-lint

SA4009: argument co is overwritten before first use (staticcheck)
co = opts

Check failure on line 393 in test/e2e/util/mongotester/mongotester.go

View workflow job for this annotation

GitHub Actions / Golangci-lint

ineffectual assignment to co (ineffassign)

return nil
})
}

mongoClient, err := mongo.Connect(clientOptions)
if err != nil {
return err
}
Expand Down Expand Up @@ -537,9 +546,10 @@

// WithURI will add URI connection string
func WithURI(uri string) OptionApplier {
opt := &options.ClientOptions{}
opt.ApplyURI(uri)
return clientOptionAdder{option: opt}
var clientOptions options.ClientOptions
_ = options.Client().ApplyURI(uri).Opts[0](&clientOptions)

return clientOptionAdder{option: &clientOptions}
}

// WithReplicaSet will explicitly add a replicaset name
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/util/mongotester/mongotester_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/v2/mongo/options"
)

func TestTlsRemoval_RemovesCorrectConfig(t *testing.T) {
Expand Down
Loading