Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

K8s upgrade dockerfile and example #19

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 63 additions & 18 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,20 +1,65 @@
FROM python:3.11-bookworm AS builder

RUN apt-get update && apt-get -y upgrade && apt-get install -y cmake && apt-get -y clean && mkdir -p /app/ && python3 -m venv /app/.venv
RUN rm /bin/sh && ln -s /bin/bash /bin/sh
COPY requirements.txt /app/
RUN cd app && source .venv/bin/activate && pip3 install -r requirements.txt

FROM python:3.11-bookworm AS runtime-image
RUN apt-get update && apt-get -y upgrade && apt-get -y clean
RUN useradd --create-home --shell /bin/sh --uid 8000 opencost
COPY --from=builder /app /app
COPY src/opencost_parquet_exporter.py /app/opencost_parquet_exporter.py
COPY src/data_types.json /app/data_types.json
COPY src/rename_cols.json /app/rename_cols.json
COPY src/ignore_alloc_keys.json /app/ignore_alloc_keys.json
RUN chmod 755 /app/opencost_parquet_exporter.py && chown -R opencost /app/
# Stage 1: Builder
FROM python:3.12-bookworm AS builder

# Set environment variables for paths to avoid repetition
ENV APP_DIR=/app \
VENV_DIR=/app/.venv

# Update system and install required packages in a single RUN command to reduce layers
RUN apt-get update && apt-get -y upgrade && apt-get install -y --no-install-recommends \
cmake \
build-essential \
libatlas-base-dev \
gfortran \
libssl-dev \
libffi-dev \
libxml2-dev \
libxslt1-dev \
zlib1g-dev \
libcurl4-openssl-dev \
libboost-all-dev \
libprotobuf-dev \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/* \
&& mkdir -p $APP_DIR \
&& python3 -m venv $VENV_DIR

# Copy the requirements file first to leverage Docker caching in case dependencies don't change
COPY requirements.txt $APP_DIR/

# Activate virtual environment and install Python dependencies
# Use the full path for activation
RUN /bin/bash -c "source $VENV_DIR/bin/activate && pip3 install --no-cache-dir -r $APP_DIR/requirements.txt"

# Stage 2: Runtime Image
FROM python:3.12-bookworm AS runtime-image

# Set environment variables for paths
ENV APP_DIR=/app \
VENV_DIR=/app/.venv \
PATH="$VENV_DIR/bin:$PATH"

# Update and clean up in a single step to minimize image size
RUN apt-get update && apt-get -y upgrade \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*

# Create a non-root user for security
RUN useradd --create-home --shell /bin/sh --uid 8000 opencost

# Copy application files from the builder stage
COPY --from=builder $APP_DIR $APP_DIR

# Copy all files from /src to /app
COPY src/ $APP_DIR/

# Set correct permissions for the application files
RUN chmod 755 $APP_DIR/opencost_parquet_exporter.py \
&& chown -R opencost $APP_DIR

# Switch to the non-root user
USER opencost
ENV PATH="/app/.venv/bin:$PATH"
CMD ["/app/opencost_parquet_exporter.py"]

# Default entrypoint and command
ENTRYPOINT ["/app/.venv/bin/python3"]
CMD ["/app/opencost_parquet_exporter.py"]
1 change: 1 addition & 0 deletions examples/k8s_cron_job.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ spec:
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
command: ["/app/.venv/bin/python3"] # Update this is if the ENTRYPOINT changes
args: ["/app/opencost_parquet_exporter.py"] # Update this is if the ENTRYPOINT changes
dnsConfig:
options:
- name: single-request-reopen
Expand Down
28 changes: 14 additions & 14 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
numpy==1.26.3
pandas==2.1.4
boto3==1.34.21
requests==2.32.0
python-dateutil==2.8.2
pytz==2023.3.post1
numpy==2.1.1
pandas==2.2.3
boto3==1.35.31
requests==2.32.3
python-dateutil==2.9.0.post0
pytz==2024.2
six==1.16.0
tzdata==2023.4
pyarrow==14.0.1
azure-storage-blob==12.19.1
azure-identity==1.16.1
# The dependencies bellow are only used for development.
freezegun==1.4.0
pylint==3.0.3
pytest==7.4.4
tzdata==2024.2
pyarrow==17.0.0
azure-storage-blob>=12.19.1
azure-identity>=1.16.1
# The dependencies below are only used for development.
freezegun>=1.4.0
pylint>=3.0.3
pytest>=7.4.4
20 changes: 10 additions & 10 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
numpy==1.26.3
pandas==2.1.4
boto3==1.34.21
requests==2.32.0
python-dateutil==2.8.2
pytz==2023.3.post1
numpy==2.1.1
pandas==2.2.3
boto3==1.35.31
requests==2.32.3
python-dateutil==2.9.0.post0
pytz==2024.2
six==1.16.0
tzdata==2023.4
pyarrow==14.0.1
azure-storage-blob==12.19.1
azure-identity==1.16.1
tzdata==2024.2
pyarrow==17.0.0
azure-storage-blob>=12.19.1
azure-identity>=1.16.1