Skip to content
This repository has been archived by the owner on Jul 9, 2019. It is now read-only.

Commit

Permalink
Merge pull request #16 from Ultimaker/k8s
Browse files Browse the repository at this point in the history
Fixing missing getOperatorAdminSecret method
  • Loading branch information
ChrisTerBeke authored Jun 13, 2018
2 parents fb34145 + 467b049 commit cda006d
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 11 deletions.
12 changes: 8 additions & 4 deletions mongoOperator/helpers/AdminSecretChecker.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ class AdminSecretChecker(BaseResourceChecker):
def getClusterName(cls, resource_name: str) -> str:
return resource_name.replace(cls.NAME_FORMAT.format(""), "")

@classmethod
def getSecretName(cls, cluster_name: str) -> str:
return cls.NAME_FORMAT.format(cluster_name)

@staticmethod
def _generateSecretData() -> Dict[str, str]:
"""Generates a root user with a random secure password to use in secrets."""
Expand All @@ -35,17 +39,17 @@ def listResources(self) -> List[T]:
return self.kubernetes_service.listAllSecretsWithLabels().items

def getResource(self, cluster_object: V1MongoClusterConfiguration) -> T:
name = self.NAME_FORMAT.format(cluster_object.metadata.name)
name = self.getSecretName(cluster_object.metadata.name)
return self.kubernetes_service.getSecret(name, cluster_object.metadata.namespace)

def createResource(self, cluster_object: V1MongoClusterConfiguration) -> T:
name = self.NAME_FORMAT.format(cluster_object.metadata.name)
name = self.getSecretName(cluster_object.metadata.name)
return self.kubernetes_service.createSecret(name, cluster_object.metadata.namespace, self._generateSecretData())

def updateResource(self, cluster_object: V1MongoClusterConfiguration) -> T:
name = self.NAME_FORMAT.format(cluster_object.metadata.name)
name = self.getSecretName(cluster_object.metadata.name)
return self.kubernetes_service.updateSecret(name, cluster_object.metadata.namespace, self._generateSecretData())

def deleteResource(self, cluster_name: str, namespace: str) -> V1Status:
secret_name = self.NAME_FORMAT.format(cluster_name)
secret_name = self.getSecretName(cluster_name)
return self.kubernetes_service.deleteSecret(secret_name, namespace)
4 changes: 3 additions & 1 deletion mongoOperator/services/MongoService.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from kubernetes.client.rest import ApiException

from mongoOperator.helpers.AdminSecretChecker import AdminSecretChecker
from mongoOperator.helpers.MongoResources import MongoResources
from mongoOperator.models.V1MongoClusterConfiguration import V1MongoClusterConfiguration
from mongoOperator.services.KubernetesService import KubernetesService
Expand Down Expand Up @@ -150,7 +151,8 @@ def createUsers(self, cluster_object: V1MongoClusterConfiguration) -> None:
namespace = cluster_object.metadata.namespace
replicas = cluster_object.spec.mongodb.replicas

admin_credentials = self.kubernetes_service.getOperatorAdminSecret(cluster_name, namespace)
secret_name = AdminSecretChecker.getSecretName(cluster_name)
admin_credentials = self.kubernetes_service.getSecret(secret_name, namespace)
create_admin_command = MongoResources.createCreateAdminCommand(admin_credentials)

logging.info("Creating users for %s pods", replicas)
Expand Down
2 changes: 1 addition & 1 deletion tests/helpers/TestClusterChecker.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def test_checkCluster_new_version(self, check_mock, admin_mock, backup_mock):
self.assertEqual({("mongo-cluster", "default"): "100"}, self.checker.cluster_versions)
expected = [call.execInPod('mongodb', 'mongo-cluster-0', 'default',
['mongo', 'localhost:27017/admin', '--eval', 'rs.status()']),
call.getOperatorAdminSecret('mongo-cluster', 'default'),
call.getSecret('mongo-cluster-admin-credentials', 'default'),
call.execInPod('mongodb', 'mongo-cluster-0', 'default', [
'mongo', 'localhost:27017/admin', '--eval', admin_mock.return_value
])]
Expand Down
10 changes: 5 additions & 5 deletions tests/services/TestMongoService.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class TestMongoService(TestCase):
def setUp(self):
super().setUp()
self.kubernetes_service: Union[MagicMock, KubernetesService] = MagicMock()
self.kubernetes_service.getOperatorAdminSecret.return_value = V1Secret(
self.kubernetes_service.getSecret.return_value = V1Secret(
metadata=V1ObjectMeta(name="mongo-cluster-admin-credentials", namespace="default"),
data={"password": b64encode(b"random-password"), "username": b64encode(b"root")},
)
Expand Down Expand Up @@ -253,7 +253,7 @@ def test_createUsers_ok(self):

self.service.createUsers(self.cluster_object)
expected_calls = [
call.getOperatorAdminSecret('mongo-cluster', 'default'),
call.getSecret('mongo-cluster-admin-credentials', 'default'),
call.execInPod('mongodb', 'mongo-cluster-0', 'default',
['mongo', 'localhost:27017/admin', '--eval', self.expected_user_create])
]
Expand All @@ -265,7 +265,7 @@ def test_createUsers_ValueError(self):
with self.assertRaises(ValueError) as context:
self.service.createUsers(self.cluster_object)
expected_calls = [
call.getOperatorAdminSecret('mongo-cluster', 'default'),
call.getSecret('mongo-cluster-admin-credentials', 'default'),
call.execInPod('mongodb', 'mongo-cluster-0', 'default',
['mongo', 'localhost:27017/admin', '--eval', self.expected_user_create])
]
Expand All @@ -279,7 +279,7 @@ def test_createUsers_not_master_then_already_exists(self):

self.service.createUsers(self.cluster_object)
expected_calls = [
call.getOperatorAdminSecret('mongo-cluster', 'default'),
call.getSecret('mongo-cluster-admin-credentials', 'default'),
call.execInPod('mongodb', 'mongo-cluster-0', 'default',
['mongo', 'localhost:27017/admin', '--eval', self.expected_user_create]),
call.execInPod('mongodb', 'mongo-cluster-1', 'default',
Expand All @@ -292,7 +292,7 @@ def test_createUsers_TimeoutError(self):

with self.assertRaises(TimeoutError) as context:
self.service.createUsers(self.cluster_object)
expected_calls = [call.getOperatorAdminSecret('mongo-cluster', 'default')] + [
expected_calls = [call.getSecret('mongo-cluster-admin-credentials', 'default')] + [
call.execInPod('mongodb', 'mongo-cluster-' + str(pod), 'default',
['mongo', 'localhost:27017/admin', '--eval', self.expected_user_create])
for _ in range(4) for pod in range(3)
Expand Down

0 comments on commit cda006d

Please sign in to comment.