How to successfully run a plugin in the local mode?

I am trying to build a Traefik plugin and test it in local mode based on GitHub - traefik/plugindemo: This repository includes an example plugin, for you to use as a reference for developing your own plugins

Right now this plugin does nothing and just return "Hello".

Here is my file structure:

In the traefik/plugins-local/src/github.com/Hongbo-Miao/traefik-plugin-disable-graphql-introspection folder, I have:

.traefik.yml

entryPoints:
  graphql-server-entrypoint:
    address: :9000
api:
  insecure: true
  dashboard: true
providers:
  file:
    filename: dynamic_conf.yaml
log:
  level: DEBUG
experimental:
  localPlugins:
    traefik-plugin-disable-graphql-introspection:
      modulename: github.com/Hongbo-Miao/traefik-plugin-disable-graphql-introspection

go.mod

module github.com/Hongbo-Miao/traefik-plugin-disable-graphql-introspection

go 1.17

main.go

package main

import (
	"context"
	"net/http"
)

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

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

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

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

func (a *DisableGraphQLIntrospection) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
	rw.Write([]byte("hello"))
}

In the root folder, I have

traefik.yaml

entryPoints:
  graphql-server-entrypoint:
    address: :9000
api:
  insecure: true
  dashboard: true
providers:
  file:
    filename: dynamic_conf.yaml
log:
  level: DEBUG
experimental:
  localPlugins:
    traefik-plugin-disable-graphql-introspection:
      modulename: github.com/Hongbo-Miao/traefik-plugin-disable-graphql-introspection

dynamic_conf.yaml

http:
  routers:
    graphql-server-entrypoint:
      service: graphql-server-service
      entrypoints:
        - graphql-server-entrypoint
      rule: Host(`localhost`)
      middlewares:
        - my-traefik-plugin-disable-graphql-introspection
  services:
    graphql-server-service:
      loadBalancer:
        servers:
          - url: http://localhost:5000/
  middlewares:
    my-traefik-plugin-disable-graphql-introspection:
      plugin:
        traefik-plugin-disable-graphql-introspection:
          headers:
            Foo: Bar

I have a GraphQL sever running at http://localhost:5000

I want it go through Taefik and expose by http://localhost:9000

However, when I run

traefik --configfile=traefik.yaml

in the root folder, I got error

traefik.go:79: command traefik error: failed to eval New: 1:28: undefined: traefik_plugin_disable_graphql_introspection 119

Traefik plugins are executed on the fly by Yaegi, an embedded Go interpreter.

The error seems threw by Yaegi, however, I have no clue how to debug.

Any guide would be appreciate!

Got an answer from Tom Moulard at GitHub and thanks!

Your error means that Yaegi cannot find the New function of the traefik_plugin_disable_graphql_introspection package. Therefore, you can tell that Yaegi found your plugin, loaded it, but could not find the package. To fix this, you need to change the line package main in your plugin's code, to package traefik_plugin_disable_graphql_introspection.

After changing package main to package traefik_plugin_disable_graphql_introspection in the main.go file, it works now! :smiley:

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.