@@ -16,10 +16,7 @@ import (
1616 "knative.dev/pkg/logging"
1717)
1818
19- var orgMappings = map [string ]string {
20- "appscode" : "appscode-images" ,
21- "kubedb" : "kubedb-images" ,
22- }
19+ var opts * Options
2320
2421func redact (in http.Header ) http.Header {
2522 h := in .Clone ()
@@ -29,7 +26,8 @@ func redact(in http.Header) http.Header {
2926 return h
3027}
3128
32- func New () http.Handler {
29+ func New (o * Options ) http.Handler {
30+ opts = o
3331 router := mux .NewRouter ()
3432
3533 router .Use (func (next http.Handler ) http.Handler {
@@ -114,7 +112,7 @@ func token(resp http.ResponseWriter, req *http.Request) {
114112
115113 vals := req .URL .Query ()
116114 scope := vals .Get ("scope" )
117- for orgKey , ghOrg := range orgMappings {
115+ for orgKey , ghOrg := range opts . OrgMapping {
118116 if strings .HasPrefix (scope , "repository:" + orgKey + "/" ) {
119117 scope = strings .Replace (scope , "repository:" + orgKey + "/" , "repository:" + ghOrg + "/" , 1 )
120118 break
@@ -166,7 +164,7 @@ func proxy(resp http.ResponseWriter, req *http.Request) {
166164 repo := mux .Vars (req )["repo" ]
167165 rest := mux .Vars (req )["rest" ]
168166
169- url := fmt .Sprintf ("https://ghcr.io/v2/%s/%s/%s" , orgMappings [org ], repo , rest )
167+ url := fmt .Sprintf ("https://ghcr.io/v2/%s/%s/%s" , opts . OrgMapping [org ], repo , rest )
170168 if query := req .URL .Query ().Encode (); query != "" {
171169 url += "?" + query
172170 }
@@ -221,7 +219,7 @@ func proxy(resp http.ResponseWriter, req *http.Request) {
221219 link := back .Header .Get ("Link" )
222220 if link != "" {
223221 rewrittenLink := link
224- for orgKey , ghOrg := range orgMappings {
222+ for orgKey , ghOrg := range opts . OrgMapping {
225223 if strings .HasPrefix (link , "/v2/" + ghOrg + "/" ) {
226224 rewrittenLink = strings .Replace (link , "/v2/" + ghOrg + "/" , "/v2/" + orgKey + "/" , 1 )
227225 break
@@ -240,7 +238,7 @@ func proxy(resp http.ResponseWriter, req *http.Request) {
240238 http .Error (resp , err .Error (), http .StatusInternalServerError )
241239 return
242240 }
243- for _ , ghOrg := range orgMappings {
241+ for _ , ghOrg := range opts . OrgMapping {
244242 if strings .HasPrefix (lr .Name , ghOrg + "/" ) {
245243 lr .Name = strings .TrimPrefix (lr .Name , ghOrg + "/" )
246244 break
@@ -279,7 +277,7 @@ func ghpage(resp http.ResponseWriter, req *http.Request) {
279277 logger := logging .FromContext (ctx )
280278
281279 url := req .URL .String ()
282- for orgKey , ghOrg := range orgMappings {
280+ for orgKey , ghOrg := range opts . OrgMapping {
283281 if req .URL .Path == "/" + orgKey || strings .HasPrefix (req .URL .Path , "/" + orgKey + "/" ) {
284282 url = fmt .Sprintf ("https://ghcr.io%s" , strings .Replace (req .URL .Path , "/" + orgKey , "/" + ghOrg , 1 ))
285283 break
0 commit comments