Search code examples
gotraefiktraefik-pluginsyaegi

traefik.go: command traefik error: failed to eval New: undefined: xxx


I am trying to build a Traefik plugin and test it in local mode based on https://github.com/traefik/plugindemo#local-mode

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

Here is my file structure:

enter image description here

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"
)

type Config struct{}

func CreateConfig() *Config {
    return &Config{}
}

type DisableGraphQLIntrospection struct {
    next http.Handler
    name string
}

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!


Solution

  • Got an answer from Tom Moulard and thanks! https://github.com/traefik/plugindemo/issues/15#issuecomment-1123741949

    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!