Repository: kumahq/kuma
Pull request #3257 (2021-11-22 22:19:57)

feat(k8s/util) move ServiceTag for k8s Service to utils
There's probably more that should be moved out of `k8s/controllers` but this is needed for #3109 

codecov-commenter: # [Codecov](https://codecov.io/gh/kumahq/kuma/pull/3257?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kumahq) Report
> Merging [#3257](https://codecov.io/gh/kumahq/kuma/pull/3257?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kumahq) (a0217a6) into [master](https://codecov.io/gh/kumahq/kuma/commit/21eb3f09ad46bfea0b25282a894ec435e8318b3d?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kumahq) (21eb3f0) will **decrease** coverage by `0.02%`.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/kumahq/kuma/pull/3257/graphs/tree.svg?width=650&height=150&src=pr&token=D7Yvqs6rwV&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kumahq)](https://codecov.io/gh/kumahq/kuma/pull/3257?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kumahq)

```diff
@@            Coverage Diff             @@
##           master    #3257      +/-   ##
==========================================
- Coverage   52.40%   52.37%   -0.03%     
==========================================
  Files         928      928              
  Lines       54044    54047       +3     
==========================================
- Hits        28321    28309      -12     
- Misses      23503    23514      +11     
- Partials     2220     2224       +4     
```


| [Impacted Files](https://codecov.io/gh/kumahq/kuma/pull/3257?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kumahq) | Coverage Δ | |
|---|---|---|
| [...ugins/runtime/k8s/controllers/inbound\_converter.go](https://codecov.io/gh/kumahq/kuma/pull/3257/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kumahq#diff-cGtnL3BsdWdpbnMvcnVudGltZS9rOHMvY29udHJvbGxlcnMvaW5ib3VuZF9jb252ZXJ0ZXIuZ28=) | `87.75% <100.00%> (-0.25%)` | :arrow_down: |
| [pkg/plugins/runtime/k8s/util/util.go](https://codecov.io/gh/kumahq/kuma/pull/3257/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kumahq#diff-cGtnL3BsdWdpbnMvcnVudGltZS9rOHMvdXRpbC91dGlsLmdv) | `100.00% <100.00%> (ø)` | |
| [pkg/test/grpc/clientstream.go](https://codecov.io/gh/kumahq/kuma/pull/3257/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kumahq#diff-cGtnL3Rlc3QvZ3JwYy9jbGllbnRzdHJlYW0uZ28=) | `84.00% <0.00%> (-8.00%)` | :arrow_down: |
| [pkg/core/runtime/component/component.go](https://codecov.io/gh/kumahq/kuma/pull/3257/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kumahq#diff-cGtnL2NvcmUvcnVudGltZS9jb21wb25lbnQvY29tcG9uZW50Lmdv) | `81.13% <0.00%> (-7.55%)` | :arrow_down: |
| [pkg/kds/client/sink.go](https://codecov.io/gh/kumahq/kuma/pull/3257/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kumahq#diff-cGtnL2tkcy9jbGllbnQvc2luay5nbw==) | `54.05% <0.00%> (-5.41%)` | :arrow_down: |
| [pkg/plugins/runtime/gateway/route/sorter.go](https://codecov.io/gh/kumahq/kuma/pull/3257/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kumahq#diff-cGtnL3BsdWdpbnMvcnVudGltZS9nYXRld2F5L3JvdXRlL3NvcnRlci5nbw==) | `61.53% <0.00%> (-5.13%)` | :arrow_down: |
| [pkg/mads/v1/client/client.go](https://codecov.io/gh/kumahq/kuma/pull/3257/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kumahq#diff-cGtnL21hZHMvdjEvY2xpZW50L2NsaWVudC5nbw==) | `41.25% <0.00%> (-2.50%)` | :arrow_down: |
| [pkg/xds/generator/direct\_access\_proxy\_generator.go](https://codecov.io/gh/kumahq/kuma/pull/3257/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kumahq#diff-cGtnL3hkcy9nZW5lcmF0b3IvZGlyZWN0X2FjY2Vzc19wcm94eV9nZW5lcmF0b3IuZ28=) | `82.75% <0.00%> (-1.15%)` | :arrow_down: |
| [api/observability/v1/mads.pb.go](https://codecov.io/gh/kumahq/kuma/pull/3257/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kumahq#diff-YXBpL29ic2VydmFiaWxpdHkvdjEvbWFkcy5wYi5nbw==) | `34.53% <0.00%> (-1.04%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/kumahq/kuma/pull/3257?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kumahq).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kumahq)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/kumahq/kuma/pull/3257?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kumahq). Last update [21eb3f0...a0217a6](https://codecov.io/gh/kumahq/kuma/pull/3257?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kumahq). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kumahq).


Patch:
diff --git a/pkg/plugins/runtime/k8s/controllers/inbound_converter.go b/pkg/plugins/runtime/k8s/controllers/inbound_converter.go
index 5eb422631..69d60e17c 100644
--- a/pkg/plugins/runtime/k8s/controllers/inbound_converter.go
+++ b/pkg/plugins/runtime/k8s/controllers/inbound_converter.go
@@ -131,7 +131,7 @@ func InboundTagsForService(zone string, pod *kube_core.Pod, svc *kube_core.Servi
 	if tags == nil {
 		tags = make(map[string]string)
 	}
-	tags[mesh_proto.ServiceTag] = ServiceTagFor(svc, svcPort)
+	tags[mesh_proto.ServiceTag] = util_k8s.ServiceTagFor(svc, svcPort)
 	if zone != "" {
 		tags[mesh_proto.ZoneTag] = zone
 	}
@@ -142,10 +142,6 @@ func InboundTagsForService(zone string, pod *kube_core.Pod, svc *kube_core.Servi
 	return tags
 }
 
-func ServiceTagFor(svc *kube_core.Service, svcPort *kube_core.ServicePort) string {
-	return fmt.Sprintf("%s_%s_svc_%d", svc.Name, svc.Namespace, svcPort.Port)
-}
-
 // ProtocolTagFor infers service protocol from a `<port>.service.kuma.io/protocol` annotation or `appProtocol`.
 func ProtocolTagFor(svc *kube_core.Service, svcPort *kube_core.ServicePort) string {
 	var protocolValue string
diff --git a/pkg/plugins/runtime/k8s/util/util.go b/pkg/plugins/runtime/k8s/util/util.go
index 6cdd80443..31bc17fcc 100644
--- a/pkg/plugins/runtime/k8s/util/util.go
+++ b/pkg/plugins/runtime/k8s/util/util.go
@@ -143,3 +143,13 @@ func MeshFor(obj kube_meta.Object) string {
 
 	return mesh
 }
+
+// ServiceTagFor returns the canonical service name for a Kubernetes service,
+// optionally with a specific port.
+func ServiceTagFor(svc *kube_core.Service, svcPort *kube_core.ServicePort) string {
+	port := ""
+	if svcPort != nil {
+		port = fmt.Sprintf("_%d", svcPort.Port)
+	}
+	return fmt.Sprintf("%s_%s_svc%s", svc.Name, svc.Namespace, port)
+}


Test patch:
diff --git a/pkg/plugins/runtime/k8s/controllers/pod_converter_test.go b/pkg/plugins/runtime/k8s/controllers/pod_converter_test.go
index 0a874eed0..8c4f03319 100644
--- a/pkg/plugins/runtime/k8s/controllers/pod_converter_test.go
+++ b/pkg/plugins/runtime/k8s/controllers/pod_converter_test.go
@@ -520,33 +520,6 @@ var _ = Describe("InboundTagsForService(..)", func() {
 	)
 })
 
-var _ = Describe("ServiceTagFor(..)", func() {
-	It("should use Service FQDN", func() {
-		// given
-		svc := &kube_core.Service{
-			ObjectMeta: kube_meta.ObjectMeta{
-				Namespace: "demo",
-				Name:      "example",
-			},
-			Spec: kube_core.ServiceSpec{
-				Ports: []kube_core.ServicePort{
-					{
-						Name: "http",
-						Port: 80,
-						TargetPort: kube_intstr.IntOrString{
-							Type:   kube_intstr.Int,
-							IntVal: 8080,
-						},
-					},
-				},
-			},
-		}
-
-		// then
-		Expect(ServiceTagFor(svc, &svc.Spec.Ports[0])).To(Equal("example_demo_svc_80"))
-	})
-})
-
 var _ = Describe("ProtocolTagFor(..)", func() {
 
 	type testCase struct {
diff --git a/pkg/plugins/runtime/k8s/util/util_test.go b/pkg/plugins/runtime/k8s/util/util_test.go
index a8d5de947..1c0a65b26 100644
--- a/pkg/plugins/runtime/k8s/util/util_test.go
+++ b/pkg/plugins/runtime/k8s/util/util_test.go
@@ -9,6 +9,7 @@ import (
 	. "github.com/onsi/gomega"
 	kube_core "k8s.io/api/core/v1"
 	kube_meta "k8s.io/apimachinery/pkg/apis/meta/v1"
+	kube_intstr "k8s.io/apimachinery/pkg/util/intstr"
 
 	"github.com/kumahq/kuma/pkg/plugins/runtime/k8s/util"
 )
@@ -558,4 +559,30 @@ var _ = Describe("Util", func() {
 			)
 		})
 	})
+	Describe("ServiceTagFor", func() {
+		It("should use Service FQDN", func() {
+			// given
+			svc := &kube_core.Service{
+				ObjectMeta: kube_meta.ObjectMeta{
+					Namespace: "demo",
+					Name:      "example",
+				},
+				Spec: kube_core.ServiceSpec{
+					Ports: []kube_core.ServicePort{
+						{
+							Name: "http",
+							Port: 80,
+							TargetPort: kube_intstr.IntOrString{
+								Type:   kube_intstr.Int,
+								IntVal: 8080,
+							},
+						},
+					},
+				},
+			}
+
+			// then
+			Expect(util.ServiceTagFor(svc, &svc.Spec.Ports[0])).To(Equal("example_demo_svc_80"))
+		})
+	})
 })