Skip to content

Commit e51a244

Browse files
authored
internal/dryrun/transport: simplify (#2085)
1 parent 1bf3bf9 commit e51a244

File tree

7 files changed

+23
-139
lines changed

7 files changed

+23
-139
lines changed

internal/controller/atlas/provider.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"go.mongodb.org/atlas/mongodbatlas"
1515
"go.uber.org/zap"
1616
corev1 "k8s.io/api/core/v1"
17-
"k8s.io/client-go/tools/record"
1817
"sigs.k8s.io/controller-runtime/pkg/client"
1918

2019
"github.com/mongodb/mongodb-atlas-kubernetes/v2/api"
@@ -48,7 +47,7 @@ type ProductionProvider struct {
4847
k8sClient client.Client
4948
domain string
5049
globalSecretRef client.ObjectKey
51-
dryRunRecorder record.EventRecorder
50+
dryRun bool
5251
}
5352

5453
type credentialsSecret struct {
@@ -57,12 +56,12 @@ type credentialsSecret struct {
5756
PrivateKey string
5857
}
5958

60-
func NewProductionProvider(atlasDomain string, globalSecretRef client.ObjectKey, k8sClient client.Client, dryRunRecorder record.EventRecorder) *ProductionProvider {
59+
func NewProductionProvider(atlasDomain string, globalSecretRef client.ObjectKey, k8sClient client.Client, dryRun bool) *ProductionProvider {
6160
return &ProductionProvider{
6261
k8sClient: k8sClient,
6362
domain: atlasDomain,
6463
globalSecretRef: globalSecretRef,
65-
dryRunRecorder: dryRunRecorder,
64+
dryRun: dryRun,
6665
}
6766
}
6867

@@ -171,11 +170,11 @@ func (p *ProductionProvider) SdkClientSet(ctx context.Context, secretRef *client
171170
}
172171

173172
func (p *ProductionProvider) newDryRunTransport(delegate http.RoundTripper) http.RoundTripper {
174-
if p.dryRunRecorder == nil {
175-
return delegate
173+
if p.dryRun {
174+
return dryrun.NewDryRunTransport(delegate)
176175
}
177176

178-
return dryrun.NewDryRunTransport(p.dryRunRecorder, delegate)
177+
return delegate
179178
}
180179

181180
func getSecrets(ctx context.Context, k8sClient client.Client, secretRef, fallbackRef *client.ObjectKey) (*credentialsSecret, error) {

internal/controller/atlas/provider_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func TestProvider_Client(t *testing.T) {
4343
Build()
4444

4545
t.Run("should return Atlas API client and organization id using global secret", func(t *testing.T) {
46-
p := NewProductionProvider("https://cloud.mongodb.com/", client.ObjectKey{Name: "api-secret", Namespace: "default"}, k8sClient, nil)
46+
p := NewProductionProvider("https://cloud.mongodb.com/", client.ObjectKey{Name: "api-secret", Namespace: "default"}, k8sClient, false)
4747

4848
c, id, err := p.Client(context.Background(), nil, zaptest.NewLogger(t).Sugar())
4949
assert.NoError(t, err)
@@ -52,7 +52,7 @@ func TestProvider_Client(t *testing.T) {
5252
})
5353

5454
t.Run("should return Atlas API client and organization id using connection secret", func(t *testing.T) {
55-
p := NewProductionProvider("https://cloud.mongodb.com/", client.ObjectKey{Name: "global-secret", Namespace: "default"}, k8sClient, nil)
55+
p := NewProductionProvider("https://cloud.mongodb.com/", client.ObjectKey{Name: "global-secret", Namespace: "default"}, k8sClient, false)
5656

5757
c, id, err := p.Client(context.Background(), &client.ObjectKey{Name: "api-secret", Namespace: "default"}, zaptest.NewLogger(t).Sugar())
5858
assert.NoError(t, err)
@@ -63,17 +63,17 @@ func TestProvider_Client(t *testing.T) {
6363

6464
func TestProvider_IsCloudGov(t *testing.T) {
6565
t.Run("should return false for invalid domain", func(t *testing.T) {
66-
p := NewProductionProvider("http://x:namedport", client.ObjectKey{}, nil, nil)
66+
p := NewProductionProvider("http://x:namedport", client.ObjectKey{}, nil, false)
6767
assert.False(t, p.IsCloudGov())
6868
})
6969

7070
t.Run("should return false for commercial Atlas domain", func(t *testing.T) {
71-
p := NewProductionProvider("https://cloud.mongodb.com/", client.ObjectKey{}, nil, nil)
71+
p := NewProductionProvider("https://cloud.mongodb.com/", client.ObjectKey{}, nil, false)
7272
assert.False(t, p.IsCloudGov())
7373
})
7474

7575
t.Run("should return true for Atlas for government domain", func(t *testing.T) {
76-
p := NewProductionProvider("https://cloud.mongodbgov.com/", client.ObjectKey{}, nil, nil)
76+
p := NewProductionProvider("https://cloud.mongodbgov.com/", client.ObjectKey{}, nil, false)
7777
assert.True(t, p.IsCloudGov())
7878
})
7979
}
@@ -173,7 +173,7 @@ func TestProvider_IsResourceSupported(t *testing.T) {
173173

174174
for desc, data := range dataProvider {
175175
t.Run(desc, func(t *testing.T) {
176-
p := NewProductionProvider(data.domain, client.ObjectKey{}, nil, nil)
176+
p := NewProductionProvider(data.domain, client.ObjectKey{}, nil, false)
177177
assert.Equal(t, data.expectation, p.IsResourceSupported(data.resource))
178178
})
179179
}

internal/controller/workflow/context.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111

1212
"github.com/mongodb/mongodb-atlas-kubernetes/v2/api"
1313
"github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/controller/atlas"
14-
"github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/dryrun"
1514
)
1615

1716
// Context is a container for some information that is needed on all levels of function calls during reconciliation.
@@ -50,7 +49,7 @@ func NewContext(log *zap.SugaredLogger, conditions []api.Condition, context cont
5049
return &Context{
5150
status: NewStatus(conditions),
5251
Log: log,
53-
Context: dryrun.WithRuntimeObject(context, obj),
52+
Context: context,
5453
}
5554
}
5655

internal/dryrun/context.go

Lines changed: 0 additions & 27 deletions
This file was deleted.

internal/dryrun/transport.go

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
package dryrun
22

33
import (
4-
"errors"
54
"net/http"
6-
7-
v1 "k8s.io/api/core/v1"
8-
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
9-
"k8s.io/client-go/tools/record"
105
)
116

127
const DryRunReason = "DryRun"
@@ -19,13 +14,11 @@ var verbMap = map[string]string{
1914
}
2015

2116
type DryRunTransport struct {
22-
Recorder record.EventRecorder
2317
Delegate http.RoundTripper
2418
}
2519

26-
func NewDryRunTransport(recorder record.EventRecorder, delegate http.RoundTripper) *DryRunTransport {
20+
func NewDryRunTransport(delegate http.RoundTripper) *DryRunTransport {
2721
return &DryRunTransport{
28-
Recorder: recorder,
2922
Delegate: delegate,
3023
}
3124
}
@@ -37,24 +30,13 @@ func (t *DryRunTransport) RoundTrip(req *http.Request) (*http.Response, error) {
3730
case http.MethodTrace:
3831
case http.MethodHead:
3932
default:
40-
obj, ok := runtimeObjectFrom(req.Context())
41-
if !ok {
42-
return nil, errors.New("no object present in context: cannot record dry run without an object")
43-
}
44-
45-
meta, ok := obj.(metav1.ObjectMetaAccessor)
46-
if !ok {
47-
return nil, errors.New("object does not implement ObjectMetaAccessor")
48-
}
49-
5033
verb, ok := verbMap[req.Method]
5134
if !ok {
5235
verb = "execute " + req.Method
5336
}
5437
msg := "Would %v %v"
55-
t.Recorder.Eventf(obj, v1.EventTypeNormal, DryRunReason, msg, verb, req.URL.Path)
5638

57-
return nil, NewDryRunError(obj.GetObjectKind(), meta, v1.EventTypeNormal, DryRunReason, msg, verb, req.URL.Path)
39+
return nil, NewDryRunError(nil, nil, "", "", msg, verb, req.URL.Path)
5840
}
5941

6042
return t.Delegate.RoundTrip(req)

internal/dryrun/transport_test.go

Lines changed: 7 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,11 @@
11
package dryrun
22

33
import (
4-
"context"
54
"net/http"
65
"net/url"
76
"testing"
8-
"time"
97

10-
"github.com/stretchr/testify/assert"
118
"github.com/stretchr/testify/require"
12-
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
13-
"k8s.io/apimachinery/pkg/runtime"
14-
"k8s.io/apimachinery/pkg/runtime/schema"
15-
"k8s.io/apimachinery/pkg/util/wait"
16-
"k8s.io/client-go/tools/record"
17-
18-
akov2 "github.com/mongodb/mongodb-atlas-kubernetes/v2/api/v1"
199
)
2010

2111
type RoundTripperFunc func(*http.Request) (*http.Response, error)
@@ -26,103 +16,44 @@ func (fn RoundTripperFunc) RoundTrip(req *http.Request) (*http.Response, error)
2616

2717
func TestDryRunTransport(t *testing.T) {
2818
for _, tc := range []struct {
29-
name string
30-
req *http.Request
31-
obj runtime.Object
32-
wantEvents []string
33-
wantErr string
19+
name string
20+
req *http.Request
21+
wantErr string
3422
}{
3523
{
3624
name: "GET request",
3725
req: &http.Request{
3826
Method: http.MethodGet,
3927
},
4028
},
41-
{
42-
name: "no object",
43-
req: &http.Request{
44-
Method: http.MethodPost,
45-
},
46-
wantErr: "no object present in context: cannot record dry run without an object",
47-
},
4829
{
4930
name: "unknown verb",
5031
req: &http.Request{
5132
Method: "UNKNOWN",
5233
URL: &url.URL{Path: "/test"},
5334
},
54-
obj: &akov2.AtlasProject{
55-
ObjectMeta: metav1.ObjectMeta{
56-
Name: "foo",
57-
Namespace: "bar",
58-
},
59-
},
60-
wantErr: "DryRun event GVK=atlas.mongodb.com/v1, Kind=AtlasProject, Namespace=bar, Name=foo, EventType=Normal, Reason=DryRun, Message=Would execute UNKNOWN /test",
61-
wantEvents: []string{
62-
"Normal DryRun Would execute UNKNOWN /test",
63-
},
35+
wantErr: "DryRun event GVK=unknown, Namespace=unknown, Name=unknown, EventType=, Reason=, Message=Would execute UNKNOWN /test",
6436
},
6537
{
6638
name: "POST request",
6739
req: &http.Request{
6840
Method: http.MethodPost,
6941
URL: &url.URL{Path: "/test"},
7042
},
71-
obj: &akov2.AtlasProject{},
72-
wantErr: "DryRun event GVK=atlas.mongodb.com/v1, Kind=AtlasProject, Namespace=, Name=, EventType=Normal, Reason=DryRun, Message=Would create (POST) /test",
73-
wantEvents: []string{
74-
"Normal DryRun Would create (POST) /test",
75-
},
43+
wantErr: "DryRun event GVK=unknown, Namespace=unknown, Name=unknown, EventType=, Reason=, Message=Would create (POST) /test",
7644
},
7745
} {
7846
t.Run(tc.name, func(t *testing.T) {
79-
rec := record.NewFakeRecorder(100)
8047
nopDelegate := RoundTripperFunc(func(req *http.Request) (*http.Response, error) {
8148
return nil, nil
8249
})
83-
transport := NewDryRunTransport(rec, nopDelegate)
84-
85-
if tc.obj != nil {
86-
scheme := runtime.NewScheme()
87-
assert.NoError(t, akov2.AddToScheme(scheme))
88-
gvks, _, err := scheme.ObjectKinds(tc.obj)
89-
require.NoError(t, err)
90-
meta := tc.obj.(schema.ObjectKind)
91-
meta.SetGroupVersionKind(gvks[0])
92-
}
93-
94-
req := tc.req.WithContext(WithRuntimeObject(context.Background(), tc.obj))
95-
_, err := transport.RoundTrip(req)
50+
transport := NewDryRunTransport(nopDelegate)
51+
_, err := transport.RoundTrip(tc.req)
9652
gotErr := ""
9753
if err != nil {
9854
gotErr = err.Error()
9955
}
10056
require.Equal(t, tc.wantErr, gotErr)
101-
assertEqualEvents(t, tc.wantEvents, rec.Events)
10257
})
10358
}
10459
}
105-
106-
func assertEqualEvents(t *testing.T, expected []string, actual <-chan string) {
107-
c := time.After(wait.ForeverTestTimeout)
108-
for _, e := range expected {
109-
select {
110-
case a := <-actual:
111-
if e != a {
112-
t.Errorf("Expected event %q, got %q", e, a)
113-
return
114-
}
115-
case <-c:
116-
t.Errorf("Expected event %q, got nothing", e)
117-
// continue iterating to print all expected events
118-
}
119-
}
120-
for {
121-
select {
122-
case a := <-actual:
123-
t.Errorf("Unexpected event: %q", a)
124-
default:
125-
return // No more events, as expected.
126-
}
127-
}
128-
}

internal/operator/builder.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ func (b *Builder) Build(ctx context.Context) (cluster.Cluster, error) {
208208
}
209209

210210
if b.atlasProvider == nil {
211-
b.atlasProvider = atlas.NewProductionProvider(b.atlasDomain, b.apiSecret, mgr.GetClient(), nil)
211+
b.atlasProvider = atlas.NewProductionProvider(b.atlasDomain, b.apiSecret, mgr.GetClient(), false)
212212
}
213213

214214
if err := controllerRegistry.RegisterWithManager(mgr, b.skipNameValidation, b.atlasProvider); err != nil {

0 commit comments

Comments
 (0)