| 
									
										
										
										
											2019-06-09 00:10:57 -07:00
										 |  |  | package kubernetes
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							|  |  |  | 	"context"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-31 14:35:22 -05:00
										 |  |  | 	"github.com/coredns/coredns/plugin"
 | 
					
						
							| 
									
										
										
										
											2019-06-09 00:10:57 -07:00
										 |  |  | 	"github.com/coredns/coredns/plugin/metadata"
 | 
					
						
							|  |  |  | 	"github.com/coredns/coredns/request"
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Metadata implements the metadata.Provider interface.
 | 
					
						
							|  |  |  | func (k *Kubernetes) Metadata(ctx context.Context, state request.Request) context.Context {
 | 
					
						
							| 
									
										
										
										
											2020-05-05 14:34:24 -04:00
										 |  |  | 	pod := k.podWithIP(state.IP())
 | 
					
						
							|  |  |  | 	if pod != nil {
 | 
					
						
							|  |  |  | 		metadata.SetValueFunc(ctx, "kubernetes/client-namespace", func() string {
 | 
					
						
							|  |  |  | 			return pod.Namespace
 | 
					
						
							|  |  |  | 		})
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		metadata.SetValueFunc(ctx, "kubernetes/client-pod-name", func() string {
 | 
					
						
							|  |  |  | 			return pod.Name
 | 
					
						
							|  |  |  | 		})
 | 
					
						
							| 
									
										
										
										
											2024-03-07 14:34:09 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		for k, v := range pod.Labels {
 | 
					
						
							|  |  |  | 			metadata.SetValueFunc(ctx, "kubernetes/client-label/"+k, func() string {
 | 
					
						
							|  |  |  | 				return v
 | 
					
						
							|  |  |  | 			})
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2020-05-05 14:34:24 -04:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-31 14:35:22 -05:00
										 |  |  | 	zone := plugin.Zones(k.Zones).Matches(state.Name())
 | 
					
						
							| 
									
										
										
										
											2020-05-05 14:34:24 -04:00
										 |  |  | 	if zone == "" {
 | 
					
						
							|  |  |  | 		return ctx
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2025-05-19 07:58:16 +02:00
										 |  |  | 	multicluster := false
 | 
					
						
							|  |  |  | 	if z := plugin.Zones(k.opts.multiclusterZones).Matches(state.Zone); z != "" {
 | 
					
						
							|  |  |  | 		multicluster = true
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2019-06-09 00:10:57 -07:00
										 |  |  | 	// possible optimization: cache r so it doesn't need to be calculated again in ServeDNS
 | 
					
						
							| 
									
										
										
										
											2025-05-19 07:58:16 +02:00
										 |  |  | 	r, err := parseRequest(state.Name(), zone, multicluster)
 | 
					
						
							| 
									
										
										
										
											2019-06-09 00:10:57 -07:00
										 |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		metadata.SetValueFunc(ctx, "kubernetes/parse-error", func() string {
 | 
					
						
							|  |  |  | 			return err.Error()
 | 
					
						
							|  |  |  | 		})
 | 
					
						
							|  |  |  | 		return ctx
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	metadata.SetValueFunc(ctx, "kubernetes/port-name", func() string {
 | 
					
						
							|  |  |  | 		return r.port
 | 
					
						
							|  |  |  | 	})
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	metadata.SetValueFunc(ctx, "kubernetes/protocol", func() string {
 | 
					
						
							|  |  |  | 		return r.protocol
 | 
					
						
							|  |  |  | 	})
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	metadata.SetValueFunc(ctx, "kubernetes/endpoint", func() string {
 | 
					
						
							|  |  |  | 		return r.endpoint
 | 
					
						
							|  |  |  | 	})
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-19 07:58:16 +02:00
										 |  |  | 	if multicluster {
 | 
					
						
							|  |  |  | 		metadata.SetValueFunc(ctx, "kubernetes/cluster", func() string {
 | 
					
						
							|  |  |  | 			return r.cluster
 | 
					
						
							|  |  |  | 		})
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-09 00:10:57 -07:00
										 |  |  | 	metadata.SetValueFunc(ctx, "kubernetes/service", func() string {
 | 
					
						
							|  |  |  | 		return r.service
 | 
					
						
							|  |  |  | 	})
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	metadata.SetValueFunc(ctx, "kubernetes/namespace", func() string {
 | 
					
						
							|  |  |  | 		return r.namespace
 | 
					
						
							|  |  |  | 	})
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	metadata.SetValueFunc(ctx, "kubernetes/kind", func() string {
 | 
					
						
							|  |  |  | 		return r.podOrSvc
 | 
					
						
							|  |  |  | 	})
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return ctx
 | 
					
						
							|  |  |  | }
 |