forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[extension/observer/k8sobserver] add k8s.ingress endpoint (open-telem…
…etry#33005) **Description:** Add support for k8s.ingress endpoint As described in the issue, this will allow users to dynamically obtain Kubernetes ingress ressource, facilitating the monitoring of certificate expiration with the Blackbox Exporter for example. I didn't create a global ingress resource with sub-endpoints for each path because I don't see a relevant 'target'. I'm uncertain about the impact of my decision regarding the 'UID' as it's structured as '{namespace}/{ingress UID}/{host}{path}'. Since a path automatically starts with a '/', and can contain other '/', should I escape them ? **Link to tracking Issue:** <Issue number if applicable> open-telemetry#32971 --------- Signed-off-by: Ludovic Ortega <[email protected]> Co-authored-by: Chris Mark <[email protected]>
- Loading branch information
Showing
17 changed files
with
457 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Use this changelog template to create an entry for release notes. | ||
|
||
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' | ||
change_type: enhancement | ||
|
||
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) | ||
component: k8sobserver | ||
|
||
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
note: Add support for k8s.ingress endpoint. | ||
|
||
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. | ||
issues: [32971] | ||
|
||
# (Optional) One or more lines of additional information to render under the primary note. | ||
# These lines will be padded with 2 spaces and then inserted directly into the document. | ||
# Use pipe (|) for multiline entries. | ||
subtext: | ||
|
||
# If your change doesn't affect end users or the exported elements of any package, | ||
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. | ||
# Optional: The change log or logs in which this entry should be included. | ||
# e.g. '[user]' or '[user, api]' | ||
# Include 'user' if the change is relevant to end users. | ||
# Include 'api' if there is a change to a library API. | ||
# Default: '[user]' | ||
change_logs: [] |
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,103 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package k8sobserver // import "github.com/open-telemetry/opentelemetry-collector-contrib/extension/observer/k8sobserver" | ||
|
||
import ( | ||
"fmt" | ||
"net/url" | ||
"strings" | ||
|
||
v1 "k8s.io/api/networking/v1" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/extension/observer" | ||
) | ||
|
||
// convertIngressToEndpoints converts a ingress instance into a slice of endpoints. The endpoints | ||
// include an endpoint for each path that is mapped to an ingress. | ||
func convertIngressToEndpoints(idNamespace string, ingress *v1.Ingress) []observer.Endpoint { | ||
endpoints := []observer.Endpoint{} | ||
|
||
// Loop through every ingress rule to get every defined path. | ||
for _, rule := range ingress.Spec.Rules { | ||
scheme := getScheme(rule.Host, getTLSHosts(ingress)) | ||
|
||
if rule.HTTP != nil { | ||
// Create endpoint for each ingress rule. | ||
for _, path := range rule.HTTP.Paths { | ||
endpointID := observer.EndpointID(fmt.Sprintf("%s/%s/%s%s", idNamespace, ingress.UID, rule.Host, path.Path)) | ||
endpoints = append(endpoints, observer.Endpoint{ | ||
ID: endpointID, | ||
Target: (&url.URL{ | ||
Scheme: scheme, | ||
Host: rule.Host, | ||
Path: path.Path, | ||
}).String(), | ||
Details: &observer.K8sIngress{ | ||
Name: ingress.Name, | ||
UID: string(endpointID), | ||
Labels: ingress.Labels, | ||
Annotations: ingress.Annotations, | ||
Namespace: ingress.Namespace, | ||
Scheme: scheme, | ||
Host: rule.Host, | ||
Path: path.Path, | ||
}, | ||
}) | ||
} | ||
} | ||
|
||
} | ||
|
||
return endpoints | ||
} | ||
|
||
// getTLSHosts return a list of tls hosts for an ingress ressource. | ||
func getTLSHosts(i *v1.Ingress) []string { | ||
var hosts []string | ||
|
||
for _, tls := range i.Spec.TLS { | ||
hosts = append(hosts, tls.Hosts...) | ||
} | ||
|
||
return hosts | ||
} | ||
|
||
// matchesHostPattern returns true if the host matches the host pattern or wildcard pattern. | ||
func matchesHostPattern(pattern string, host string) bool { | ||
// if host match the pattern (host pattern). | ||
if pattern == host { | ||
return true | ||
} | ||
|
||
// if string does not contains any dot, don't do the next part as it's for wildcard pattern. | ||
if !strings.Contains(host, ".") { | ||
return false | ||
} | ||
|
||
patternParts := strings.Split(pattern, ".") | ||
hostParts := strings.Split(host, ".") | ||
|
||
// If the first part of the pattern is not a wildcard pattern. | ||
if patternParts[0] != "*" { | ||
return false | ||
} | ||
|
||
// If host and pattern without wildcard part does not match. | ||
if strings.Join(patternParts[1:], ".") != strings.Join(hostParts[1:], ".") { | ||
return false | ||
} | ||
|
||
return true | ||
} | ||
|
||
// getScheme return the scheme of an ingress host based on tls configuration. | ||
func getScheme(host string, tlsHosts []string) string { | ||
for _, pattern := range tlsHosts { | ||
if matchesHostPattern(pattern, host) { | ||
return "https" | ||
} | ||
} | ||
|
||
return "http" | ||
} |
Oops, something went wrong.