Repository: clusternet/clusternet
Pull request #29 (2021-07-13 08:59:38)

fix proxy url join error
<!--  Thanks for sending a pull request!

-->

#### What type of PR is this?
kind/bug

#### What this PR does / why we need it:

#### Which issue(s) this PR fixes:
<!--
*Automatically closes linked issue when PR is merged.
Usage: `Fixes #<issue number>`, or `Fixes (paste link of issue)`.
-->
Fixes #

#### Special notes for your reviewer:


dixudx: cc @xiaodongluo 

Patch:
diff --git a/pkg/hub/deployer/helm/helm.go b/pkg/hub/deployer/helm/helm.go
index 3924ec7..d9db4e7 100644
--- a/pkg/hub/deployer/helm/helm.go
+++ b/pkg/hub/deployer/helm/helm.go
@@ -18,8 +18,8 @@ package helm
 
 import (
 	"context"
+	"errors"
 	"fmt"
-	"path"
 	"reflect"
 	"strings"
 
@@ -41,6 +41,7 @@ import (
 	utilpointer "k8s.io/utils/pointer"
 
 	appsapi "github.com/clusternet/clusternet/pkg/apis/apps/v1alpha1"
+	clusterapi "github.com/clusternet/clusternet/pkg/apis/clusters/v1beta1"
 	proxiesapi "github.com/clusternet/clusternet/pkg/apis/proxies/v1alpha1"
 	"github.com/clusternet/clusternet/pkg/controllers/apps/helmchart"
 	"github.com/clusternet/clusternet/pkg/controllers/apps/helmrelease"
@@ -270,11 +271,10 @@ func (hd *HelmDeployer) handleHelmRelease(hr *appsapi.HelmRelease) error {
 		klog.Warningf("found multiple ManagedCluster declarations in namespace %s", hr.Namespace)
 	}
 	if mcls[0].Status.UseSocket {
-		childClusterAPIServer := path.Join([]string{
-			mcls[0].Status.ParentAPIServerURL,
-			"apis", proxiesapi.SchemeGroupVersion.String(), "sockets", string(mcls[0].Spec.ClusterID),
-			"proxy/direct"}...)
-
+		childClusterAPIServer, err := getChildAPIServerProxyURL(mcls[0])
+		if err != nil {
+			return err
+		}
 		config = utils.CreateKubeConfigForSocketProxyWithToken(
 			childClusterAPIServer,
 			string(childClusterSecret.Data[corev1.ServiceAccountTokenKey]),
@@ -391,3 +391,14 @@ func (hd *HelmDeployer) handleSecret(secret *corev1.Secret) error {
 	}
 	return err
 }
+
+func getChildAPIServerProxyURL(mcls *clusterapi.ManagedCluster) (string, error) {
+	if mcls == nil {
+		return "", errors.New("unable to generate child cluster apiserver proxy url from nil ManagedCluster object")
+	}
+
+	return strings.Join([]string{
+		strings.TrimRight(mcls.Status.ParentAPIServerURL, "/"),
+		"apis", proxiesapi.SchemeGroupVersion.String(), "sockets", string(mcls.Spec.ClusterID),
+		"proxy/direct"}, "/"), nil
+}


Test patch:
diff --git a/pkg/hub/deployer/helm/helm_test.go b/pkg/hub/deployer/helm/helm_test.go
new file mode 100644
index 0000000..0196095
--- /dev/null
+++ b/pkg/hub/deployer/helm/helm_test.go
@@ -0,0 +1,81 @@
+/*
+Copyright 2021 The Clusternet Authors.
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+package helm
+
+import (
+	"testing"
+
+	clusterapi "github.com/clusternet/clusternet/pkg/apis/clusters/v1beta1"
+)
+
+func TestGetChildAPIServerProxyURL(t *testing.T) {
+	tests := []struct {
+		name    string
+		mcls    *clusterapi.ManagedCluster
+		want    string
+		wantErr bool
+	}{
+		{
+			name: "no trailing slash in apiserver url",
+			mcls: &clusterapi.ManagedCluster{
+				Spec: clusterapi.ManagedClusterSpec{
+					ClusterID: "a-valid-uid",
+				},
+				Status: clusterapi.ManagedClusterStatus{
+					ParentAPIServerURL: "https://10.0.0.10:6443",
+				},
+			},
+			want: "https://10.0.0.10:6443/apis/proxies.clusternet.io/v1alpha1/sockets/a-valid-uid/proxy/direct",
+		},
+		{
+			name: "one trailing slash in apiserver url",
+			mcls: &clusterapi.ManagedCluster{
+				Spec: clusterapi.ManagedClusterSpec{
+					ClusterID: "a-valid-uid",
+				},
+				Status: clusterapi.ManagedClusterStatus{
+					ParentAPIServerURL: "https://10.0.0.10:6443/",
+				},
+			},
+			want: "https://10.0.0.10:6443/apis/proxies.clusternet.io/v1alpha1/sockets/a-valid-uid/proxy/direct",
+		},
+		{
+			name: "multiple trailing slashes in apiserver url",
+			mcls: &clusterapi.ManagedCluster{
+				Spec: clusterapi.ManagedClusterSpec{
+					ClusterID: "a-valid-uid",
+				},
+				Status: clusterapi.ManagedClusterStatus{
+					ParentAPIServerURL: "https://10.0.0.10:6443////",
+				},
+			},
+			want: "https://10.0.0.10:6443/apis/proxies.clusternet.io/v1alpha1/sockets/a-valid-uid/proxy/direct",
+		},
+	}
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			got, err := getChildAPIServerProxyURL(tt.mcls)
+			if (err != nil) != tt.wantErr {
+				t.Errorf("getChildAPIServerProxyURL() error = %v, wantErr %v", err, tt.wantErr)
+				return
+			}
+			if got != tt.want {
+				t.Errorf("getChildAPIServerProxyURL() got = %v, want %v", got, tt.want)
+			}
+		})
+	}
+}