traefik / plugindemo

This repository includes an example plugin, for you to use as a reference for developing your own plugins
https://plugins.traefik.io/plugins/628c9ee2108ecc83915d7764/demo-plugin
Apache License 2.0
140 stars 45 forks source link

How to debug in the local mode? #15

Closed hongbo-miao closed 2 years ago

hongbo-miao commented 2 years ago

Originally asked at https://stackoverflow.com/questions/72178972/traefik-go-command-traefik-error-failed-to-eval-new-undefined-xxx

BTW, super love Traefik's idea using plugins and middlewares! šŸ˜Š


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!

tomMoulard commented 2 years ago

Hello @Hongbo-Miao,

Thanks for your interest in Traefik!

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.

As, it seems your issue might be a configuration problem, this issue will be closed. Feel free to join our Community Forum and reach out to us on the Traefik section to discuss this further.

hongbo-miao commented 2 years ago

Thanks @tomMoulard ! After changing package main to package traefik_plugin_disable_graphql_introspection, it works now! šŸš€