Repository: openyurtio/openyurt (Go)

Issue:
[feature request]improve discardcloudservice filter in yurthub component
<!-- Please only use this template for submitting feature requests -->

**What would you like to be added**:
Because clients on edges nodes can not connect cloud applications through pod ip, so LoadBalancer service for cloud application is discarded in yurthub component in order to make kube-proxy unaware of LoadBalancer service on edge nodes. and only LoadBalancer service with annotation `openyurt.io/skip-discard=true` will be skipped. 
the following are detail codes:

https://github.com/openyurtio/openyurt/blob/81d21b7f438be4fb912ebe9748f0c9a4a9296f5f/pkg/yurthub/filter/discardcloudservice/filter.go#L81-L86

but in the proposal: https://github.com/openyurtio/openyurt/blob/master/docs/proposals/20231121-high-availability-of-edge-services.md, LoadBalancer service across NodePools will be supported, this means that LoadBalancer service should be aware of on edge nodes.

so we will improve discardcloudservice filter in yurthub component, and only discard LoadBalancer service which has annotation `svc.openyurt.io/discard=true`, this means that LoadBalancer service will not be discarded by default.

**others**
/kind feature



Discussion:
@gnunu @JameKeal @YTGhost Do you have any comments?
It seems we first need to add a new annotation `svc.openyurt.io/discard=true` after line 48 in the following code
https://github.com/openyurtio/openyurt/blob/81d21b7f438be4fb912ebe9748f0c9a4a9296f5f/pkg/yurthub/filter/constant.go#L48
and then we need to modify the following code, discard LoadBalancer service which has `svc.openyurt.io/discard=true`, instead of which doesn't have `openyurt.io/skip-discard=true`.
https://github.com/openyurtio/openyurt/blob/81d21b7f438be4fb912ebe9748f0c9a4a9296f5f/pkg/yurthub/filter/discardcloudservice/filter.go#L81-L86

I have raised a pr to implement the logic described above to fix this issue.
> It seems we first need to add a new annotation `svc.openyurt.io/discard=true` after line 48 in the following code
> 
> https://github.com/openyurtio/openyurt/blob/81d21b7f438be4fb912ebe9748f0c9a4a9296f5f/pkg/yurthub/filter/constant.go#L48
> 
> 
> and then we need to modify the following code, discard LoadBalancer service which has `svc.openyurt.io/discard=true`, instead of which doesn't have `openyurt.io/skip-discard=true`.
> https://github.com/openyurtio/openyurt/blob/81d21b7f438be4fb912ebe9748f0c9a4a9296f5f/pkg/yurthub/filter/discardcloudservice/filter.go#L81-L86
> 
> I have raised a pr to implement the logic described above to fix this issue.

@huangchenzhao yes, you're right. by the way, unit tests also should be tuned.



