From 0810d4ab7595cfa643d2ba98722d0bbcae8c330c Mon Sep 17 00:00:00 2001
From: Lan Luo <luola@vmware.com>
Date: Thu, 23 Jun 2022 10:15:27 +0800
Subject: [PATCH] Update Precedence config and resources output

1. Update GatewayIPPrecedence to support "external/internal" option
   which is requal to `public/private`.
2. Update MC doc to add more columns info.
3. Exclude leader in the totalClusters in ClusterSet status since MC
   supports one leader only.

Signed-off-by: Lan Luo <luola@vmware.com>
---
 docs/multicluster/quick-start.md                 |  2 +-
 docs/multicluster/user-guide.md                  | 16 ++++++++--------
 .../v1alpha1/multiclusterconfig_types.go         |  6 ++++--
 .../multicluster/leader_clusterset_controller.go |  8 ++++----
 .../leader_clusterset_controller_test.go         |  2 +-
 .../multicluster/member_clusterset_controller.go |  2 +-
 .../controllers/multicluster/node_controller.go  |  7 ++++---
 7 files changed, 23 insertions(+), 20 deletions(-)

diff --git a/docs/multicluster/quick-start.md b/docs/multicluster/quick-start.md
index 572cb87d31f..93d754402ff 100644
--- a/docs/multicluster/quick-start.md
+++ b/docs/multicluster/quick-start.md
@@ -56,7 +56,7 @@ Namepsace `kube-system`.
 ```bash
 $kubectl apply -f https://github.com/antrea-io/antrea/releases/download/$TAG/antrea-multicluster-leader-global.yml
 $kubectl create ns antrea-multicluster
-$kubectl apply -f https://github.com/antrea-io/antrea/releases/download/$TAG/antrea-multicluster-leader-namespaced.yml > antrea-multicluster-leader-namespaced.yml
+$kubectl apply -f https://github.com/antrea-io/antrea/releases/download/$TAG/antrea-multicluster-leader-namespaced.yml
 $kubectl apply -f https://github.com/antrea-io/antrea/releases/download/$TAG/antrea-multicluster-member.yml
 ```
 
diff --git a/docs/multicluster/user-guide.md b/docs/multicluster/user-guide.md
index b863467516c..987ec12ea65 100644
--- a/docs/multicluster/user-guide.md
+++ b/docs/multicluster/user-guide.md
@@ -96,7 +96,7 @@ To deploy Multi-cluster Controller in a dual-role cluster, please refer to
 
   ```bash
   kubectl create ns antrea-multicluster
-  kubectl apply -f https://github.com/antrea-io/antrea/releases/download/$TAG/antrea-multicluster-leader-namespaced.yml >   antrea-multicluster-leader-namespaced.yml
+  kubectl apply -f https://github.com/antrea-io/antrea/releases/download/$TAG/antrea-multicluster-leader-namespaced.yml
   ```
 
 The Multi-cluster Controller in the leader cluster will be deployed in Namespace `antrea-multicluster`
@@ -419,7 +419,7 @@ there are several possibilities:
 
 * By default, the K8s Node's `InternalIP` is used as `gatewayIP` too.
 * You can choose to use the K8s Node's `ExternalIP` as `gatewayIP`, by changing
-the configuration option `gatewayIPPrecedence` to value: `public`, when
+the configuration option `gatewayIPPrecedence` to value: `external` or `public`, when
 deploying the member Multi-cluster Controller. The configration option is
 defined in ConfigMap `antrea-mc-controller-config-***` in `antrea-multicluster-member.yml`.
 * When the Gateway Node has a separate IP for external communication or is
@@ -505,14 +505,14 @@ ResourceImport CRs. You can check them in the leader cluster with commands:
 
 ```bash
 $kubectl get resourceexport -n antrea-multicluster
-NAME                                        AGE
-test-cluster-west-default-nginx-endpoints   30s
-test-cluster-west-default-nginx-service     30s
+NAME                                         CLUSTER ID          KIND          NAMESPACE     NAME      AGE
+test-cluster-west-default-nginx-endpoints    test-cluster-west   Endpoints     default       nginx     30s
+test-cluster-west-default-nginx-service      test-cluster-west   Service       default       nginx     30s
 
 $kubectl get resourceimport -n antrea-multicluster
-NAME                      AGE
-default-nginx-endpoints   99s
-default-nginx-service     99s
+NAME                      KIND            NAMESPACE       NAME           AGE
+default-nginx-endpoints   Endpoints       default         nginx          99s
+default-nginx-service     ServiceImport   default         nginx          99s
 ```
 
 When there is any new change on the exported Service, the imported multi-cluster
