Skip to content

Commit

Permalink
Rewrite of python init container
Browse files Browse the repository at this point in the history
  • Loading branch information
TimPansino committed May 10, 2024
1 parent 098296d commit 27c99de
Show file tree
Hide file tree
Showing 6 changed files with 432 additions and 9 deletions.
39 changes: 30 additions & 9 deletions python/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,38 @@
# - Grant the necessary access to `/instrumentation` directory. `chmod -R go+r /instrumentation`

FROM python:3.10-alpine AS build

WORKDIR /operator-build
ARG version
ENV NEW_RELIC_EXTENSIONS = False
# WARNING: Disabling optional C extension components of the Python agent
# will result in some non core features of the Python agent, such as
# capacity analysis instance busy metrics, that will not be available.
# Pure Python versions of code supporting some features, rather than the
# optimised C versions, will also be used resulting in additional overheads.
ADD requirements.txt .
RUN mkdir workspace && pip install --target workspace newrelic==$version

# Install dependencies
COPY requirements-builder.txt .
RUN pip install -r requirements-builder.txt

# Download and prepare wheels
COPY download_wheels.py .
RUN python ./download_wheels.py

# Install sdist without extensions to set up the directory
ENV NEW_RELIC_EXTENSIONS False
ENV WRAPT_DISABLE_EXTENSIONS True
RUN pip install ./workspace/newrelic.tar.gz --target=./workspace/newrelic && \
rm ./workspace/newrelic.tar.gz

# Install pip as a vendored package
COPY requirements-vendor.txt .
RUN mkdir -p ./workspace/vendor && \
pip install --target=./workspace/vendor -r requirements-vendor.txt

# Install sitecustomize and newrelic_k8s_operator modules
RUN cp ./workspace/newrelic/newrelic/bootstrap/sitecustomize.py ./workspace/sitecustomize.py
COPY newrelic_k8s_operator.py ./workspace/

# initcontainer
FROM busybox

COPY --from=build /operator-build/workspace /instrumentation
RUN chmod -R go+r /instrumentation

# *** TODO: Remove this after you're done testing locally ***
COPY sitecustomize.py /instrumentation/
COPY sitecustomize.py /instrumentation/newrelic/bootstrap/
100 changes: 100 additions & 0 deletions python/download_wheels.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# Copyright 2010 New Relic, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import requests
import os
import shutil
import subprocess
import tempfile


AGENT_VERSION = "9.9.0"
FILE_DIR = os.path.dirname(__file__)
WORKSPACE_DIR = os.path.join(FILE_DIR, "workspace")


def main():
with requests.session() as session:
# Fetch JSON list of all agent package artifacts
resp = session.get("https://pypi.org/pypi/newrelic/json")
resp.raise_for_status()
resp_dict = resp.json()

# Grab the correct release
release = resp_dict["releases"][AGENT_VERSION]

# Filter artifacts for wheels and tarballs
wheel_urls = [
artifact["url"]
for artifact in release
if artifact["url"].endswith(".whl")
]

tarball_url = [
artifact["url"]
for artifact in release
if artifact["url"].endswith(".tar.gz")
][0]

# Make workspace directory
if not os.path.exists(WORKSPACE_DIR):
os.mkdir(WORKSPACE_DIR)

# Download tarball
resp = session.get(tarball_url)
resp.raise_for_status()

tarball_filepath = os.path.join(WORKSPACE_DIR, "newrelic.tar.gz")
with open(tarball_filepath, "wb") as file:
file.write(resp.content)

# Download and extract all wheels
for wheel_url in wheel_urls:
# Download wheel
resp = session.get(wheel_url)
resp.raise_for_status()

# Write wheel to file under a folder of the same name
with tempfile.TemporaryDirectory() as wheel_temp_dir:
# Compute paths
wheel_filename = wheel_url.split("/")[-1]
wheel_filepath = os.path.join(wheel_temp_dir, wheel_filename)

# Write wheel file to tempdir
with open(wheel_filepath, "wb") as file:
file.write(resp.content)

# Unpack wheel using wheel command
subprocess.run(
["wheel", "unpack", str(wheel_filename)],
cwd=wheel_temp_dir,
)

# Delete wheel file after unpacking
os.remove(wheel_filepath)

# Move unpacked module from tempdir to final destination
unpacked_module_folder = os.path.join(
wheel_temp_dir, list(os.listdir(wheel_temp_dir))[0]
)

# Put wheel contents into folders named after the original wheel types
final_wheel_dest = os.path.join(
WORKSPACE_DIR, wheel_filename.rstrip(".whl")
)
shutil.move(str(unpacked_module_folder), str(final_wheel_dest))


if __name__ == "__main__":
main()
86 changes: 86 additions & 0 deletions python/newrelic_k8s_operator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# Copyright 2010 New Relic, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import sys

INSTRUMENTATION_PATH = os.path.dirname(__file__)
INSTRUMENTATION_VENDOR_PATH = os.path.join(INSTRUMENTATION_PATH, "vendor")
SDIST_PATH = str(os.path.join(INSTRUMENTATION_PATH, "newrelic"))


def get_supported_tags():
"""
Load pip and run compatibility_tags.get_supported(). If pip is not present
or does not contain this function, load our vendored version of pip instead.
"""
try:
# Attempt to use any existing installs of pip to find supported tags
from pip._internal.utils.compatibility_tags import get_supported

return get_supported()
except Exception:
pass

# Unable to find pip, or pip version does not have compatibility tags
if "pip" in sys.modules:
# Save original pip module and load our own copy
original_pip = sys.modules["pip"]
del sys.modules["pip"]
else:
original_pip = None

try:
# Insert our vendored version of pip into the path, and try to return
# the supported tags again.
sys.path.insert(0, INSTRUMENTATION_VENDOR_PATH)
from pip._internal.utils.compatibility_tags import get_supported

return get_supported()
finally:
# Remove our vendored version of pip from the path
if INSTRUMENTATION_VENDOR_PATH in sys.path:
del sys.path[sys.path.index(INSTRUMENTATION_VENDOR_PATH)]

# Replace original pip module for compatibility
if original_pip:
sys.modules["pip"] = original_pip
elif "pip" in sys.modules:
del sys.modules["pip"]


def find_supported_newrelic_distribution():
wheels = list(os.listdir(INSTRUMENTATION_PATH))
for tag in get_supported_tags():
tag = str(tag)
for wheel in wheels:
if tag in wheel:
return str(os.path.join(INSTRUMENTATION_PATH, wheel))
else:
return SDIST_PATH


def insert_newrelic_distribution():
# Find path of supported distribution
try:
new_relic_path = find_supported_newrelic_distribution()
except Exception:
new_relic_path = SDIST_PATH

# Add path to sys.path and import
sys.path.insert(0, new_relic_path)
import newrelic.config

# Returned path will be logged and then removed from sys.path by sitecustomize
return new_relic_path
2 changes: 2 additions & 0 deletions python/requirements-builder.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
requests
setuptools>=40.8.0
1 change: 1 addition & 0 deletions python/requirements-vendor.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pip
Loading

0 comments on commit 27c99de

Please sign in to comment.