예제 #1
0
 def setUp(self):
     super().setUp()
     self.cluster = Cluster.load()
     self.obj1 = ClusterNodeFactory(cluster=self.cluster)
     self.obj2 = ClusterNodeFactory(cluster=self.cluster)
     self.gpu_obj1 = GPUFactory(cluster_node=self.obj1)
     self.gpu_obj2 = GPUFactory(cluster_node=self.obj2)
예제 #2
0
 def setUp(self):
     self.cluster = Cluster.load()
     self.admin = UserFactory(is_staff=True, is_superuser=True)
     self.user = UserFactory()
     self.publisher = PublishTrackerService()
     self.publisher.setup()
     super(PublishTrackerTest, self).setUp()
예제 #3
0
def update_system_info():
    k8s_manager = K8SManager(in_cluster=True)
    version_api = k8s_manager.get_version()
    cluster = Cluster.load()
    if cluster.version_api != version_api:
        cluster.version_api = version_api
        cluster.save()
        auditor.record(event_type=CLUSTER_UPDATED,
                       instance=cluster,
                       is_upgrade=settings.CHART_IS_UPGRADE)
예제 #4
0
 def setUp(self):
     self.cluster = Cluster.load()
     self.cluster_node = ClusterNodeFactory(cluster=self.cluster)
     self.node_gpu = GPUFactory(cluster_node=self.cluster_node)
     auditor.validate()
     auditor.setup()
     tracker.validate()
     tracker.setup()
     activitylogs.validate()
     activitylogs.setup()
     super(AuditorClusterTest, self).setUp()
예제 #5
0
 def get_cluster_or_wait(self, log_sleep_interval):
     max_trials = 10
     trials = 0
     while trials < max_trials:
         try:
             return Cluster.load()
         except (InterfaceError, ProgrammingError, OperationalError) as e:
             monitor.logger.exception("Database is not synced yet %s\n", e)
             trials += 1
             time.sleep(log_sleep_interval * 2)
     return None
예제 #6
0
    def get_cluster_id(self):
        if self.cluster_id:
            return self.cluster_id

        from db.models.clusters import Cluster
        try:
            cluster_uuid = Cluster.load().uuid.hex
            self.cluster_id = cluster_uuid
        except (Cluster.DoesNotExist, InterfaceError, ProgrammingError, OperationalError):
            pass
        return self.cluster_id
예제 #7
0
def update_system_nodes():
    k8s_manager = K8SManager(in_cluster=True)
    nodes = k8s_manager.list_nodes()
    cluster = Cluster.load()
    nodes_to_update = {}
    nodes_to_create = {node.metadata.name: node for node in nodes}
    deprecated_nodes = []
    for node in cluster.nodes.all():
        if node.name in nodes_to_create:
            nodes_to_update[node.name] = (node, nodes_to_create.pop(node.name))
        else:
            deprecated_nodes.append(node)

    cluster_updated = False
    for node in deprecated_nodes:
        node.is_current = False
        node.save()
        cluster_updated = True
        auditor.record(event_type=CLUSTER_NODE_DELETED, instance=node)

    for node in nodes_to_create.values():
        node_dict = ClusterNode.from_node_item(node)
        node_dict['cluster'] = cluster
        instance = ClusterNode.objects.create(**node_dict)
        cluster_updated = True
        auditor.record(event_type=CLUSTER_NODE_CREATED, instance=instance)

    for current_node, new_node in nodes_to_update.values():
        node_dict = ClusterNode.from_node_item(new_node)
        node_updated = False
        for k, v in node_dict.items():
            if v != getattr(current_node, k):
                setattr(current_node, k, v)
                node_updated = True
        if node_updated:
            current_node.save()
            cluster_updated = True
            auditor.record(event_type=CLUSTER_NODE_UPDATED, instance=current_node)

    if cluster_updated:
        cluster = get_cluster_resources()
        auditor.record(event_type=CLUSTER_RESOURCES_UPDATED,
                       instance=cluster,
                       n_nodes=cluster.n_nodes,
                       memory=round(cluster.memory / (1000 ** 3), 2),
                       n_cpus=cluster.n_cpus,
                       n_gpus=cluster.n_gpus)
