Skip to content

Commit 24289d7

Browse files
committed
moved config change to app layer
1 parent 59385fe commit 24289d7

File tree

2 files changed

+70
-7
lines changed

2 files changed

+70
-7
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package kcp
2+
3+
import (
4+
"net/url"
5+
"strings"
6+
)
7+
8+
// stripAPIExportPath removes APIExport virtual workspace paths from a URL to get the base KCP host
9+
func stripAPIExportPath(hostURL string) string {
10+
parsedURL, err := url.Parse(hostURL)
11+
if err != nil {
12+
// If we can't parse the URL, return it as-is
13+
return hostURL
14+
}
15+
16+
// Check if the path contains an APIExport pattern: /services/apiexport/...
17+
if strings.HasPrefix(parsedURL.Path, "/services/apiexport/") {
18+
// Strip the APIExport path to get the base KCP host
19+
parsedURL.Path = ""
20+
return parsedURL.String()
21+
}
22+
23+
// If it's not an APIExport URL, return as-is
24+
return hostURL
25+
}
26+
27+
// extractClusterHashFromAPIExportURL extracts the cluster hash from an APIExport URL
28+
// Expected format: https://host/services/apiexport/{cluster-hash}/{apiexport-name}/
29+
func extractClusterHashFromAPIExportURL(hostURL string) string {
30+
parsedURL, err := url.Parse(hostURL)
31+
if err != nil {
32+
return ""
33+
}
34+
35+
// Check if this is an APIExport URL
36+
if !strings.HasPrefix(parsedURL.Path, "/services/apiexport/") {
37+
return ""
38+
}
39+
40+
// Split the path and extract the cluster hash
41+
pathParts := strings.Split(strings.Trim(parsedURL.Path, "/"), "/")
42+
// Expected: ["services", "apiexport", "{cluster-hash}", "{apiexport-name}"]
43+
if len(pathParts) >= 3 && pathParts[0] == "services" && pathParts[1] == "apiexport" {
44+
return pathParts[2] // This is the cluster hash
45+
}
46+
47+
return ""
48+
}

listener/reconciler/kcp/reconciler.go

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,26 @@ func NewKCPManager(
7272
// to discover workspaces, not the base KCP host
7373
apiexportConfig := rest.CopyConfig(opts.Config)
7474

75-
// Construct the APIExport URL from the base host
76-
// We know the APIExport name is "core.platform-mesh.io" and we need to find the cluster hash
77-
baseHost := opts.Config.Host
75+
// Extract base KCP host from kubeconfig, stripping any APIExport paths
76+
// This ensures we work with both base KCP hosts and APIExport URLs in kubeconfig
77+
originalHost := opts.Config.Host
78+
baseHost := stripAPIExportPath(originalHost)
7879

79-
// For now, we'll construct a known APIExport URL
80-
// TODO: This should be made configurable or discovered dynamically
81-
apiexportURL := baseHost + "/services/apiexport/1mx3340lwq4c8kkw/core.platform-mesh.io/"
80+
log.Info().
81+
Str("originalHost", originalHost).
82+
Str("baseHost", baseHost).
83+
Msg("Extracted base KCP host from kubeconfig")
84+
85+
// Construct the APIExport URL for multicluster-provider discovery
86+
// We need to extract the cluster hash from the original APIExport URL if present
87+
clusterHash := extractClusterHashFromAPIExportURL(originalHost)
88+
if clusterHash == "" {
89+
// Fallback to a known cluster hash - this should be made configurable
90+
clusterHash = "1mx3340lwq4c8kkw"
91+
log.Warn().Str("fallbackHash", clusterHash).Msg("Could not extract cluster hash from kubeconfig, using fallback")
92+
}
93+
94+
apiexportURL := fmt.Sprintf("%s/services/apiexport/%s/core.platform-mesh.io/", baseHost, clusterHash)
8295

8396
log.Info().Str("baseHost", baseHost).Str("apiexportURL", apiexportURL).Msg("Using APIExport URL for multicluster provider")
8497
apiexportConfig.Host = apiexportURL
@@ -253,7 +266,9 @@ func (m *KCPManager) generateAndWriteSchemaForWorkspace(ctx context.Context, wor
253266
// The multicluster-provider is only used for workspace discovery in the listener
254267
// The gateway will use standard Kubernetes clients with direct workspace URLs
255268
baseConfig := m.mcMgr.GetLocalManager().GetConfig()
256-
baseHost := baseConfig.Host
269+
270+
// Strip APIExport path from the base config host to get the clean KCP host
271+
baseHost := stripAPIExportPath(baseConfig.Host)
257272

258273
// Construct direct workspace URL like main branch: /clusters/{workspace}
259274
directWorkspaceHost := fmt.Sprintf("%s/clusters/%s", baseHost, workspacePath)

0 commit comments

Comments
 (0)