Red Hat Insights App Common Python
app-common-python is a simple client access library designed to retrieve configuration for applications running within the Red Hat OpenShift Clowder operator environment. It provides Python wrappers and helper functions to easily access details related to Kafka topics, RDS certificate authorities, Kafka broker servers, object storage buckets, and other service dependency endpoints, abstracting away the underlying JSON configuration provided by Clowder. The current version is 0.2.9, with releases occurring periodically to update schemas and introduce new features.
Common errors
-
ModuleNotFoundError: No module named 'app_common_python'
cause The `app-common-python` library is not installed in the current Python environment or there's a typo in the import statement.fixInstall the package using `pip install app-common-python`. Double-check the import statement: `from app_common_python import LoadedConfig`. -
AttributeError: type object 'LoadedConfig' has no attribute 'PublicPort'
cause The `PublicPort` attribute is not present in the Clowder configuration loaded by `LoadedConfig`, or the application is not running in a Clowder-enabled environment (`isClowderEnabled()` is False).fixVerify that your application's `ClowdApp` configuration in OpenShift explicitly defines a public port. If running locally, ensure `ACG_CONFIG` is set and points to a valid JSON file containing the `PublicPort` entry. Always check `if hasattr(LoadedConfig, 'PublicPort'):` before accessing such attributes. -
KeyError: 'my-topic'
cause The requested Kafka topic 'my-topic' is not defined or available in the Clowder-provided configuration, or it's requested with an incorrect `requestedName`.fixEnsure the Kafka topic is correctly configured and referenced by its `requestedName` in your Clowder `ClowdApp` resource. Access Kafka topics robustly using `if 'my-topic' in LoadedConfig.KafkaTopics:`.
Warnings
- gotcha This library is specifically designed to interact with the Red Hat OpenShift Clowder operator for configuration management. It is not a general-purpose configuration library. Usage outside a Clowder-managed environment will result in `isClowderEnabled()` returning `False` and `LoadedConfig` attributes being unavailable or empty.
- gotcha Configuration values accessed via `LoadedConfig` (e.g., `LoadedConfig.PublicPort`, `LoadedConfig.KafkaServers`) directly reflect the JSON provided by Clowder. If the underlying Clowder configuration for your application is missing or malformed for a specific attribute, accessing that attribute will likely result in an `AttributeError` or an empty value.
- gotcha For local development or testing without a full Clowder deployment, configuration often needs to be supplied via environment variables, such as `ACG_CONFIG` pointing to a local JSON file. Failing to set these variables or providing an invalid file will lead to an unconfigured state.
Install
-
pip install app-common-python
Imports
- LoadedConfig
from app_common_python import LoadedConfig
- isClowderEnabled
from app_common_python import isClowderEnabled
Quickstart
from app_common_python import LoadedConfig, isClowderEnabled
import os
def main():
if isClowderEnabled():
print(f"Application running in Clowder environment.")
# Access public port, for example
if hasattr(LoadedConfig, 'PublicPort'):
print(f"Public Port: {LoadedConfig.PublicPort}")
else:
print("PublicPort attribute not found in LoadedConfig. Check Clowder configuration.")
# Example: Get Kafka servers
kafka_servers = LoadedConfig.KafkaServers
print(f"Kafka Servers: {kafka_servers}")
# Example: Get a specific Kafka topic by name
# Assuming 'my-topic' is a configured topic
if 'my-topic' in LoadedConfig.KafkaTopics:
my_topic = LoadedConfig.KafkaTopics['my-topic']
print(f"Kafka Topic 'my-topic': {{'name': {my_topic.Name}, 'hostname': {my_topic.Hostname}}}")
else:
print("Application not running in Clowder environment or Clowder not enabled.")
print("Configuration might need to be provided via environment variables (e.g., ACG_CONFIG).")
if __name__ == "__main__":
# For local testing without Clowder, you might set ACG_CONFIG
# e.g., os.environ['ACG_CONFIG'] = 'path/to/test.json'
main()