@@ -197,19 +197,41 @@ public ContainerRef rewrite(ContainerRef ref) {
197197 return ref ;
198198 }
199199 // No rewrite possible if location and prefix are not set
200- String registry = matchingConfig .get ().location ();
200+ String location = matchingConfig .get ().location ();
201201 String prefix = matchingConfig .get ().prefix ();
202- if (registry == null || registry .isBlank () || prefix == null || prefix .isBlank ()) {
202+ if (location == null || location .isBlank () || prefix == null || prefix .isBlank ()) {
203203 return ref ;
204204 }
205205 String currentRefString = ref .toString ();
206- String rewrittenRefString = currentRefString .replaceFirst (prefix , registry );
206+ String rewrittenRefString ;
207+
208+ // Replace all subdomain if prefix starts with "*." (e.g., *.example.com → my-registry.com)
209+ if (prefix .startsWith ("*." )) {
210+
211+ // The subdomain replacement can include an optional path
212+ int firtSlashIndex = prefix .indexOf ('/' );
213+ String prefixPath = firtSlashIndex < 0 ? "" : prefix .substring (firtSlashIndex );
214+
215+ // Remove matched host + optional prefixPath
216+ String remainder = currentRefString .substring (ref .getRegistry ().length ());
217+ if (!prefixPath .isEmpty () && remainder .startsWith (prefixPath )) {
218+ remainder = remainder .substring (prefixPath .length ());
219+ }
220+
221+ rewrittenRefString = location + remainder ;
222+ }
223+
224+ // Just replace the prefix with the location (e.g., docker.io/library → my-registry.com/library)
225+ else {
226+ rewrittenRefString = location + currentRefString .substring (prefix .length ());
227+ }
228+
207229 LOG .debug (
208230 "Rewriting container reference from '{}' to '{}' using registry config with prefix '{}' and location '{}'" ,
209231 currentRefString ,
210232 rewrittenRefString ,
211233 prefix ,
212- registry );
234+ location );
213235 return ContainerRef .parse (rewrittenRefString );
214236 }
215237
@@ -248,14 +270,25 @@ private boolean matches(ContainerRef ref, @Nullable String prefix) {
248270
249271 // No path restriction → host-only match
250272 if (p .path ().isEmpty ()) {
273+ LOG .debug ("Found registry table '{}'" , p );
251274 return true ;
252275 }
253276
254277 // Path prefix match (namespace/repo)
255278 String refPath = String .join ("/" , ref .getNamespace ()) + "/" + ref .getRepository ();
256- return refPath .equals (p .path ()) || refPath .startsWith (p .path () + "/" );
279+ boolean result = refPath .equals (p .path ()) || refPath .startsWith (p .path () + "/" );
280+ if (result ) {
281+ LOG .debug ("Found registry table '{}' matching path '{}'" , p , refPath );
282+ }
283+ return result ;
257284 }
258285
286+ /**
287+ * Check if the given host matches the specified prefix host, which can be a specific hostname or a wildcard pattern (e.g., *.example.com).
288+ * @param host the host to check for a match against the prefix host, which is the host component of the prefix.
289+ * @param prefixHost the prefix host to match against, which can be a specific hostname or a wildcard pattern (e.g., *.example.com).
290+ * @return true if the host matches the prefix host, false otherwise.
291+ */
259292 private boolean hostMatches (String host , String prefixHost ) {
260293 if (prefixHost .startsWith ("*." )) {
261294 String domain = prefixHost .substring (2 );
0 commit comments