예제 #8
0
    def test_patch(self):
        secret = K8SSecretFactory()

        data = {
            'name': 'foo',
            'description': 'new description',
            'tags': ['foo', 'bar'],
            'host': 'https://foo.bar',
            'k8s_secret': secret.id,
        }
        assert self.object.name != data['name']
        assert self.object.description != data['description']
        assert self.object.tags != data['tags']
        assert self.object.host != data['host']
        assert self.object.k8s_secret != data['k8s_secret']

        resp = self.auth_client.patch(self.url, data=data)
        assert resp.status_code == status.HTTP_200_OK
        new_object = self.model_class.objects.get(id=self.object.id)
        assert new_object.name == data['name']
        assert new_object.description == data['description']
        assert set(new_object.tags) == set(data['tags'])
        assert new_object.host == data['host']
        assert new_object.k8s_secret.id == data['k8s_secret']

        # Path an access without a host
        new_secret = K8SSecretFactory()
        last_object = RegistryAccess.objects.create(owner=self.object.owner,
                                                    name='my_registry',
                                                    k8s_secret=secret)
        assert last_object.owner.owner == Cluster.load()
        assert last_object.name == 'my_registry'
        assert last_object.description is None
        assert last_object.tags is None
        assert last_object.host == ''
        assert last_object.k8s_secret.id == data['k8s_secret']
        url = self.get_access_url(last_object.name)

        # Patch
        data = {
            'k8s_secret': new_secret.id,
        }
        resp = self.auth_client.patch(url, data=data)
        assert resp.status_code == status.HTTP_200_OK
        new_object = self.model_class.objects.get(id=last_object.id)
        assert new_object.name == 'my_registry'
        assert new_object.description is None
        assert new_object.tags is None
        assert new_object.host == ''
        assert new_object.k8s_secret.id == data['k8s_secret']

        # Patch
        data = {
            'tags': ['foo', 'bar'],
        }
        resp = self.auth_client.patch(url, data=data)
        assert resp.status_code == status.HTTP_200_OK
        new_object = self.model_class.objects.get(id=last_object.id)
        assert new_object.name == 'my_registry'
        assert new_object.description is None
        assert set(new_object.tags) == set(data['tags'])
        assert new_object.host == ''
        assert new_object.k8s_secret.id == new_secret.id

        # Patch
        data = {'tags': ['foo', 'bar'], 'host': ''}
        resp = self.auth_client.patch(url, data=data)
        assert resp.status_code == status.HTTP_200_OK
        new_object = self.model_class.objects.get(id=last_object.id)
        assert new_object.name == 'my_registry'
        assert new_object.description is None
        assert set(new_object.tags) == set(data['tags'])
        assert new_object.host == ''
        assert new_object.k8s_secret.id == new_secret.id

        # Non admin
        resp = self.normal_client.patch(self.url, data=data)
        assert resp.status_code == status.HTTP_403_FORBIDDEN
예제 #9
0
def get_cluster_node(**kwargs):
    cluster = Cluster.load()
    return ClusterNodeFactory(cluster=cluster, **kwargs)
예제 #10
0
def get_gpu(**kwargs):
    if not kwargs.get('cluster_node'):
        cluster = Cluster.load()
        cluster['cluster_node'] = ClusterNodeFactory(cluster=cluster, **kwargs)

    return GPUFactory(**kwargs)
예제 #11
0
def get_cluster_node(**kwargs):
    cluster = Cluster.load()
    return ClusterNodeFactory(cluster=cluster, **kwargs)
예제 #12
0
 def setUp(self):
     super().setUp()
     self.owner = Owner.objects.get(name=Cluster.load().uuid)
     self.db_service = DummyDBService()
     self.db_service.option_manager = OptionManager()
     self.db_service.setup()
예제 #13
0
 def setUp(self):
     super().setUp()
     node = ClusterNodeFactory(cluster=Cluster.load())
     self.obj1 = self.factory_class(cluster_node=node)
     self.obj2 = self.factory_class(cluster_node=node)
예제 #14
0
 def test_loads_works(self):
     assert Cluster.load() is not None
예제 #15
0
 def get_node():
     cluster = Cluster.load()
     node = ClusterNode.objects.filter(cluster=cluster, name=settings.K8S_NODE_NAME)
     if node.exists():
         return node.first()
     return None
 def setUp(self):
     super().setUp()
     self.owner = Owner.objects.get(name=Cluster.load().uuid)
     self.cluster_options_handler = ClusterOptionsHandler()
예제 #17
0
 def get_object(self):
     return Cluster.load()
예제 #18
0
 def setUp(self):
     self.cluster = Cluster.load()
     self.cluster_node = ClusterNodeFactory(cluster=self.cluster)
     self.node_gpu = GPUFactory(cluster_node=self.cluster_node)
     super().setUp()
예제 #19
0
 def perform_create(self, serializer):
     serializer.save(cluster=Cluster.load())
