Skip to content

Commit

Permalink
feat(registry): add registry proxy plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
pinglin committed Mar 15, 2024
1 parent 7df0fed commit aa7ec89
Show file tree
Hide file tree
Showing 9 changed files with 135 additions and 7 deletions.
4 changes: 4 additions & 0 deletions config/.env
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,7 @@ INFLUXDB_PORT=8086
JAEGER_ENABLED=false
JAEGER_HOST=jaeger
JAEGER_PORT=14268 # accept jaeger.thrift directly from clients

# registry
REGISTRY_HOST=registry
REGISTRY_PORT=5000
4 changes: 4 additions & 0 deletions config/.env.envsubst
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,7 @@ INFLUXDB_PORT=${INFLUXDB_PORT}
JAEGER_ENABLED=${JAEGER_ENABLED}
JAEGER_HOST=${JAEGER_HOST}
JAEGER_PORT=${JAEGER_PORT}

# registry
REGISTRY_HOST=${REGISTRY_HOST}
REGISTRY_PORT=${REGISTRY_PORT}
9 changes: 6 additions & 3 deletions config/base.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,14 @@
{{ template "influxdb_metrics.tmpl" . }},
{{ template "cors.tmpl" . }},
"plugin/http-server": {
"name": ["multi-auth", "grpc-proxy-server"],
"name": ["multi-auth", "grpc-proxy-server", "registry"],
"multi-auth":{
"grpc_server": "{{ .backends.mgmt_grpc }}"
"grpc_server": "{{ .plugins.mgmt_grpc }}"
},
"grpc-proxy-server": {}
"grpc-proxy-server": {},
"registry": {
"hostport": "{{ .plugins.registry_hostport }}"
}
}
},
"endpoints": [
Expand Down
1 change: 0 additions & 1 deletion config/settings-env/backends.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"debug": "https://${API_GATEWAY_HOST}:${API_GATEWAY_PORT}",
"mgmt": "http://${MGMT_BACKEND_HOST}:${MGMT_BACKEND_PUBLICPORT}",
"mgmt_grpc": "${MGMT_BACKEND_HOST}:${MGMT_BACKEND_PUBLICPORT}",
"model": "http://${MODEL_BACKEND_HOST}:${MODEL_BACKEND_PUBLICPORT}",
"pipeline": "http://${PIPELINE_BACKEND_HOST}:${PIPELINE_BACKEND_PUBLICPORT}"
}
4 changes: 1 addition & 3 deletions config/settings-env/endpoints.json
Original file line number Diff line number Diff line change
Expand Up @@ -1248,7 +1248,6 @@
"method": "POST",
"timeout": "600s"
},

{
"endpoint": "/vdp.pipeline.v1beta.PipelinePublicService/CreateOrganizationPipeline",
"url_pattern": "/vdp.pipeline.v1beta.PipelinePublicService/CreateOrganizationPipeline",
Expand Down Expand Up @@ -1375,7 +1374,6 @@
"method": "POST",
"timeout": "600s"
},

{
"endpoint": "/vdp.pipeline.v1beta.PipelinePublicService/GetOperation",
"url_pattern": "/vdp.pipeline.v1beta.PipelinePublicService/GetOperation",
Expand Down Expand Up @@ -2088,4 +2086,4 @@
}
]
}
}
}
4 changes: 4 additions & 0 deletions config/settings-env/plugins.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"mgmt_grpc": "${MGMT_BACKEND_HOST}:${MGMT_BACKEND_PUBLICPORT}",
"registry_hostport": "${REGISTRY_HOST}:${REGISTRY_PORT}"
}
3 changes: 3 additions & 0 deletions registry-plugin/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module registry-plugin

go 1.21
14 changes: 14 additions & 0 deletions registry-plugin/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ=
go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8=
go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
99 changes: 99 additions & 0 deletions registry-plugin/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package main

import (
"context"
"errors"
"fmt"
"io"
"net/http"
"strings"
)

// pluginName is the plugin name
var pluginName = "registry"

// HandlerRegisterer is the symbol the plugin loader will try to load. It must implement the Registerer interface
var HandlerRegisterer = registerer(pluginName)

type registerer string

func (r registerer) RegisterHandlers(f func(
name string,
handler func(context.Context, map[string]interface{}, http.Handler) (http.Handler, error),
)) {
f(string(r), r.registerHandlers)
}

func (r registerer) registerHandlers(_ context.Context, extra map[string]interface{}, h http.Handler) (http.Handler, error) {

config, ok := extra[pluginName].(map[string]interface{})
if !ok {
return h, errors.New("configuration not found")
}

hostport, _ := config["hostport"].(string)

return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {

// If the URL path contains /v2/, indicating a request to Distribution Registry HTTP API V2,
// the traffic is hijacked and directed to the registry
if strings.Contains(req.URL.Path, "/v2/") {
req.URL.Scheme = "http"
req.URL.Host = hostport
req.RequestURI = ""

resp, err := http.DefaultClient.Do(req)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

// Copy headers, status codes, and body from the backend to the response writer
for k, hs := range resp.Header {
for _, h := range hs {
w.Header().Add(k, h)
}
}
w.WriteHeader(resp.StatusCode)
io.Copy(w, resp.Body)
resp.Body.Close()
} else {
h.ServeHTTP(w, req)
}

}), nil
}

func main() {}

// This logger is replaced by the RegisterLogger method to load the one from KrakenD
var logger Logger = noopLogger{}

func (registerer) RegisterLogger(v interface{}) {
l, ok := v.(Logger)
if !ok {
return
}
logger = l
logger.Debug(fmt.Sprintf("[PLUGIN: %s] Logger loaded", HandlerRegisterer))
}

// Logger is an interface for logging functionality.
type Logger interface {
Debug(v ...interface{})
Info(v ...interface{})
Warning(v ...interface{})
Error(v ...interface{})
Critical(v ...interface{})
Fatal(v ...interface{})
}

// Empty logger implementation
type noopLogger struct{}

func (n noopLogger) Debug(_ ...interface{}) {}
func (n noopLogger) Info(_ ...interface{}) {}
func (n noopLogger) Warning(_ ...interface{}) {}
func (n noopLogger) Error(_ ...interface{}) {}
func (n noopLogger) Critical(_ ...interface{}) {}
func (n noopLogger) Fatal(_ ...interface{}) {}

0 comments on commit aa7ec89

Please sign in to comment.