diff --git a/cmd/service-proxy/instrumentation.go b/cmd/service-proxy/instrumentation.go index dfb43f87f..5a6092daa 100644 --- a/cmd/service-proxy/instrumentation.go +++ b/cmd/service-proxy/instrumentation.go @@ -9,6 +9,8 @@ import ( "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promhttp" + + "github.com/cloudoperators/greenhouse/pkg/common" ) type ctxClusterKey struct { @@ -67,7 +69,7 @@ func InstrumentHandler(next http.Handler, registry prometheus.Registerer) http.H injector := func(next http.Handler) http.Handler { return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) { - if name, namespace, cluster, err := SplitHost(req.Host); err == nil { + if name, cluster, namespace, err := common.SplitHost(req.Host); err == nil { ctx := req.Context() ctx = context.WithValue(ctx, ctxClusterKey{}, cluster) ctx = context.WithValue(ctx, ctxNamespaceKey{}, namespace) diff --git a/cmd/service-proxy/proxy.go b/cmd/service-proxy/proxy.go index 83c6ce3a3..ad78607b4 100644 --- a/cmd/service-proxy/proxy.go +++ b/cmd/service-proxy/proxy.go @@ -10,7 +10,6 @@ import ( "net/http/httputil" "net/url" "regexp" - "strings" "sync" "github.com/go-logr/logr" @@ -27,6 +26,7 @@ import ( greenhouseapis "github.com/cloudoperators/greenhouse/pkg/apis" greenhousev1alpha1 "github.com/cloudoperators/greenhouse/pkg/apis/greenhouse/v1alpha1" "github.com/cloudoperators/greenhouse/pkg/clientutil" + "github.com/cloudoperators/greenhouse/pkg/common" ) func NewProxyManager() *ProxyManager { @@ -171,8 +171,8 @@ func (pm *ProxyManager) rewrite(req *httputil.ProxyRequest) { req.Out = req.Out.WithContext(log.IntoContext(req.Out.Context(), l)) }() - // hostname is expected to have the format $name--$namespace--$cluster.$organisation.$basedomain - name, namespace, cluster, err := SplitHost(req.In.Host) + // hostname is expected to have the format $name--$cluster--$namespace.$organisation.$basedomain + name, cluster, namespace, err := common.SplitHost(req.In.Host) if err != nil { return } @@ -243,16 +243,3 @@ func enqueuePluginForCluster(_ context.Context, o client.Object) []ctrl.Request } return []ctrl.Request{{NamespacedName: types.NamespacedName{Namespace: plugin.Namespace, Name: plugin.Spec.ClusterName}}} } - -// SplitHost splits a host into its name, namespace and cluster parts -func SplitHost(host string) (name, namespace, cluster string, err error) { - parts := strings.SplitN(host, ".", 2) - if len(parts) < 2 { - return "", "", "", fmt.Errorf("invalid host: %s", host) - } - parts = strings.SplitN(parts[0], "--", 3) - if len(parts) < 3 { - return "", "", "", fmt.Errorf("invalid host: %s", host) - } - return parts[0], parts[1], parts[2], nil -} diff --git a/cmd/service-proxy/proxy_test.go b/cmd/service-proxy/proxy_test.go index 653cc962a..e761eb415 100644 --- a/cmd/service-proxy/proxy_test.go +++ b/cmd/service-proxy/proxy_test.go @@ -41,7 +41,7 @@ func TestRewrite(t *testing.T) { }{ { name: "valid host", - url: "https://name--namespace--cluster.organisation.basedomain/abcd", + url: "https://name--cluster--namespace.organisation.basedomain/abcd", expectedURL: "https://apiserver/proxy/url/abcd", contextVal: "cluster", }, diff --git a/pkg/common/url.go b/pkg/common/url.go index 97ff85130..9d12ead0a 100644 --- a/pkg/common/url.go +++ b/pkg/common/url.go @@ -6,6 +6,7 @@ package common import ( "crypto/sha256" "fmt" + "strings" greenhousev1alpha1 "github.com/cloudoperators/greenhouse/pkg/apis/greenhouse/v1alpha1" ) @@ -27,3 +28,19 @@ func URLForExposedServiceInPlugin(serviceName string, plugin *greenhousev1alpha1 subdomain, plugin.GetNamespace(), DNSDomain, ) } + +// SplitHost splits an exposed service host into its name, namespace and cluster parts +// The pattern shall be $service--$cluster--$namespace +// TODO, need to fix: We know that information on the namespace and even the cluster might be lost when the host was hashed due to it's length. +// Currently only cluster is critical, need to fix this. +func SplitHost(host string) (name, cluster, namespace string, err error) { + parts := strings.SplitN(host, ".", 2) + if len(parts) < 2 { + return "", "", "", fmt.Errorf("invalid host: %s", host) + } + parts = strings.SplitN(parts[0], "--", 3) + if len(parts) < 3 { + return "", "", "", fmt.Errorf("invalid host: %s", host) + } + return parts[0], parts[1], parts[2], nil +}