Skip to content

Commit

Permalink
feat: add kpack k8s resources
Browse files Browse the repository at this point in the history
  • Loading branch information
Panaetius committed Nov 22, 2024
1 parent a67abce commit 7d0dacf
Show file tree
Hide file tree
Showing 7 changed files with 396 additions and 1 deletion.
10 changes: 10 additions & 0 deletions .devcontainer/kpack/clusterstack.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
apiVersion: kpack.io/v1alpha2
kind: ClusterStack
metadata:
name: base
spec:
id: "io.buildpacks.stacks.jammy"
buildImage:
image: "paketobuildpacks/build-jammy-base"
runImage:
image: "paketobuildpacks/run-jammy-base"
12 changes: 12 additions & 0 deletions .devcontainer/kpack/clusterstore.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
apiVersion: kpack.io/v1alpha2
kind: ClusterStore
metadata:
name: default
spec:
sources:
- image: gcr.io/paketo-buildpacks/java
- image: gcr.io/paketo-buildpacks/nodejs
- image: gcr.io/paketo-buildpacks/python
- image: gcr.io/paketo-buildpacks/procfile
- image: gcr.io/paketo-buildpacks/source-removal
- image: gcr.io/paketo-buildpacks/environment-variables
19 changes: 19 additions & 0 deletions .devcontainer/kpack/python-builder.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
apiVersion: kpack.io/v1alpha2
kind: Builder
metadata:
name: python-builder-2
namespace: default
spec:
tag: k3d-myregistry.localhost:12345/apps/python-builder
stack:
name: base
kind: ClusterStack
store:
name: default
kind: ClusterStore
order:
- group:
- id: paketo-buildpacks/python
- id: paketo-buildpacks/procfile
- id: paketo-buildpacks/source-removal
- id: paketo-buildpacks/environment-variables
8 changes: 7 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -156,13 +156,19 @@ help: ## Display this help.

k3d_cluster: ## Creates a k3d cluster for testing
k3d cluster delete
k3d cluster create --agents 1 --k3s-arg --disable=metrics-server@server:0
k3d registry create myregistry.localhost --port 12345
k3d cluster create --agents 1 --k3s-arg --disable=metrics-server@server:0 --registry-use k3d-myregistry.localhost:12345

install_amaltheas: ## Installs both version of amalthea in the. NOTE: It uses the currently active k8s context.
helm repo add renku https://swissdatasciencecenter.github.io/helm-charts
helm repo update
helm upgrade --install amalthea-js renku/amalthea --version $(AMALTHEA_JS_VERSION)
helm upgrade --install amalthea-se renku/amalthea-sessions --version ${AMALTHEA_SESSIONS_VERSION}
install_kpack:
curl -L https://github.com/buildpacks-community/kpack/releases/download/v0.15.0/release-0.15.0.yaml | kubectl apply -f -
kubectl apply -f .devcontainer/kpack/clusterstore.yaml
kubectl apply -f .devcontainer/kpack/clusterstack.yaml
kubectl apply -f .devcontainer/kpack/python-builder.yaml

# TODO: Add the version variables from the top of the file here when the charts are fully published
amalthea_schema: ## Updates generates pydantic classes from CRDs
Expand Down
16 changes: 16 additions & 0 deletions components/renku_data_services/errors/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,19 @@ class SecretCreationError(BaseError):
code: int = 1511
message: str = "An error occurred creating secrets."
status_code: int = 500


@dataclass
class CannotStartBuildError(ProgrammingError):
"""Raised when an image build couldn't be started."""

code: int = 1512
message: str = "An error occurred creating an image build." ""


@dataclass
class DeleteBuildError(ProgrammingError):
"""Raised when an image build couldn't be deleted."""

code: int = 1513
message: str = "An error occurred deleting an image build." ""
178 changes: 178 additions & 0 deletions components/renku_data_services/session/crs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
"""Custom Resources for environments, mainly kpack."""

from datetime import datetime
from typing import Self

from pydantic import BaseModel, ConfigDict, Field, model_validator


class Metadata(BaseModel):
"""Basic k8s metadata spec."""

class Config:
"""Do not exclude unknown properties."""

extra = "allow"

name: str
namespace: str | None = None
labels: dict[str, str] = Field(default_factory=dict)
annotations: dict[str, str] = Field(default_factory=dict)
uid: str | None = None
creationTimestamp: datetime | None = None
deletionTimestamp: datetime | None = None


class EnvItem(BaseModel):
"""Environment variable definition."""

name: str
value: str


class ResourceRequest(BaseModel):
"""Resource request entry."""

cpu: str
memory: str


class K8sResourceRequest(BaseModel):
"""K8s resource request."""

requests: ResourceRequest
limits: ResourceRequest


class ImagePullSecret(BaseModel):
"""K8s image pull secret."""

name: str


class PersistentVolumeReference(BaseModel):
"""Reference to a persistent volume claim."""

persistentVolumeClaimName: str


class KpackBuilderReference(BaseModel):
"""Refernce to Kpack builder."""

name: str
kind: str = "Builder"


class DockerImage(BaseModel):
"""Docker Image."""

image: str


class DockerImageWithSecret(DockerImage):
"""Docker image with a pull secret."""

imagePullSecrets: list[ImagePullSecret]


class KpackGitSource(BaseModel):
"""Git repository source."""

url: str
revision: str


class KpackBlobSource(BaseModel):
"""Blob/file archive source."""

url: str
stripComponents: str


class KpackSource(BaseModel):
"""Kpack files source resource."""

git: KpackGitSource | None = None
blob: KpackBlobSource | None = None

@model_validator(mode="after")
def validate(self) -> Self:
"""Validate mode data."""
if bool(self.git) == bool(self.blob):
raise ValueError("'git' and 'blob' are mutually exclusive and one of them must be set.")
return self


class KpackBuildCustomization(BaseModel):
"""Customization of a kpack build."""

env: list[EnvItem]


class KpackImageSpec(BaseModel):
"""KPack image spec model."""

tag: str
additionalTags: list[str]
serviceAccountName: str
builder: KpackBuilderReference
source: KpackSource
build: KpackBuildCustomization
successBuildHistoryLimit: int = 1
failedBuildHistoryLimit: int = 1


class KpackImage(BaseModel):
"""Kpack Image resource."""

model_config = ConfigDict(
extra="allow",
)
kind: str = "Image"
apiVersion: str = "kpack.io/v1alpha2"
metadata: Metadata
spec: KpackImageSpec


class KpackVolumeCache(BaseModel):
"""Persistent volume to serve as cache for kpack build."""

volume: PersistentVolumeReference


class ImageTagReference(BaseModel):
"""Reference to an image tag."""

tag: str


class KpackCacheImage(BaseModel):
"""Image definition to use as build cache."""

registry: ImageTagReference


class KpackBuildSpec(BaseModel):
"""Spec for kpack build."""

builder: DockerImageWithSecret
cache: KpackVolumeCache | KpackCacheImage
env: list[EnvItem]
resources: K8sResourceRequest
runImage: DockerImage
serviceAccountName: str
source: KpackSource
tags: list[str]
activeDeadlineSeconds: int = 1800


class KpackBuild(BaseModel):
"""KPack build resource."""

model_config = ConfigDict(
extra="allow",
)
kind: str = "Build"
apiVersion: str = "kpack.io/v1alpha2"
metadata: Metadata
spec: KpackBuildSpec
Loading

0 comments on commit 7d0dacf

Please sign in to comment.