Skip to content

Commit

Permalink
Merge pull request #305 from ricardobranco777/novault
Browse files Browse the repository at this point in the history
Rename vault_namespace to namespace
  • Loading branch information
asmorodskyi authored Aug 31, 2023
2 parents 1759a6b + ba89a23 commit 70fe67c
Show file tree
Hide file tree
Showing 15 changed files with 74 additions and 52 deletions.
8 changes: 4 additions & 4 deletions ocw/lib/aks.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ class AKS(Provider):
__instances = {}
default_region: str = 'eu-central-1'

def __new__(cls, vault_namespace):
if vault_namespace not in AKS.__instances:
AKS.__instances[vault_namespace] = self = object.__new__(cls)
def __new__(cls, namespace):
if namespace not in AKS.__instances:
AKS.__instances[namespace] = self = object.__new__(cls)
self.__kubectl_client = {}
return AKS.__instances[vault_namespace]
return AKS.__instances[namespace]

def subscription(self) -> str:
return self.get_data('subscription_id')
Expand Down
8 changes: 4 additions & 4 deletions ocw/lib/azure.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ def __init__(self, namespace: str):
self.check_credentials()
self.__gallery = PCWConfig.get_feature_property('cleanup', 'azure-gallery-name', namespace)

def __new__(cls, vault_namespace: str) -> 'Azure':
if vault_namespace not in Azure.__instances:
Azure.__instances[vault_namespace] = self = object.__new__(cls)
def __new__(cls, namespace: str) -> 'Azure':
if namespace not in Azure.__instances:
Azure.__instances[namespace] = self = object.__new__(cls)
self.__compute_mgmt_client = None
self.__sp_credentials = None
self.__resource_mgmt_client = None
self.__blob_service_client = None
return Azure.__instances[vault_namespace]
return Azure.__instances[namespace]

