Skip to content

Commit

Permalink
feat: wire more dependencies and modules
Browse files Browse the repository at this point in the history
we drop the dependency managers and use the DI containers and providers
  • Loading branch information
nosahama committed Nov 27, 2024
1 parent d9d471d commit 8224657
Show file tree
Hide file tree
Showing 71 changed files with 1,090 additions and 959 deletions.
1 change: 0 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
!LICENSE
!pyproject.toml
!setup.py
!container/start.sh
!container/healthcheck.py

# Ignore some files in source directories.
Expand Down
7 changes: 4 additions & 3 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ env:
FORCE_COLOR: 1
PIP_PROGRESS_BAR: off
PYTHONUNBUFFERED: 1
KARAPACE_DOTENV: ${{ github.workspace }}/karapace.config.env

jobs:
tests:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [ '3.9', '3.10', '3.11', '3.12' ]
python-version: [ '3.10', '3.11', '3.12' ]
env:
PYTEST_ADDOPTS: >-
--log-dir=/tmp/ci-logs
Expand All @@ -44,11 +45,11 @@ jobs:
- run: make unit-tests
env:
COVERAGE_FILE: ".coverage.${{ matrix.python-version }}"
PYTEST_ARGS: "--cov=karapace --cov-append --numprocesses 4"
PYTEST_ARGS: "--cov=src --cov-append --numprocesses 4"
- run: make integration-tests
env:
COVERAGE_FILE: ".coverage.${{ matrix.python-version }}"
PYTEST_ARGS: "--cov=karapace --cov-append --random-order --numprocesses 4"
PYTEST_ARGS: "--cov=src --cov-append --random-order --numprocesses 4"

- name: Archive logs
uses: actions/upload-artifact@v4
Expand Down
7 changes: 7 additions & 0 deletions GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ SHELL := /usr/bin/env bash
VENV_DIR ?= $(CURDIR)/venv
PIP ?= pip3 --disable-pip-version-check --no-input --require-virtualenv
PYTHON ?= python3
CLI ?= docker-compose -f container/compose.yml run karapace-cli
PYTHON_VERSION ?= 3.9

define PIN_VERSIONS_COMMAND
Expand Down Expand Up @@ -102,3 +103,9 @@ schema:
.PHONY: pin-requirements
pin-requirements:
docker run -e CUSTOM_COMPILE_COMMAND='make pin-requirements' -it -v .:/karapace --security-opt label=disable python:$(PYTHON_VERSION)-bullseye /bin/bash -c "$(PIN_VERSIONS_COMMAND)"

cli:
# $(CLI) python3 -m pytest -vvv tests/integration/test_client.py
# $(CLI) python3 -m pytest -vvv tests/integration/schema_registry/test_jsonschema.py
$(CLI) python3 -m pytest -vvv tests/integration/
# $(CLI) python3 -m pytest -vvv tests/unit
1 change: 1 addition & 0 deletions bin/smoke-test-registry.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ for ((i = 0; i <= retries; i++)); do
response=$(
curl --silent --verbose --fail --request POST \
--header 'Content-Type: application/vnd.schemaregistry.v1+json' \
--header 'Authorization: Basic YWRtaW46YWRtaW4=' \
--data '{"schema": "{\"type\": \"record\", \"name\": \"Obj\", \"fields\":[{\"name\": \"age\", \"type\": \"int\"}]}"}' \
http://localhost:8081/subjects/test-key/versions
)
Expand Down
4 changes: 0 additions & 4 deletions container/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,6 @@ RUN apt-get update \
COPY --from=builder /venv /venv
ENV PATH="/venv/bin:$PATH"

COPY ./container/start.sh /opt/karapace
RUN chmod 500 /opt/karapace/start.sh \
&& chown karapace:karapace /opt/karapace/start.sh

COPY ./container/healthcheck.py /opt/karapace

WORKDIR /opt/karapace
Expand Down
62 changes: 62 additions & 0 deletions container/Dockerfile.dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Current versions of avro and zstandard don't yet have wheels for 3.11.
FROM python:3.10.11-bullseye AS builder

