Skip to content

Commit a1c133d

Browse files
authored
CLOUDP-128679: Allow to sync back Private Endpoints (#603)
* Allow to sync back PEs * Improve the tests for PEs a bit * Regenerate zz_generated.deepcopy changes
1 parent aed52c5 commit a1c133d

File tree

11 files changed

+263
-198
lines changed

11 files changed

+263
-198
lines changed

pkg/api/v1/privateendpoint.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"go.mongodb.org/atlas/mongodbatlas"
77

88
"github.com/mongodb/mongodb-atlas-kubernetes/pkg/api/v1/provider"
9+
"github.com/mongodb/mongodb-atlas-kubernetes/pkg/api/v1/status"
910
"github.com/mongodb/mongodb-atlas-kubernetes/pkg/util/compat"
1011
)
1112

@@ -50,7 +51,7 @@ func (i PrivateEndpoint) ToAtlas() (*mongodbatlas.PrivateEndpoint, error) {
5051

5152
// Identifier is required to satisfy "Identifiable" iterface
5253
func (i PrivateEndpoint) Identifier() interface{} {
53-
return string(i.Provider) + i.Region
54+
return string(i.Provider) + status.TransformRegionToID(i.Region)
5455
}
5556

5657
func (endpoints GCPEndpoints) ConvertToAtlas() ([]*mongodbatlas.GCPEndpoint, error) {

pkg/api/v1/status/atlasproject.go

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -28,40 +28,9 @@ func AtlasProjectAddPrivateEnpointsOption(privateEndpoints []ProjectPrivateEndpo
2828
}
2929
}
3030

31-
func AtlasProjectUpdatePrivateEnpointsOption(privateEndpoints []ProjectPrivateEndpoint) AtlasProjectStatusOption {
31+
func AtlasProjectSetPrivateEnpointsOption(privateEndpoints []ProjectPrivateEndpoint) AtlasProjectStatusOption {
3232
return func(s *AtlasProjectStatus) {
33-
result := []ProjectPrivateEndpoint{}
34-
35-
for _, currentPE := range privateEndpoints {
36-
var matchedPE *ProjectPrivateEndpoint
37-
for peIdx, statusPE := range s.PrivateEndpoints {
38-
if currentPE.ID == statusPE.ID {
39-
if currentPE.ServiceName != "" {
40-
s.PrivateEndpoints[peIdx].ServiceName = currentPE.ServiceName
41-
}
42-
if currentPE.ServiceResourceID != "" {
43-
s.PrivateEndpoints[peIdx].ServiceResourceID = currentPE.ServiceResourceID
44-
}
45-
if currentPE.InterfaceEndpointID != "" {
46-
s.PrivateEndpoints[peIdx].InterfaceEndpointID = currentPE.InterfaceEndpointID
47-
}
48-
if len(currentPE.ServiceAttachmentNames) != 0 {
49-
s.PrivateEndpoints[peIdx].ServiceAttachmentNames = currentPE.ServiceAttachmentNames
50-
}
51-
if len(currentPE.Endpoints) != 0 {
52-
s.PrivateEndpoints[peIdx].Endpoints = currentPE.Endpoints
53-
}
54-
55-
matchedPE = &s.PrivateEndpoints[peIdx]
56-
}
57-
}
58-
59-
if matchedPE != nil {
60-
result = append(result, *matchedPE)
61-
}
62-
}
63-
64-
s.PrivateEndpoints = result
33+
s.PrivateEndpoints = privateEndpoints
6534
}
6635
}
6736

pkg/api/v1/status/privateendpoint.go

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
package status
22

3-
import "github.com/mongodb/mongodb-atlas-kubernetes/pkg/api/v1/provider"
3+
import (
4+
"regexp"
5+
"sort"
6+
"strings"
7+
8+
"github.com/mongodb/mongodb-atlas-kubernetes/pkg/api/v1/provider"
9+
)
410

511
type ProjectPrivateEndpoint struct {
612
// Unique identifier for AWS or AZURE Private Link Connection.
@@ -28,5 +34,26 @@ type GCPEndpoint struct {
2834
}
2935

3036
func (pe ProjectPrivateEndpoint) Identifier() interface{} {
31-
return string(pe.Provider) + pe.Region
37+
return string(pe.Provider) + TransformRegionToID(pe.Region)
38+
}
39+
40+
// TransformRegionToID makes the same ID from region and regionName fields for PE Connections to match them
41+
// it leaves only characters which are letters or numbers starting from 2
42+
// it also makes a couple swaps and sorts the resulting string
43+
// this function is a temporary work around caused by the empty "region" field in Atlas reply
44+
func TransformRegionToID(region string) string {
45+
reg := regexp.MustCompile("[^a-z2-9]+")
46+
temp := strings.ToLower(region)
47+
48+
// this is GCP specific
49+
temp = strings.ReplaceAll(temp, "northern", "north")
50+
temp = strings.ReplaceAll(temp, "southern", "south")
51+
temp = strings.ReplaceAll(temp, "western", "west")
52+
temp = strings.ReplaceAll(temp, "eastern", "east")
53+
54+
temp = reg.ReplaceAllString(temp, "")
55+
56+
tempSlice := strings.Split(temp, "")
57+
sort.Strings(tempSlice)
58+
return strings.Join(tempSlice, "")
3259
}

pkg/api/v1/zz_generated.deepcopy.go

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

pkg/controller/atlasproject/atlasproject_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ func (r *AtlasProjectReconciler) Reconcile(context context.Context, req ctrl.Req
156156
if customresource.ResourceShouldBeLeftInAtlas(project) {
157157
log.Infof("Not removing the Atlas Project from Atlas as the '%s' annotation is set", customresource.ResourcePolicyAnnotation)
158158
} else {
159-
if result = DeleteAllPrivateEndpoints(ctx, atlasClient, projectID, project.Status.PrivateEndpoints, log); !result.IsOk() {
159+
if result = DeleteAllPrivateEndpoints(ctx, projectID); !result.IsOk() {
160160
setCondition(ctx, status.PrivateEndpointReadyType, result)
161161
return result.ReconcileResult(), nil
162162
}

0 commit comments

Comments
 (0)