add client labels to k8s plugin metadata (#6475)

Signed-off-by: Nolan Miles <nolanpmiles@gmail.com>
This commit is contained in:
miles-to-go 2024-03-07 14:34:09 -05:00 committed by GitHub
parent 3d67ee907d
commit 92b7e658e3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 24 additions and 5 deletions

View file

@ -207,9 +207,11 @@ plugin is also enabled:
* `kubernetes/service`: the service name in the query * `kubernetes/service`: the service name in the query
* `kubernetes/client-namespace`: the client pod's namespace (see requirements below) * `kubernetes/client-namespace`: the client pod's namespace (see requirements below)
* `kubernetes/client-pod-name`: the client pod's name (see requirements below) * `kubernetes/client-pod-name`: the client pod's name (see requirements below)
* `kubernetes/client-label/<label key>`: a label on the client pod (see requirements below)
The `kubernetes/client-namespace` and `kubernetes/client-pod-name` metadata work by reconciling the The `kubernetes/client-namespace`, `kubernetes/client-pod-name`, and `kubernetes/client-label/<label key>`
client IP address in the DNS request packet to a known pod IP address. Therefore the following is required: metadata work by reconciling the client IP address in the DNS request packet to a known pod IP address.
Therefore the following is required:
* `pods verified` mode must be enabled * `pods verified` mode must be enabled
* the remote IP address in the DNS packet received by CoreDNS must be the IP address * the remote IP address in the DNS packet received by CoreDNS must be the IP address
of the Pod that sent the request. of the Pod that sent the request.

View file

@ -580,7 +580,13 @@ func (APIConnServeTest) PodIndex(ip string) []*object.Pod {
return []*object.Pod{} return []*object.Pod{}
} }
a := []*object.Pod{ a := []*object.Pod{
{Namespace: "podns", Name: "foo", PodIP: "10.240.0.1"}, // Remote IP set in test.ResponseWriter {
Namespace: "podns", Name: "foo", PodIP: "10.240.0.1",
Labels: map[string]string{
"app.kubernetes.io/name": "foo",
"bar": "baz",
},
}, // Remote IP set in test.ResponseWriter
} }
return a return a
} }

View file

@ -19,6 +19,13 @@ func (k *Kubernetes) Metadata(ctx context.Context, state request.Request) contex
metadata.SetValueFunc(ctx, "kubernetes/client-pod-name", func() string { metadata.SetValueFunc(ctx, "kubernetes/client-pod-name", func() string {
return pod.Name return pod.Name
}) })
for k, v := range pod.Labels {
v := v
metadata.SetValueFunc(ctx, "kubernetes/client-label/"+k, func() string {
return v
})
}
} }
zone := plugin.Zones(k.Zones).Matches(state.Name()) zone := plugin.Zones(k.Zones).Matches(state.Name())

View file

@ -141,8 +141,10 @@ func TestMetadataPodsVerified(t *testing.T) {
k.Metadata(ctx, state) k.Metadata(ctx, state)
expect := map[string]string{ expect := map[string]string{
"kubernetes/client-namespace": "podns", "kubernetes/client-namespace": "podns",
"kubernetes/client-pod-name": "foo", "kubernetes/client-pod-name": "foo",
"kubernetes/client-label/app.kubernetes.io/name": "foo",
"kubernetes/client-label/bar": "baz",
} }
md := make(map[string]string) md := make(map[string]string)

View file

@ -16,6 +16,7 @@ type Pod struct {
PodIP string PodIP string
Name string Name string
Namespace string Namespace string
Labels map[string]string
*Empty *Empty
} }
@ -33,6 +34,7 @@ func ToPod(obj meta.Object) (meta.Object, error) {
PodIP: apiPod.Status.PodIP, PodIP: apiPod.Status.PodIP,
Namespace: apiPod.GetNamespace(), Namespace: apiPod.GetNamespace(),
Name: apiPod.GetName(), Name: apiPod.GetName(),
Labels: apiPod.GetLabels(),
} }
t := apiPod.ObjectMeta.DeletionTimestamp t := apiPod.ObjectMeta.DeletionTimestamp
if t != nil && !(*t).Time.IsZero() { if t != nil && !(*t).Time.IsZero() {