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

Add Knative Eventing Mtchannel_broker rock and tests #35

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
78 changes: 78 additions & 0 deletions knative-eventing-mtchannel_broker/rockcraft.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# Based on ko image: https://github.com/knative/eventing/tree/knative-v1.16.1/cmd/broker/filter
name: knative-eventing-mtchannel-broker
summary: Knative Eventing Mtchannel-broker
description: "Knative Eventing Mtchannel-broker"
version: "1.16.1"
license: Apache-2.0
base: ubuntu@22.04
platforms:
amd64:
run-user: _daemon_

environment:
# Required due to the go codebase relying on the OS Env being set
# See https://github.com/knative/operator/blob/knative-v1.16.0/pkg/reconciler/common/releases.go#L36
KO_DATA_PATH: "/var/run/ko"
# env identifies where to locate the SSL certificate file
SSL_CERT_FILE: "/etc/ssl/certs/ca-certificates.crt"

services:
knative-eventing-mtchannel-broker:
override: replace
summary: "Knative Eventing Mtchannel Broker service"
startup: enabled
command: "/ko-app/mtchannel_broker"

parts:
security-team-requirement:
plugin: nil
override-build: |
mkdir -p ${CRAFT_PART_INSTALL}/usr/share/rocks
(echo "# os-release" && cat /etc/os-release && echo "# dpkg-query" && \
dpkg-query --root=${CRAFT_PROJECT_DIR}/../bundles/ubuntu-22.04/rootfs/ -f '${db:Status-Abbrev},${binary:Package},${Version},${source:Package},${Source:Version}\n' -W) \
> ${CRAFT_PART_INSTALL}/usr/share/rocks/dpkg.query

mtchannel_broker:
plugin: go
source: https://github.com/knative/eventing
source-type: git
source-tag: knative-v1.16.1
overlay-packages:
# Install ca-certificates found in the base image
# reference: https://github.com/GoogleContainerTools/distroless/blob/main/base/README.md?plain=1#L9.
# Install in overlay instead of stage packages due to https://github.com/canonical/rockcraft/issues/334.
- ca-certificates
build-snaps:
- go/1.22/stable
build-environment:
- CGO_ENABLED: 0
- GOOS: linux
stage-packages:
# Install packages existing in the base for the upstream image.
# Base image is set upstream in https://github.com/knative/operator/blob/knative-v1.16.0/.ko.yaml#L1.
# Packages existing in the base image are documented
# in https://github.com/GoogleContainerTools/distroless/blob/main/base/README.md#image-contents.
- netbase
- tzdata
override-build: |
# patch readOnlyRootFilesystem in manifests applied by the operator
# More details in https://github.com/canonical/knative-operators/issues/291
# Remove once pebble won't need to always write some state to disk
# https://github.com/canonical/pebble/issues/462
find . -type f \
-exec sed -i \
"s#readOnlyRootFilesystem: true#readOnlyRootFilesystem: false#g" \
{} +

go mod download

# Build
go build -a -o mtchannel_broker ./cmd/mtchannel_broker

# Copy the files from the ko-data directory to the install directory
mkdir -p $CRAFT_PART_INSTALL/var/run/ko
#cp -r cmd/mtchannel_broker/kodata/. $CRAFT_PART_INSTALL/var/run/ko

# Copy the go binary to the install directory
mkdir $CRAFT_PART_INSTALL/ko-app
cp -r mtchannel_broker $CRAFT_PART_INSTALL/ko-app/mtchannel_broker
79 changes: 79 additions & 0 deletions knative-eventing-mtchannel_broker/tests/test_rock.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# Copyright 2024 Canonical Ltd.
# See LICENSE file for licensing details.

import pytest
import subprocess

from charmed_kubeflow_chisme.rock import CheckRock


@pytest.mark.abort_on_fail
def test_rock():
"""Test rock."""
check_rock = CheckRock("rockcraft.yaml")
rock_image = check_rock.get_name()
rock_version = check_rock.get_version()
LOCAL_ROCK_IMAGE = f"{rock_image}:{rock_version}"

# assert the rock contains the expected files
subprocess.run(
[
"docker",
"run",
"--rm",
"--entrypoint",
"/bin/bash",
LOCAL_ROCK_IMAGE,
"-c",
"ls -la /var/run/ko",
],
check=True,
)

subprocess.run(
[
"docker",
"run",
"--rm",
"--entrypoint",
"/bin/bash",
LOCAL_ROCK_IMAGE,
"-c",
"ls -la /ko-app/mtchannel_broker",
],
check=True,
)

# check for SSL cert file
subprocess.run(
[
"docker",
"run",
"--rm",
"--entrypoint",
"/bin/bash",
LOCAL_ROCK_IMAGE,
"-c",
"ls -la /etc/ssl/certs/ca-certificates.crt",
],
check=True,
)
# ensure no "readOnlyRootFilesystem: true" in the manifests
subprocess.run(
[
"docker",
"run",
"--rm",
"--entrypoint",
"/bin/bash",
LOCAL_ROCK_IMAGE,
"-c",
# A. if grep found the string (test should fail) then grep returns 0.
# But we want the test to fail, so we do && to return exit code 1
# B. if grep did NOT find the string (test should succecced) then grep returns 1.
# But we want the test to succeed, so in this case the && is not calculated,
# since we have a failing exit code and || exit 0 happens
'grep -ri "readOnlyRootFilesystem: true" /var/run/ko && exit 1 || exit 0',
],
check=True,
)
54 changes: 54 additions & 0 deletions knative-eventing-mtchannel_broker/tox.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Copyright 2024 Canonical Ltd.
# See LICENSE file for licensing details.
[tox]
skipsdist = True
skip_missing_interpreters = True
envlist = pack, export-to-docker, sanity, integration

[testenv]
setenv =
PYTHONPATH={toxinidir}
PYTHONBREAKPOINT=ipdb.set_trace
CHARM_REPO=https://github.com/canonical/kserve-operators.git
CHARM_BRANCH=main
LOCAL_CHARM_DIR=charm_repo

[testenv:pack]
passenv = *
allowlist_externals =
rockcraft
commands =
rockcraft pack

[testenv:export-to-docker]
passenv = *
allowlist_externals =
rockcraft
bash
yq
commands =
# export rock to docker
bash -c 'NAME=$(yq eval .name rockcraft.yaml) && \
VERSION=$(yq eval .version rockcraft.yaml) && \
ARCH=$(yq eval ".platforms | keys | .[0]" rockcraft.yaml) && \
ROCK="$\{NAME\}_$\{VERSION\}_$\{ARCH\}.rock" && \
DOCKER_IMAGE=$NAME:$VERSION && \
echo "Exporting $ROCK to docker as $DOCKER_IMAGE" && \
rockcraft.skopeo --insecure-policy copy oci-archive:$ROCK docker-daemon:$DOCKER_IMAGE'

[testenv:sanity]
passenv = *
deps =
charmed-kubeflow-chisme
pytest
pytest-operator
commands =
# run rock tests
pytest -s -v --tb native --show-capture=all --log-cli-level=INFO {posargs} {toxinidir}/tests

[testenv:integration]
passenv = *
allowlist_externals =
echo
commands =
# TODO: Implement integration tests here