• Stars
    star
    121
  • Rank 283,605 (Top 6 %)
  • Language
    Go
  • License
    Apache License 2.0
  • Created almost 4 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

This repository includes an example plugin, for you to use as a reference for developing your own plugins

This repository includes an example plugin, demo, for you to use as a reference for developing your own plugins.

Build Status

The existing plugins can be browsed into the Plugin Catalog.

Developing a Traefik plugin

Traefik plugins are developed using the Go language.

A Traefik middleware plugin is just a Go package that provides an http.Handler to perform specific processing of requests and responses.

Rather than being pre-compiled and linked, however, plugins are executed on the fly by Yaegi, an embedded Go interpreter.

Usage

For a plugin to be active for a given Traefik instance, it must be declared in the static configuration.

Plugins are parsed and loaded exclusively during startup, which allows Traefik to check the integrity of the code and catch errors early on. If an error occurs during loading, the plugin is disabled.

For security reasons, it is not possible to start a new plugin or modify an existing one while Traefik is running.

Once loaded, middleware plugins behave exactly like statically compiled middlewares. Their instantiation and behavior are driven by the dynamic configuration.

Plugin dependencies must be vendored for each plugin. Vendored packages should be included in the plugin's GitHub repository. (Go modules are not supported.)

Configuration

For each plugin, the Traefik static configuration must define the module name (as is usual for Go packages).

The following declaration (given here in YAML) defines a plugin:

# Static configuration

experimental:
  plugins:
    example:
      moduleName: github.com/traefik/plugindemo
      version: v0.2.1

Here is an example of a file provider dynamic configuration (given here in YAML), where the interesting part is the http.middlewares section:

# Dynamic configuration

http:
  routers:
    my-router:
      rule: host(`demo.localhost`)
      service: service-foo
      entryPoints:
        - web
      middlewares:
        - my-plugin

  services:
   service-foo:
      loadBalancer:
        servers:
          - url: http://127.0.0.1:5000
  
  middlewares:
    my-plugin:
      plugin:
        example:
          headers:
            Foo: Bar

Local Mode

Traefik also offers a developer mode that can be used for temporary testing of plugins not hosted on GitHub. To use a plugin in local mode, the Traefik static configuration must define the module name (as is usual for Go packages) and a path to a Go workspace, which can be the local GOPATH or any directory.

The plugins must be placed in ./plugins-local directory, which should be in the working directory of the process running the Traefik binary. The source code of the plugin should be organized as follows:

./plugins-local/
    └── src
        └── github.com
            └── traefik
                └── plugindemo
                    β”œβ”€β”€ demo.go
                    β”œβ”€β”€ demo_test.go
                    β”œβ”€β”€ go.mod
                    β”œβ”€β”€ LICENSE
                    β”œβ”€β”€ Makefile
                    └── readme.md
# Static configuration

experimental:
  localPlugins:
    example:
      moduleName: github.com/traefik/plugindemo

(In the above example, the plugindemo plugin will be loaded from the path ./plugins-local/src/github.com/traefik/plugindemo.)

# Dynamic configuration

http:
  routers:
    my-router:
      rule: host(`demo.localhost`)
      service: service-foo
      entryPoints:
        - web
      middlewares:
        - my-plugin

  services:
   service-foo:
      loadBalancer:
        servers:
          - url: http://127.0.0.1:5000
  
  middlewares:
    my-plugin:
      plugin:
        example:
          headers:
            Foo: Bar

Defining a Plugin

A plugin package must define the following exported Go objects:

  • A type type Config struct { ... }. The struct fields are arbitrary.
  • A function func CreateConfig() *Config.
  • A function func New(ctx context.Context, next http.Handler, config *Config, name string) (http.Handler, error).
// Package example a example plugin.
package example

import (
	"context"
	"net/http"
)

// Config the plugin configuration.
type Config struct {
	// ...
}

