Skip to content

Commit

Permalink
feat: add knative webhook Rock
Browse files Browse the repository at this point in the history
  • Loading branch information
NohaIhab committed Nov 22, 2024
1 parent dc35fc7 commit 3da9e22
Show file tree
Hide file tree
Showing 3 changed files with 184 additions and 0 deletions.
70 changes: 70 additions & 0 deletions knative-webhook/rockcraft.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
name: knative-webhook
summary: Knative webhook
description: "Knative webhook"
version: "1.12.4"
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.12.4/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-operator-webhook:
override: replace
summary: "Knative webhook service"
startup: enabled
command: "/ko-app/webhook"

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
webhook:
plugin: go
source: https://github.com/knative/operator
source-type: git
source-tag: knative-v1.12.4
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.19/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.12.4/.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: |
go mod download
# Build
go build -a -o webhook ./cmd/webhook
# Copy the files from the ko-data directory to the install directory
mkdir -p $CRAFT_PART_INSTALL/var/run/ko
# cp with `-L` to copy the linked file rather than the symlink.
cp -L -r $CRAFT_PART_SRC/cmd/webhook/kodata/. $CRAFT_PART_INSTALL/var/run/ko
# Copy the go binary to the install directory
mkdir $CRAFT_PART_INSTALL/ko-app
cp -r webhook $CRAFT_PART_INSTALL/ko-app/webhook
60 changes: 60 additions & 0 deletions knative-webhook/tests/test_rock.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# 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/webhook",
],
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,
)
54 changes: 54 additions & 0 deletions knative-webhook/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

0 comments on commit 3da9e22

Please sign in to comment.