-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(registry): add registry proxy plugin
- Loading branch information
Showing
9 changed files
with
135 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module registry-plugin | ||
|
||
go 1.21 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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{}) {} |