Skip to content

Commit

Permalink
Merge pull request #28 from vnsamsonov/main
Browse files Browse the repository at this point in the history
Сhange checking mutation
  • Loading branch information
vnsamsonov authored Aug 10, 2023
2 parents 2cea9ef + efcc1bd commit 0f2b545
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,19 @@ def is_kk_conn_used_by_obj(annotations: dict) -> bool:
return has_required_annotations

@staticmethod
def containers_contain_required_envs(spec: dict) -> bool:
def any_containers_contain_required_envs(spec: dict) -> bool:
all_containers = chain(
spec.get("containers", []),
spec.get("initContainers", [])
)

required_envs = set(env for env, _ in specifications.KEYCLOAK_VAR_NAMES)

for container in all_containers:
for env_name, _ in specifications.KEYCLOAK_VAR_NAMES:
envs = [e.get("name") for e in container.get("env", {})]
if env_name not in envs:
return False
return True
envs = set(e.get("name") for e in container.get("env", []))
if (envs & required_envs) == required_envs:
return True
return False

def on_create_deployment(self, ms_kk_conn: KeycloakConnectorMicroserviceDto):
kk_connector = KubernetesService.get_keycloak_connector(ms_kk_conn.keycloak_instance_name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,19 @@ def is_pg_conn_used_by_object(annotations: dict) -> bool:
return all(annotation_name in annotations for annotation_name in PG_CON_REQUIRED_ANNOTATION_NAMES)

@staticmethod
def containers_contain_required_envs(spec: dict) -> bool:
def any_containers_contain_required_envs(spec: dict) -> bool:
all_containers = chain(
spec.get("containers", []),
spec.get("initContainers", [])
)

required_envs = set(env for env, _ in specifications.DATABASE_VAR_NAMES)

for container in all_containers:
for env_name, _ in specifications.DATABASE_VAR_NAMES:
envs = [e.get("name") for e in container.get("env", {})]
if env_name not in envs:
return False
return True
envs = set(e.get("name") for e in container.get("env", []))
if (envs & required_envs) == required_envs:
return True
return False

def get_or_create_db_credentials(self, pg_instance_cred: PgConnectorInstanceSecretDto,
ms_pg_con: PgConnectorMicroserviceDto) -> PgConnectorDbSecretDto:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,19 @@ def is_rabbit_conn_used_by_object(annotations: dict) -> bool:
)

@staticmethod
def containers_contain_required_envs(spec: dict) -> bool:
def any_containers_contain_required_envs(spec: dict) -> bool:
all_containers = chain(
spec.get("containers", []),
spec.get("initContainers", [])
)

required_envs = set(env for env, _ in specifications.RABBIT_VAR_NAMES)

for container in all_containers:
for env_name, _ in specifications.RABBIT_VAR_NAMES:
envs = [e.get("name") for e in container.get("env", {})]
if env_name not in envs:
return False
return True
envs = set(e.get("name") for e in container.get("env", []))
if (envs & required_envs) == required_envs:
return True
return False

def get_or_create_rabbit_credentials(self, rabbit_api_cred: RabbitApiSecretDto,
ms_rabbit_con: RabbitConnectorMicroserviceDto) -> RabbitMsSecretDto:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,19 @@ def is_sentry_conn_used_by_object(annotations: dict, labels: dict) -> bool:
return has_required_annotations and has_required_labels

@staticmethod
def containers_contain_required_envs(spec: dict) -> bool:
def any_containers_contain_required_envs(spec: dict) -> bool:
all_containers = chain(
spec.get("containers", []),
spec.get("initContainers", [])
)

required_envs = set(env for env, _ in specifications.SENTRY_VAR_NAMES)

for container in all_containers:
for env_name, _ in specifications.SENTRY_VAR_NAMES:
envs = [e.get("name") for e in container.get("env", {})]
if env_name not in envs:
return False
return True
envs = set(e.get("name") for e in container.get("env", []))
if (envs & required_envs) == required_envs:
return True
return False

def on_create_deployment(self, ms_sentry_conn: SentryConnectorMicroserviceDto):
sentry_connector = KubernetesService.get_sentry_connector(ms_sentry_conn.sentry_instance_name)
Expand Down
2 changes: 1 addition & 1 deletion k8s-itlabs-operator/operators/keycloak.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def check_creation(annotations, body, **_):
status.is_used = True
status.is_success = True
spec = body.get("spec", {})
if not KeycloakConnectorService.containers_contain_required_envs(spec):
if not KeycloakConnectorService.any_containers_contain_required_envs(spec):
kopf.event(
body,
type="Error",
Expand Down
10 changes: 1 addition & 9 deletions k8s-itlabs-operator/operators/postgresconnector.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,7 @@ def check_creation(annotations, body, new, **_):
status.is_used = True
status.is_success = True
spec = body.get("spec", {})
if not PostgresConnectorService.containers_contain_required_envs(spec):
# debug
logging.info(
f"Failure on check creation for Postgres:"
f"\n\t(annotations): {annotations}"
f"\n\t(body): {body}"
f"\n\t(new): {new}"
)

if not PostgresConnectorService.any_containers_contain_required_envs(spec):
kopf.event(
body,
type="Error",
Expand Down
2 changes: 1 addition & 1 deletion k8s-itlabs-operator/operators/rabbitconnector.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def check_creation(annotations, body, **_):
status.is_used = True
status.is_success = True
spec = body.get("spec", {})
if not RabbitConnectorService.containers_contain_required_envs(spec):
if not RabbitConnectorService.any_containers_contain_required_envs(spec):
kopf.event(
body,
type="Error",
Expand Down
11 changes: 1 addition & 10 deletions k8s-itlabs-operator/operators/sentry.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,7 @@ def check_creation(annotations, labels, body, new, **_):
status.is_used = True
status.is_success = True
spec = body.get("spec", {})
if not SentryConnectorService.containers_contain_required_envs(spec):
# debug
logging.info(
f"Failure on check creation for Sentry:"
f"\n\t(annotations): {annotations}"
f"\n\t(labels): {labels}"
f"\n\t(body): {body}"
f"\n\t(new): {new}"
)

if not SentryConnectorService.any_containers_contain_required_envs(spec):
kopf.event(
body,
type="Error",
Expand Down

0 comments on commit 0f2b545

Please sign in to comment.