Skip to content

Commit

Permalink
Update Python version to 3.13.2 and enhance request processing with r…
Browse files Browse the repository at this point in the history
…etry logic.Fixed Bugs around parsing abd running image. Improved Documentation.
  • Loading branch information
Moyo Oyegunle committed Feb 11, 2025
1 parent b4cb016 commit bb69eb0
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 56 deletions.
16 changes: 7 additions & 9 deletions util-scripts/acs-correlation-example/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ FROM registry.access.redhat.com/ubi9:9.1.0-1782
#Change User
USER 0


# Install the required software
RUN dnf install -y wget yum-utils make gcc openssl-devel bzip2-devel libffi-devel zlib-devel && \
wget https://www.python.org/ftp/python/3.10.8/Python-3.10.8.tgz && \
tar xzf Python-3.10.8.tgz && \
cd Python-3.10.8 && \
wget https://www.python.org/ftp/python/3.13.2/Python-3.13.2.tgz && \
tar xzf Python-3.13.2.tgz && \
cd Python-3.13.2 && \
./configure --with-system-ffi --with-computed-gotos --enable-loadable-sqlite-extensions && \
make altinstall && \
cd .. && \
rm Python-3.10.8.tgz
rm Python-3.13.2.tgz

# # Install pip
# RUN curl -O https://bootstrap.pypa.io/get-pip.py && python3 get-pip.py && python3 get-pip.py
Expand All @@ -30,16 +31,13 @@ COPY ./ ./app
WORKDIR ./app

#Install App Dependecies
RUN pip3.10 install -r requirements.txt && pip3.10 install --upgrade pip
RUN pip3.13 install -r requirements.txt && pip3.13 install --upgrade pip

#Expose Ports
EXPOSE 8080/tcp

#Change Permissions to allow not root-user work
RUN chmod -R g+rw ./

#Change User
USER 1001

#ENTRY
ENTRYPOINT python3.10 app.py
ENTRYPOINT python3.13 app.py
27 changes: 25 additions & 2 deletions util-scripts/acs-correlation-example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,30 @@
podman build -t quick_acs_app .
```

- Copy and update endpoint list file and token
```bash
export CENTRAL_API_URL="https://console-openshift-console.apps.cluster1.sandbox568.opentlc.com"
export MAIN_ACS_TOKEN=""
export ENDPOINT_DIR=$(mktemp -d -t ACS_Endpoint_List_XXXX )
export OUTPUT_FILE_DIR=$(mktemp -d -t ACS_Output_DIR_XXXX )
cat ./endpoint_list.json | envsubst > ${ENDPOINT_DIR}/endpoint_list.json
```

- Run Container
```bash
podman run --env $MAIN_ACS_TOKEN --env OUTPUT_FOLDER=/output -v /tmp/output:/output:Z localhost/quick_acs_app
```
podman run --name acs_correlator \
--replace \
--userns=keep-id \
--env MAIN_ACS_TOKEN=${MAIN_ACS_TOKEN} \
--env ENDPOINT_LIST_JSON_PATH=/endpoint/endpoint_list.json \
--env OUTPUT_FOLDER=/output \
-v ${OUTPUT_FILE_DIR}:/output:z \
-v ${ENDPOINT_DIR}:/endpoint:z \
localhost/quick_acs_app
```
- If All goes well sample output should get written out to ${OUTPUT_FILE_DIR}

