Skip to content

Commit 69cf55b

Browse files
authored
Add ServiceAttachmentNames field to GCP PE Status (#534)
1 parent 18d9958 commit 69cf55b

File tree

5 files changed

+61
-41
lines changed

5 files changed

+61
-41
lines changed

config/crd/bases/atlas.mongodb.com_atlasprojects.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,13 @@ spec:
419419
description: Cloud provider region for which you want to create
420420
the private endpoint service.
421421
type: string
422+
serviceAttachmentNames:
423+
description: Unique alphanumeric and special character strings
424+
that identify the service attachments associated with the
425+
GCP Private Service Connect endpoint service.
426+
items:
427+
type: string
428+
type: array
422429
serviceName:
423430
description: Name of the AWS or Azure Private Link Service that
424431
Atlas manages.

pkg/api/v1/status/atlasproject.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ func AtlasProjectUpdatePrivateEnpointsOption(privateEndpoints []ProjectPrivateEn
4545
if currentPE.InterfaceEndpointID != "" {
4646
s.PrivateEndpoints[peIdx].InterfaceEndpointID = currentPE.InterfaceEndpointID
4747
}
48+
if len(currentPE.ServiceAttachmentNames) != 0 {
49+
s.PrivateEndpoints[peIdx].ServiceAttachmentNames = currentPE.ServiceAttachmentNames
50+
}
4851
if len(currentPE.Endpoints) != 0 {
4952
s.PrivateEndpoints[peIdx].Endpoints = currentPE.Endpoints
5053
}

pkg/api/v1/status/privateendpoint.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ type ProjectPrivateEndpoint struct {
1515
ServiceResourceID string `json:"serviceResourceId,omitempty"`
1616
// Unique identifier of the AWS or Azure Private Link Interface Endpoint.
1717
InterfaceEndpointID string `json:"interfaceEndpointId,omitempty"`
18+
// Unique alphanumeric and special character strings that identify the service attachments associated with the GCP Private Service Connect endpoint service.
19+
ServiceAttachmentNames []string `json:"serviceAttachmentNames,omitempty"`
1820
// Collection of individual GCP private endpoints that comprise your network endpoint group.
1921
Endpoints []GCPEndpoint `json:"endpoints,omitempty"`
2022
}

pkg/api/v1/status/zz_generated.deepcopy.go

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/controller/atlasproject/private_endpoint.go

Lines changed: 44 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package atlasproject
22

33
import (
44
"context"
5+
"errors"
56

67
"go.mongodb.org/atlas/mongodbatlas"
78
"go.uber.org/zap"
@@ -39,24 +40,22 @@ func createOrDeletePEInAtlas(ctx *workflow.Context, projectID string, specPEs []
3940
return result
4041
}
4142

42-
endpointsToCreate := set.Difference(specPEs, statusPEs)
43-
endpointsToUpdate := set.Intersection(specPEs, statusPEs)
4443
endpointsToDelete := set.Difference(statusPEs, specPEs)
45-
46-
log.Debugw("Items to create", "difference", endpointsToCreate)
47-
log.Debugw("Items to update", "difference", endpointsToUpdate)
48-
log.Debugw("Items to delete", "difference", endpointsToDelete)
49-
44+
log.Debugw("Private Endpoints to delete", "difference", endpointsToDelete)
5045
if result := deletePrivateEndpointsFromAtlas(ctx, projectID, endpointsToDelete); !result.IsOk() {
5146
return result
5247
}
5348

49+
endpointsToCreate := set.Difference(specPEs, statusPEs)
50+
log.Debugw("Private Endpoints to create", "difference", endpointsToCreate)
5451
newConnections, err := createPeServiceInAtlas(ctx.Client, projectID, endpointsToCreate)
5552
if err != nil {
5653
log.Debugw("Failed to create PE Service in Atlas", "error", err)
5754
}
5855
ctx.EnsureStatusOption(status.AtlasProjectAddPrivateEnpointsOption(convertAllToStatus(ctx, projectID, newConnections)))
5956

57+
endpointsToUpdate := set.Intersection(specPEs, statusPEs)
58+
log.Debugw("Private Endpoints to update", "difference", endpointsToUpdate)
6059
if err = createPrivateEndpointInAtlas(ctx.Client, projectID, endpointsToUpdate, log); err != nil {
6160
log.Debugw("Failed to create PE Interface in Atlas", "error", err)
6261
}
@@ -212,8 +211,8 @@ func clearOutNotLinkedPEs(ctx *workflow.Context, projectID string, atlasConns []
212211
endpointsWithoutPair := []status.ProjectPrivateEndpoint{}
213212
endpointsAreDeleting := false
214213
for _, atlasConn := range atlasConns {
215-
if isDeleting(atlasConn.Status) {
216-
endpointsAreDeleting = true
214+
if !isAvailable(atlasConn.Status) {
215+
continue
217216
}
218217

219218
atlasPE := convertOneToStatus(ctx, projectID, atlasConn)
@@ -226,6 +225,7 @@ func clearOutNotLinkedPEs(ctx *workflow.Context, projectID string, atlasConns []
226225

227226
if !found {
228227
endpointsWithoutPair = append(endpointsWithoutPair, atlasPE)
228+
endpointsAreDeleting = true
229229
}
230230
}
231231

@@ -271,65 +271,68 @@ func convertAllToStatus(ctx *workflow.Context, projectID string, peList []mongod
271271
return result
272272
}
273273

274-
func convertOneToStatus(ctx *workflow.Context, projectID string, endpoint mongodbatlas.PrivateEndpointConnection) status.ProjectPrivateEndpoint {
274+
func convertOneToStatus(ctx *workflow.Context, projectID string, conn mongodbatlas.PrivateEndpointConnection) status.ProjectPrivateEndpoint {
275275
pe := status.ProjectPrivateEndpoint{
276-
ID: endpoint.ID,
277-
Provider: provider.ProviderName(endpoint.ProviderName),
278-
Region: endpoint.Region,
276+
ID: conn.ID,
277+
Provider: provider.ProviderName(conn.ProviderName),
278+
Region: conn.Region,
279279
}
280280

281281
switch pe.Provider {
282282
case provider.ProviderAWS:
283-
pe.ServiceName = endpoint.EndpointServiceName
284-
pe.ServiceResourceID = endpoint.ID
285-
if len(endpoint.InterfaceEndpoints) != 0 {
286-
pe.InterfaceEndpointID = endpoint.InterfaceEndpoints[0]
283+
pe.ServiceName = conn.EndpointServiceName
284+
pe.ServiceResourceID = conn.ID
285+
if len(conn.InterfaceEndpoints) != 0 {
286+
pe.InterfaceEndpointID = conn.InterfaceEndpoints[0]
287287
}
288288
case provider.ProviderAzure:
289-
pe.ServiceName = endpoint.PrivateLinkServiceName
290-
pe.ServiceResourceID = endpoint.PrivateLinkServiceResourceID
291-
if len(endpoint.PrivateEndpoints) != 0 {
292-
pe.InterfaceEndpointID = endpoint.PrivateEndpoints[0]
289+
pe.ServiceName = conn.PrivateLinkServiceName
290+
pe.ServiceResourceID = conn.PrivateLinkServiceResourceID
291+
if len(conn.PrivateEndpoints) != 0 {
292+
pe.InterfaceEndpointID = conn.PrivateEndpoints[0]
293293
}
294294
case provider.ProviderGCP:
295-
pe.InterfaceEndpointID, pe.Endpoints = getGCPEndpointData(ctx, projectID, endpoint)
295+
pe.ServiceAttachmentNames = conn.ServiceAttachmentNames
296+
if len(conn.EndpointGroupNames) != 0 {
297+
var err error
298+
pe.InterfaceEndpointID = conn.EndpointGroupNames[0]
299+
pe.Endpoints, err = getGCPInterfaceEndpoint(ctx, projectID, pe)
300+
if err != nil {
301+
ctx.Log.Warnw("failed to get Interface Endpoint Data for GCP", "err", err, "pe", pe)
302+
}
303+
}
296304
}
305+
ctx.Log.Debugw("Converted Status", "status", pe, "connection", conn)
306+
297307
return pe
298308
}
299309

300-
// getGCPEndpointData returns an InterfaceEndpointID and a list of GCP endpoints
301-
func getGCPEndpointData(ctx *workflow.Context, projectID string, endpoint mongodbatlas.PrivateEndpointConnection) (string, []status.GCPEndpoint) {
310+
// getGCPInterfaceEndpoint returns an InterfaceEndpointID and a list of GCP endpoints
311+
func getGCPInterfaceEndpoint(ctx *workflow.Context, projectID string, endpoint status.ProjectPrivateEndpoint) ([]status.GCPEndpoint, error) {
302312
log := ctx.Log
303-
serviceID := endpoint.ID
304-
if len(endpoint.EndpointGroupNames) == 0 {
305-
return "", nil
313+
if endpoint.InterfaceEndpointID == "" {
314+
return nil, errors.New("InterfaceEndpointID is empty")
306315
}
307-
endpointID := endpoint.EndpointGroupNames[0]
308-
interfaceEndpointConn, _, err := ctx.Client.PrivateEndpoints.GetOnePrivateEndpoint(context.Background(), projectID, string(provider.ProviderGCP), serviceID, endpointID)
316+
interfaceEndpointConn, _, err := ctx.Client.PrivateEndpoints.GetOnePrivateEndpoint(context.Background(), projectID, string(provider.ProviderGCP), endpoint.ID, endpoint.InterfaceEndpointID)
309317
if err != nil {
310-
return endpointID, nil
318+
return nil, err
311319
}
312320

313-
endpointConns := interfaceEndpointConn.Endpoints
314-
listOfEndpoints := make([]status.GCPEndpoint, 0)
315-
for _, e := range endpointConns {
321+
interfaceConns := interfaceEndpointConn.Endpoints
322+
listOfInterfaces := make([]status.GCPEndpoint, 0)
323+
for _, e := range interfaceConns {
316324
endpoint := status.GCPEndpoint{
317325
Status: e.Status,
318326
EndpointName: e.EndpointName,
319327
IPAddress: e.IPAddress,
320328
}
321-
listOfEndpoints = append(listOfEndpoints, endpoint)
329+
listOfInterfaces = append(listOfInterfaces, endpoint)
322330
}
331+
log.Debugw("Result of getGCPEndpointData", "endpoint.ID", endpoint.ID, "listOfInterfaces", listOfInterfaces)
323332

324-
log.Debugw("Result of getGCPEndpointData", "endpointID", endpointID, "listOfEndpoints", listOfEndpoints)
325-
326-
return endpointID, listOfEndpoints
333+
return listOfInterfaces, nil
327334
}
328335

329336
func isAvailable(status string) bool {
330337
return status == "AVAILABLE"
331338
}
332-
333-
func isDeleting(status string) bool {
334-
return status == "DELETING"
335-
}

0 commit comments

Comments
 (0)