diff --git a/pkg/plugins/discovery/k8s/controllers/pod_converter.go b/pkg/plugins/discovery/k8s/controllers/pod_converter.go index 7dda5ea9f8b9..eefc52cdb9f4 100644 --- a/pkg/plugins/discovery/k8s/controllers/pod_converter.go +++ b/pkg/plugins/discovery/k8s/controllers/pod_converter.go @@ -1,8 +1,11 @@ package controllers import ( + "strconv" "strings" + "github.com/pkg/errors" + mesh_proto "github.com/Kong/kuma/api/mesh/v1alpha1" "github.com/Kong/kuma/pkg/core" mesh_k8s "github.com/Kong/kuma/pkg/plugins/resources/k8s/native/api/v1alpha1" @@ -72,6 +75,12 @@ func (p *PodConverter) DataplaneFor(pod *kube_core.Pod, services []*kube_core.Se } dataplane.Networking.Outbound = ofaces + metrics, err := MetricsFor(pod) + if err != nil { + return nil, err + } + dataplane.Metrics = metrics + return dataplane, nil } @@ -84,3 +93,31 @@ func GatewayFor(pod *kube_core.Pod, services []*kube_core.Service) (*mesh_proto. Tags: interfaces[0].Tags, // InboundInterfacesFor() returns either a non-empty list or an error }, nil } + +func MetricsFor(pod *kube_core.Pod) (*mesh_proto.MetricsBackend, error) { + path := pod.GetAnnotations()[injector_metadata.KumaMetricsPrometheusPath] + port := pod.GetAnnotations()[injector_metadata.KumaMetricsPrometheusPort] + if path == "" && port == "" { + return nil, nil + } + + cfg := &mesh_proto.PrometheusMetricsBackendConfig{ + Path: path, + } + if port != "" { + portValue, err := strconv.ParseUint(port, 10, 32) + if err != nil { + return nil, errors.Wrapf(err, "could not parse port from %s annotation", injector_metadata.KumaMetricsPrometheusPort) + } + cfg.Port = uint32(portValue) + } + + str, err := util_proto.ToStruct(cfg) + if err != nil { + return nil, err + } + return &mesh_proto.MetricsBackend{ + Type: mesh_proto.MetricsPrometheusType, + Conf: &str, + }, nil +} diff --git a/pkg/plugins/discovery/k8s/controllers/pod_converter_test.go b/pkg/plugins/discovery/k8s/controllers/pod_converter_test.go index 5c6808cd5889..ea93c1902436 100644 --- a/pkg/plugins/discovery/k8s/controllers/pod_converter_test.go +++ b/pkg/plugins/discovery/k8s/controllers/pod_converter_test.go @@ -196,6 +196,11 @@ var _ = Describe("PodToDataplane(..)", func() { otherServices: "06.other-services.yaml", dataplane: "06.dataplane.yaml", }), + Entry("07.Pod with metrics override", testCase{ + pod: "07.pod.yaml", + servicesForPod: "07.services-for-pod.yaml", + dataplane: "07.dataplane.yaml", + }), ) Context("when Dataplane cannot be generated", func() { diff --git a/pkg/plugins/discovery/k8s/controllers/testdata/07.dataplane.yaml b/pkg/plugins/discovery/k8s/controllers/testdata/07.dataplane.yaml new file mode 100644 index 000000000000..f9aac79d7aa1 --- /dev/null +++ b/pkg/plugins/discovery/k8s/controllers/testdata/07.dataplane.yaml @@ -0,0 +1,18 @@ +mesh: default +metadata: + creationTimestamp: null +spec: + metrics: + conf: + path: /non-standard-path + port: 1234 + type: prometheus + networking: + address: 192.168.0.1 + inbound: + - port: 7070 + tags: + app: example + protocol: tcp + service: sample.playground.svc:7071 + version: "0.1" diff --git a/pkg/plugins/discovery/k8s/controllers/testdata/07.pod.yaml b/pkg/plugins/discovery/k8s/controllers/testdata/07.pod.yaml new file mode 100644 index 000000000000..572b6d86f7e0 --- /dev/null +++ b/pkg/plugins/discovery/k8s/controllers/testdata/07.pod.yaml @@ -0,0 +1,15 @@ +metadata: + namespace: demo + name: example + labels: + app: example + version: "0.1" + annotations: + prometheus.metrics.kuma.io/port: "1234" + prometheus.metrics.kuma.io/path: "/non-standard-path" +spec: + containers: + - ports: + - containerPort: 7070 +status: + podIP: 192.168.0.1 diff --git a/pkg/plugins/discovery/k8s/controllers/testdata/07.services-for-pod.yaml b/pkg/plugins/discovery/k8s/controllers/testdata/07.services-for-pod.yaml new file mode 100644 index 000000000000..4011de72b1d4 --- /dev/null +++ b/pkg/plugins/discovery/k8s/controllers/testdata/07.services-for-pod.yaml @@ -0,0 +1,10 @@ +--- +metadata: + namespace: playground + name: sample +spec: + clusterIP: 192.168.0.1 + ports: + - protocol: TCP + port: 7071 + targetPort: 7070