Skip to content

Commit 31058bd

Browse files
Merge pull request #67 from matzew/discover_cluster_proxy_addon_host
OCPMCP-27: Auto-discover cluster-proxy-addon-user route/service if not configured
2 parents 5d43b9e + d4522c8 commit 31058bd

File tree

1 file changed

+55
-8
lines changed

1 file changed

+55
-8
lines changed

pkg/kubernetes/provider_acm_hub.go

Lines changed: 55 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ const (
3232
// ACMProviderConfig holds ACM-specific configuration that users can set in config.toml
3333
type ACMProviderConfig struct {
3434
// The host for the ACM cluster proxy addon
35-
// If using the acm-kubeconfig strategy, this should be the route for the proxy
36-
// If using the acm strategy, this should be the service for the proxy
35+
// Optional: If not provided, will auto-discover the cluster-proxy-addon-user OCP route
36+
// If using the acm-kubeconfig strategy, this should be the route hostname for the proxy
37+
// If using the acm strategy, this should be the service name for the proxy
3738
ClusterProxyAddonHost string `toml:"cluster_proxy_addon_host,omitempty"`
3839

3940
// Whether to skip verifying the TLS certs from the cluster proxy
@@ -45,11 +46,6 @@ type ACMProviderConfig struct {
4546

4647
func (c *ACMProviderConfig) Validate() error {
4748
var err error = nil
48-
49-
if c.ClusterProxyAddonHost == "" {
50-
err = errors.Join(err, fmt.Errorf("cluster_proxy_addon_host is required"))
51-
}
52-
5349
if !c.ClusterProxyAddonSkipTLSVerify && c.ClusterProxyAddonCAFile == "" {
5450
err = errors.Join(err, fmt.Errorf("cluster_proxy_addon_ca_file is required if tls verification is not disabled"))
5551
}
@@ -193,11 +189,62 @@ func newACMKubeConfigClusterProvider(cfg *config.StaticConfig) (Provider, error)
193189
return newACMClusterProvider(baseManager, &acmKubeConfigProviderCfg.ACMProviderConfig, true)
194190
}
195191

192+
func discoverClusterProxyHost(m *Manager, isOpenShift bool) (string, error) {
193+
ctx := context.Background()
194+
195+
// Try to discover the cluster-proxy route (OpenShift) or service (vanilla Kubernetes)
196+
if isOpenShift {
197+
// Try OpenShift Route in multicluster-engine namespace
198+
routeGVR := schema.GroupVersionResource{
199+
Group: "route.openshift.io",
200+
Version: "v1",
201+
Resource: "routes",
202+
}
203+
204+
route, err := m.dynamicClient.Resource(routeGVR).Namespace("multicluster-engine").Get(ctx, "cluster-proxy-addon-user", metav1.GetOptions{})
205+
if err == nil {
206+
host, found, err := unstructured.NestedString(route.Object, "spec", "host")
207+
if err == nil && found && host != "" {
208+
klog.V(2).Infof("Auto-discovered cluster-proxy route: %s", host)
209+
return host, nil
210+
}
211+
}
212+
}
213+
214+
// Fallback: Try to find the service
215+
svcClient, err := m.accessControlClientSet.Services("multicluster-engine")
216+
if err != nil {
217+
return "", fmt.Errorf("failed to get services client: %w", err)
218+
}
219+
220+
svc, err := svcClient.Get(ctx, "cluster-proxy-addon-user", metav1.GetOptions{})
221+
if err == nil {
222+
host := fmt.Sprintf("%s.%s.svc.cluster.local", svc.Name, svc.Namespace)
223+
klog.V(2).Infof("Auto-discovered cluster-proxy service: %s", host)
224+
return host, nil
225+
}
226+
227+
return "", fmt.Errorf("failed to auto-discover cluster-proxy host: route and service not found")
228+
}
229+
196230
func newACMClusterProvider(m *Manager, cfg *ACMProviderConfig, watchKubeConfig bool) (Provider, error) {
197231
if !m.IsACMHub() {
198232
return nil, fmt.Errorf("not deployed in an ACM hub cluster")
199233
}
200234

235+
// Auto-discover cluster-proxy host if not provided
236+
clusterProxyHost := cfg.ClusterProxyAddonHost
237+
if clusterProxyHost == "" {
238+
ctx := context.Background()
239+
isOpenShift := m.IsOpenShift(ctx)
240+
discoveredHost, err := discoverClusterProxyHost(m, isOpenShift)
241+
if err != nil {
242+
return nil, fmt.Errorf("cluster_proxy_addon_host not provided and auto-discovery failed: %w", err)
243+
}
244+
clusterProxyHost = discoveredHost
245+
klog.V(1).Infof("Using auto-discovered cluster-proxy host: %s", clusterProxyHost)
246+
}
247+
201248
// Create cancellable context for the watch goroutine
202249
watchCtx, watchCancel := context.WithCancel(context.Background())
203250

@@ -207,7 +254,7 @@ func newACMClusterProvider(m *Manager, cfg *ACMProviderConfig, watchKubeConfig b
207254
watchKubeConfig: watchKubeConfig,
208255
watchCtx: watchCtx,
209256
watchCancel: watchCancel,
210-
clusterProxyHost: cfg.ClusterProxyAddonHost,
257+
clusterProxyHost: clusterProxyHost,
211258
clusterProxyCAFile: cfg.ClusterProxyAddonCAFile,
212259
skipTLSVerify: cfg.ClusterProxyAddonSkipTLSVerify,
213260
}

0 commit comments

Comments
 (0)