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 aeed20c
Show file tree
Hide file tree
Showing 16 changed files with 80 additions and 3 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
16 changes: 16 additions & 0 deletions pkg/plugins/runtime/k8s/controllers/pod_converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,8 @@ var _ = Describe("InboundTagsForService(..)", func() {
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/service-name": "example",
"k8s.kuma.io/service-port": "80",
"k8s.kuma.io/namespace": "test",
},
}),
Expand All @@ -427,6 +429,8 @@ var _ = Describe("InboundTagsForService(..)", func() {
"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",
},
}),
Expand All @@ -442,6 +446,8 @@ var _ = Describe("InboundTagsForService(..)", func() {
"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",
},
}),
Expand All @@ -459,6 +465,8 @@ var _ = Describe("InboundTagsForService(..)", func() {
"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",
},
}),
Expand All @@ -476,6 +484,8 @@ var _ = Describe("InboundTagsForService(..)", func() {
"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",
},
}),
Expand All @@ -491,6 +501,8 @@ var _ = Describe("InboundTagsForService(..)", func() {
"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",
},
}),
Expand All @@ -507,6 +519,8 @@ var _ = Describe("InboundTagsForService(..)", func() {
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",
},
}),
Expand All @@ -520,6 +534,8 @@ var _ = Describe("InboundTagsForService(..)", func() {
"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
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,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
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

0 comments on commit aeed20c

Please sign in to comment.