ARG KARAPACE_VERSION

# Create, activate, and enforce usage of virtualenv.
RUN python3 -m venv /venv
ENV PATH="/venv/bin:$PATH"
ENV PIP_REQUIRE_VIRTUALENV=true

# Install golang needed by extensions
ENV GO_VERSION=1.21.0
ENV PATH="/usr/local/go/bin:${PATH}"
RUN wget --progress=dot:giga "https://go.dev/dl/go${GO_VERSION}.linux-$(dpkg --print-architecture).tar.gz" \
&& tar -C /usr/local -xzf "go${GO_VERSION}.linux-$(dpkg --print-architecture).tar.gz" \
&& rm "go${GO_VERSION}.linux-$(dpkg --print-architecture).tar.gz"

# Copy the requirements.txt and install dependencies in venv. Using a separate
# command to use layer caching.
#
# Note: the requirements.txt is pinned, if any of the dependencies is updated
# the cache will be invalidated and the image regenerated, which is the
# intended behavior.
COPY ./requirements/requirements.txt /build/
COPY ./requirements/requirements-dev.txt /build/
RUN --mount=type=cache,target=/root/.cache/pip \
python3 -m pip install -r /build/requirements.txt -r /build/requirements-dev.txt

COPY . /build/karapace-repo
WORKDIR /build/karapace-repo
RUN --mount=type=cache,target=/root/.cache/pip \
if [ -z "${KARAPACE_VERSION}" ]; then \
PRETEND_VERSION="$(python -c 'from src.karapace import version; print(version.__version__)')"; \
else \
PRETEND_VERSION=$KARAPACE_VERSION; \
fi; \
SETUPTOOLS_SCM_PRETEND_VERSION=$PRETEND_VERSION python3 -m pip install --no-deps .

# Karapace image, i.e. production.
FROM python:3.10.11-slim-bullseye AS karapace

# Setup user and directories.
RUN groupadd --system karapace \
&& useradd --system --gid karapace karapace \
&& mkdir /opt/karapace /opt/karapace/runtime /var/log/karapace \
&& chown --recursive karapace:karapace /opt/karapace /var/log/karapace