def subscription(self) -> str:
return self.get_data('subscription_id')
Expand Down
24 changes: 12 additions & 12 deletions ocw/lib/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
def save_or_update_instance(csp_data: dict) -> None:
provider = csp_data['provider']
namespace = csp_data['namespace']
if Instance.objects.filter(provider=provider, instance_id=csp_data['id'], vault_namespace=namespace).exists():
if Instance.objects.filter(provider=provider, instance_id=csp_data['id'], namespace=namespace).exists():
logger.debug("[%s] Update instance %s:%s", namespace, provider, csp_data['id'])
local_instance = Instance.objects.get(provider=provider, instance_id=csp_data['id'], vault_namespace=namespace)
local_instance = Instance.objects.get(provider=provider, instance_id=csp_data['id'], namespace=namespace)
if local_instance.region != csp_data['region']:
logger.info("[%s] Instance %s:%s changed region from %s to %s", namespace,
provider, csp_data['id'], local_instance.region, csp_data['region'])
Expand All @@ -38,7 +38,7 @@ def save_or_update_instance(csp_data: dict) -> None:
logger.debug("[%s] Create instance %s:%s", namespace, provider, csp_data['id'])
local_instance = Instance(
provider=provider,
vault_namespace=namespace,
namespace=namespace,
first_seen=csp_data['first_seen'],
instance_id=csp_data['id'],
ttl=timedelta(seconds=int(csp_data['tags'].get('openqa_ttl', csp_data['default_ttl']))),
Expand Down Expand Up @@ -107,7 +107,7 @@ def gce_extract_data(csp_instance, namespace: str, default_ttl: int) -> dict:


def _update_provider(provider: str, namespace: str, default_ttl: int) -> None:
instance_cnt = Instance.objects.filter(provider=provider, vault_namespace=namespace).update(active=False)
instance_cnt = Instance.objects.filter(provider=provider, namespace=namespace).update(active=False)
logger.debug("%d got active state false", instance_cnt)
if ProviderChoice.from_str(provider) == ProviderChoice.AZURE:
instances = Azure(namespace).list_resource_groups()
Expand All @@ -129,7 +129,7 @@ def _update_provider(provider: str, namespace: str, default_ttl: int) -> None:
for i in instances:
save_or_update_instance(gce_extract_data(i, namespace, default_ttl))
logger.info("%d instances from GCE successfully processed", len(instances))
Instance.objects.filter(provider=provider, vault_namespace=namespace,
Instance.objects.filter(provider=provider, namespace=namespace,
active=False).update(state=StateChoice.DELETED)


Expand Down Expand Up @@ -166,11 +166,11 @@ def update_run() -> None:

def delete_instance(instance: type[Instance]) -> None:
if instance.provider == ProviderChoice.AZURE:
Azure(instance.vault_namespace).delete_resource(instance.instance_id)
Azure(instance.namespace).delete_resource(instance.instance_id)
elif instance.provider == ProviderChoice.EC2:
EC2(instance.vault_namespace).delete_instance(instance.region, instance.instance_id)
EC2(instance.namespace).delete_instance(instance.region, instance.instance_id)
elif instance.provider == ProviderChoice.GCE:
GCE(instance.vault_namespace).delete_instance(instance.instance_id, instance.region)
GCE(instance.namespace).delete_instance(instance.instance_id, instance.region)
else:
raise NotImplementedError(
f"Provider({instance.provider}).delete() isn't implemented")
Expand All @@ -183,22 +183,22 @@ def auto_delete_instances() -> None:
for namespace in PCWConfig.get_namespaces_for('default'):
logger.debug("Running auto_delete_instances for %s", namespace)
instances = [
i for i in Instance.objects.filter(state=StateChoice.ACTIVE, vault_namespace=namespace).exclude(ignore=True)
i for i in Instance.objects.filter(state=StateChoice.ACTIVE, namespace=namespace).exclude(ignore=True)
if i.ttl_expired() or i.is_cancelled()
]
logger.debug("Found %d instances for deletion", len(instances))
email_text = set()
for instance in instances:
if instance.ttl_expired():
logger.debug("[%s] TTL expired for instance %s:%s %s", instance.vault_namespace,
logger.debug("[%s] TTL expired for instance %s:%s %s", instance.namespace,
instance.provider, instance.instance_id, instance.all_time_fields())
else:
logger.debug("[%s] Job cancelled for instance %s:%s %s", instance.vault_namespace,
logger.debug("[%s] Job cancelled for instance %s:%s %s", instance.namespace,
instance.provider, instance.instance_id, instance.all_time_fields())
try:
delete_instance(instance)
except Exception:
msg = f"[{instance.vault_namespace}] Deleting instance ({instance.provider}:{instance.instance_id}) failed"
msg = f"[{instance.namespace}] Deleting instance ({instance.provider}:{instance.instance_id}) failed"
logger.exception(msg)
email_text.add(f"{msg}\n\n{traceback.format_exc()}")

Expand Down
8 changes: 4 additions & 4 deletions ocw/lib/ec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ def __init__(self, namespace: str):
else:
self.all_regions = self.get_all_regions()

def __new__(cls, vault_namespace: str):
if vault_namespace not in EC2.__instances:
EC2.__instances[vault_namespace] = self = object.__new__(cls)
def __new__(cls, namespace: str):
if namespace not in EC2.__instances:
EC2.__instances[namespace] = self = object.__new__(cls)
self.__ec2_client = {}
self.__ec2_resource = {}
self.__secret = None
self.__key = None

return EC2.__instances[vault_namespace]
return EC2.__instances[namespace]

def check_credentials(self) -> None:
self.__secret = self.get_data('secret_access_key')
Expand Down
8 changes: 4 additions & 4 deletions ocw/lib/eks.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ class EKS(Provider):
default_region: str = 'eu-central-1'
__cluster_regions = []

def __new__(cls, vault_namespace):
if vault_namespace not in EKS.__instances:
EKS.__instances[vault_namespace] = self = object.__new__(cls)
def __new__(cls, namespace):
if namespace not in EKS.__instances:
EKS.__instances[namespace] = self = object.__new__(cls)
self.__eks_client = {}
self.__kubectl_client = {}
self.__aws_dir = None

return EKS.__instances[vault_namespace]
return EKS.__instances[namespace]

def __init__(self, namespace: str):
super().__init__(namespace)
Expand Down
4 changes: 2 additions & 2 deletions ocw/lib/emailnotify.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def draw_instance_table(objects):
obj.provider,
obj.instance_id,
obj.cspinfo.get_tag('openqa_created_by', 'N/A'),
obj.vault_namespace,
obj.namespace,
obj.age_formatted(),
build_absolute_uri(reverse(views.delete, args=[obj.id])),
"" if link is None else link['url']
Expand All @@ -40,7 +40,7 @@ def send_leftover_notification():
# Handle namespaces
for namespace in PCWConfig.get_namespaces_for('notify'):
receiver_email = PCWConfig.get_feature_property('notify', 'to', namespace)
namespace_objects = all_instances.filter(vault_namespace=namespace)
namespace_objects = all_instances.filter(namespace=namespace)
if namespace_objects.filter(notified=False).count() > 0 and receiver_email:
send_mail(f'CSP left overs - {namespace}',
body_prefix + draw_instance_table(namespace_objects), receiver_email=receiver_email)
Expand Down
8 changes: 4 additions & 4 deletions ocw/lib/gce.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ class GCE(Provider):
__instances = {}
__skip_networks = frozenset({"default"})

def __new__(cls, vault_namespace):
if vault_namespace not in GCE.__instances:
GCE.__instances[vault_namespace] = object.__new__(cls)
return GCE.__instances[vault_namespace]
def __new__(cls, namespace):
if namespace not in GCE.__instances:
GCE.__instances[namespace] = object.__new__(cls)
return GCE.__instances[namespace]

def __init__(self, namespace):
super().__init__(namespace)
Expand Down
8 changes: 4 additions & 4 deletions ocw/lib/gke.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
class GKE(GCE):
__instances = {}

def __new__(cls, vault_namespace):
if vault_namespace not in GKE.__instances:
GKE.__instances[vault_namespace] = self = object.__new__(cls)
def __new__(cls, namespace):
if namespace not in GKE.__instances:
GKE.__instances[namespace] = self = object.__new__(cls)
self.__gke_client = None
self.__kubectl_client = {}

return GKE.__instances[vault_namespace]
return GKE.__instances[namespace]

def gke_client(self):
if self.__gke_client is None:
Expand Down
8 changes: 4 additions & 4 deletions ocw/lib/openstack.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ def __init__(self, namespace: str):
super().__init__(namespace)
self.client()

def __new__(cls, vault_namespace: str):
if vault_namespace not in Openstack.__instances:
Openstack.__instances[vault_namespace] = self = object.__new__(cls)
def __new__(cls, namespace: str):
if namespace not in Openstack.__instances:
Openstack.__instances[namespace] = self = object.__new__(cls)
self.__client = None
return Openstack.__instances[vault_namespace]
return Openstack.__instances[namespace]

def client(self) -> None:
if self.__client is None:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Generated by Django 4.2.4 on 2023-08-31 10:55

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('ocw', '0011_alter_instance_provider'),
]

operations = [
migrations.RenameField(
model_name='instance',
old_name='vault_namespace',
new_name='namespace',
),
migrations.AlterUniqueTogether(
name='instance',
unique_together={('provider', 'instance_id', 'namespace')},
),
]
4 changes: 2 additions & 2 deletions ocw/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Instance(models.Model):
help_text='Local computed state of that Instance')
instance_id = models.CharField(max_length=200)
region = models.CharField(max_length=64, default='')
vault_namespace = models.CharField(max_length=64, default='')
namespace = models.CharField(max_length=64, default='')
notified = models.BooleanField(default=False)
ignore = models.BooleanField(default=False)
TAG_IGNORE = 'pcw_ignore'
Expand Down Expand Up @@ -81,7 +81,7 @@ def is_cancelled(self) -> bool:
return False

class Meta: # pylint: disable=too-few-public-methods
unique_together = (('provider', 'instance_id', 'vault_namespace'),)
unique_together = (('provider', 'instance_id', 'namespace'),)


class CspInfo(models.Model):
Expand Down
2 changes: 1 addition & 1 deletion ocw/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def instance_json(request):
"ttl",
"instance_id",
"region",
"vault_namespace",
"namespace",
"ignore"
),
)
Expand Down
2 changes: 1 addition & 1 deletion tests/generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def generate_model_instance(jobid_tag, created_by_tag):
cspinfo=CspInfo(tags=json_dump_tags, type=fake.uuid4()),
provider=fake.uuid4(),
instance_id=fake.uuid4(),
vault_namespace=fake.uuid4()
namespace=fake.uuid4()
)


