|
| 1 | +# Architecture |
| 2 | + |
| 3 | +## High level overview |
| 4 | + |
| 5 | +Knative Func is a CLI that simplifies building and deploying serverless functions |
| 6 | +on Kubernetes. Instead of writing Dockerfiles and Kubernetes manifests, |
| 7 | +developers run `func create`, `func build`, and `func deploy`. The CLI handles |
| 8 | +image building (via Buildpacks, S2I, or direct OCI), registry pushing, and |
| 9 | +Knative Service creation or newly available bare K8s deployment. |
| 10 | + |
| 11 | +## Code Map |
| 12 | + |
| 13 | +``` |
| 14 | +cmd/ # CLI commands (Cobra) - START HERE |
| 15 | + func/main.go # Entry point |
| 16 | + build.go, deploy.go # Individual commands |
| 17 | +
|
| 18 | +pkg/ |
| 19 | + functions/ # Core library - the "brain" |
| 20 | + client.go # Central orchestrator, defines all interfaces |
| 21 | + function.go # Function type representing a project |
| 22 | +
|
| 23 | + buildpacks/ # Pack/Cloud Native Buildpacks builder (in container) |
| 24 | + s2i/ # Source-to-Image builder (in container) |
| 25 | + oci/ # Direct OCI image builder (on host) |
| 26 | +
|
| 27 | + knative/ # Knative Serving deployer |
| 28 | + k8s/ # Kubernetes utilities |
| 29 | +
|
| 30 | + pipelines/tekton/ # Remote builds via Tekton |
| 31 | + mcp/ # AI agent integration (MCP server) |
| 32 | +
|
| 33 | +templates/ # Function scaffolding (go, node, python, etc.) |
| 34 | +generate/ # Embedded filesystem (generated) |
| 35 | +schema/ # JSON schemas |
| 36 | +``` |
| 37 | + |
| 38 | +## Entry Point |
| 39 | + |
| 40 | +``` |
| 41 | +cmd/func/main.go |
| 42 | + └── pkg/app.Main() |
| 43 | + └── cmd.NewRootCmd() |
| 44 | + └── Individual commands (build, deploy, create, ...) |
| 45 | +``` |
| 46 | + |
| 47 | +The CLI in `cmd/` servers as a terminal frontend and a reference implementation |
| 48 | +for using `pkg/functions`. Commands parse flags, create a `Client` and |
| 49 | +delegate to `pkg/functions/client.go`: |
| 50 | + |
| 51 | +```go |
| 52 | +// Typical command pattern |
| 53 | +client, _ := fn.New(fn.WithBuilder(buildpacks.NewBuilder())) |
| 54 | +client.Build(ctx, f) |
| 55 | +``` |
| 56 | + |
| 57 | +## Key Search Terms |
| 58 | + |
| 59 | +When exploring the codebase, search for these: |
| 60 | + |
| 61 | +| Term | Where | What it does | |
| 62 | +|------|-------|--------------| |
| 63 | +| `fn.Client` | `pkg/functions/client.go` | Main client type for all operations | |
| 64 | +| `NewClient` | `cmd/client.go` | Creates fully-configured client | |
| 65 | +| `ClientFactory` | `cmd/root.go` | Function type for client creation | |
| 66 | +| `Builder` | `pkg/functions/client.go` | Interface for function builders | |
| 67 | +| `Deployer` | `pkg/functions/client.go` | Interface for function deployers | |
| 68 | +| `Function` | `pkg/functions/function.go` | Type representing a function | |
| 69 | + |
| 70 | +## Layer Architecture |
| 71 | + |
| 72 | +```mermaid |
| 73 | +flowchart TB |
| 74 | + CLI[CLI Layer<br/>cmd/] --> Client[Client Layer<br/>pkg/functions/] |
| 75 | + Client --> Builders[Builders] |
| 76 | + Client --> Deployers[Deployers<br/>pkg/k8s/, pkg/knative/] |
| 77 | + Client --> Pipelines[Pipelines<br/>pkg/pipelines/] |
| 78 | + Client --> Other[Other Providers] |
| 79 | +
|
| 80 | + Builders --> Buildpack[Buildpacks<br/>pkg/buildpacks/] |
| 81 | + Builders --> S2I[S2I<br/>pkg/s2i/] |
| 82 | + Builders --> OCI[OCI<br/>pkg/oci/] |
| 83 | +``` |
| 84 | + |
| 85 | +### CLI Layer (`cmd/`) |
| 86 | + |
| 87 | +- Cobra commands: build.go, deploy.go, create.go, run.go, delete.go, etc. |
| 88 | +- Handles flags, user prompts, output formatting |
| 89 | +- Delegates all business logic to Client |
| 90 | + |
| 91 | +### Client Layer (`pkg/functions/`) |
| 92 | + |
| 93 | +- `client.go`: Central orchestrator for all function operations |
| 94 | +- Defines core interfaces: Builder, Pusher, Deployer, Runner, Remover, Lister, Describer |
| 95 | +- `function.go`: Function type representing a function project |
| 96 | + |
| 97 | +### Builders |
| 98 | + |
| 99 | +Implement the `Builder` interface (source → OCI image): |
| 100 | +- `pkg/buildpacks/`: Cloud Native Buildpacks via Pack |
| 101 | +- `pkg/s2i/`: Red Hat Source-to-Image |
| 102 | +- `pkg/oci/`: Direct OCI image building on host |
| 103 | + |
| 104 | +### Deployers |
| 105 | + |
| 106 | +- `pkg/knative/`: Creates/updates Knative Services |
| 107 | +- `pkg/k8s/`: Kubernetes utilities & Services |
| 108 | + |
| 109 | +## Function Lifecycle |
| 110 | + |
| 111 | +```mermaid |
| 112 | +flowchart LR |
| 113 | + Create[func create] --> Build[func build] |
| 114 | + Create[func create] --> Deploy[func Deploy] |
| 115 | + Create[func create] --> Run[func Run] |
| 116 | + Deploy --> Invoke[func invoke] |
| 117 | + Run --> Invoke[func invoke] |
| 118 | +
|
| 119 | + Build --> Deploy[func deploy] |
| 120 | + Build --> Run[func run] |
| 121 | +
|
| 122 | + subgraph "Optional" |
| 123 | + Build[func build] |
| 124 | + end |
| 125 | +
|
| 126 | + subgraph "Local Dev" |
| 127 | + Run[func run] |
| 128 | + end |
| 129 | +``` |
| 130 | + |
| 131 | +| Stage | Client Method | Description | |
| 132 | +|-------|---------------|-------------| |
| 133 | +| Create | `Init()` | Create boilerplate project from template | |
| 134 | +| Scaffold | `Scaffold()` | Scaffold project as a service| |
| 135 | +| Build | `Build()` | Produce OCI image via Builder | |
| 136 | +| Deploy | `Deploy()` | Push image, create K8s/Knative Service | |
| 137 | +| Run | `Run()` | Local execution for development | |
| 138 | +| Invoke | `Invoke()` | Send request to deployed function | |
| 139 | + |
| 140 | +## Key Interfaces |
| 141 | + |
| 142 | +Defined in `pkg/functions/client.go`: |
| 143 | + |
| 144 | +| Interface | Purpose | |
| 145 | +|-----------|---------| |
| 146 | +| `Builder` | Source → OCI image | |
| 147 | +| `Pusher` | Image → Registry | |
| 148 | +| `Deployer` | Image → Service on K8s | |
| 149 | +| `Runner` | Local execution | |
| 150 | +| `Remover` | Delete deployed function | |
| 151 | +| `Lister` | List deployed functions | |
| 152 | +| `Describer` | Describe function instances | |
| 153 | +| `PipelinesProvider` | Remote build orchestration | |
| 154 | + |
| 155 | +## Subsystems |
| 156 | + |
| 157 | +### Pipelines (Remote Builds) |
| 158 | + |
| 159 | +Located in `pkg/pipelines/tekton/`. Enables `func deploy --remote` to build on-cluster via Tekton instead of locally. Supports Pipelines-as-Code (PAC) for GitOps workflows. |
| 160 | + |
| 161 | +### MCP Server |
| 162 | + |
| 163 | +Located in `pkg/mcp/`. Started via `func mcp start` (Agents should run this). Allows AI agents (Claude Code, Cursor) to create/build/deploy functions programmatically. Read-only by default; set `FUNC_ENABLE_MCP_WRITE=true` for full access. |
| 164 | + |
| 165 | +### Configuration |
| 166 | + |
| 167 | +- `func.yaml`: Function metadata, build/deploy options, environment variables. Schema: `schema/func_yaml-schema.json` |
| 168 | +- Templates in `templates/` embedded at build time into `generate/zz_filesystem_generated.go` |
| 169 | +- Runtimes: go, node, python, rust, quarkus, springboot, typescript |
| 170 | +- Invocation styles: http, cloudevents |
| 171 | + |
| 172 | +## AI |
| 173 | + |
| 174 | +### Custom instructions file |
| 175 | + |
| 176 | +Currently the `AGENTS.md` file is read by many agents by default. The very top |
| 177 | +of that file defines an optional custom override `AGENTS.override.md` and instructs |
| 178 | +agents to read it with highest precedence. You can create that file and add it at |
| 179 | +root of the repository if you so desire. It won't be source controlled (see `.gitignore`) |
0 commit comments