# Install protobuf compiler.
ARG PROTOBUF_COMPILER_VERSION="3.12.4-1+deb11u1"
RUN apt-get update \
&& apt-get install --assume-yes --no-install-recommends \
protobuf-compiler=$PROTOBUF_COMPILER_VERSION \
&& rm -rf /var/lib/apt/lists/*

# Copy virtualenv from builder and activate it.
COPY --from=builder /venv /venv
ENV PATH="/venv/bin:$PATH"

COPY ./container/healthcheck.py /opt/karapace

WORKDIR /opt/karapace
USER karapace
63 changes: 36 additions & 27 deletions container/compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ services:
zookeeper:
image: confluentinc/cp-zookeeper:latest
ports:
- "2181:2181"
- 2181:2181
environment:
ZOOKEEPER_CLIENT_PORT: 2181
ZOOKEEPER_TICK_TIME: 2000
Expand All @@ -14,16 +14,16 @@ services:
depends_on:
- zookeeper
ports:
- "9101:9101" # JMX
- "9092:9092" # Kafka
- 9101:9101 # JMX
- 9092:9092 # Kafka
environment:
# Listeners:
# PLAINTEXT_HOST -> Expose kafka to the host network
# PLAINTEXT -> Used by kafka for inter broker communication / containers
KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://kafka:29092,PLAINTEXT_HOST://localhost:9092
KAFKA_GROUP_INITIAL_REBALANCE_DELAY_MS: 0
KAFKA_CONFLUENT_SCHEMA_REGISTRY_URL: http://karapace-registry:8081
KAFKA_CONFLUENT_SCHEMA_REGISTRY_URL: http://karapace-schema-registry:8081
# Metrics:
KAFKA_JMX_PORT: 9101
KAFKA_JMX_HOSTNAME: localhost
Expand Down Expand Up @@ -54,51 +54,60 @@ services:
KAFKA_ZOOKEEPER_CONNECTION_TIMEOUT_MS: 6000
KAFKA_ZOOKEEPER_CONNECT: "zookeeper:2181"

karapace-registry:
karapace-schema-registry:
image: ghcr.io/aiven-open/karapace:develop
build:
context: ..
dockerfile: container/Dockerfile
entrypoint:
- /bin/bash
- /opt/karapace/start.sh
- registry
- python3
- -m
- schema_registry
depends_on:
- kafka
ports:
- "8081:8081"
- 8081:8081
volumes:
- ./karapace.env:/opt/karapace/karapace.env
- ./karapace.registry.env:/opt/karapace/karapace.env
environment:
KARAPACE_DOTENV: /opt/karapace/karapace.env
KARAPACE_PORT: 8081

karapace-rest:
karapace-rest-proxy:
image: ghcr.io/aiven-open/karapace:develop
build:
context: ..
dockerfile: container/Dockerfile
entrypoint:
- /bin/bash
- /opt/karapace/start.sh
- rest
- python3
- -m
- karapace.karapace_all
depends_on:
- kafka
- karapace-registry
- karapace-schema-registry
ports:
- "8082:8082"
- 8082:8082
volumes:
- ./karapace.rest.env:/opt/karapace/karapace.env
environment:
KARAPACE_DOTENV: /opt/karapace/karapace.env
KARAPACE_PORT: 8082
KARAPACE_HOST: 0.0.0.0
KARAPACE_ADVERTISED_HOSTNAME: karapace-rest
KARAPACE_BOOTSTRAP_URI: kafka:29092
KARAPACE_REGISTRY_HOST: karapace-registry
KARAPACE_REGISTRY_PORT: 8081
KARAPACE_ADMIN_METADATA_MAX_AGE: 0
KARAPACE_LOG_LEVEL: WARNING
KARAPACE_STATSD_HOST: statsd-exporter
KARAPACE_STATSD_PORT: 8125
KARAPACE_KAFKA_SCHEMA_READER_STRICT_MODE: false
KARAPACE_KAFKA_RETRIABLE_ERRORS_SILENCED: true

karapace-cli:
image: ghcr.io/aiven-open/karapace:cli
build:
context: ..
dockerfile: container/Dockerfile.dev
tty: true
depends_on:
- kafka
- karapace-schema-registry
- karapace-rest-proxy
volumes:
- ../tests:/opt/karapace/tests
- ../karapace.config.env:/opt/karapace/karapace.env
environment:
KARAPACE_DOTENV: /opt/karapace/karapace.env

prometheus:
image: prom/prometheus
Expand Down
5 changes: 3 additions & 2 deletions container/karapace.env → container/karapace.registry.env
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ FETCH_MIN_BYTES=1
GROUP_ID=karapace-schema-registry
HOST=0.0.0.0
PORT=8081
REGISTRY_HOST=karapace-registry
REGISTRY_HOST=karapace-schema-registry
REGISTRY_PORT=8081
REST_AUTHORIZATION=False
LOG_HANDLER=stdout
LOG_LEVEL=DEBUG
LOG_LEVEL=WARNING
LOG_FORMAT=%(asctime)s [%(threadName)s] %(filename)s:%(funcName)s:%(lineno)d %(message)s
MASTER_ELIGIBILITY=True
REPLICATION_FACTOR=1
Expand All @@ -35,6 +35,7 @@ PRODUCER_MAX_REQUEST_SIZE=1048576
SESSION_TIMEOUT_MS=10000
KARAPACE_REST=False
KARAPACE_REGISTRY=True
KARAPACE_PORT=8081
NAME_STRATEGY=topic_name
NAME_STRATEGY_VALIDATION=True
MASTER_ELECTION_STRATEGY=lowest
Expand Down
51 changes: 51 additions & 0 deletions container/karapace.rest.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
KARAPACE_DOTENV=/opt/karapace/karapace.env
ACCESS_LOGS_DEBUG=False
# ACCESS_LOG_CLASS=karapace.utils.DebugAccessLogger
ACCESS_LOG_CLASS=aiohttp.web_log.AccessLogger
ADVERTISED_HOSTNAME=karapace-rest-proxy
ADVERTISED_PORT=8082
ADVERTISED_PROTOCOL=http
BOOTSTRAP_URI=kafka:29092
CLIENT_ID=karapace-rest-proxy
COMPATIBILITY=BACKWARD
CONNECTIONS_MAX_IDLE_MS=15000
CONSUMER_ENABLE_AUTO_COMMIT=True
CONSUMER_REQUEST_TIMEOUT_MS=11000
CONSUMER_REQUEST_MAX_BYTES=67108864
CONSUMER_IDLE_DISCONNECT_TIMEOUT=0
FETCH_MIN_BYTES=1
GROUP_ID=karapace-rest-proxy
HOST=0.0.0.0
PORT=8082
REGISTRY_HOST=karapace-schema-registry
REGISTRY_PORT=8081
REST_AUTHORIZATION=False
LOG_HANDLER=stdout
LOG_LEVEL=WARNING
LOG_FORMAT=%(asctime)s [%(threadName)s] %(filename)s:%(funcName)s:%(lineno)d %(message)s
MASTER_ELIGIBILITY=True
REPLICATION_FACTOR=1
SECURITY_PROTOCOL=PLAINTEXT
SSL_CHECK_HOSTNAME=True
TOPIC_NAME=_schemas
METADATA_MAX_AGE_MS=60000
ADMIN_METADATA_MAX_AGE=5
PRODUCER_ACKS=1
PRODUCER_COUNT=5
PRODUCER_LINGER_MS=100
PRODUCER_MAX_REQUEST_SIZE=1048576
SESSION_TIMEOUT_MS=10000
KARAPACE_REST=True
KARAPACE_REGISTRY=False
KARAPACE_PORT=8082
NAME_STRATEGY=topic_name
NAME_STRATEGY_VALIDATION=True
MASTER_ELECTION_STRATEGY=lowest
PROTOBUF_RUNTIME_DIRECTORY=runtime
STATSD_HOST=statsd-exporter
STATSD_PORT=8125
KAFKA_SCHEMA_READER_STRICT_MODE=False
KAFKA_RETRIABLE_ERRORS_SILENCED=True
USE_PROTOBUF_FORMATTER=False
HTTP_REQUEST_MAX_SIZE=1048576
TAGS='{ "app": "karapace-rest-proxy" }'
39 changes: 0 additions & 39 deletions container/start.sh

This file was deleted.

4 changes: 3 additions & 1 deletion karapace.config.env
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
ACCESS_LOGS_DEBUG=False
ACCESS_LOG_CLASS=aiohttp.web_log.AccessLogger
ADVERTISED_HOSTNAME=127.0.0.1
ADVERTISED_PORT=8081
ADVERTISED_PROTOCOL=http
BOOTSTRAP_URI=127.0.0.1:9092
BOOTSTRAP_URI=kafka:29092
CLIENT_ID=sr-1
COMPATIBILITY=BACKWARD
CONNECTIONS_MAX_IDLE_MS=15000
Expand Down Expand Up @@ -43,3 +44,4 @@ STATSD_PORT=8125
KAFKA_SCHEMA_READER_STRICT_MODE=False
KAFKA_RETRIABLE_ERRORS_SILENCED=True
USE_PROTOBUF_FORMATTER=False
REST_BASE_URI=http://karapace-rest-proxy:8082
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ include-package-data = true
where = ["src"]

[tool.setuptools.package-data]
schema_registry = ["*.yaml"]
karapace = ["*.yaml"]

[tool.setuptools_scm]
version_file = "src/karapace/version.py"
Expand Down
Loading

0 comments on commit 8224657

Please sign in to comment.