Skip to content

Commit 4da02c0

Browse files
DBAAS-972 Grant access to the newly created database instance in Atlas Operator: address review comments
1 parent 07e3aaf commit 4da02c0

File tree

5 files changed

+48
-7
lines changed

5 files changed

+48
-7
lines changed

config/dbaasprovider/dbaas_provider.yaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,7 @@ spec:
6464
type: string
6565
required: false
6666
defaultValue: M0
67-
- name: IPAccessList
67+
- name: ipAccessList
6868
displayName: List of IP Addresses or Ranges (Space Separated) to Access the Cluster
6969
type: string
7070
required: false
71-
defaultValue: 0.0.0.0/0

pkg/api/dbaas/mongodbatlasinstance_types.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ limitations under the License.
1414
package dbaas
1515

1616
import (
17+
"encoding/json"
18+
"io/ioutil"
19+
"net/http"
20+
1721
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1822

1923
dbaasv1alpha1 "github.com/RHEcosystemAppEng/dbaas-operator/api/v1alpha1"
@@ -94,3 +98,24 @@ func GetInstanceCondition(inv *MongoDBAtlasInstance, condType string) *metav1.Co
9498
}
9599
return nil
96100
}
101+
102+
type IP struct {
103+
Query string
104+
}
105+
106+
// GetPublicIP returns the static outbound public IP of the OpenShift Cluster
107+
// Or when the operator runs locally, the h
108+
func GetPublicIP() (string, error) {
109+
req, err := http.Get("http://ip-api.com/json/")
110+
if err != nil {
111+
return "", err
112+
}
113+
defer req.Body.Close()
114+
body, err := ioutil.ReadAll(req.Body)
115+
if err != nil {
116+
return "", err
117+
}
118+
var ip IP
119+
json.Unmarshal(body, &ip)
120+
return ip.Query, nil
121+
}

pkg/controller/atlasinstance/atlasinstance_controller.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -477,8 +477,13 @@ func getInstanceData(log *zap.SugaredLogger, inst *dbaas.MongoDBAtlasInstance) (
477477

478478
accessIP, ok := inst.Spec.OtherInstanceParams[dbaas.IPAccessListKey]
479479
if !ok || len(strings.TrimSpace(accessIP)) == 0 {
480-
accessIP = "0.0.0.0/0"
481-
log.Infof("%v is missing, default value %s is used to allow access from anywhere.", dbaas.IPAccessListKey, accessIP)
480+
ip, err := dbaas.GetPublicIP()
481+
if err != nil {
482+
log.Infof("Failed to get the public IP")
483+
return nil, err
484+
}
485+
accessIP = ip
486+
log.Infof("%v is missing, current IP %s is used.", dbaas.IPAccessListKey, accessIP)
482487
}
483488

484489
return &InstanceData{

pkg/controller/atlasinstance/atlasinstance_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ func TestGetInstanceData(t *testing.T) {
118118
expRegionName: "AWS_REGION",
119119
expInstanceSizeName: "M10",
120120
expErrMsg: "",
121-
expIPAccessList: "0.0.0.0/0",
121+
expIPAccessList: "52.206.222.245/32",
122122
},
123123
"UseDefaultRegion": {
124124
deploymentName: "myDeployment",
@@ -142,7 +142,7 @@ func TestGetInstanceData(t *testing.T) {
142142
expRegionName: "US_EAST_1",
143143
expInstanceSizeName: "M0",
144144
expErrMsg: "",
145-
expIPAccessList: "0.0.0.0/0",
145+
expIPAccessList: "52.206.222.245/32",
146146
},
147147
}
148148

@@ -406,7 +406,7 @@ func TestAtlasInstanceReconcile(t *testing.T) {
406406
tcName := "mytest"
407407
deploymentName := "mydeploymentnew"
408408
projectName := "myproject"
409-
ipAccessList := "0.0.0.0/0"
409+
ipAccessList := "52.206.222.245/32"
410410
expectedPhase := dbaasv1alpha1.InstancePhasePending
411411
expectedErrString := "CLUSTER_NOT_FOUND"
412412
expectedRequeue := true

pkg/controller/dbaasprovider/dbaasprovider_reconciler.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"time"
2727

2828
dbaasoperator "github.com/RHEcosystemAppEng/dbaas-operator/api/v1alpha1"
29+
"github.com/mongodb/mongodb-atlas-kubernetes/pkg/api/dbaas"
2930
"go.uber.org/zap"
3031
v1 "k8s.io/api/apps/v1"
3132
rbac "k8s.io/api/rbac/v1"
@@ -164,6 +165,17 @@ func (r *DBaaSProviderReconciler) getAtlasProviderCR(clusterRoleList *rbac.Clust
164165
if err != nil {
165166
return nil, err
166167
}
168+
169+
for ind, spec := range instance.Spec.InstanceParameterSpecs {
170+
if spec.Name == dbaas.IPAccessListKey {
171+
ip, err := dbaas.GetPublicIP()
172+
if err != nil {
173+
return nil, err
174+
}
175+
spec.DefaultValue = ip
176+
instance.Spec.InstanceParameterSpecs[ind] = spec
177+
}
178+
}
167179
instance.ObjectMeta.OwnerReferences = []metav1.OwnerReference{
168180
{
169181
APIVersion: "rbac.authorization.k8s.io/v1",

0 commit comments

Comments
 (0)