// CreateConfig creates the default plugin configuration.
func CreateConfig() *Config {
	return &Config{
		// ...
	}
}

// Example a plugin.
type Example struct {
	next     http.Handler
	name     string
	// ...
}

// New created a new plugin.
func New(ctx context.Context, next http.Handler, config *Config, name string) (http.Handler, error) {
	// ...
	return &Example{
		// ...
	}, nil
}

func (e *Example) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
	// ...
	e.next.ServeHTTP(rw, req)
}

Logs

Currently, the only way to send logs to Traefik is to use os.Stdout.WriteString("...") or os.Stderr.WriteString("...").

In the future, we will try to provide something better and based on levels.

Plugins Catalog

Traefik plugins are stored and hosted as public GitHub repositories.

Every 30 minutes, the Plugins Catalog online service polls Github to find plugins and add them to its catalog.

Prerequisites

To be recognized by Plugins Catalog, your repository must meet the following criteria:

  • The traefik-plugin topic must be set.
  • The .traefik.yml manifest must exist, and be filled with valid contents.

If your repository fails to meet either of these prerequisites, Plugins Catalog will not see it.

Manifest

A manifest is also mandatory, and it should be named .traefik.yml and stored at the root of your project.

This YAML file provides Plugins Catalog with information about your plugin, such as a description, a full name, and so on.

Here is an example of a typical .traefik.ymlfile:

# The name of your plugin as displayed in the Plugins Catalog web UI.
displayName: Name of your plugin

# For now, `middleware` is the only type available.
type: middleware

# The import path of your plugin.
import: github.com/username/my-plugin

# A brief description of what your plugin is doing.
summary: Description of what my plugin is doing

# Medias associated to the plugin (optional)
iconPath: foo/icon.png
bannerPath: foo/banner.png

# Configuration data for your plugin.
# This is mandatory,
# and Plugins Catalog will try to execute the plugin with the data you provide as part of its startup validity tests.
testData:
  Headers:
    Foo: Bar

Properties include:

  • displayName (required): The name of your plugin as displayed in the Plugins Catalog web UI.
  • type (required): For now, middleware is the only type available.
  • import (required): The import path of your plugin.
  • summary (required): A brief description of what your plugin is doing.
  • testData (required): Configuration data for your plugin. This is mandatory, and Plugins Catalog will try to execute the plugin with the data you provide as part of its startup validity tests.
  • iconPath (optional): A local path in the repository to the icon of the project.
  • bannerPath (optional): A local path in the repository to the image that will be used when you will share your plugin page in social medias.

There should also be a go.mod file at the root of your project. Plugins Catalog will use this file to validate the name of the project.

Tags and Dependencies

Plugins Catalog gets your sources from a Go module proxy, so your plugins need to be versioned with a git tag.

Last but not least, if your plugin middleware has Go package dependencies, you need to vendor them and add them to your GitHub repository.

If something goes wrong with the integration of your plugin, Plugins Catalog will create an issue inside your Github repository and will stop trying to add your repo until you close the issue.

Troubleshooting

If Plugins Catalog fails to recognize your plugin, you will need to make one or more changes to your GitHub repository.

In order for your plugin to be successfully imported by Plugins Catalog, consult this checklist:

  • The traefik-plugin topic must be set on your repository.
  • There must be a .traefik.yml file at the root of your project describing your plugin, and it must have a valid testData property for testing purposes.
  • There must be a valid go.mod file at the root of your project.
  • Your plugin must be versioned with a git tag.
  • If you have package dependencies, they must be vendored and added to your GitHub repository.

More Repositories

1

traefik

The Cloud Native Application Proxy
Go
47,533
star
2

yaegi

Yaegi is Another Elegant Go Interpreter
Go
6,609
star
3

mesh

Traefik Mesh - Simpler Service Mesh
Go
1,979
star
4

traefik-helm-chart

Traefik Proxy Helm Chart
Smarty
939
star
5

