Skip to content

Commit

Permalink
feat(kuma-cp): add service-name and service-port tags
Browse files Browse the repository at this point in the history
Signed-off-by: Jakub Dyszkiewicz <jakub.dyszkiewicz@gmail.com>
  • Loading branch information
jakubdyszkiewicz committed Dec 8, 2021
1 parent 8f1a8df commit a8cc1ce
Show file tree
Hide file tree
Showing 16 changed files with 118 additions and 41 deletions.
13 changes: 10 additions & 3 deletions pkg/plugins/runtime/k8s/controllers/inbound_converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package controllers

import (
"fmt"
"strconv"
"strings"

"github.com/pkg/errors"
Expand All @@ -12,7 +13,11 @@ import (
util_k8s "github.com/kumahq/kuma/pkg/plugins/runtime/k8s/util"
)

const NamespaceTag = "k8s.kuma.io/namespace"
const (
KubeNamespaceTag = "k8s.kuma.io/namespace"
KubeServiceTag = "k8s.kuma.io/service-name"
KubePortTag = "k8s.kuma.io/service-port"
)

func inboundForService(zone string, pod *kube_core.Pod, service *kube_core.Service) (ifaces []*mesh_proto.Dataplane_Networking_Inbound) {
for _, svcPort := range service.Spec.Ports {
Expand Down Expand Up @@ -133,7 +138,9 @@ func InboundTagsForService(zone string, pod *kube_core.Pod, svc *kube_core.Servi
if tags == nil {
tags = make(map[string]string)
}
tags[NamespaceTag] = pod.Namespace
tags[KubeNamespaceTag] = pod.Namespace
tags[KubeServiceTag] = svc.Name
tags[KubePortTag] = strconv.Itoa(int(svcPort.Port))
tags[mesh_proto.ServiceTag] = util_k8s.ServiceTagFor(svc, svcPort)
if zone != "" {
tags[mesh_proto.ZoneTag] = zone
Expand Down Expand Up @@ -177,7 +184,7 @@ func InboundTagsForPod(zone string, pod *kube_core.Pod) map[string]string {
if tags == nil {
tags = make(map[string]string)
}
tags[NamespaceTag] = pod.Namespace
tags[KubeNamespaceTag] = pod.Namespace
tags[mesh_proto.ServiceTag] = fmt.Sprintf("%s_%s_svc", nameFromPod(pod), pod.Namespace)
if zone != "" {
tags[mesh_proto.ZoneTag] = zone
Expand Down
8 changes: 8 additions & 0 deletions pkg/plugins/runtime/k8s/controllers/pod_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,13 +389,17 @@ var _ = Describe("PodReconciler", func() {
app: sample
kuma.io/protocol: http
kuma.io/service: example_demo_svc_80
k8s.kuma.io/service-name: example
k8s.kuma.io/service-port: "80"
k8s.kuma.io/namespace: demo
- health: {}
port: 6060
tags:
app: sample
kuma.io/service: example_demo_svc_6061
kuma.io/protocol: tcp
k8s.kuma.io/service-name: example
k8s.kuma.io/service-port: "6061"
k8s.kuma.io/namespace: demo
`))
})
Expand Down Expand Up @@ -465,13 +469,17 @@ var _ = Describe("PodReconciler", func() {
app: sample
kuma.io/protocol: http
kuma.io/service: example_demo_svc_80
k8s.kuma.io/service-name: example
k8s.kuma.io/service-port: "80"
k8s.kuma.io/namespace: demo
- health: {}
port: 6060
tags:
app: sample
kuma.io/service: example_demo_svc_6061
kuma.io/protocol: tcp
k8s.kuma.io/service-name: example
k8s.kuma.io/service-port: "6061"
k8s.kuma.io/namespace: demo
`))
})
Expand Down
92 changes: 54 additions & 38 deletions pkg/plugins/runtime/k8s/controllers/pod_converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,9 +411,11 @@ var _ = Describe("InboundTagsForService(..)", func() {
isGateway: false,
podLabels: nil,
expected: map[string]string{
"kuma.io/service": "example_demo_svc_80",
"kuma.io/protocol": "tcp", // we want Kuma's default behavior to be explicit to a user
"k8s.kuma.io/namespace": "test",
"kuma.io/service": "example_demo_svc_80",
"kuma.io/protocol": "tcp", // we want Kuma's default behavior to be explicit to a user
"k8s.kuma.io/service-name": "example",
"k8s.kuma.io/service-port": "80",
"k8s.kuma.io/namespace": "test",
},
}),
Entry("Pod with labels", testCase{
Expand All @@ -423,11 +425,13 @@ var _ = Describe("InboundTagsForService(..)", func() {
"version": "0.1",
},
expected: map[string]string{
"app": "example",
"version": "0.1",
"kuma.io/service": "example_demo_svc_80",
"kuma.io/protocol": "tcp", // we want Kuma's default behavior to be explicit to a user
"k8s.kuma.io/namespace": "test",
"app": "example",
"version": "0.1",
"kuma.io/service": "example_demo_svc_80",
"kuma.io/protocol": "tcp", // we want Kuma's default behavior to be explicit to a user
"k8s.kuma.io/service-name": "example",
"k8s.kuma.io/service-port": "80",
"k8s.kuma.io/namespace": "test",
},
}),
Entry("Pod with `service` label", testCase{
Expand All @@ -438,11 +442,13 @@ var _ = Describe("InboundTagsForService(..)", func() {
"version": "0.1",
},
expected: map[string]string{
"app": "example",
"version": "0.1",
"kuma.io/service": "example_demo_svc_80",
"kuma.io/protocol": "tcp", // we want Kuma's default behavior to be explicit to a user
"k8s.kuma.io/namespace": "test",
"app": "example",
"version": "0.1",
"kuma.io/service": "example_demo_svc_80",
"kuma.io/protocol": "tcp", // we want Kuma's default behavior to be explicit to a user
"k8s.kuma.io/service-name": "example",
"k8s.kuma.io/service-port": "80",
"k8s.kuma.io/namespace": "test",
},
}),
Entry("Service with a `<port>.service.kuma.io/protocol` annotation and an unknown value", testCase{
Expand All @@ -455,11 +461,13 @@ var _ = Describe("InboundTagsForService(..)", func() {
"80.service.kuma.io/protocol": "not-yet-supported-protocol",
},
expected: map[string]string{
"app": "example",
"version": "0.1",
"kuma.io/service": "example_demo_svc_80",
"kuma.io/protocol": "not-yet-supported-protocol", // we want Kuma's behavior to be straightforward to a user (just copy annotation value "as is")
"k8s.kuma.io/namespace": "test",
"app": "example",
"version": "0.1",
"kuma.io/service": "example_demo_svc_80",
"kuma.io/protocol": "not-yet-supported-protocol", // we want Kuma's behavior to be straightforward to a user (just copy annotation value "as is")
"k8s.kuma.io/service-name": "example",
"k8s.kuma.io/service-port": "80",
"k8s.kuma.io/namespace": "test",
},
}),
Entry("Service with a `<port>.service.kuma.io/protocol` annotation and a known value", testCase{
Expand All @@ -472,11 +480,13 @@ var _ = Describe("InboundTagsForService(..)", func() {
"80.service.kuma.io/protocol": "http",
},
expected: map[string]string{
"app": "example",
"version": "0.1",
"kuma.io/service": "example_demo_svc_80",
"kuma.io/protocol": "http",
"k8s.kuma.io/namespace": "test",
"app": "example",
"version": "0.1",
"kuma.io/service": "example_demo_svc_80",
"kuma.io/protocol": "http",
"k8s.kuma.io/service-name": "example",
"k8s.kuma.io/service-port": "80",
"k8s.kuma.io/namespace": "test",
},
}),
Entry("Service with appProtocol and a known value", testCase{
Expand All @@ -487,11 +497,13 @@ var _ = Describe("InboundTagsForService(..)", func() {
},
appProtocol: utilpointer.StringPtr("http"),
expected: map[string]string{
"app": "example",
"version": "0.1",
"kuma.io/service": "example_demo_svc_80",
"kuma.io/protocol": "http",
"k8s.kuma.io/namespace": "test",
"app": "example",
"version": "0.1",
"kuma.io/service": "example_demo_svc_80",
"kuma.io/protocol": "http",
"k8s.kuma.io/service-name": "example",
"k8s.kuma.io/service-port": "80",
"k8s.kuma.io/namespace": "test",
},
}),
Entry("Inject a zone tag if Zone is set", testCase{
Expand All @@ -502,12 +514,14 @@ var _ = Describe("InboundTagsForService(..)", func() {
"version": "0.1",
},
expected: map[string]string{
"app": "example",
"version": "0.1",
mesh_proto.ServiceTag: "example_demo_svc_80",
mesh_proto.ZoneTag: "zone-1",
mesh_proto.ProtocolTag: "tcp",
"k8s.kuma.io/namespace": "test",
"app": "example",
"version": "0.1",
mesh_proto.ServiceTag: "example_demo_svc_80",
mesh_proto.ZoneTag: "zone-1",
mesh_proto.ProtocolTag: "tcp",
"k8s.kuma.io/service-name": "example",
"k8s.kuma.io/service-port": "80",
"k8s.kuma.io/namespace": "test",
},
}),
Entry("Pod with empty labels", testCase{
Expand All @@ -517,10 +531,12 @@ var _ = Describe("InboundTagsForService(..)", func() {
"version": "",
},
expected: map[string]string{
"app": "example",
"kuma.io/service": "example_demo_svc_80",
"kuma.io/protocol": "tcp",
"k8s.kuma.io/namespace": "test",
"app": "example",
"kuma.io/service": "example_demo_svc_80",
"kuma.io/protocol": "tcp",
"k8s.kuma.io/service-name": "example",
"k8s.kuma.io/service-port": "80",
"k8s.kuma.io/namespace": "test",
},
}),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ spec:
tags:
app: example
k8s.kuma.io/namespace: demo
k8s.kuma.io/service-name: example
k8s.kuma.io/service-port: "80"
kuma.io/protocol: http
kuma.io/service: example_demo_svc_80
kuma.io/zone: zone-1
Expand All @@ -17,6 +19,8 @@ spec:
tags:
app: example
k8s.kuma.io/namespace: demo
k8s.kuma.io/service-name: example
k8s.kuma.io/service-port: "443"
kuma.io/protocol: tcp
kuma.io/service: example_demo_svc_443
kuma.io/zone: zone-1
Expand All @@ -27,6 +31,8 @@ spec:
tags:
app: example
k8s.kuma.io/namespace: demo
k8s.kuma.io/service-name: sample
k8s.kuma.io/service-port: "7071"
kuma.io/protocol: MONGO
kuma.io/service: sample_playground_svc_7071
kuma.io/zone: zone-1
Expand All @@ -37,6 +43,8 @@ spec:
tags:
app: example
k8s.kuma.io/namespace: demo
k8s.kuma.io/service-name: sample
k8s.kuma.io/service-port: "6061"
kuma.io/protocol: tcp
kuma.io/service: sample_playground_svc_6061
kuma.io/zone: zone-1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ spec:
tags:
app: example
k8s.kuma.io/namespace: demo
k8s.kuma.io/service-name: example
k8s.kuma.io/service-port: "80"
kuma.io/protocol: tcp
kuma.io/service: example_demo_svc_80
kuma.io/zone: zone-1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ spec:
tags:
app: example
k8s.kuma.io/namespace: demo
k8s.kuma.io/service-name: example
k8s.kuma.io/service-port: "80"
kuma.io/protocol: tcp
kuma.io/service: example_demo_svc_80
kuma.io/zone: zone-1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ spec:
tags:
app: example
k8s.kuma.io/namespace: demo
k8s.kuma.io/service-name: example
k8s.kuma.io/service-port: "80"
kuma.io/protocol: tcp
kuma.io/service: example_demo_svc_80
kuma.io/zone: zone-1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ spec:
tags:
app: example
k8s.kuma.io/namespace: demo
k8s.kuma.io/service-name: example
k8s.kuma.io/service-port: "80"
kuma.io/protocol: tcp
kuma.io/service: example_demo_svc_80
kuma.io/zone: zone-1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ spec:
tags:
app: example
k8s.kuma.io/namespace: demo
k8s.kuma.io/service-name: example
k8s.kuma.io/service-port: "80"
kuma.io/instance: example
kuma.io/protocol: tcp
kuma.io/service: example_demo_svc_80
Expand All @@ -18,6 +20,8 @@ spec:
tags:
app: example
k8s.kuma.io/namespace: demo
k8s.kuma.io/service-name: example
k8s.kuma.io/service-port: "443"
kuma.io/instance: example
kuma.io/protocol: tcp
kuma.io/service: example_demo_svc_443
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ spec:
tags:
app: example
k8s.kuma.io/namespace: demo
k8s.kuma.io/service-name: sample
k8s.kuma.io/service-port: "7071"
kuma.io/protocol: tcp
kuma.io/service: sample_playground_svc_7071
kuma.io/zone: zone-1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ spec:
tags:
app: example
k8s.kuma.io/namespace: demo
k8s.kuma.io/service-name: example
k8s.kuma.io/service-port: "80"
kuma.io/protocol: tcp
kuma.io/service: example_demo_svc_80
kuma.io/zone: zone-1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ spec:
tags:
app: kuma-ingress
k8s.kuma.io/namespace: kuma-system
k8s.kuma.io/service-name: kuma-ingress
k8s.kuma.io/service-port: "10001"
kuma.io/protocol: tcp
kuma.io/service: kuma-ingress_kuma-system_svc_10001
kuma.io/zone: zone-1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ spec:
tags:
app: example
k8s.kuma.io/namespace: demo
k8s.kuma.io/service-name: example
k8s.kuma.io/service-port: "80"
kuma.io/protocol: http
kuma.io/service: example_demo_svc_80
kuma.io/zone: zone-1
Expand All @@ -17,6 +19,8 @@ spec:
tags:
app: example
k8s.kuma.io/namespace: demo
k8s.kuma.io/service-name: example
k8s.kuma.io/service-port: "443"
kuma.io/protocol: tcp
kuma.io/service: example_demo_svc_443
kuma.io/zone: zone-1
Expand All @@ -25,6 +29,8 @@ spec:
tags:
app: example
k8s.kuma.io/namespace: demo
k8s.kuma.io/service-name: sample
k8s.kuma.io/service-port: "7071"
kuma.io/protocol: MONGO
kuma.io/service: sample_playground_svc_7071
kuma.io/zone: zone-1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ spec:
tags:
app: example
k8s.kuma.io/namespace: demo
k8s.kuma.io/service-name: example
k8s.kuma.io/service-port: "80"
kuma.io/protocol: http
kuma.io/service: example_demo_svc_80
kuma.io/zone: zone-1
Expand All @@ -19,6 +21,8 @@ spec:
tags:
app: example
k8s.kuma.io/namespace: demo
k8s.kuma.io/service-name: example
k8s.kuma.io/service-port: "443"
kuma.io/protocol: tcp
kuma.io/service: example_demo_svc_443
kuma.io/zone: zone-1
Expand All @@ -28,6 +32,8 @@ spec:
tags:
app: example
k8s.kuma.io/namespace: demo
k8s.kuma.io/service-name: sample
k8s.kuma.io/service-port: "7071"
kuma.io/protocol: MONGO
kuma.io/service: sample_playground_svc_7071
kuma.io/zone: zone-1
Expand Down
Loading

0 comments on commit a8cc1ce

Please sign in to comment.