-
Notifications
You must be signed in to change notification settings - Fork 351
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
dataclients/kubernetes: add metadata route
Adds a special route with a predefined id `kube__metadata` that contains `False` predicate, no filters and endpoints metadata. Endpoints metadata is encoded as JSON via [data URI scheme](https://en.wikipedia.org/wiki/Data_URI_scheme) into the route backend address field. Example eskip: ``` kube__metadata: False() -> "data:application/json;base64,eyJhZGRyZXNzZXMiO..."; ... ``` A special route pre-processor detects and removes this route, decodes metadata and stores it as EndpointRegistry metrics. The endpoint metrics then used to obtain endpoint zone, node and pod names for tracing and logging. TODO: - [ ] add flag to enable metadata route - [ ] tests Fixes #1559 Signed-off-by: Alexander Yastrebov <[email protected]>
- Loading branch information
1 parent
7a440ef
commit d214c11
Showing
12 changed files
with
278 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,188 @@ | ||
package kubernetes | ||
|
||
import ( | ||
"encoding/base64" | ||
"encoding/json" | ||
"fmt" | ||
"net" | ||
|
||
log "github.com/sirupsen/logrus" | ||
"github.com/zalando/skipper/eskip" | ||
"github.com/zalando/skipper/predicates" | ||
"github.com/zalando/skipper/routing" | ||
|
||
snet "github.com/zalando/skipper/net" | ||
) | ||
|
||
type MetadataPreProcessorOptions struct { | ||
EndpointRegistry *routing.EndpointRegistry | ||
} | ||
|
||
type metadataPreProcessor struct { | ||
options MetadataPreProcessorOptions | ||
} | ||
|
||
type kubeRouteMetadata struct { | ||
Addresses []kubeRouteMetadataAddress `json:"addresses"` | ||
} | ||
|
||
type kubeRouteMetadataAddress struct { | ||
Address string `json:"address"` | ||
Zone string `json:"zone"` | ||
NodeName string `json:"nodeName"` | ||
TargetRef *objectReference `json:"targetRef"` | ||
} | ||
|
||
// NewMetadataPreProcessor creates pre-processor for metadata route. | ||
func NewMetadataPreProcessor(options MetadataPreProcessorOptions) routing.PreProcessor { | ||
return &metadataPreProcessor{options: options} | ||
} | ||
|
||
func (pp *metadataPreProcessor) Do(routes []*eskip.Route) []*eskip.Route { | ||
var metadataRoute *eskip.Route | ||
filtered := make([]*eskip.Route, 0, len(routes)) | ||
|
||
for _, r := range routes { | ||
if r.Id == MetadataRouteID { | ||
if metadataRoute == nil { | ||
metadataRoute = r | ||
} else { | ||
log.Errorf("Found multiple metadata routes, using the first one") | ||
} | ||
} else { | ||
filtered = append(filtered, r) | ||
} | ||
} | ||
|
||
if metadataRoute == nil { | ||
log.Errorf("Metadata route not found") | ||
return routes | ||
} | ||
|
||
metadata, err := decodeMetadata(metadataRoute) | ||
if err != nil { | ||
log.Errorf("Failed to decode metadata route: %v", err) | ||
return filtered | ||
} | ||
|
||
for _, r := range filtered { | ||
if r.BackendType == eskip.NetworkBackend { | ||
pp.addMetadata(metadata, r.Backend) | ||
} else if r.BackendType == eskip.LBBackend { | ||
for _, ep := range r.LBEndpoints { | ||
pp.addMetadata(metadata, ep) | ||
} | ||
} | ||
} | ||
return filtered | ||
} | ||
|
||
// metadataRoute creates a route with [MetadataRouteID] id that matches no requests and | ||
// contains metadata for each endpoint address used by Ingresses and RouteGroups. | ||
func metadataRoute(s *clusterState) *eskip.Route { | ||
var metadata kubeRouteMetadata | ||
for id := range s.cachedEndpoints { | ||
if s.enableEndpointSlices { | ||
if eps, ok := s.endpointSlices[id.ResourceID]; ok { | ||
for _, ep := range eps.Endpoints { | ||
metadata.Addresses = append(metadata.Addresses, kubeRouteMetadataAddress{ | ||
Address: ep.Address, | ||
Zone: ep.Zone, | ||
NodeName: ep.NodeName, | ||
TargetRef: ep.TargetRef, | ||
}) | ||
} | ||
} | ||
} else { | ||
if ep, ok := s.endpoints[id.ResourceID]; ok { | ||
for _, subset := range ep.Subsets { | ||
for _, addr := range subset.Addresses { | ||
metadata.Addresses = append(metadata.Addresses, kubeRouteMetadataAddress{ | ||
Address: addr.IP, | ||
NodeName: addr.NodeName, | ||
TargetRef: addr.TargetRef, | ||
}) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
return &eskip.Route{ | ||
Id: MetadataRouteID, | ||
Predicates: []*eskip.Predicate{{Name: predicates.FalseName}}, | ||
BackendType: eskip.NetworkBackend, | ||
Backend: encodeDataURI(&metadata), | ||
} | ||
} | ||
|
||
func decodeMetadata(r *eskip.Route) (map[string]*kubeRouteMetadataAddress, error) { | ||
metadata, err := decodeDataURI(r.Backend) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
result := make(map[string]*kubeRouteMetadataAddress, len(metadata.Addresses)) | ||
for i := range metadata.Addresses { | ||
addr := &metadata.Addresses[i] | ||
result[addr.Address] = addr | ||
} | ||
return result, nil | ||
} | ||
|
||
const dataUriPrefix = "data:application/json;base64," | ||
|
||
// encodeDataURI encodes metadata into data URI. | ||
// See https://datatracker.ietf.org/doc/html/rfc2397 | ||
func encodeDataURI(metadata *kubeRouteMetadata) string { | ||
data, _ := json.Marshal(&metadata) | ||
|
||
buf := make([]byte, len(dataUriPrefix)+base64.StdEncoding.EncodedLen(len(data))) | ||
|
||
copy(buf, dataUriPrefix) | ||
base64.StdEncoding.Encode(buf[len(dataUriPrefix):], data) | ||
|
||
return string(buf) | ||
} | ||
|
||
// encodeDataURI encodes metadata into data URI. | ||
// See https://datatracker.ietf.org/doc/html/rfc2397 | ||
func decodeDataURI(uri string) (*kubeRouteMetadata, error) { | ||
var metadata kubeRouteMetadata | ||
|
||
data, err := base64.StdEncoding.DecodeString(uri[len(dataUriPrefix):]) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to decode base64: %w", err) | ||
} | ||
|
||
if err := json.Unmarshal(data, &metadata); err != nil { | ||
return nil, fmt.Errorf("failed to decode json: %w", err) | ||
} | ||
return &metadata, nil | ||
} | ||
|
||
func (pp *metadataPreProcessor) addMetadata(metadata map[string]*kubeRouteMetadataAddress, endpoint string) { | ||
_, hostPort, err := snet.SchemeHost(endpoint) | ||
if err != nil { | ||
return | ||
} | ||
|
||
host, _, _ := net.SplitHostPort(hostPort) | ||
if err != nil { | ||
host = hostPort | ||
} | ||
|
||
addr, ok := metadata[host] | ||
if !ok { | ||
return | ||
} | ||
|
||
metrics := pp.options.EndpointRegistry.GetMetrics(hostPort) | ||
|
||
metrics.SetTag("zone", addr.Zone) | ||
metrics.SetTag("nodeName", addr.NodeName) | ||
if addr.TargetRef != nil && addr.TargetRef.Kind == "Pod" { | ||
metrics.SetTag("podName", addr.TargetRef.Name) | ||
metrics.SetTag("namespace", addr.TargetRef.Namespace) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.