예제 #20
0
    def test_create(self):

        data = {}
        resp = self.auth_client.post(self.url, data)
        assert resp.status_code == status.HTTP_400_BAD_REQUEST

        secret = K8SSecretFactory()

        # URL
        data = {
            'name': 'foo',
            'description': 'new description',
            'tags': ['foo', 'bar'],
            'host': 'https://foo.bar',
            'k8s_secret': secret.id,
        }
        resp = self.auth_client.post(self.url, data)
        assert resp.status_code == status.HTTP_201_CREATED
        assert self.model_class.objects.count() == self.num_objects + 1
        last_object = self.model_class.objects.last()
        assert last_object.owner.owner == Cluster.load()
        assert last_object.name == data['name']
        assert last_object.description == data['description']
        assert last_object.tags == data['tags']
        assert last_object.host == data['host']
        assert last_object.k8s_secret.id == data['k8s_secret']

        # No host
        data = {
            'name': 'foo-host',
            'description': 'new description',
            'tags': ['foo', 'bar'],
            'k8s_secret': secret.id,
        }
        resp = self.auth_client.post(self.url, data)
        assert resp.status_code == status.HTTP_201_CREATED
        assert self.model_class.objects.count() == self.num_objects + 2
        last_object = self.model_class.objects.last()
        assert last_object.owner.owner == Cluster.load()
        assert last_object.name == data['name']
        assert last_object.description == data['description']
        assert last_object.tags == data['tags']
        assert last_object.host == ''
        assert last_object.k8s_secret.id == data['k8s_secret']

        # IP
        data = {
            'name': 'foo2',
            'description': 'new description',
            'tags': ['foo', 'bar'],
            'host': '123.123.123.123:5000',
            'k8s_secret': secret.id,
        }
        resp = self.auth_client.post(self.url, data)
        assert resp.status_code == status.HTTP_201_CREATED
        assert self.model_class.objects.count() == self.num_objects + 3
        last_object = self.model_class.objects.last()
        assert last_object.owner.owner == Cluster.load()
        assert last_object.name == data['name']
        assert last_object.description == data['description']
        assert last_object.tags == data['tags']
        assert last_object.host == data['host']
        assert last_object.k8s_secret.id == data['k8s_secret']

        # Localhost
        data = {
            'name': 'foo3',
            'description': 'new description',
            'tags': ['foo', 'bar'],
            'host': 'localhost:5000',
            'k8s_secret': secret.id,
        }
        resp = self.auth_client.post(self.url, data)
        assert resp.status_code == status.HTTP_201_CREATED
        assert self.model_class.objects.count() == self.num_objects + 4
        last_object = self.model_class.objects.last()
        assert last_object.owner.owner == Cluster.load()
        assert last_object.name == data['name']
        assert last_object.description == data['description']
        assert last_object.tags == data['tags']
        assert last_object.host == data['host']
        assert last_object.k8s_secret.id == data['k8s_secret']

        # Non admin
        data = {}
        resp = self.normal_client.post(self.url, data)
        assert resp.status_code == status.HTTP_403_FORBIDDEN

        data = {
            'name': 'foo',
            'description': 'new description',
            'tags': ['foo', 'bar'],
            'host': 'https:foo.bar',
            'k8s_secret': secret.id,
        }
        resp = self.normal_client.post(self.url, data)
        assert resp.status_code == status.HTTP_403_FORBIDDEN
예제 #21
0
def get_gpu(**kwargs):
    if not kwargs.get('cluster_node'):
        cluster = Cluster.load()
        cluster['cluster_node'] = ClusterNodeFactory(cluster=cluster, **kwargs)

    return GPUFactory(**kwargs)
예제 #22
0
 def get_object(self):
     if self._object:
         return self._object
     self._object = Cluster.load()
     return self._object
예제 #23
0
 def setUp(self):
     super().setUp()
     self.owner = Cluster.get_or_create_owner(Cluster.load())
     self.db_service = DummyDBService()
     self.db_service.option_manager = OptionManager()
     self.db_service.setup()
예제 #24
0
 def setUp(self):
     super().setUp()
     self.cluster = Cluster.load()
     ClusterNodeFactory(cluster=self.cluster)
     ClusterNodeFactory(cluster=self.cluster)
예제 #25
0
 def get_owner(self):
     cluster = Cluster.load()
     return cluster.get_or_create_owner(cluster)
예제 #26
0
 def setUp(self):
     super().setUp()
     self.obj1 = self.factory_class(cluster=Cluster.load())
     self.obj2 = self.factory_class(cluster=Cluster.load())
예제 #27
0
 def get_owner(self):
     return Cluster.get_or_create_owner(Cluster.load())
예제 #28
0
 def setUp(self):
     super().setUp()
     self.owner = Cluster.get_or_create_owner(Cluster.load())
예제 #29
0
 def perform_create(self, serializer):
     serializer.save(cluster=Cluster.load())
예제 #30
0
 def get_owner(self):
     return Owner.objects.get(name=Cluster.load().uuid)
예제 #31
0
 def setUp(self):
     super().setUp()
     self.cluster = Cluster.load()
     ClusterNodeFactory(cluster=self.cluster)
     ClusterNodeFactory(cluster=self.cluster)
예제 #32
0
 def setUp(self):
     super().setUp()
     self.owner = Owner.objects.get(name=Cluster.load().uuid)
예제 #33
0
 def setUp(self):
     super().setUp()
     node = ClusterNodeFactory(cluster=Cluster.load())
     self.obj1 = self.factory_class(cluster_node=node)
     self.obj2 = self.factory_class(cluster_node=node)
예제 #34
0
def cluster(request):
    return {'cluster': Cluster.load()}
예제 #35
0
 def setUp(self):
     super().setUp()
     self.obj1 = self.factory_class(cluster=Cluster.load())
     self.obj2 = self.factory_class(cluster=Cluster.load())
예제 #36
0
 def setUp(self):
     Cluster.load()
     super().setUp()
예제 #37
0
def cluster(request):
    return {'cluster': Cluster.load()}