Skip to content

[chore] Optimize k8s steps in GHA#123

Merged
marcorosa merged 3 commits intodevelopfrom
fix/k8s-steps
Oct 2, 2025
Merged

[chore] Optimize k8s steps in GHA#123
marcorosa merged 3 commits intodevelopfrom
fix/k8s-steps

Conversation

@marcorosa
Copy link
Copy Markdown
Member

Changed

  • Try several strategies to optimize space in backend Dockerfile. This was done in order to respect the space limitations imposed by GitHub Actions (to be tested if this works or not).

Fixes

  • Add missing step to setup kubelogin (needed to deploy on SAP BTP Kyma)

Minor

  • Do not force tracing (i.e., no longer mandatory need to import status and set traces -> if not done, traces will just not be stored)

Add many optimization strategies to try not to exceed the space
limitations from GitHub Actions.
@marcorosa marcorosa requested a review from a team as a code owner October 2, 2025 12:59
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Oct 2, 2025

The recent changes you made include optimizations to Dockerfile configurations for the backend-agent, and enhancements to the GitHub Actions workflow. These alterations focus on reducing build sizes and improving security, as well as setting up additional tooling within the CI/CD pipeline. The modifications lead to better performance and trace reliability in the backend application and CI/CD procedures.

Walkthrough

  • Chore: Updated the Dockerfile for backend-agent with aggressive cleanup strategies post-build to minimize footprint and optimize image size.
  • Chore: Introduced Azure Kubernetes login setup into the GitHub Actions workflow to enhance integrations and security practices.
  • Bug Fix: Adjusted the trace mechanism in status.py to ensure trace operations only occur when trace context is active, preventing potential errors.

Model: gpt-4o | Prompt Tokens: 1483 | Completion Tokens: 154

Copy link
Copy Markdown
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's a supportive code review enhanced with AI assistance. These suggestions offer potential improvements and insights to consider, though they're recommendations rather than requirements. You have full control over which feedback to incorporate based on your project's needs and goals. AI serves as a helpful tool to supplement your expertise and decision-making process.


Always critique what AI says. Do not let AI replace YOUR I.
Model: anthropic--claude-4-sonnet | Prompt Tokens: 2762 | Completion Tokens: 1058

Comment on lines +220 to 229
- name: Set up kubelogin
uses: azure/use-kubelogin@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
kubelogin-version: 'latest'

- name: Configure kubectl for SAP BTP Kyma
run: |
mkdir -p ~/.kube
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code duplication detected: The kubelogin setup step is duplicated in the workflow. Consider extracting this into a reusable action or using workflow templates to avoid redundancy.

# Create a separate action or use anchors/templates
- name: Setup Kubelogin
  uses: ./.github/actions/setup-kubelogin
  with:
    version: 'latest'

Comment on lines +220 to +225
- name: Set up kubelogin
uses: azure/use-kubelogin@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
kubelogin-version: 'latest'
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider pinning to a specific version instead of 'latest' for better reproducibility and security. Using 'latest' can lead to unexpected behavior when the action is updated.

- name: Set up kubelogin
  uses: azure/use-kubelogin@v1.0.0  # Use specific version
  with:
    kubelogin-version: 'v0.0.32'     # Pin kubelogin version too

Comment thread backend-agent/Dockerfile
Comment on lines 31 to +58
RUN . ~/.cargo/env && \
uv sync --frozen --no-dev --no-cache && \
# Clean up any temporary files to reduce layer size
rm -rf /root/.cache/uv /tmp/* /var/tmp/* && \
# Remove Rust toolchain after build to reduce image size
rustup self uninstall -y
# Install dependencies with bytecode compilation for better performance
uv sync --frozen --no-dev --no-cache --compile-bytecode && \
# Immediate cleanup of build artifacts during installation
find /app/.venv -type d -name "__pycache__" -exec rm -rf {} + 2>/dev/null || true && \
find /app/.venv -name "*.pyc" -delete 2>/dev/null || true && \
find /app/.venv -name "*.pyo" -delete 2>/dev/null || true && \
# Remove test files and documentation from packages (keeping runtime libs)
find /app/.venv -type d -name "tests" -exec rm -rf {} + 2>/dev/null || true && \
find /app/.venv -type d -name "test" -exec rm -rf {} + 2>/dev/null || true && \
find /app/.venv -type d -name "docs" -exec rm -rf {} + 2>/dev/null || true && \
# Strip debug symbols from shared libraries to reduce size
find /app/.venv -name "*.so" -exec strip {} + 2>/dev/null || true && \
# Aggressive cache and temporary file cleanup
rm -rf /root/.cache/uv \
/root/.cache/pip \
/root/.cache/* \
/tmp/* \
/var/tmp/* \
/root/.cargo/registry \
/root/.cargo/git \
/app/.venv/share \
&& \
# Remove Rust toolchain completely after build
rustup self uninstall -y && \
# Final build tools cleanup to free space
apt-get autoremove -y build-essential git curl pkg-config && \
apt-get autoclean
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The aggressive cleanup strategy is good for reducing image size, but some operations might be redundant or risky. The find commands with 2>/dev/null || true could mask legitimate errors. Consider using more targeted cleanup and verify that stripping shared libraries doesn't break functionality.

# More targeted and safer cleanup
RUN . ~/.cargo/env && \
    uv sync --frozen --no-dev --no-cache --compile-bytecode && \
    # Remove only Python cache files
    find /app/.venv -name "*.pyc" -delete && \
    find /app/.venv -name "__pycache__" -type d -exec rm -rf {} + && \
    # Test that libraries still work before stripping
    python -c "import torch; print('Libraries OK')" && \
    find /app/.venv -name "*.so" -exec strip --strip-unneeded {} \; && \
    # Clean up build artifacts
    rm -rf /root/.cache /tmp/* /var/tmp/* && \
    rustup self uninstall -y

Comment thread backend-agent/Dockerfile
Comment on lines +64 to +73
# Install minimal runtime dependencies
RUN apt-get update && apt-get install -y --no-install-recommends \
libssl3 \
libffi8 \
# Add required libraries for ML packages
libgomp1 \
libglib2.0-0 \
&& rm -rf /var/lib/apt/lists/* \
&& apt-get clean
&& apt-get autoremove -y \
&& apt-get autoclean
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good optimization using --no-install-recommends and proper cleanup. Consider adding a verification step to ensure the image still functions correctly after aggressive cleanup.

# Add runtime verification
RUN apt-get update && apt-get install -y --no-install-recommends \
    libssl3 \
    libffi8 \
    libgomp1 \
    libglib2.0-0 \
    && rm -rf /var/lib/apt/lists/* \
    && apt-get autoremove -y \
    && apt-get autoclean \
    # Verify critical libraries are available
    && python3 -c "import ssl, ctypes; print('Runtime deps OK')"

Comment thread backend-agent/status.py
Comment on lines +181 to +183
# Only trace if there's an active trace context
if hasattr(self, 'trace') and self.trace:
self.trace['llm_messages'].append(message)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good defensive programming to check for trace context. However, the condition could be more explicit about what constitutes a valid trace. Consider using a more specific check.

# More explicit trace validation
if hasattr(self, 'trace') and self.trace is not None and isinstance(self.trace, dict) and 'llm_messages' in self.trace:
    self.trace['llm_messages'].append(message)

@marcorosa marcorosa merged commit 3688052 into develop Oct 2, 2025
7 checks passed
@marcorosa marcorosa deleted the fix/k8s-steps branch October 2, 2025 14:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant