Skip to content

Commit

Permalink
chore(registry): add registry prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
pinglin committed Mar 16, 2024
1 parent 1d08812 commit 15b881c
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 7 deletions.
1 change: 1 addition & 0 deletions config/.env
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,4 @@ JAEGER_PORT=14268 # accept jaeger.thrift directly from clients
# registry
REGISTRY_HOST=registry
REGISTRY_PORT=5000
REGISTRY_PREFIX=/artifact/
1 change: 1 addition & 0 deletions config/.env.envsubst
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@ JAEGER_PORT=${JAEGER_PORT}
# registry
REGISTRY_HOST=${REGISTRY_HOST}
REGISTRY_PORT=${REGISTRY_PORT}
REGISTRY_PREFIX=${REGISTRY_PREFIX}
3 changes: 2 additions & 1 deletion config/base.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
},
"grpc-proxy-server": {},
"registry": {
"hostport": "{{ .plugins.registry_hostport }}"
"hostport": "{{ .plugins.registry_hostport }}",
"prefix": "{{ .plugins.registry_prefix }}"
}
}
},
Expand Down
3 changes: 2 additions & 1 deletion config/settings-env/plugins.json
Original file line number Diff line number Diff line change
@@ -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}"
}
19 changes: 14 additions & 5 deletions registry-plugin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 15b881c

Please sign in to comment.