Skip to content

Commit

Permalink
feat(kmesh-cp) add kubernetes tags automatically (#3439)
Browse files Browse the repository at this point in the history
(cherry picked from commit 19b4a04)

# Conflicts:
#	pkg/plugins/runtime/k8s/controllers/inbound_converter.go
  • Loading branch information
jakubdyszkiewicz authored and mergify-bot committed Dec 13, 2021
1 parent 6e4b525 commit 8fa6ad4
Show file tree
Hide file tree
Showing 19 changed files with 414 additions and 267 deletions.
15 changes: 15 additions & 0 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 @@ -13,6 +14,12 @@ import (
util_k8s "github.com/kumahq/kuma/pkg/plugins/runtime/k8s/util"
)

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 {
if svcPort.Protocol != "" && svcPort.Protocol != kube_core.ProtocolTCP {
Expand Down Expand Up @@ -137,7 +144,14 @@ func InboundTagsForService(zone string, pod *kube_core.Pod, svc *kube_core.Servi
if tags == nil {
tags = make(map[string]string)
}
<<<<<<< HEAD
tags[mesh_proto.ServiceTag] = ServiceTagFor(svc, svcPort)
=======
tags[KubeNamespaceTag] = pod.Namespace
tags[KubeServiceTag] = svc.Name
tags[KubePortTag] = strconv.Itoa(int(svcPort.Port))
tags[mesh_proto.ServiceTag] = util_k8s.ServiceTagFor(svc, svcPort)
>>>>>>> 19b4a049 (feat(kmesh-cp) add kubernetes tags automatically (#3439))
if zone != "" {
tags[mesh_proto.ZoneTag] = zone
}
Expand Down Expand Up @@ -184,6 +198,7 @@ func InboundTagsForPod(zone string, pod *kube_core.Pod) map[string]string {
if tags == nil {
tags = make(map[string]string)
}
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
12 changes: 12 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,12 +389,18 @@ 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 @@ -463,12 +469,18 @@ 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
94 changes: 58 additions & 36 deletions pkg/plugins/runtime/k8s/controllers/pod_converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package controllers_test

import (
"context"
"encoding/json"
"fmt"
"os"
"path/filepath"
Expand Down Expand Up @@ -139,11 +138,9 @@ var _ = Describe("PodToDataplane(..)", func() {
// then
Expect(err).ToNot(HaveOccurred())

actual, err := json.Marshal(dataplane)
actual, err := yaml.Marshal(dataplane)
Expect(err).ToNot(HaveOccurred())
expected, err := os.ReadFile(filepath.Join("testdata", given.dataplane))
Expect(err).ToNot(HaveOccurred())
Expect(actual).To(MatchYAML(expected))
Expect(actual).To(MatchGoldenYAML("testdata", given.dataplane))
},
Entry("01.Pod with 2 Services", testCase{
pod: "01.pod.yaml",
Expand Down Expand Up @@ -416,7 +413,8 @@ var _ = Describe("InboundTagsForService(..)", func() {
// given
pod := &kube_core.Pod{
ObjectMeta: kube_meta.ObjectMeta{
Labels: given.podLabels,
Namespace: "demo",
Labels: given.podLabels,
},
}
// and
Expand Down Expand Up @@ -451,8 +449,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
"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": "demo",
},
}),
Entry("Pod with labels", testCase{
Expand All @@ -462,10 +463,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
"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": "demo",
},
}),
Entry("Pod with `service` label", testCase{
Expand All @@ -476,10 +480,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
"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": "demo",
},
}),
Entry("Service with a `<port>.service.kuma.io/protocol` annotation and an unknown value", testCase{
Expand All @@ -492,10 +499,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")
"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": "demo",
},
}),
Entry("Service with a `<port>.service.kuma.io/protocol` annotation and a known value", testCase{
Expand All @@ -508,10 +518,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",
"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": "demo",
},
}),
Entry("Service with appProtocol and a known value", testCase{
Expand All @@ -522,10 +535,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",
"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": "demo",
},
}),
Entry("Inject a zone tag if Zone is set", testCase{
Expand All @@ -536,11 +552,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",
"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": "demo",
},
}),
Entry("Pod with empty labels", testCase{
Expand All @@ -550,9 +569,12 @@ var _ = Describe("InboundTagsForService(..)", func() {
"version": "",
},
expected: map[string]string{
"app": "example",
"kuma.io/service": "example_demo_svc_80",
"kuma.io/protocol": "tcp",
"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": "demo",
},
}),
)
Expand Down
76 changes: 44 additions & 32 deletions pkg/plugins/runtime/k8s/controllers/testdata/01.dataplane.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,47 @@ spec:
networking:
address: 192.168.0.1
inbound:
- port: 8080
tags:
app: example
kuma.io/protocol: http
kuma.io/service: example_demo_svc_80
version: "0.1"
kuma.io/zone: "zone-1"
- port: 8443
tags:
app: example
kuma.io/protocol: tcp
kuma.io/service: example_demo_svc_443
version: "0.1"
kuma.io/zone: "zone-1"
- port: 7070
health:
ready: true
tags:
app: example
kuma.io/protocol: MONGO
kuma.io/service: sample_playground_svc_7071
version: "0.1"
kuma.io/zone: "zone-1"
- port: 6060
health:
ready: true
tags:
app: example
kuma.io/protocol: tcp
kuma.io/service: sample_playground_svc_6061
version: "0.1"
kuma.io/zone: "zone-1"
- port: 8080
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
version: "0.1"
- port: 8443
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
version: "0.1"
- health:
ready: true
port: 7070
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
version: "0.1"
- health:
ready: true
port: 6060
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
version: "0.1"
33 changes: 18 additions & 15 deletions pkg/plugins/runtime/k8s/controllers/testdata/02.dataplane.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,22 @@ spec:
networking:
address: 192.168.0.1
inbound:
- port: 8080
tags:
app: example
kuma.io/protocol: tcp
kuma.io/service: example_demo_svc_80
version: "0.1"
kuma.io/zone: "zone-1"
- port: 8080
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
version: "0.1"
outbound:
- address: 10.108.144.24
port: 443
tags:
kuma.io/service: test-app_playground_svc_443
- address: 10.108.144.24
port: 80
tags:
kuma.io/service: test-app_playground_svc_80
- address: 10.108.144.24
port: 443
tags:
kuma.io/service: test-app_playground_svc_443
- address: 10.108.144.24
port: 80
tags:
kuma.io/service: test-app_playground_svc_80
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ spec:
gateway:
tags:
app: example
kuma.io/service: example_demo_svc_80
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
version: "0.1"
kuma.io/zone: "zone-1"
Loading

0 comments on commit 8fa6ad4

Please sign in to comment.