Documentation
¶
Overview ¶
Package azurefunctions emulates Azure Functions and Linux App Service web apps. Function apps' HTTP-triggered functions run in the official Azure Functions host containers behind <app>.<domain>/api/<function>, with the host's own authLevel key enforcement; web apps run their stack or custom image behind <app>.<domain>. Both get the Kudu zip-deploy API on <app>.scm.<domain>. Apps are created through the ARM Microsoft.Web provider, which shares their settings, keys and packages. See PARITY.md.
Index ¶
- Constants
- Variables
- func SlotName(app, slot string) string
- func ValidateConfig(cfg AppConfig) error
- type AppConfig
- type AppInfo
- type ConfigProvider
- type Deployment
- type FunctionDef
- type FunctionInfo
- type Handler
- func (h *Handler) ExtractOperation(*echo.Context) string
- func (h *Handler) ExtractResource(*echo.Context) string
- func (h *Handler) GetSupportedOperations() []string
- func (h *Handler) Handler() echo.HandlerFunc
- func (h *Handler) MatchPriority() int
- func (h *Handler) Name() string
- func (h *Handler) Reset()
- func (h *Handler) Restore(ctx context.Context, data []byte) error
- func (h *Handler) RouteMatcher() service.Matcher
- func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (h *Handler) Shutdown(ctx context.Context)
- func (h *Handler) Snapshot(ctx context.Context) []byte
- func (h *Handler) StartWorker(ctx context.Context) error
- type HostKeys
- type Hosting
- type Identity
- type IdentityResolver
- type InMemoryBackend
- func (b *InMemoryBackend) AdminToken(name string) (string, error)
- func (b *InMemoryBackend) App(name string) (AppInfo, bool)
- func (b *InMemoryBackend) Build(ctx context.Context, name string, zipData []byte) ([]byte, string, error)
- func (b *InMemoryBackend) Close()
- func (b *InMemoryBackend) Configure(s Settings)
- func (b *InMemoryBackend) DeleteApp(name string)
- func (b *InMemoryBackend) DeleteFunction(ctx context.Context, name, fn string) error
- func (b *InMemoryBackend) DeleteFunctionKey(name, fn, keyName string) error
- func (b *InMemoryBackend) DeleteHostKey(name, keyType, keyName string) error
- func (b *InMemoryBackend) Deploy(name string, zipData []byte, deployer, message string) (Deployment, error)
- func (b *InMemoryBackend) Deployments(name string) ([]Deployment, error)
- func (b *InMemoryBackend) Function(ctx context.Context, name, fn string) (FunctionInfo, error)
- func (b *InMemoryBackend) FunctionKeys(name, fn string) (map[string]string, error)
- func (b *InMemoryBackend) Functions(ctx context.Context, name string) ([]FunctionInfo, error)
- func (b *InMemoryBackend) HostKeys(name string) (HostKeys, error)
- func (b *InMemoryBackend) HostName(name string) string
- func (b *InMemoryBackend) IsFlex(name string) bool
- func (b *InMemoryBackend) Names() []string
- func (b *InMemoryBackend) Package(name string) []byte
- func (b *InMemoryBackend) PublishingCredentials(name string) (string, string, string, error)
- func (b *InMemoryBackend) PutApp(name string, cfg AppConfig) AppInfo
- func (b *InMemoryBackend) ReapIdle() int
- func (b *InMemoryBackend) Reset()
- func (b *InMemoryBackend) Restart(name string) error
- func (b *InMemoryBackend) Restore(ctx context.Context, data []byte) error
- func (b *InMemoryBackend) RunCommand(ctx context.Context, name, command, dir string) (container.ExecResult, error)
- func (b *InMemoryBackend) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (b *InMemoryBackend) SetClock(now func() time.Time)
- func (b *InMemoryBackend) SetConnectionStrings(name string, env map[string]string) error
- func (b *InMemoryBackend) SetFunctionKey(name, fn, keyName, value string) (string, error)
- func (b *InMemoryBackend) SetHealthTiming(every, replaceAfter time.Duration)
- func (b *InMemoryBackend) SetHostKey(name, keyType, keyName, value string) (string, error)
- func (b *InMemoryBackend) SetIdentity(r IdentityResolver, t TokenIssuer)
- func (b *InMemoryBackend) SetNetwork(n Network)
- func (b *InMemoryBackend) SetOnClose(fn func())
- func (b *InMemoryBackend) SetPortPool(p PortPool)
- func (b *InMemoryBackend) SetRegistryExchanger(fn RegistryExchanger)
- func (b *InMemoryBackend) SetRuntime(fn func() (Runner, error))
- func (b *InMemoryBackend) SetSettings(name string, settings map[string]string) error
- func (b *InMemoryBackend) SetSlotHooks(rules func(app string) []RoutingRule, ...)
- func (b *InMemoryBackend) SetStopped(name string, stopped bool) error
- func (b *InMemoryBackend) Settings(name string) (map[string]string, error)
- func (b *InMemoryBackend) Snapshot(ctx context.Context) []byte
- func (b *InMemoryBackend) SwapContent(first, second string) error
- func (b *InMemoryBackend) SyncTriggers(ctx context.Context, name string) error
- func (b *InMemoryBackend) WarmUp(ctx context.Context, name string) error
- type Network
- type PortPool
- type Provider
- type RegistryExchanger
- type RoutingRule
- type Runner
- type Settings
- type TokenIssuer
- type UserIdentity
- type WebJobRun
Constants ¶
const ( // KeyTypeFunction and KeyTypeSystem are the host key collections ARM addresses as host/default/{keyType}. KeyTypeFunction = "functionKeys" // KeyTypeSystem is the host key collection extensions use. KeyTypeSystem = "systemKeys" )
const ( // DefaultPort is the function-app and Kudu listener's default TCP port. DefaultPort = 10012 // DefaultDomain is the default hostname suffix: <app>.<domain> serves functions, <app>.scm.<domain> Kudu. DefaultDomain = "azurewebsites.localhost" // EngineAuto runs function apps in the container runtime when one is reachable. EngineAuto = "auto" // EngineNone never starts containers: apps deploy but invocations return 503. EngineNone = "none" // DefaultStartupTimeout bounds how long a function host container may take to accept connections. DefaultStartupTimeout = 5 * time.Minute )
const SlotSep = "/"
SlotSep joins an app name and slot name into the data-plane name of a deployment slot.
Variables ¶
var ( // ErrNilAppContext is returned when Init is called with a nil AppContext. ErrNilAppContext = errors.New("azurefunctions: nil app context") // ErrAppNotFound is returned for an unknown function app. ErrAppNotFound = errors.New("azurefunctions: function app not found") // ErrFunctionNotFound is returned for an unknown function. ErrFunctionNotFound = errors.New("azurefunctions: function not found") // ErrKeyNotFound is returned for an unknown host or function key. ErrKeyNotFound = errors.New("azurefunctions: key not found") // ErrInvalidKeyType is returned for a host key type other than functionKeys or systemKeys. ErrInvalidKeyType = errors.New("azurefunctions: key type must be functionKeys or systemKeys") // ErrInvalidPackage is returned when a deployment is not a readable zip archive. ErrInvalidPackage = errors.New("azurefunctions: deployment package is not a valid zip archive") )
var ErrFunctionAppOnly = errors.New("azurefunctions: operation is only valid for function apps")
ErrFunctionAppOnly is returned by operations that only apply to function apps, not web apps.
var ErrFunctionInCode = errors.New(
"azurefunctions: function is defined in code; remove it from the source and redeploy",
)
ErrFunctionInCode is returned when deleting a function that has no function.json in the deployed package.
Functions ¶
func ValidateConfig ¶
ValidateConfig reports whether the emulator can run the app's stack; function apps are validated at start.
Types ¶
type AppConfig ¶
type AppConfig struct {
Kind string
LinuxFx string
AppCommandLine string
HealthCheckPath string
ACRIdentityID string
Hosting Hosting
Flex bool
AlwaysOn bool
AlwaysReady bool
ACRManagedID bool
}
AppConfig is what ARM supplies about a site: its kind and the runtime stack it selected.
type AppInfo ¶
type AppInfo struct {
Created time.Time
LastDeploy time.Time
HostName string
SCMHost string
Image string
Worker string
Failure string
Stopped bool
Deployed bool
}
AppInfo is the data plane's view of an app.
type ConfigProvider ¶
type ConfigProvider interface {
GetAzureFunctionsSettings() Settings
}
ConfigProvider extracts the Azure Functions settings from the AppContext config.
type Deployment ¶
type Deployment struct {
Received time.Time `json:"received"`
Finished time.Time `json:"finished"`
ID string `json:"id"`
Deployer string `json:"deployer"`
Message string `json:"message"`
Log string `json:"log,omitempty"`
Succeeded bool `json:"succeeded"`
}
Deployment is one Kudu deployment record.
type FunctionDef ¶
FunctionDef is one function discovered from a deployed package's function.json files.
type FunctionInfo ¶
type FunctionInfo struct {
Config map[string]any
Name string
Language string
InvokeURL string
IsDisabled bool
}
FunctionInfo describes one function of an app.
type Handler ¶
type Handler struct {
Backend *InMemoryBackend
Settings Settings
Port int
// contains filtered or unexported fields
}
Handler owns the function-app listener.
func NewHandler ¶
func NewHandler(backend *InMemoryBackend, settings Settings) *Handler
NewHandler returns a Handler over backend.
func (*Handler) ExtractOperation ¶
ExtractOperation implements service.Service.
func (*Handler) ExtractResource ¶
ExtractResource implements service.Service.
func (*Handler) GetSupportedOperations ¶
GetSupportedOperations implements service.Service.
func (*Handler) Handler ¶
func (h *Handler) Handler() echo.HandlerFunc
Handler returns a 404 handler; the real API is on the dedicated port.
func (*Handler) MatchPriority ¶
MatchPriority implements service.Registerable.
func (*Handler) RouteMatcher ¶
RouteMatcher never matches: function apps listen on their own port.
func (*Handler) ServeHTTP ¶
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP serves the dedicated listener.
type HostKeys ¶
type HostKeys struct {
FunctionKeys map[string]string
SystemKeys map[string]string
MasterKey string
}
HostKeys are an app's host-level keys.
type Hosting ¶
type Hosting string
Hosting is the plan family an app runs on; it decides when an idle host is unloaded.
type Identity ¶
type Identity struct {
TenantID string
SystemPrincipalID string
User []UserIdentity
}
Identity is the managed identity of an app.
type IdentityResolver ¶
IdentityResolver returns the managed identity of the named app.
type InMemoryBackend ¶
type InMemoryBackend struct {
// contains filtered or unexported fields
}
InMemoryBackend holds all function apps under one lock.
func NewInMemoryBackend ¶
func NewInMemoryBackend() *InMemoryBackend
NewInMemoryBackend creates an empty backend with no container runtime.
func (*InMemoryBackend) AdminToken ¶
func (b *InMemoryBackend) AdminToken(name string) (string, error)
AdminToken returns a JWT the app's Functions host accepts as a bearer token on its /admin APIs.
func (*InMemoryBackend) App ¶
func (b *InMemoryBackend) App(name string) (AppInfo, bool)
App returns the app's data-plane view.
func (*InMemoryBackend) Build ¶
func (b *InMemoryBackend) Build(ctx context.Context, name string, zipData []byte) ([]byte, string, error)
Build installs the package's dependencies in a throwaway container of the app's runtime image and returns the rebuilt zip and the build log.
func (*InMemoryBackend) Close ¶
func (b *InMemoryBackend) Close()
Close stops every host and runs the onClose hook.
func (*InMemoryBackend) Configure ¶
func (b *InMemoryBackend) Configure(s Settings)
Configure applies the listener-facing settings.
func (*InMemoryBackend) DeleteApp ¶
func (b *InMemoryBackend) DeleteApp(name string)
DeleteApp stops and removes the app.
func (*InMemoryBackend) DeleteFunction ¶
func (b *InMemoryBackend) DeleteFunction(ctx context.Context, name, fn string) error
DeleteFunction removes a function.json function's directory from the deployed package and restarts the host.
func (*InMemoryBackend) DeleteFunctionKey ¶
func (b *InMemoryBackend) DeleteFunctionKey(name, fn, keyName string) error
DeleteFunctionKey removes a function key.
func (*InMemoryBackend) DeleteHostKey ¶
func (b *InMemoryBackend) DeleteHostKey(name, keyType, keyName string) error
DeleteHostKey removes a host key.
func (*InMemoryBackend) Deploy ¶
func (b *InMemoryBackend) Deploy(name string, zipData []byte, deployer, message string) (Deployment, error)
Deploy stores a zip package as the app's site content and records the deployment.
func (*InMemoryBackend) Deployments ¶
func (b *InMemoryBackend) Deployments(name string) ([]Deployment, error)
Deployments returns the app's deployments, newest last.
func (*InMemoryBackend) Function ¶
func (b *InMemoryBackend) Function(ctx context.Context, name, fn string) (FunctionInfo, error)
Function returns one function of the app.
func (*InMemoryBackend) FunctionKeys ¶
func (b *InMemoryBackend) FunctionKeys(name, fn string) (map[string]string, error)
FunctionKeys returns a function's keys, creating its default key on first use as Azure does.
func (*InMemoryBackend) Functions ¶
func (b *InMemoryBackend) Functions(ctx context.Context, name string) ([]FunctionInfo, error)
Functions lists the app's functions. With a container runtime they come from the running host's admin API, which also sees code-first (no function.json) functions; without one, from the deployed package's function.json files.
func (*InMemoryBackend) HostKeys ¶
func (b *InMemoryBackend) HostKeys(name string) (HostKeys, error)
HostKeys returns the app's master, function and system keys.
func (*InMemoryBackend) HostName ¶
func (b *InMemoryBackend) HostName(name string) string
HostName is the default hostname apps are served under, including the listener port.
func (*InMemoryBackend) IsFlex ¶
func (b *InMemoryBackend) IsFlex(name string) bool
IsFlex reports whether the app is on the Flex Consumption plan.
func (*InMemoryBackend) Names ¶
func (b *InMemoryBackend) Names() []string
Names lists app names in order.
func (*InMemoryBackend) Package ¶
func (b *InMemoryBackend) Package(name string) []byte
Package returns the deployed zip, or nil.
func (*InMemoryBackend) PublishingCredentials ¶
PublishingCredentials returns the Kudu user name, password and base URL.
func (*InMemoryBackend) PutApp ¶
func (b *InMemoryBackend) PutApp(name string, cfg AppConfig) AppInfo
PutApp creates the app or updates its kind and stack, keeping settings, keys and package.
func (*InMemoryBackend) ReapIdle ¶
func (b *InMemoryBackend) ReapIdle() int
ReapIdle unloads the host of every app that has gone idle past its plan's limit and returns how many it took down. The next request starts the host again (a cold start). Apps with in-flight requests, with a stopped state, or whose triggers need the host are left alone.
func (*InMemoryBackend) Reset ¶
func (b *InMemoryBackend) Reset()
Reset stops every host and clears all apps.
func (*InMemoryBackend) Restart ¶
func (b *InMemoryBackend) Restart(name string) error
Restart discards the running host; the next request (or, for background triggers, a fresh start) replaces it.
func (*InMemoryBackend) Restore ¶
func (b *InMemoryBackend) Restore(ctx context.Context, data []byte) error
Restore loads a snapshot, replacing every app; hosts start again on first use.
func (*InMemoryBackend) RunCommand ¶
func (b *InMemoryBackend) RunCommand(ctx context.Context, name, command, dir string) (container.ExecResult, error)
RunCommand runs a shell command in dir inside the app's host container.
func (*InMemoryBackend) ServeHTTP ¶
func (b *InMemoryBackend) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP routes a request to an app's function host or its Kudu API.
func (*InMemoryBackend) SetClock ¶
func (b *InMemoryBackend) SetClock(now func() time.Time)
SetClock replaces the clock that idle reaping measures against.
func (*InMemoryBackend) SetConnectionStrings ¶
func (b *InMemoryBackend) SetConnectionStrings(name string, env map[string]string) error
SetConnectionStrings replaces the app's connection strings, given as the prefixed environment variables the host sees (SQLCONNSTR_name and the like); a running host restarts on its next request.
func (*InMemoryBackend) SetFunctionKey ¶
func (b *InMemoryBackend) SetFunctionKey(name, fn, keyName, value string) (string, error)
SetFunctionKey creates or replaces a function key; an empty value generates one.
func (*InMemoryBackend) SetHealthTiming ¶
func (b *InMemoryBackend) SetHealthTiming(every, replaceAfter time.Duration)
SetHealthTiming sets how often a running web app's health check path is pinged and how long continuous failures last before the instance is replaced.
func (*InMemoryBackend) SetHostKey ¶
func (b *InMemoryBackend) SetHostKey(name, keyType, keyName, value string) (string, error)
SetHostKey creates or replaces a host key; an empty value generates one. It returns the stored value.
func (*InMemoryBackend) SetIdentity ¶
func (b *InMemoryBackend) SetIdentity(r IdentityResolver, t TokenIssuer)
SetIdentity wires the managed identity lookup and token issuer behind the apps' local token service.
func (*InMemoryBackend) SetNetwork ¶
func (b *InMemoryBackend) SetNetwork(n Network)
SetNetwork wires the container-visible address of the emulator's storage listeners.
func (*InMemoryBackend) SetOnClose ¶
func (b *InMemoryBackend) SetOnClose(fn func())
SetOnClose registers a hook run by Close after every container is stopped.
func (*InMemoryBackend) SetPortPool ¶
func (b *InMemoryBackend) SetPortPool(p PortPool)
SetPortPool wires the host port source for published containers.
func (*InMemoryBackend) SetRegistryExchanger ¶
func (b *InMemoryBackend) SetRegistryExchanger(fn RegistryExchanger)
SetRegistryExchanger wires the registry token exchange behind managed-identity image pulls.
func (*InMemoryBackend) SetRuntime ¶
func (b *InMemoryBackend) SetRuntime(fn func() (Runner, error))
SetRuntime wires the container runtime source; nil disables function hosts.
func (*InMemoryBackend) SetSettings ¶
func (b *InMemoryBackend) SetSettings(name string, settings map[string]string) error
SetSettings replaces the app's application settings; a running host restarts on its next request.
func (*InMemoryBackend) SetSlotHooks ¶
func (b *InMemoryBackend) SetSlotHooks( rules func(app string) []RoutingRule, onDeploy func(ctx context.Context, name string), )
SetSlotHooks wires what the data plane asks ARM about slots: the traffic routing rules of a production site and what to do after a deployment (auto swap).
func (*InMemoryBackend) SetStopped ¶
func (b *InMemoryBackend) SetStopped(name string, stopped bool) error
SetStopped starts (false) or stops (true) the app.
func (*InMemoryBackend) Settings ¶
func (b *InMemoryBackend) Settings(name string) (map[string]string, error)
Settings returns a copy of the app's application settings.
func (*InMemoryBackend) Snapshot ¶
func (b *InMemoryBackend) Snapshot(ctx context.Context) []byte
Snapshot serialises apps, settings, keys, deployments and package bytes; running hosts are not part of it.
func (*InMemoryBackend) SwapContent ¶
func (b *InMemoryBackend) SwapContent(first, second string) error
SwapContent exchanges the deployed site content of two apps (or an app and its slot) and restarts both.
func (*InMemoryBackend) SyncTriggers ¶
func (b *InMemoryBackend) SyncTriggers(ctx context.Context, name string) error
SyncTriggers refreshes the app's trigger metadata: it starts the host if needed and reads its function list.
type Network ¶
type Network struct {
// Host is the address containers use for the emulator; empty picks the container runtime's default gateway.
Host string
BlobPort int
QueuePort int
TablePort int
}
Network tells function hosts how to reach the emulator's data-plane listeners from inside a container.
type Provider ¶
type Provider struct{}
Provider implements service.Provider for Azure Functions.
func (*Provider) Init ¶
func (p *Provider) Init(ctx *service.AppContext) (service.Registerable, error)
Init builds the backend and handler; the port is bound by Handler.StartWorker.
type RegistryExchanger ¶
type RegistryExchanger func(ctx context.Context, registryHost, tenant, accessToken string) (string, error)
RegistryExchanger trades a Microsoft Entra access token for a registry refresh token at the login server's /oauth2/exchange endpoint, the way az acr login does.
type RoutingRule ¶
RoutingRule sends a share of a production site's traffic to one of its slots.
type Settings ¶
type Settings struct {
Engine string `` //nolint:lll // config struct tags are intentionally verbose
/* 229-byte string literal not displayed */
Domain string `` //nolint:lll // config struct tags are intentionally verbose
/* 300-byte string literal not displayed */
ContainerHost string `` //nolint:lll // config struct tags are intentionally verbose
/* 322-byte string literal not displayed */
Port int `` //nolint:lll // config struct tags are intentionally verbose
/* 256-byte string literal not displayed */
StartupTimeout time.Duration `` //nolint:lll // config struct tags are intentionally verbose
/* 226-byte string literal not displayed */
}
Settings is the Kong-embedded configuration of the Azure Functions emulator.
func DefaultSettings ¶
func DefaultSettings() Settings
DefaultSettings returns the settings used when no ConfigProvider is available.
type TokenIssuer ¶
TokenIssuer mints an access token for resource on behalf of the service principal objectID of clientID.
type UserIdentity ¶
UserIdentity is a user-assigned managed identity of an app.
Source Files
¶
- admin.go
- autostart.go
- backend.go
- build.go
- codefirst.go
- errors.go
- functions.go
- handler.go
- health.go
- identity.go
- idle.go
- keymgmt.go
- keys.go
- kudu.go
- kudu_continuous.go
- kudu_exec.go
- kudu_vfs.go
- kudu_vfs_home.go
- kudu_webjobs.go
- lifecycle.go
- network.go
- package_url.go
- persistence.go
- provider.go
- registry.go
- routing.go
- runtimes.go
- serve.go
- settings.go
- slots.go
- webapp.go
- zip.go