Fn Project

repository·master·Indexed 27 days ago

https://github.com/fnproject/fn

An open-source, event-driven Functions-as-a-Service (FaaS) compute platform that allows developers to run any Docker container as a function. It supports any programming language and runs on public, private, or hybrid clouds. The project includes the Fn CLI for managing functions and apps, the Fn Server for platform orchestration, and a general-purpose container abstraction library in the api/agent/drivers package.

Tokens
3.7K
Snippets
11
Records
26
Agent score
91%

What's inside fn

  1. Implement migration up and down functions

    master

    Each migration file must implement both an up function (to apply the change) and a down function (to revert the change). These functions must match the signature func(context.Context, *sqlx.Tx) error.

    To register the migration, you must use an init() function to append a migratex.MigFields object to the exported global Migrations slice in the migrations package.

    package migrations
    
    import (
    	"context"
    
    	"github.com/fnproject/fn/api/datastore/sql/migratex"
    	"github.com/jmoiron/sqlx"
    )
    
    func up1(ctx context.Context, tx *sqlx.Tx) error {
    	_, err := tx.ExecContext(ctx, "ALTER TABLE routes ADD created_at text;")
    	return err
    }
    
    func down1(ctx context.Context, tx *sqlx.Tx) error {
    	_, err := tx.ExecContext(ctx, "ALTER TABLE routes DROP COLUMN created_at;")
    	return err
    }
    
    func init() {
    	Migrations = append(Migrations, &migratex.MigFields{
    		VersionFunc: vfunc(1),
    		UpFunc:      up1,
    		DownFunc:    down1,
    	})
    }
  2. Install the Fn CLI tool

    master

    The Fn command line tool is used to manage functions, apps, and the Fn server. You can install it using one of the following methods:

    • macOS (Homebrew): Use brew install fn.
    • Linux and macOS (Shell script): Execute the official installation script via curl. If you are behind a proxy, ensure http_proxy and https_proxy environment variables are set.
    • Windows: Follow the specific Windows client installation guide.
    • Manual: Download the binaries directly from the official releases page.
    # macOS
    brew update && brew install fn
    
    # Linux and macOS
    curl -LSs https://raw.githubusercontent.com/fnproject/cli/master/install | sh
  3. Use the Docker-in-Docker (DinD) base image for local development

    master

    The fnproject/dind image is a specialized base image designed for local development, specifically when you need to build other local images (such as images/runner/Dockerfile).

    Key advantages over official docker images:

    • Automatic Filesystem Selection: It automatically selects the best filesystem, avoiding the performance issues of the vfs default used in official images.
    • MTU Mirroring: It attempts to mirror the default external interface's MTU to the DinD network. This resolves connectivity issues when running DinD-based images on Kubernetes clusters using overlay networks that reduce pod MTUs.

    Important Usage Rule: When using this as a base image, you must use CMD for your program, NOT ENTRYPOINT. The image's internal logic handles the startup process via CMD.

    FROM fnproject/dind
    # OTHER STUFF
    CMD ["./myproggie"]
  4. Run the Fn Server

    master

    To start a local Fn server in single server mode (using an embedded database and message queue), use the fn start command.

    Podman or Rancher Desktop Users: You must use a volume to allow the FnServer to create a unix socket file for communication with other Fn containers. You can specify a volume or a directory in the host VM using the --iofs-dir flag.

    Manual Volume Creation (if needed): If you prefer to create the volume manually via Docker/Podman: docker volume create --opt device=tmpfs --opt type=tmpfs --opt o=size=2M,dev,noexec <volume name>

  5. Generate clients using build.rb

    master

    To generate all clients for all Swagger supported languages, follow these steps:

    1. If the API spec has changed, update the version number in swagger.yml.
    2. Execute the build script using Ruby.

    This process uses build.rb to automate the generation of clients based on the Swagger specification.

    ruby build.rb
  6. Create and deploy your first function

    master

    Follow these steps to initialize, create an app, deploy, and invoke a function:

    1. Initialize the function: Use fn init with a --runtime (e.g., go, node, java, python) and a name. This creates a directory for your function.
    2. Create an app: Use fn create app <app_name> to create a top-level collection for your functions.
    3. Deploy the function: Use fn deploy. Use the --local flag to skip pushing to a remote container registry, which speeds up local development.
    4. Invoke the function: Use fn invoke <app_name> <function_name> to execute the function.
  7. Create SQL migrations for the Fn datastore

    master

    When adding database changes to the Fn SQL datastore, you must create migration files following a specific naming convention and structure. Each database change (e.g., a new table, a new column, or a type change) must be treated as an individual migration.

    Naming Convention

    Files must follow the pattern: [0-9]+_[add|remove]_model[_field]*.go.

    The leading number must be monotonically increasing based on the highest existing number in the directory. For example, if 11_add_foo_bar.go exists, your new file should be 12_add_bar_baz.go.

  8. Run the Fn Server

    master

    The Fn server is the core component of the Fn platform. It can be initialized and started using the server.NewFromEnv(ctx) pattern, which configures the server based on environment variables. Once initialized, calling .Start(ctx) begins the server execution. The server includes built-in views for monitoring latency, I/O, memory, and CPU distribution, as well as specialized views for Docker, agents, and containers.

    // Conceptual usage of the server entrypoint
    ctx := context.Background()
    funcServer := server.NewFromEnv(ctx)
    funcServer.Start(ctx)
  9. Simulate container startup delays via environment variables

    master

    You can control the container's initialization and exit behavior using the following environment variables:

    • ENABLE_INIT_DELAY_MSEC: Delay container start by the specified number of milliseconds.
    • ENABLE_INIT_EXIT: Exit the container immediately after start with the specified exit code.
    • ENABLE_EXIT_DELAY_MSEC: Delay container shutdown by the specified number of milliseconds.
    • ENABLE_FOOTER: If set, logs "Container ending" during shutdown.
    • ENABLE_HEADER: If set, logs "Container starting" during startup.