|
| 1 | +// Copyright 2024 MongoDB Inc |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package project |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + "strings" |
| 20 | + |
| 21 | + "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/kubernetes/operator/features" |
| 22 | + "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/kubernetes/operator/resources" |
| 23 | + "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/store" |
| 24 | + akoapi "github.com/mongodb/mongodb-atlas-kubernetes/v2/pkg/api" |
| 25 | + akov2 "github.com/mongodb/mongodb-atlas-kubernetes/v2/pkg/api/v1" |
| 26 | + akov2common "github.com/mongodb/mongodb-atlas-kubernetes/v2/pkg/api/v1/common" |
| 27 | + akov2status "github.com/mongodb/mongodb-atlas-kubernetes/v2/pkg/api/v1/status" |
| 28 | + atlasv2 "go.mongodb.org/atlas-sdk/v20241113004/admin" |
| 29 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 30 | +) |
| 31 | + |
| 32 | +type PrivateEndpointRequest struct { |
| 33 | + ProjectName string |
| 34 | + ProjectID string |
| 35 | + TargetNamespace string |
| 36 | + Version string |
| 37 | + Credentials string |
| 38 | + IndependentResource bool |
| 39 | + Dictionary map[string]string |
| 40 | +} |
| 41 | + |
| 42 | +func BuildPrivateEndpointCustomResources( |
| 43 | + provider store.OperatorPrivateEndpointStore, |
| 44 | + request PrivateEndpointRequest, |
| 45 | +) ([]akov2.AtlasPrivateEndpoint, error) { |
| 46 | + services := make([]atlasv2.EndpointService, 0) |
| 47 | + |
| 48 | + for _, cloud := range []string{"AWS", "AZURE", "GCP"} { |
| 49 | + cloudServices, err := provider.PrivateEndpoints(request.ProjectID, cloud) |
| 50 | + if err != nil { |
| 51 | + return nil, err |
| 52 | + } |
| 53 | + |
| 54 | + services = append(services, cloudServices...) |
| 55 | + } |
| 56 | + |
| 57 | + privateEndpoints := make([]akov2.AtlasPrivateEndpoint, 0, len(services)) |
| 58 | + for _, service := range services { |
| 59 | + resourceName := resources.NormalizeAtlasName( |
| 60 | + fmt.Sprintf("%s-pe-%s-%s", request.ProjectName, service.GetCloudProvider(), strings.ToLower(strings.ReplaceAll(service.GetRegionName(), "_", ""))), |
| 61 | + request.Dictionary, |
| 62 | + ) |
| 63 | + resource := akov2.AtlasPrivateEndpoint{ |
| 64 | + TypeMeta: metav1.TypeMeta{ |
| 65 | + Kind: "AtlasPrivateEndpoint", |
| 66 | + APIVersion: "atlas.mongodb.com/v1", |
| 67 | + }, |
| 68 | + ObjectMeta: metav1.ObjectMeta{ |
| 69 | + Name: resourceName, |
| 70 | + Namespace: request.TargetNamespace, |
| 71 | + Labels: map[string]string{ |
| 72 | + features.ResourceVersion: request.Version, |
| 73 | + }, |
| 74 | + }, |
| 75 | + Spec: akov2.AtlasPrivateEndpointSpec{ |
| 76 | + Provider: service.GetCloudProvider(), |
| 77 | + Region: service.GetRegionName(), |
| 78 | + }, |
| 79 | + Status: akov2status.AtlasPrivateEndpointStatus{ |
| 80 | + Common: akoapi.Common{ |
| 81 | + Conditions: []akoapi.Condition{}, |
| 82 | + }, |
| 83 | + }, |
| 84 | + } |
| 85 | + |
| 86 | + if request.IndependentResource { |
| 87 | + resource.Spec.ExternalProject = &akov2.ExternalProjectReference{ |
| 88 | + ID: request.ProjectID, |
| 89 | + } |
| 90 | + resource.Spec.LocalCredentialHolder = akoapi.LocalCredentialHolder{ |
| 91 | + ConnectionSecret: &akoapi.LocalObjectReference{ |
| 92 | + Name: resources.NormalizeAtlasName(request.Credentials, request.Dictionary), |
| 93 | + }, |
| 94 | + } |
| 95 | + } else { |
| 96 | + resource.Spec.Project = &akov2common.ResourceRefNamespaced{ |
| 97 | + Name: request.ProjectName, |
| 98 | + Namespace: request.TargetNamespace, |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + awsConfigs, err := buildAWSInterfaces(provider, request.ProjectID, service.GetId(), service.GetInterfaceEndpoints()) |
| 103 | + if err != nil { |
| 104 | + return nil, err |
| 105 | + } |
| 106 | + resource.Spec.AWSConfiguration = awsConfigs |
| 107 | + |
| 108 | + azureConfigs, err := buildAzureInterfaces(provider, request.ProjectID, service.GetId(), service.GetPrivateEndpoints()) |
| 109 | + if err != nil { |
| 110 | + return nil, err |
| 111 | + } |
| 112 | + resource.Spec.AzureConfiguration = azureConfigs |
| 113 | + |
| 114 | + gcpConfigs, err := buildGCPInterfaces(provider, request.ProjectID, service.GetId(), service.GetEndpointGroupNames()) |
| 115 | + if err != nil { |
| 116 | + return nil, err |
| 117 | + } |
| 118 | + resource.Spec.GCPConfiguration = gcpConfigs |
| 119 | + |
| 120 | + privateEndpoints = append(privateEndpoints, resource) |
| 121 | + } |
| 122 | + |
| 123 | + return privateEndpoints, nil |
| 124 | +} |
| 125 | + |
| 126 | +func buildAWSInterfaces( |
| 127 | + provider store.OperatorPrivateEndpointStore, |
| 128 | + projectID string, |
| 129 | + serviceID string, |
| 130 | + interfaceIDs []string, |
| 131 | +) ([]akov2.AWSPrivateEndpointConfiguration, error) { |
| 132 | + if len(interfaceIDs) == 0 { |
| 133 | + return nil, nil |
| 134 | + } |
| 135 | + |
| 136 | + configs := make([]akov2.AWSPrivateEndpointConfiguration, 0, len(interfaceIDs)) |
| 137 | + |
| 138 | + for _, interfaceID := range interfaceIDs { |
| 139 | + pe, err := provider.InterfaceEndpoint(projectID, "AWS", serviceID, interfaceID) |
| 140 | + if err != nil { |
| 141 | + return nil, err |
| 142 | + } |
| 143 | + |
| 144 | + configs = append(configs, akov2.AWSPrivateEndpointConfiguration{ID: pe.GetInterfaceEndpointId()}) |
| 145 | + } |
| 146 | + |
| 147 | + return configs, nil |
| 148 | +} |
| 149 | + |
| 150 | +func buildAzureInterfaces( |
| 151 | + provider store.OperatorPrivateEndpointStore, |
| 152 | + projectID string, |
| 153 | + serviceID string, |
| 154 | + interfaceIDs []string, |
| 155 | +) ([]akov2.AzurePrivateEndpointConfiguration, error) { |
| 156 | + if len(interfaceIDs) == 0 { |
| 157 | + return nil, nil |
| 158 | + } |
| 159 | + |
| 160 | + configs := make([]akov2.AzurePrivateEndpointConfiguration, 0, len(interfaceIDs)) |
| 161 | + |
| 162 | + for _, interfaceID := range interfaceIDs { |
| 163 | + pe, err := provider.InterfaceEndpoint(projectID, "AZURE", serviceID, interfaceID) |
| 164 | + if err != nil { |
| 165 | + return nil, err |
| 166 | + } |
| 167 | + |
| 168 | + configs = append( |
| 169 | + configs, |
| 170 | + akov2.AzurePrivateEndpointConfiguration{ |
| 171 | + ID: pe.GetPrivateEndpointResourceId(), |
| 172 | + IP: pe.GetPrivateEndpointIPAddress(), |
| 173 | + }, |
| 174 | + ) |
| 175 | + } |
| 176 | + |
| 177 | + return configs, nil |
| 178 | +} |
| 179 | + |
| 180 | +func buildGCPInterfaces( |
| 181 | + provider store.OperatorPrivateEndpointStore, |
| 182 | + projectID string, |
| 183 | + serviceID string, |
| 184 | + interfaceIDs []string, |
| 185 | +) ([]akov2.GCPPrivateEndpointConfiguration, error) { |
| 186 | + if len(interfaceIDs) == 0 { |
| 187 | + return nil, nil |
| 188 | + } |
| 189 | + |
| 190 | + configs := make([]akov2.GCPPrivateEndpointConfiguration, 0, len(interfaceIDs)) |
| 191 | + |
| 192 | + for _, interfaceID := range interfaceIDs { |
| 193 | + pe, err := provider.InterfaceEndpoint(projectID, "GCP", serviceID, interfaceID) |
| 194 | + if err != nil { |
| 195 | + return nil, err |
| 196 | + } |
| 197 | + |
| 198 | + gcpEPs := make([]akov2.GCPPrivateEndpoint, 0, len(pe.GetEndpoints())) |
| 199 | + for _, gcpEP := range pe.GetEndpoints() { |
| 200 | + gcpEPs = append( |
| 201 | + gcpEPs, |
| 202 | + akov2.GCPPrivateEndpoint{ |
| 203 | + Name: gcpEP.GetEndpointName(), |
| 204 | + IP: gcpEP.GetIpAddress(), |
| 205 | + }, |
| 206 | + ) |
| 207 | + } |
| 208 | + |
| 209 | + configs = append( |
| 210 | + configs, |
| 211 | + akov2.GCPPrivateEndpointConfiguration{ |
| 212 | + // GCP ProjectID is not returned by Atlas and therefore, need to be set by the user after resource is exported |
| 213 | + ProjectID: "", |
| 214 | + GroupName: pe.GetEndpointGroupName(), |
| 215 | + Endpoints: gcpEPs, |
| 216 | + }, |
| 217 | + ) |
| 218 | + } |
| 219 | + |
| 220 | + return configs, nil |
| 221 | +} |
0 commit comments