Giter Site home page Giter Site logo

apro-example-plugin's Introduction

Ambassador Pro example filters

This is an example filter for Ambassador Pro that injects an X-Wikipedia header with the URL for a random Wikipedia article before passing the request off to the backend service. It uses https://en.wikipedia.org/wiki/Special:Random to obtain the URL; as an example of a middleware needing to talk to an external service.

Compiling

Prerequisites

  • A Linux or MacOS host (WSL on Windows is okay)
  • Docker
  • rsync

The Plugin is compiled by building a container with a stable Golang environment and using rsync to copy files to and from the container.


To compile all .go files in the root of this repository just run

```shell
$ make DOCKER_REGISTRY=...
```

It will generate a Docker image named

```shell
DOCKER_REGISTRY/amb-sidecar-custom:VERSION
```

where:

  • VERSION is git describe --tags --always --dirty.
  • DOCKER_REGISTRY is the $DOCKER_REGISTRY environment variable, or localhost:31000 if the variable is not set.

Options

To generate a Docker image with a custom name run

```shell
$ make DOCKER_IMAGE={{REGISTRY}}/{{IMAGE_NAME}}:{{IMAGE_TAG}}
```

To build .go files in a non-root directory run

```shell
$ make DOCKER_REGISTRY=... PLUGIN_DIR=...
```
where `PLUGIN_DIR` is the directory for your `.go` files.

To compile for a specific version of Ambassador Pro, set APRO_VERSION:

$ make APRO_VERSION=0.2.2-rc2 DOCKER_REGISTRY=...

When switching Ambassador Pro versions, it may be nescessary to edit the go.mod file.

Deploying

Push that amb-sidecar-plugin Docker image to a registry that your cluster has access to. You can do this by running make push DOCKER_REGISTRY=....

Use that image you just pushed instead of docker.io/datawire/ambassador_pro:amb-sidecar when deploying Ambassador Pro. For more details on deployment, consult the Filter documentation.

How a plugin works

In package main, define a functions named PluginMain with this signature:

func PluginMain(w http.ResponseWriter, r *http.Request) { … }

The *http.Request is the incoming HTTP request that you have the opportunity to mutate or intercept, which you do via the http.ResponseWriter.

You may mutate the headers by calling w.Header().Set(HEADERNAME, VALUE). Finalize this by calling w.WriteHeader(http.StatusOK)

If you call w.WriteHeader() with any value other than 200 (http.StatusOK) instead of modifying the request, the middleware has taken over the request. You can call w.Write() to write the body of an error page.

This is all very similar to writing an Envoy ext_authz authorization service.

apro-example-plugin's People

Contributors

acookin avatar inoahnothing avatar kflynn avatar lukeshu avatar richarddli avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.