Patch:
diff --git a/pkg/yurthub/filter/constant.go b/pkg/yurthub/filter/constant.go
index b2623e6a71a..084788b3de3 100644
--- a/pkg/yurthub/filter/constant.go
+++ b/pkg/yurthub/filter/constant.go
@@ -46,6 +46,11 @@ const (
 	// If end users want to use specified LB service at the edge side,
 	// End users should add annotation["openyurt.io/skip-discard"]="true" for LB service.
 	SkipDiscardServiceAnnotation = "openyurt.io/skip-discard"
+
+	// DiscardServiceAnnotation is annotation used by LB service.
+	// If end users want to discard specified LB service at the edge side,
+	// End users should add annotation["svc.openyurt.io/discard"]="true" for LB service.
+	DiscardServiceAnnotation = "svc.openyurt.io/discard"
 )
 
 var (
diff --git a/pkg/yurthub/filter/discardcloudservice/filter.go b/pkg/yurthub/filter/discardcloudservice/filter.go
index d8cd75d5759..58b4320ac25 100644
--- a/pkg/yurthub/filter/discardcloudservice/filter.go
+++ b/pkg/yurthub/filter/discardcloudservice/filter.go
@@ -79,7 +79,7 @@ func discardCloudService(svc *v1.Service) *v1.Service {
 	nsName := fmt.Sprintf("%s/%s", svc.Namespace, svc.Name)
 	// remove cloud LoadBalancer service
 	if svc.Spec.Type == v1.ServiceTypeLoadBalancer {
-		if svc.Annotations[filter.SkipDiscardServiceAnnotation] != "true" {
+		if svc.Annotations[filter.DiscardServiceAnnotation] == "true" {
 			klog.V(2).Infof("load balancer service(%s) is discarded in StreamResponseFilter of discardCloudServiceFilterHandler", nsName)
 			return nil
 		}


Test patch:
diff --git a/pkg/yurthub/filter/discardcloudservice/filter_test.go b/pkg/yurthub/filter/discardcloudservice/filter_test.go
index b1d8832fe36..25cc33b6ece 100644
--- a/pkg/yurthub/filter/discardcloudservice/filter_test.go
+++ b/pkg/yurthub/filter/discardcloudservice/filter_test.go
@@ -67,7 +67,7 @@ func TestFilter(t *testing.T) {
 							Name:      "svc1",
 							Namespace: "default",
 							Annotations: map[string]string{
-								filter.SkipDiscardServiceAnnotation: "false",
+								filter.DiscardServiceAnnotation: "true",
 							},
 						},
 						Spec: corev1.ServiceSpec{
@@ -150,7 +150,7 @@ func TestFilter(t *testing.T) {
 							Name:      "svc1",
 							Namespace: "default",
 							Annotations: map[string]string{
-								filter.SkipDiscardServiceAnnotation: "true",
+								filter.DiscardServiceAnnotation: "false",
 							},
 						},
 						Spec: corev1.ServiceSpec{
@@ -177,7 +177,7 @@ func TestFilter(t *testing.T) {
 							Name:      "svc1",
 							Namespace: "default",
 							Annotations: map[string]string{
-								filter.SkipDiscardServiceAnnotation: "true",
+								filter.DiscardServiceAnnotation: "false",
 							},
 						},
 						Spec: corev1.ServiceSpec{
@@ -205,6 +205,9 @@ func TestFilter(t *testing.T) {
 						ObjectMeta: metav1.ObjectMeta{
 							Name:      "svc1",
 							Namespace: "default",
+							Annotations: map[string]string{
+								filter.DiscardServiceAnnotation: "true",
+							},
 						},
 						Spec: corev1.ServiceSpec{
 							ClusterIP: "10.96.105.187",
@@ -230,6 +233,9 @@ func TestFilter(t *testing.T) {
 				ObjectMeta: metav1.ObjectMeta{
 					Name:      "svc1",
 					Namespace: "default",
+					Annotations: map[string]string{
+						filter.DiscardServiceAnnotation: "true",
+					},
 				},
 				Spec: corev1.ServiceSpec{
 					ClusterIP: "10.96.105.187",
@@ -256,9 +262,6 @@ func TestFilter(t *testing.T) {
 				ObjectMeta: metav1.ObjectMeta{
 					Name:      "svc1",
 					Namespace: "default",
-					Annotations: map[string]string{
-						filter.SkipDiscardServiceAnnotation: "true",
-					},
 				},
 				Spec: corev1.ServiceSpec{
 					ClusterIP: "10.96.105.187",
@@ -269,9 +272,6 @@ func TestFilter(t *testing.T) {
 				ObjectMeta: metav1.ObjectMeta{
 					Name:      "svc1",
 					Namespace: "default",
-					Annotations: map[string]string{
-						filter.SkipDiscardServiceAnnotation: "true",
-					},
 				},
 				Spec: corev1.ServiceSpec{
 					ClusterIP: "10.96.105.187",