- TODO:
- Example uses the [Pydantic Library to create models to export out objects relationships](https://docs.pydantic.dev/1.10/usage/exporting_models/#advanced-include-and-exclude).
- The sample relationships used for output can be seen in the [app.py](util-scripts/acs-correlation-example/app.py) on line 866
- Will eventually extend this example to get custom relationships and export out a file.
29 changes: 22 additions & 7 deletions util-scripts/acs-correlation-example/acs_request.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from httpx import AsyncClient,HTTPError,NetworkError,RequestError,TimeoutException,ConnectTimeout,InvalidURL,ProtocolError,ConnectError
import os
from httpx._config import SSLConfig
#from httpx._config import SSLConfig
from logging import getLogger, config
import typing as t
import asyncio
Expand Down Expand Up @@ -128,14 +128,29 @@ async def request_processing(full_url_path,insecure:bool=False,headers:dict=None
"""Send the Request and process the response"""
logger.debug(f"request_processing -start: url:{full_url_path} verify_ssl:{insecure}")
error=None
retry_count=3
response_dict={"response_object":[],"error_object":None}

if params is None:
response_dict = await make_request(full_url_path,insecure,headers,params)
else:
if "pagination.limit" in params and "total_expected_count" in params:
response_dict = await request_processing_pagination(full_url_path,insecure,headers,params)


while retry_count > 0 and retry_count < 4:
if params is None:
response_dict = await make_request(full_url_path,insecure,headers,params)
else:
response_dict = await make_request(full_url_path,insecure,headers,params)
if "pagination.limit" in params and "total_expected_count" in params:
response_dict = await request_processing_pagination(full_url_path,insecure,headers,params)
else:
response_dict = await make_request(full_url_path,insecure,headers,params)

if response_dict["error_object"] is not None:
logger.error(f"request_processing - error: {response_dict['error_object']}")
retry_count-=1
logger.info(f"Retrying request: {retry_count} attempts left")
logger.info(f"Sleeping for 5 seconds before retry")
await asyncio.sleep(5)
else:
break

return response_dict

async def get_acs_alert(url,alert_id: str,insecure:bool=False,headers:dict=None,params:dict=None) -> dict:
Expand Down
6 changes: 5 additions & 1 deletion util-scripts/acs-correlation-example/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -861,7 +861,7 @@ async def generate_cluster_namespace_deployment_alert_output_file():
"""
Generate the output for the Cluster and Deployment
"""
logger.info("Generating Output for Cluster and Deployment")
logger.info("Generating Output File for Cluster-Deployment-Namespace-Alerts-Relationship")

exclude_keys = {
"clusters": {
Expand Down Expand Up @@ -953,6 +953,10 @@ async def main():

# Load the ACS Endpoints
result_endpoint_list = await read_parse_acs_endpoints(settings.endpoint_list_json_path)
while result_endpoint_list is None:
logger.error("Error reading endpoint list file")
logger.info("Exiting")
return
for endpoint in result_endpoint_list.endpoints:
await ParsedMemory.check_endpoint_valid_healthy(endpoint)

Expand Down
2 changes: 1 addition & 1 deletion util-scripts/acs-correlation-example/endpoint_list.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"endpoints": [
{
"endpoint_name": "ACS_Demo_Environment",
"endpoint_url": "https://central-rhacs-operator.apps.cluster11.sandbox2585.opentlc.com",
"endpoint_url": "${CENTRAL_API_URL}",
"endpoint_url_description": "ACS API endpoint for the application to make requess to",
"endpoint_token_env_variable_name": "MAIN_ACS_TOKEN",
"verify_endpoint_ssl" : "False",
Expand Down
4 changes: 2 additions & 2 deletions util-scripts/acs-correlation-example/logging.conf
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ formatter=detailedFormatter
args=(sys.stdout,)

[formatter_normalFormatter]
format=%(asctime)s loglevel=%(levelname)-6s logger=%(name)s %(funcName)s() L%(lineno)-4d %(message)s
format=%(asctime)s loglevel=%(levelname)-6s %(funcName)s() L%(lineno)-4d %(message)s

[formatter_detailedFormatter]
format=%(asctime)s loglevel=%(levelname)-6s logger=%(name)s %(funcName)s() L%(lineno)-4d %(message)s call_trace=%(pathname)s L%(lineno)-4d
format=%(asctime)s loglevel=%(levelname)-6s %(funcName)s() L%(lineno)-4d %(message)s call_trace=%(pathname)s L%(lineno)-4d
43 changes: 9 additions & 34 deletions util-scripts/acs-correlation-example/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,39 +1,14 @@
aiofiles==23.2.1
aiofiles==24.1.0
annotated-types==0.7.0
anyio==4.4.0
certifi==2024.7.4
click==8.1.7
dnspython==2.6.1
email_validator==2.1.1
exceptiongroup==1.2.1
fastapi==0.111.0
fastapi-cli==0.0.4
anyio==4.8.0
certifi==2025.1.31
h11==0.14.0
httpcore==1.0.5
httptools==0.6.1
httpx==0.27.0
idna==3.7
Jinja2==3.1.4
markdown-it-py==3.0.0
MarkupSafe==2.1.5
mdurl==0.1.2
orjson==3.10.4
pydantic==2.7.4
pydantic-settings==2.3.3
pydantic_core==2.18.4
Pygments==2.18.0
httpcore==1.0.7
httpx==0.28.1
idna==3.10
pydantic==2.10.6
pydantic-settings==2.7.1
pydantic_core==2.27.2
python-dotenv==1.0.1
python-multipart==0.0.9
PyYAML==6.0.1
rich==13.7.1
shellingham==1.5.4
sniffio==1.3.1
starlette==0.37.2
typer==0.12.3
typing_extensions==4.12.2
ujson==5.10.0
uuid==1.30
uvicorn==0.30.1
uvloop==0.19.0
watchfiles==0.22.0
websockets==12.0

0 comments on commit bb69eb0

Please sign in to comment.