Repository: clusternet/clusternet
Pull request #621 (2023-02-28 08:49:28)

add mcs featuregate
<!--  Thanks for sending a pull request!

-->

#### What type of PR is this?
/kind/feature
#### What this PR does / why we need it:
Add MCS featuregate so we won't conflict when integrate with submariner.
#### 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 https://github.com/clusternet/clusternet/issues/619

#### Special notes for your reviewer:



Patch:
diff --git a/pkg/agent/agent.go b/pkg/agent/agent.go
index ce162bc..b59d4f6 100644
--- a/pkg/agent/agent.go
+++ b/pkg/agent/agent.go
@@ -162,9 +162,9 @@ func NewAgent(opts *options.AgentOptions) (*Agent, error) {
 		return nil, err
 	}
 
-	// parse serviceExportEnabled (EndpointSliceV1Promoted) by server version
+	// parse serviceExportEnabled (MultiClusterServiceEnabled) by server version
 	var serviceExportEnabled bool
-	if serviceExportEnabled, err = utils.EndpointSliceV1Promoted(ver.String()); err != nil {
+	if serviceExportEnabled, err = utils.MultiClusterServiceEnabled(ver.String()); err != nil {
 		return nil, err
 	}
 
diff --git a/pkg/features/features.go b/pkg/features/features.go
index a95b37b..5a9c556 100644
--- a/pkg/features/features.go
+++ b/pkg/features/features.go
@@ -87,6 +87,14 @@ const (
 	// alpha: v0.10.0
 	// Setting on agent side
 	Predictor featuregate.Feature = "Predictor"
+
+	// MultiClusterService indicates whether we allow service export and service import related controllers
+	// to run. In some cases like integrating with submariner, this feature should be disabled.
+	//
+	// owner: @lmxia
+	// alpha: v0.15.0
+	// Setting on hub and agent side.
+	MultiClusterService featuregate.Feature = "MultiClusterService"
 )
 
 func init() {
@@ -105,4 +113,5 @@ var defaultClusternetFeatureGates = map[featuregate.Feature]featuregate.FeatureS
 	Recovery:            {Default: false, PreRelease: featuregate.Alpha, LockToDefault: false},
 	FeedInventory:       {Default: false, PreRelease: featuregate.Alpha, LockToDefault: false},
 	Predictor:           {Default: false, PreRelease: featuregate.Alpha, LockToDefault: false},
+	MultiClusterService: {Default: false, PreRelease: featuregate.Alpha, LockToDefault: false},
 }
diff --git a/pkg/hub/hub.go b/pkg/hub/hub.go
index 5b9a75d..ebffe13 100644
--- a/pkg/hub/hub.go
+++ b/pkg/hub/hub.go
@@ -187,7 +187,7 @@ func NewHub(opts *options.HubServerOptions) (*Hub, error) {
 	}
 
 	var serviceImportEnabled bool
-	if serviceImportEnabled, err = utils.EndpointSliceV1Promoted(ver.String()); err != nil {
+	if serviceImportEnabled, err = utils.MultiClusterServiceEnabled(ver.String()); err != nil {
 		return nil, err
 	}
 
diff --git a/pkg/utils/version.go b/pkg/utils/version.go
index 67030ca..093da01 100644
--- a/pkg/utils/version.go
+++ b/pkg/utils/version.go
@@ -17,7 +17,9 @@ limitations under the License.
 package utils
 
 import (
+	"github.com/clusternet/clusternet/pkg/features"
 	"k8s.io/apimachinery/pkg/util/version"
+	utilfeature "k8s.io/apiserver/pkg/util/feature"
 )
 
 var (
@@ -52,8 +54,12 @@ func EndpointSliceV1beta1Promoted(kubeServerVersion string) (bool, error) {
 	return parsedVersion.AtLeast(KubeV1170Beta2), nil
 }
 
-// EndpointSliceV1Promoted is used to indicate whether EndpointSlice v1 has been promoted
-func EndpointSliceV1Promoted(kubeServerVersion string) (bool, error) {
+// MultiClusterServiceEnabled is used to indicate whether EndpointSlice v1 has been promoted
+func MultiClusterServiceEnabled(kubeServerVersion string) (bool, error) {
+	mcsEnabled := utilfeature.DefaultFeatureGate.Enabled(features.MultiClusterService)
+	if !mcsEnabled {
+		return false, nil
+	}
 	parsedVersion, err := version.ParseSemantic(kubeServerVersion)
 	if err != nil {
 		return false, err


Test patch:
diff --git a/pkg/utils/version_test.go b/pkg/utils/version_test.go
index 25b8133..f9c4a12 100644
--- a/pkg/utils/version_test.go
+++ b/pkg/utils/version_test.go
@@ -20,6 +20,8 @@ import (
 	"fmt"
 	"reflect"
 	"testing"
+
+	utilfeature "k8s.io/apiserver/pkg/util/feature"
 )
 
 func TestSATokenAutoGenerated(t *testing.T) {
@@ -204,7 +206,8 @@ func TestEndpointSliceV1beta1Promoted(t *testing.T) {
 	}
 }
 
-func TestEndpointSliceV1Promoted(t *testing.T) {
+func TestMultiClusterServiceEnabled(t *testing.T) {
+	_ = utilfeature.DefaultMutableFeatureGate.Set("MultiClusterService=true")
 	tests := []struct {
 		name              string
 		kubeServerVersion string
@@ -286,12 +289,12 @@ func TestEndpointSliceV1Promoted(t *testing.T) {
 	}
 	for _, tt := range tests {
 		t.Run(tt.name, func(t *testing.T) {
-			endpointSliceV1Promoted, err := EndpointSliceV1Promoted(tt.kubeServerVersion)
+			endpointSliceV1Promoted, err := MultiClusterServiceEnabled(tt.kubeServerVersion)
 			if !reflect.DeepEqual(err, tt.err) {
 				t.Errorf("kubeServerVersion %s, error expected %v but got %v", tt.kubeServerVersion, tt.err, err)
 			}
 			if endpointSliceV1Promoted != tt.want {
-				t.Errorf("kubeServerVersion %s, EndpointSliceV1Promoted expected %v but got %v", tt.kubeServerVersion, tt.want, endpointSliceV1Promoted)
+				t.Errorf("kubeServerVersion %s, MultiClusterServiceEnabled expected %v but got %v", tt.kubeServerVersion, tt.want, endpointSliceV1Promoted)
 			}
 		})
 	}