Skip to content

Commit 822a61a

Browse files
authored
CLOUDP-81282: support for Atlas Domain configuration (#64)
1 parent 60e9775 commit 822a61a

File tree

12 files changed

+79
-35
lines changed

12 files changed

+79
-35
lines changed

.github/actions/deploy/entrypoint.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ controller-gen crd:crdVersions=v1 rbac:roleName=manager-role webhook paths="./..
1111
ns=mongodb-atlas-kubernetes-system
1212
kubectl delete deployment mongodb-atlas-kubernetes-controller-manager -n "${ns}" || true # temporary
1313
cd config/manager && kustomize edit set image controller="${INPUT_IMAGE_URL}"
14-
cd - && kustomize build config/default | kubectl apply -f -
14+
cd - && kustomize build config/dev | kubectl apply -f -
1515

1616
# Ensuring the Atlas credentials Secret
1717
kubectl delete secrets my-atlas-key --ignore-not-found -n "${ns}"

config/default/manager_auth_proxy_patch.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,4 @@ spec:
2323
args:
2424
- "--metrics-addr=127.0.0.1:8080"
2525
- "--enable-leader-election"
26+
- --atlas-domain=https://cloud.mongodb.com

config/dev/kustomization.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
3+
resources:
4+
- ../default
5+
6+
patchesStrategicMerge:
7+
- manager_configuration.yaml

config/dev/manager_configuration.yaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# This patch configures the container with production configuration values
2+
apiVersion: apps/v1
3+
kind: Deployment
4+
metadata:
5+
name: controller-manager
6+
namespace: system
7+
spec:
8+
template:
9+
spec:
10+
containers:
11+
- name: manager
12+
args:
13+
- --atlas-domain=https://cloud-qa.mongodb.com
14+
- --metrics-addr=127.0.0.1:8080 # is there any way to reuse the properties that are set by /default overlay?
15+
- --enable-leader-election

config/manager/manager.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ spec:
2222
labels:
2323
control-plane: controller-manager
2424
spec:
25+
securityContext:
26+
runAsNonRoot: true
27+
runAsUser: 2000
2528
containers:
2629
- command:
2730
- /manager

main.go

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,7 @@ func init() {
4747
}
4848

4949
func main() {
50-
var metricsAddr string
51-
var enableLeaderElection bool
52-
flag.StringVar(&metricsAddr, "metrics-addr", ":8080", "The address the metric endpoint binds to.")
53-
flag.BoolVar(&enableLeaderElection, "enable-leader-election", false,
54-
"Enable leader election for controller manager. "+
55-
"Enabling this will ensure there is only one active controller manager.")
56-
flag.Parse()
50+
config := parseConfiguration()
5751

5852
// controller-runtime/pkg/log/zap is a wrapper over zap that implements logr
5953
// logr looks quite limited in functionality so we better use Zap directly.
@@ -65,9 +59,9 @@ func main() {
6559

6660
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
6761
Scheme: scheme,
68-
MetricsBindAddress: metricsAddr,
62+
MetricsBindAddress: config.MetricsAddr,
6963
Port: 9443,
70-
LeaderElection: enableLeaderElection,
64+
LeaderElection: config.EnableLeaderElection,
7165
LeaderElectionID: "06d035fb.mongodb.com",
7266
})
7367
if err != nil {
@@ -76,18 +70,20 @@ func main() {
7670
}
7771

7872
if err = (&atlascluster.AtlasClusterReconciler{
79-
Client: mgr.GetClient(),
80-
Log: logger.Named("controllers").Named("AtlasCluster").Sugar(),
81-
Scheme: mgr.GetScheme(),
73+
Client: mgr.GetClient(),
74+
Log: logger.Named("controllers").Named("AtlasCluster").Sugar(),
75+
Scheme: mgr.GetScheme(),
76+
AtlasDomain: config.AtlasDomain,
8277
}).SetupWithManager(mgr); err != nil {
8378
setupLog.Error(err, "unable to create controller", "controller", "AtlasCluster")
8479
os.Exit(1)
8580
}
8681

8782
if err = (&atlasproject.AtlasProjectReconciler{
88-
Client: mgr.GetClient(),
89-
Log: logger.Named("controllers").Named("AtlasProject").Sugar(),
90-
Scheme: mgr.GetScheme(),
83+
Client: mgr.GetClient(),
84+
Log: logger.Named("controllers").Named("AtlasProject").Sugar(),
85+
Scheme: mgr.GetScheme(),
86+
AtlasDomain: config.AtlasDomain,
9187
}).SetupWithManager(mgr); err != nil {
9288
setupLog.Error(err, "unable to create controller", "controller", "AtlasProject")
9389
os.Exit(1)
@@ -100,3 +96,22 @@ func main() {
10096
os.Exit(1)
10197
}
10298
}
99+
100+
type Config struct {
101+
AtlasDomain string
102+
EnableLeaderElection bool
103+
MetricsAddr string
104+
}
105+
106+
// ParseConfiguration fills the 'OperatorConfig' from the flags passed to the program
107+
func parseConfiguration() Config {
108+
config := Config{}
109+
flag.StringVar(&config.AtlasDomain, "atlas-domain", "https://cloud.mongodb.com", "the Atlas URL domain name (no slash in the end).")
110+
flag.StringVar(&config.MetricsAddr, "metrics-addr", ":8080", "The address the metric endpoint binds to.")
111+
flag.BoolVar(&config.EnableLeaderElection, "enable-leader-election", false,
112+
"Enable leader election for controller manager. "+
113+
"Enabling this will ensure there is only one active controller manager.")
114+
115+
flag.Parse()
116+
return config
117+
}

pkg/controller/atlas/client.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,15 @@ var userAgent = fmt.Sprintf("%s/%s (%s;%s)", "MongoDBAtlasKubernetesOperator", "
1515

1616
// Client is the central place to create a client for Atlas using specified API keys and a server URL.
1717
// Note, that the default HTTP transport is reused globally by Go so all caching, keep-alive etc will be in action.
18-
func Client(connection Connection, log *zap.SugaredLogger) (*mongodbatlas.Client, error) {
18+
func Client(atlasDomain string, connection Connection, log *zap.SugaredLogger) (*mongodbatlas.Client, error) {
1919
withDigest := httputil.Digest(connection.PublicKey, connection.PrivateKey)
2020
withLogging := httputil.LoggingTransport(log)
2121

2222
httpClient, err := httputil.DecorateClient(basicClient(), withDigest, withLogging)
2323
if err != nil {
2424
return nil, err
2525
}
26-
// TODO configuration for base URL (as a global Operator config?)
27-
client, err := mongodbatlas.New(httpClient, mongodbatlas.SetBaseURL("https://cloud-qa.mongodb.com/api/atlas/v1.0/"))
26+
client, err := mongodbatlas.New(httpClient, mongodbatlas.SetBaseURL(atlasDomain+"/api/atlas/v1.0/"))
2827
if err != nil {
2928
return nil, err
3029
}

pkg/controller/atlascluster/atlascluster_controller.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,10 @@ import (
4040

4141
// AtlasClusterReconciler reconciles an AtlasCluster object
4242
type AtlasClusterReconciler struct {
43-
Client client.Client
44-
Log *zap.SugaredLogger
45-
Scheme *runtime.Scheme
43+
Client client.Client
44+
Log *zap.SugaredLogger
45+
Scheme *runtime.Scheme
46+
AtlasDomain string
4647
}
4748

4849
// +kubebuilder:rbac:groups=atlas.mongodb.com,resources=atlasclusters,verbs=get;list;watch;create;update;patch;delete
@@ -74,7 +75,7 @@ func (r *AtlasClusterReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error
7475
return result.ReconcileResult(), nil
7576
}
7677

77-
c, result := ensureClusterState(log, connection, project, cluster)
78+
c, result := r.ensureClusterState(log, connection, project, cluster)
7879
if c != nil && c.StateName != "" {
7980
ctx.EnsureStatusOption(status.AtlasClusterStateNameOption(c.StateName))
8081
}
@@ -135,7 +136,7 @@ func (r *AtlasClusterReconciler) Delete(obj runtime.Object) error {
135136
return errors.New("cannot read Atlas connection")
136137
}
137138

138-
atlasClient, err := atlas.Client(connection, log)
139+
atlasClient, err := atlas.Client(r.AtlasDomain, connection, log)
139140
if err != nil {
140141
return fmt.Errorf("cannot build Atlas client: %w", err)
141142
}

pkg/controller/atlascluster/cluster.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ import (
1515
"go.uber.org/zap"
1616
)
1717

18-
func ensureClusterState(log *zap.SugaredLogger, connection atlas.Connection, project *mdbv1.AtlasProject, cluster *mdbv1.AtlasCluster) (c *mongodbatlas.Cluster, _ workflow.Result) {
18+
func (r *AtlasClusterReconciler) ensureClusterState(log *zap.SugaredLogger, connection atlas.Connection, project *mdbv1.AtlasProject, cluster *mdbv1.AtlasCluster) (c *mongodbatlas.Cluster, _ workflow.Result) {
1919
ctx := context.Background()
2020

21-
client, err := atlas.Client(connection, log)
21+
client, err := atlas.Client(r.AtlasDomain, connection, log)
2222
if err != nil {
2323
return c, workflow.Terminate(workflow.Internal, err.Error())
2424
}

pkg/controller/atlasproject/atlasproject_controller.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@ import (
3333
// AtlasProjectReconciler reconciles a AtlasProject object
3434
type AtlasProjectReconciler struct {
3535
client.Client
36-
Log *zap.SugaredLogger
37-
Scheme *runtime.Scheme
36+
Log *zap.SugaredLogger
37+
Scheme *runtime.Scheme
38+
AtlasDomain string
3839
}
3940

4041
// +kubebuilder:rbac:groups=atlas.mongodb.com,resources=atlasprojects,verbs=get;list;watch;create;update;patch;delete
@@ -68,7 +69,7 @@ func (r *AtlasProjectReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error
6869
}
6970

7071
var projectID string
71-
if projectID, result = ensureProjectExists(ctx, connection, project); !result.IsOk() {
72+
if projectID, result = r.ensureProjectExists(ctx, connection, project); !result.IsOk() {
7273
ctx.SetConditionFromResult(status.ProjectReadyType, result)
7374
return result.ReconcileResult(), nil
7475
}

pkg/controller/atlasproject/project.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import (
1111
)
1212

1313
// ensureProjectExists creates the project if it doesn't exist yet. Returns the project ID
14-
func ensureProjectExists(ctx *workflow.Context, connection atlas.Connection, project *mdbv1.AtlasProject) (string, workflow.Result) {
15-
client, err := atlas.Client(connection, ctx.Log)
14+
func (r *AtlasProjectReconciler) ensureProjectExists(ctx *workflow.Context, connection atlas.Connection, project *mdbv1.AtlasProject) (string, workflow.Result) {
15+
client, err := atlas.Client(r.AtlasDomain, connection, ctx.Log)
1616
if err != nil {
1717
return "", workflow.Terminate(workflow.Internal, err.Error())
1818
}

test/int/integration_suite_test.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,16 @@ var _ = BeforeSuite(func(done Done) {
9292
Expect(err).ToNot(HaveOccurred())
9393

9494
err = (&atlasproject.AtlasProjectReconciler{
95-
Client: k8sManager.GetClient(),
96-
Log: logger.Named("controllers").Named("AtlasProject").Sugar(),
95+
Client: k8sManager.GetClient(),
96+
Log: logger.Named("controllers").Named("AtlasProject").Sugar(),
97+
AtlasDomain: "https://cloud-qa.mongodb.com",
9798
}).SetupWithManager(k8sManager)
9899
Expect(err).ToNot(HaveOccurred())
99100

100101
err = (&atlascluster.AtlasClusterReconciler{
101-
Client: k8sManager.GetClient(),
102-
Log: logger.Named("controllers").Named("AtlasCluster").Sugar(),
102+
Client: k8sManager.GetClient(),
103+
Log: logger.Named("controllers").Named("AtlasCluster").Sugar(),
104+
AtlasDomain: "https://cloud-qa.mongodb.com",
103105
}).SetupWithManager(k8sManager)
104106
Expect(err).ToNot(HaveOccurred())
105107

0 commit comments

Comments
 (0)