diff --git a/k8s-itlabs-operator/connectors/keycloak_connector/services/keycloak_connector.py b/k8s-itlabs-operator/connectors/keycloak_connector/services/keycloak_connector.py index 999d781..58ccb0c 100644 --- a/k8s-itlabs-operator/connectors/keycloak_connector/services/keycloak_connector.py +++ b/k8s-itlabs-operator/connectors/keycloak_connector/services/keycloak_connector.py @@ -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) diff --git a/k8s-itlabs-operator/connectors/postgres_connector/services/postgres_connector.py b/k8s-itlabs-operator/connectors/postgres_connector/services/postgres_connector.py index eeee12b..2194dac 100644 --- a/k8s-itlabs-operator/connectors/postgres_connector/services/postgres_connector.py +++ b/k8s-itlabs-operator/connectors/postgres_connector/services/postgres_connector.py @@ -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: diff --git a/k8s-itlabs-operator/connectors/rabbit_connector/services/rabbit_connector.py b/k8s-itlabs-operator/connectors/rabbit_connector/services/rabbit_connector.py index 3ef2a58..615f2b4 100644 --- a/k8s-itlabs-operator/connectors/rabbit_connector/services/rabbit_connector.py +++ b/k8s-itlabs-operator/connectors/rabbit_connector/services/rabbit_connector.py @@ -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: diff --git a/k8s-itlabs-operator/connectors/sentry_connector/services/sentry_connector.py b/k8s-itlabs-operator/connectors/sentry_connector/services/sentry_connector.py index 8128e29..4764c65 100644 --- a/k8s-itlabs-operator/connectors/sentry_connector/services/sentry_connector.py +++ b/k8s-itlabs-operator/connectors/sentry_connector/services/sentry_connector.py @@ -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) diff --git a/k8s-itlabs-operator/operators/keycloak.py b/k8s-itlabs-operator/operators/keycloak.py index 5da7125..a9ac734 100644 --- a/k8s-itlabs-operator/operators/keycloak.py +++ b/k8s-itlabs-operator/operators/keycloak.py @@ -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", diff --git a/k8s-itlabs-operator/operators/postgresconnector.py b/k8s-itlabs-operator/operators/postgresconnector.py index dfe511e..c32736a 100644 --- a/k8s-itlabs-operator/operators/postgresconnector.py +++ b/k8s-itlabs-operator/operators/postgresconnector.py @@ -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", diff --git a/k8s-itlabs-operator/operators/rabbitconnector.py b/k8s-itlabs-operator/operators/rabbitconnector.py index 8f32ec8..90ab917 100644 --- a/k8s-itlabs-operator/operators/rabbitconnector.py +++ b/k8s-itlabs-operator/operators/rabbitconnector.py @@ -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", diff --git a/k8s-itlabs-operator/operators/sentry.py b/k8s-itlabs-operator/operators/sentry.py index 629c387..c55b42f 100644 --- a/k8s-itlabs-operator/operators/sentry.py +++ b/k8s-itlabs-operator/operators/sentry.py @@ -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",