Expand Down
10 changes: 5 additions & 5 deletions tests/test_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def __init__(self, provider):
self.region = None
self.save_called = False
self.provider = provider
self.vault_namespace = fake.uuid4()
self.namespace = fake.uuid4()

def save(self):
self.save_called = True
Expand Down Expand Up @@ -69,7 +69,7 @@ def extract_data():

@pytest.fixture
def azure_fixture(monkeypatch, extract_data):
monkeypatch.setattr(Azure, '__new__', lambda cls, vault_namespace: AzureMock())
monkeypatch.setattr(Azure, '__new__', lambda cls, namespace: AzureMock())
return extract_data


Expand Down Expand Up @@ -219,7 +219,7 @@ def mocked_send_mail(arg1, arg2):


def test_delete_instances_azure(monkeypatch):
monkeypatch.setattr(Azure, '__new__', lambda cls, vault_namespace: AzureMock())
monkeypatch.setattr(Azure, '__new__', lambda cls, namespace: AzureMock())

instance = InstanceMock(ProviderChoice.AZURE)

Expand All @@ -230,7 +230,7 @@ def test_delete_instances_azure(monkeypatch):


def test_delete_instances_ec2(monkeypatch):
monkeypatch.setattr(EC2, '__new__', lambda cls, vault_namespace: EC2Mock())
monkeypatch.setattr(EC2, '__new__', lambda cls, namespace: EC2Mock())

instance = InstanceMock(ProviderChoice.EC2)

Expand All @@ -241,7 +241,7 @@ def test_delete_instances_ec2(monkeypatch):


def test_delete_instances_gce(monkeypatch):
monkeypatch.setattr(GCE, '__new__', lambda cls, vault_namespace: GCEMock())
monkeypatch.setattr(GCE, '__new__', lambda cls, namespace: GCEMock())

instance = InstanceMock(ProviderChoice.GCE)

Expand Down
2 changes: 1 addition & 1 deletion tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def example_instance_data():
'last_seen': datetime(2023, 1, 2, tzinfo=timezone.utc),
'instance_id': 'INSTANCE_ID',
'region': 'REGION',
'vault_namespace': 'VAULT_NAMESPACE',
'namespace': 'NAMESPACE',
}


Expand Down

0 comments on commit 70fe67c

Please sign in to comment.