Skip to content

Commit 0d150b3

Browse files
committed
Do not set status for unmanaged private endpoint(s)
1 parent 54091db commit 0d150b3

File tree

2 files changed

+80
-18
lines changed

2 files changed

+80
-18
lines changed

internal/controller/atlasproject/private_endpoint.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -590,10 +590,19 @@ func mapLastAppliedPrivateEndpoint(atlasProject *akov2.AtlasProject) (map[string
590590

591591
func hasManagedPrivateEndpoints(specPEs []akov2.PrivateEndpoint, atlasPEs []atlasPE, lastAppliedPEs map[string]akov2.PrivateEndpoint) bool {
592592
for _, pe := range atlasPEs {
593+
// if any of the PE in atlas was previously managed, return true
593594
if _, ok := lastAppliedPEs[pe.Identifier().(string)]; ok {
594595
return true
595596
}
596597
}
597598

598-
return len(set.DeprecatedDifference(specPEs, atlasPEs)) == 0
599+
//b := set.DeprecatedDifference(atlasPEs, specPEs)
600+
// if any of the PE in atlas is specified in the spec, return true
601+
a := set.DeprecatedIntersection(specPEs, atlasPEs)
602+
if len(specPEs) > 0 && len(a) > 0 {
603+
return true
604+
}
605+
606+
// if there are not managed or previously managed entries, return false
607+
return false
599608
}

internal/controller/atlasproject/private_endpoint_test.go

Lines changed: 70 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import (
55
"fmt"
66
"testing"
77

8+
"github.com/google/go-cmp/cmp"
9+
"github.com/google/go-cmp/cmp/cmpopts"
10+
"github.com/mongodb/mongodb-atlas-kubernetes/v2/api"
811
"github.com/stretchr/testify/assert"
912
"github.com/stretchr/testify/mock"
1013
"github.com/stretchr/testify/require"
@@ -215,41 +218,81 @@ func TestPrivateEndpointsNonGreedyBehaviour(t *testing.T) {
215218
atlasPEids []string
216219
wantRemoved []string
217220
wantResult workflow.Result
221+
conditions []api.Condition
218222
}{
219223
{
220-
title: "no last applied no removal in Atlas",
224+
title: "empty last applied, empty spec, two entries in atlas: shouldn't remove entries",
221225
lastAppliedPEids: []string{},
222226
specPEids: []string{},
223227
atlasPEids: []string{"pe1", "pe2"},
224228
wantRemoved: []string{},
225229
wantResult: workflow.OK(),
230+
conditions: []api.Condition{},
226231
},
227232
{
228-
title: "removed from last applied removes from Atlas",
233+
title: "empty last applied, a entry in spec, two entries in atlas: shouldn't remove missing entry",
234+
lastAppliedPEids: []string{},
235+
specPEids: []string{"pe1"},
236+
atlasPEids: []string{"pe1", "pe2"},
237+
wantRemoved: []string{},
238+
wantResult: workflow.OK().WithMessage("Interface Private Endpoint awaits configuration"),
239+
conditions: []api.Condition{
240+
api.TrueCondition(api.PrivateEndpointServiceReadyType).
241+
WithMessageRegexp("Interface Private Endpoint awaits configuration"),
242+
},
243+
},
244+
{
245+
title: "empty last applied, one entry in spec, empty atlas: no remove action",
246+
lastAppliedPEids: []string{},
247+
specPEids: []string{"pe1"},
248+
atlasPEids: []string{},
249+
wantRemoved: []string{},
250+
wantResult: notReadyServiceResult,
251+
conditions: []api.Condition{
252+
api.FalseCondition(api.PrivateEndpointServiceReadyType).
253+
WithReason(string(workflow.ProjectPEServiceIsNotReadyInAtlas)).
254+
WithMessageRegexp("Private Endpoint Service is not ready"),
255+
},
256+
},
257+
{
258+
title: "two entries in last applied, one entry in spec, two entries in atlas: should remove one entry",
229259
lastAppliedPEids: []string{"pe1", "pe2"},
230260
specPEids: []string{"pe1"},
231261
atlasPEids: []string{"pe1", "pe2"},
232262
wantRemoved: []string{"pe2"},
233263
wantResult: workflow.InProgress(
234264
workflow.ProjectPEServiceIsNotReadyInAtlas, "Private Endpoint is deleting"),
265+
conditions: []api.Condition{
266+
api.FalseCondition(api.PrivateEndpointServiceReadyType).
267+
WithReason(string(workflow.ProjectPEServiceIsNotReadyInAtlas)).
268+
WithMessageRegexp("Private Endpoint is deleting"),
269+
},
235270
},
236271
{
237-
title: "removed all from last applied removes all from Atlas",
272+
title: "two entries in last applied, empty spec, two entries in atlas: should remove entries",
238273
lastAppliedPEids: []string{"pe1", "pe2"},
239274
specPEids: []string{},
240275
atlasPEids: []string{"pe1", "pe2"},
241276
wantRemoved: []string{"pe1", "pe2"},
242277
wantResult: workflow.InProgress(
243278
workflow.ProjectPEServiceIsNotReadyInAtlas, "Private Endpoint is deleting"),
279+
conditions: []api.Condition{
280+
api.FalseCondition(api.PrivateEndpointServiceReadyType).
281+
WithReason(string(workflow.ProjectPEServiceIsNotReadyInAtlas)).
282+
WithMessageRegexp("Private Endpoint is deleting"),
283+
},
244284
},
245285
{
246-
title: "not in last applied not removed from Atlas",
286+
title: "an entry in last applied, an entry in spec, two entries in atlas: shouldn't remove an entry",
247287
lastAppliedPEids: []string{"pe1"},
248288
specPEids: []string{"pe1"},
249289
atlasPEids: []string{"pe1", "pe2"},
250290
wantRemoved: []string{},
251-
wantResult: workflow.InProgress(
252-
workflow.ProjectPrivateEndpointIsNotReadyInAtlas, "Interface Private Endpoint is not ready"),
291+
wantResult: workflow.OK().WithMessage("Interface Private Endpoint awaits configuration"),
292+
conditions: []api.Condition{
293+
api.TrueCondition(api.PrivateEndpointServiceReadyType).
294+
WithMessageRegexp("Interface Private Endpoint awaits configuration"),
295+
},
253296
},
254297
} {
255298
t.Run(tc.title, func(t *testing.T) {
@@ -279,20 +322,18 @@ func TestPrivateEndpointsNonGreedyBehaviour(t *testing.T) {
279322

280323
removals := len(tc.wantRemoved)
281324
if removals > 0 {
282-
privateEndpointsAPI.EXPECT().DeletePrivateEndpointServiceWithParams(
283-
mock.Anything, mock.Anything,
284-
).Return(admin.DeletePrivateEndpointServiceApiRequest{ApiService: privateEndpointsAPI}).Times(removals)
325+
privateEndpointsAPI.EXPECT().DeletePrivateEndpointServiceWithParams(mock.Anything, mock.Anything).
326+
Return(admin.DeletePrivateEndpointServiceApiRequest{ApiService: privateEndpointsAPI}).Times(removals)
285327
privateEndpointsAPI.EXPECT().DeletePrivateEndpointServiceExecute(
286328
mock.AnythingOfType("admin.DeletePrivateEndpointServiceApiRequest")).Return(
287329
nil, nil, nil,
288330
).Times(removals)
289331
}
290-
privateEndpointsAPI.EXPECT().CreatePrivateEndpointWithParams(
291-
mock.Anything, mock.Anything,
292-
).Return(admin.CreatePrivateEndpointApiRequest{ApiService: privateEndpointsAPI}).Maybe()
293-
privateEndpointsAPI.EXPECT().CreatePrivateEndpointExecute(
294-
mock.AnythingOfType("admin.CreatePrivateEndpointApiRequest")).Return(
295-
nil, nil, nil,
332+
privateEndpointsAPI.EXPECT().CreatePrivateEndpointService(mock.Anything, mock.Anything, mock.Anything).
333+
Return(admin.CreatePrivateEndpointServiceApiRequest{ApiService: privateEndpointsAPI}).Maybe()
334+
privateEndpointsAPI.EXPECT().CreatePrivateEndpointServiceExecute(
335+
mock.AnythingOfType("admin.CreatePrivateEndpointServiceApiRequest")).Return(
336+
&admin.EndpointService{CloudProvider: "AWS", RegionName: pointer.MakePtr("fake-region-pe1")}, nil, nil,
296337
).Maybe()
297338

298339
workflowCtx := workflow.Context{
@@ -307,6 +348,19 @@ func TestPrivateEndpointsNonGreedyBehaviour(t *testing.T) {
307348

308349
result := ensurePrivateEndpoint(&workflowCtx, prj)
309350
require.Equal(t, tc.wantResult, result)
351+
t.Log(cmp.Diff(
352+
tc.conditions,
353+
workflowCtx.Conditions(),
354+
cmpopts.IgnoreFields(api.Condition{}, "LastTransitionTime"),
355+
))
356+
assert.True(
357+
t,
358+
cmp.Equal(
359+
tc.conditions,
360+
workflowCtx.Conditions(),
361+
cmpopts.IgnoreFields(api.Condition{}, "LastTransitionTime"),
362+
),
363+
)
310364
})
311365
}
312366
}
@@ -328,7 +382,6 @@ func synthesizePEs(peIDs []string) []akov2.PrivateEndpoint {
328382
for _, id := range peIDs {
329383
pes = append(pes, akov2.PrivateEndpoint{
330384
Provider: "AWS",
331-
ID: id,
332385
Region: fmt.Sprintf("fake-region-%s", id),
333386
})
334387
}
@@ -340,7 +393,7 @@ func synthesizeAtlasPEs(peIDs []string) []admin.EndpointService {
340393
for _, id := range peIDs {
341394
atlasPEs = append(atlasPEs, admin.EndpointService{
342395
CloudProvider: "AWS",
343-
Id: pointer.MakePtr(id),
396+
Id: pointer.MakePtr(id + "-id"),
344397
RegionName: pointer.MakePtr(fmt.Sprintf("fake-region-%s", id)),
345398
Status: pointer.MakePtr("AVAILABLE"),
346399
})

0 commit comments

Comments
 (0)