Skip to content

Commit

Permalink
A basic plugin to test with
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivaylo Korakov committed Apr 8, 2021
1 parent 2063e56 commit 307b201
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 2 deletions.
15 changes: 15 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Goland
.idea

# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out
16 changes: 16 additions & 0 deletions .traefik.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# The name of your plugin as displayed in the Traefik Pilot web UI.
displayName: Traefik routing plugin

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

# The import path of your plugin.
import: github.com/kumina/traefik-routing-plugin

# A brief description of what your plugin is doing.
summary: Proof of concept! See if we can bait Traefik so that we decide from a custom configuration where each request should go!

# Test data the plugin will use to test itself on startup
testData:
routes:
http.test.com: asd
17 changes: 17 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
.PHONY: test vendor clean

export GO111MODULE=on

default: test

test:
go test -v -cover ./...

yaegi_test:
yaegi test -v .

vendor:
go mod vendor

clean:
rm -rf ./vendor
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
# traefik-routing-plugin
Proof of concept. Is it possible to bait Traefik so that we can have a custom solution for figuring out which request needs to go to which k8s service?
# Traefik Routing Plugin

**Proof of concept.**

Is it possible to bait Traefik so that we can have a custom solution for figuring out which request needs to go to which k8s service?
13 changes: 13 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package traefik_routing_plugin

// Config the plugin configuration.
type Config struct {
Routes map[string]string `json:"routes,omitempty"`
}

// CreateConfig creates the default plugin configuration.
func CreateConfig() *Config {
return &Config{
Routes: make(map[string]string),
}
}
3 changes: 3 additions & 0 deletions go.,mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/kumina/traefik-routing-plugin

go 1.15
39 changes: 39 additions & 0 deletions router.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package traefik_routing_plugin

import (
"context"
"fmt"
"net/http"
)

type Router struct {
// Required by Traefik
next http.Handler
name string

// Our custom configuration
routes map[string]string
}

// Function needed for Traefik to recognize this module as a plugin
// Uses a generic http.Handler type from golang that we can use to work with the request
// by overriding different functions of the interface
func New(ctx context.Context, next http.Handler, config *Config, name string) (http.Handler, error) {
if len(config.Routes) == 0 {
return nil, fmt.Errorf("routes cannot be empty")
}

return &Router{
routes: config.Routes,
next: next,
name: name,
}, nil
}

func (a *Router) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
for key, value := range a.routes {
req.Header.Set(key, value)
}

a.next.ServeHTTP(rw, req)
}

0 comments on commit 307b201

Please sign in to comment.