whoami

Tiny Go server that prints os information and HTTP request to output
Go
906
star
6

mocktail

Naive code generator that creates mock implementation using testify.mock
Go
199
star
7

traefik-migration-tool

A migration tool from Traefik v1 to Traefik v2
Go
146
star
8

traefik-library-image

Used to build Official Docker image of Traefik Proxy
Dockerfile
131
star
9

blog-posts

Jinja
61
star
10

plugin-rewritebody

Rewrite body is a middleware plugin for Traefik which rewrites the HTTP response body by replacing a search regex by a replacement string
Go
50
star
11

paerser

Loads configuration from many sources
Go
47
star
12

plugin-simplecache

Simple cache plugin middleware caches responses on disk
Go
40
star
13

structor

[Messor Structor 🐜] Manage multiple versions of a Mkdocs documentation
Go
37
star
14

plugin-log4shell

Log4Shell is a middleware plugin for Traefik which blocks JNDI attacks based on HTTP header values
Go
37
star
15

faency

Faency is the Traefik Labs React component library
TypeScript
36
star
16

hub-agent-traefik

Traefik Hub agent for Traefik
Go
34
star
17

lobicornis

πŸ€– [Myrmica Lobicornis 🐜] Bot: Update and Merge Pull Request
Go
29
star
18

hub-agent-kubernetes

Traefik Hub agent for Kubernetes
Go
19
star
19

aloba

πŸ€– [Myrmica Aloba 🐜] Bot: Add labels and milestone on pull requests and issues
Go
18
star
20

whoamitcp

Tiny Go server that prints os information and TCP request to output
Go
17
star
21

plugin-blockpath

Block Path is a middleware plugin for Traefik which sends an HTTP 403 Forbidden response when the requested HTTP path matches one the configured regular expressions
Go
15
star
22

pluginproviderdemo

This repository includes an example provider plugin, for you to use as a reference for developing your own plugins
Go
13
star
23

mesh-helm-chart

Traefik Mesh - Helm Chart
Mustache
13
star
24

whoamiudp

Tiny Go webserver that prints os information and UDP request to output
Go
9
star
25

gallienii

πŸ€– [Myrmica Gallienii 🐜] Bot: Keep Forks Synchronized
Go
8
star
26

hub-helm-chart

Traefik Hub helm chart
Smarty
5
star
27

yaegi-talk

Talks about Yaegi
Go
4
star
28

contributors-guide

Contributors Guide
4
star
29

bibikoffi

πŸ€– [Myrmica Bibikoffi 🐜] Bot: Closes stale issues
Go
4
star
30

kutteri

πŸ€– [Chalepoxenus Kutteri 🐜] Bot: Track a GitHub repository and publish on Slack
Go
3
star
31

seo-doc

This program aims to process a documentation folder from traefik/doc and iterate each HTML file adding the requirements for a better SEO
HTML
3
star
32

plugindemowasm

This repository includes an example wasm plugin, for you to use as a reference for developing your own plugins
Go
3
star
33

traefikee-helm-chart

This chart installs the Traefik Enterprise on a Kubernetes cluster, an optional subchart of Traefik Mesh is also bundled
Smarty
3
star
34

traefik-hub-helm-chart

Traefik Hub is a Kubernetes-native API Management solution for publishing, securing, and managing APIs. Configuration is driven by Kubernetes CRDs, labels, and selectors for effective GitOps.
Makefile
2
star
35

traefiklabs-header-app

This Project aims to solve the issue to have a consistent header in many sites running different frameworks.
TypeScript
1
star
36

hub-apiportal-ui

CSS
1
star
37

homebrew-tap

Ruby
1
star
38

mixtus

πŸ€– [Lasius Mixtus 🐜] Bot: Publish Documentation to a GitHub Repository from another
Go
1
star
39

discourse-theme

Theme of Discourse Community Forum
SCSS
1
star