diff --git a/config/.env b/config/.env index 9ae246a..ee0d851 100644 --- a/config/.env +++ b/config/.env @@ -39,3 +39,4 @@ JAEGER_PORT=14268 # accept jaeger.thrift directly from clients # registry REGISTRY_HOST=registry REGISTRY_PORT=5000 +REGISTRY_PREFIX=/artifact/ diff --git a/config/.env.envsubst b/config/.env.envsubst index ab0c45d..dc7f14d 100644 --- a/config/.env.envsubst +++ b/config/.env.envsubst @@ -35,3 +35,4 @@ JAEGER_PORT=${JAEGER_PORT} # registry REGISTRY_HOST=${REGISTRY_HOST} REGISTRY_PORT=${REGISTRY_PORT} +REGISTRY_PREFIX=${REGISTRY_PREFIX} diff --git a/config/base.json b/config/base.json index 3a5f6a2..628dba0 100644 --- a/config/base.json +++ b/config/base.json @@ -38,7 +38,8 @@ }, "grpc-proxy-server": {}, "registry": { - "hostport": "{{ .plugins.registry_hostport }}" + "hostport": "{{ .plugins.registry_hostport }}", + "prefix": "{{ .plugins.registry_prefix }}" } } }, diff --git a/config/settings-env/plugins.json b/config/settings-env/plugins.json index 9d84bf5..6442028 100644 --- a/config/settings-env/plugins.json +++ b/config/settings-env/plugins.json @@ -1,4 +1,5 @@ { "mgmt_grpc": "${MGMT_BACKEND_HOST}:${MGMT_BACKEND_PUBLICPORT}", - "registry_hostport": "${REGISTRY_HOST}:${REGISTRY_PORT}" + "registry_hostport": "${REGISTRY_HOST}:${REGISTRY_PORT}", + "registry_prefix": "${REGISTRY_PREFIX}" } \ No newline at end of file diff --git a/registry-plugin/main.go b/registry-plugin/main.go index 018c930..7adb211 100644 --- a/registry-plugin/main.go +++ b/registry-plugin/main.go @@ -32,22 +32,31 @@ func (r registerer) registerHandlers(_ context.Context, extra map[string]interfa } hostport, _ := config["hostport"].(string) + prefix, _ := config["prefix"].(string) return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { - if !strings.Contains(req.URL.Path, "/v2/") { + // If the URL path starts with "/v2/" (indicating the first handshake request to confirm registry V2 API), + // "/v2/prefix/" (before the registry prefix is applied), or "/prefix/v2/" (after the registry prefix is applied), + // it means that the request is intended for the Instill Artifact registry. In this case, the traffic is hijacked + // and directly relayed to the registry. Otherwise, if the URL path does not match any of these patterns, + // the traffic is passed through to the next handler. + if req.URL.Path != "/v2/" && + !strings.HasPrefix(req.URL.Path, fmt.Sprintf("/v2%s", prefix)) && + !strings.HasPrefix(req.URL.Path, fmt.Sprintf("%sv2/", prefix)) { h.ServeHTTP(w, req) + return } - // If the URL path contains /v2/, indicating a request to Distribution Registry HTTP API V2, - // the traffic is hijacked and directed to the registry req.URL.Scheme = "http" req.URL.Host = hostport + req.URL.Path = strings.TrimSuffix(prefix, "/") + strings.Replace(req.URL.Path, prefix, "/", 1) req.RequestURI = "" resp, err := http.DefaultClient.Do(req) if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) + logger.Error(err.Error()) + w.WriteHeader(http.StatusInternalServerError) return } @@ -75,7 +84,7 @@ func (registerer) RegisterLogger(v interface{}) { return } logger = l - logger.Debug(fmt.Sprintf("[PLUGIN: %s] Logger loaded", HandlerRegisterer)) + logger.Info(fmt.Sprintf("[PLUGIN: %s] Logger loaded", HandlerRegisterer)) } // Logger is an interface for logging functionality.