diff --git a/multicluster/apis/multicluster/v1alpha1/multiclusterconfig_types.go b/multicluster/apis/multicluster/v1alpha1/multiclusterconfig_types.go
index c96b95649c5..e0f08135a1f 100644
--- a/multicluster/apis/multicluster/v1alpha1/multiclusterconfig_types.go
+++ b/multicluster/apis/multicluster/v1alpha1/multiclusterconfig_types.go
@@ -25,8 +25,10 @@ import (
 type Precedence string
 
 const (
-	PrecedencePrivate = "private"
-	PrecedencePublic  = "public"
+	PrecedencePrivate  = "private"
+	PrecedencePublic   = "public"
+	PrecedenceInternal = "internal"
+	PrecedenceExternal = "external"
 )
 
 //+kubebuilder:object:root=true
diff --git a/multicluster/controllers/multicluster/leader_clusterset_controller.go b/multicluster/controllers/multicluster/leader_clusterset_controller.go
index 1555737da55..547d7f4f010 100644
--- a/multicluster/controllers/multicluster/leader_clusterset_controller.go
+++ b/multicluster/controllers/multicluster/leader_clusterset_controller.go
@@ -158,13 +158,13 @@ func (r *LeaderClusterSetReconciler) runBackgroundTasks() {
 }
 
 // updateStatus updates ClusterSet Status as follows:
-// 1. TotalClusters is the number of Leader and Member clusters
-//    in the ClusterSet resource last processed.
+// 1. TotalClusters is the number of member clusters in the
+//    ClusterSet resource last processed.
 // 2. ObservedGeneration is the Generation from the last processed
 //    ClusterSet resource.
 // 3. Individual cluster status is obtained from MemberClusterAnnounce
 //    controller.
-// 3. ReadyClusters is the number of clusters with "Ready" = "True"
+// 3. ReadyClusters is the number of member clusters with "Ready" = "True"
 // 4. Overall condition of the ClusterSet is also computed as follows:
 //    a. "Ready" = "True" if all clusters have "Ready" = "True".
 //       Message & Reason will be absent.
@@ -184,7 +184,7 @@ func (r *LeaderClusterSetReconciler) updateStatus() {
 	}
 
 	status := multiclusterv1alpha1.ClusterSetStatus{}
-	status.TotalClusters = int32(len(r.clusterSetConfig.Spec.Members) + len(r.clusterSetConfig.Spec.Leaders))
+	status.TotalClusters = int32(len(r.clusterSetConfig.Spec.Members))
 	status.ObservedGeneration = r.clusterSetConfig.Generation
 	clusterStatues := r.StatusManager.GetMemberClusterStatuses()
 	status.ClusterStatuses = clusterStatues
diff --git a/multicluster/controllers/multicluster/leader_clusterset_controller_test.go b/multicluster/controllers/multicluster/leader_clusterset_controller_test.go
index 735cdac93d3..3c4c2d006c1 100644
--- a/multicluster/controllers/multicluster/leader_clusterset_controller_test.go
+++ b/multicluster/controllers/multicluster/leader_clusterset_controller_test.go
@@ -233,7 +233,7 @@ func TestLeaderClusterStatus(t *testing.T) {
 	actualStatus := clusterSet.Status
 	expectedStatus := mcsv1alpha1.ClusterSetStatus{
 		ObservedGeneration: 1,
-		TotalClusters:      3,
+		TotalClusters:      2,
 		ClusterStatuses:    statues,
 		Conditions: []mcsv1alpha1.ClusterSetCondition{
 			{
diff --git a/multicluster/controllers/multicluster/member_clusterset_controller.go b/multicluster/controllers/multicluster/member_clusterset_controller.go
index 5f6362dab80..48a98a9489b 100644
--- a/multicluster/controllers/multicluster/member_clusterset_controller.go
+++ b/multicluster/controllers/multicluster/member_clusterset_controller.go
@@ -241,7 +241,7 @@ func (r *MemberClusterSetReconciler) updateStatus() {
 	}
 
 	status := multiclusterv1alpha1.ClusterSetStatus{}
-	status.TotalClusters = int32(len(r.clusterSetConfig.Spec.Members) + len(r.clusterSetConfig.Spec.Leaders))
+	status.TotalClusters = int32(len(r.clusterSetConfig.Spec.Members))
 	status.ObservedGeneration = r.clusterSetConfig.Generation
 	status.ClusterStatuses = r.remoteCommonAreaManager.GetMemberClusterStatues()
 
diff --git a/multicluster/controllers/multicluster/node_controller.go b/multicluster/controllers/multicluster/node_controller.go
index 6136c420787..dc03f8ecc77 100644
--- a/multicluster/controllers/multicluster/node_controller.go
+++ b/multicluster/controllers/multicluster/node_controller.go
@@ -55,7 +55,7 @@ func NewNodeReconciler(
 	namespace string,
 	precedence mcsv1alpha1.Precedence) *NodeReconciler {
 	if string(precedence) == "" {
-		precedence = mcsv1alpha1.PrecedencePrivate
+		precedence = mcsv1alpha1.PrecedenceInternal
 	}
 	reconciler := &NodeReconciler{
 		Client:     client,
@@ -138,12 +138,13 @@ func (r *NodeReconciler) getGatawayNodeIP(node *corev1.Node) (string, string, er
 	var gatewayIP, internalIP string
 	for _, addr := range node.Status.Addresses {
 		if addr.Type == corev1.NodeInternalIP {
-			if r.precedence == mcsv1alpha1.PrecedencePrivate {
+			if r.precedence == mcsv1alpha1.PrecedencePrivate || r.precedence == mcsv1alpha1.PrecedenceInternal {
 				gatewayIP = addr.Address
 			}
 			internalIP = addr.Address
 		}
-		if r.precedence == mcsv1alpha1.PrecedencePublic && addr.Type == corev1.NodeExternalIP {
+		if (r.precedence == mcsv1alpha1.PrecedencePublic || r.precedence == mcsv1alpha1.PrecedenceExternal) &&
+			addr.Type == corev1.NodeExternalIP {
 			gatewayIP = addr.Address
 		}
 	}