-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[extension/observer/k8sobserver] add k8s.ingress endpoint #33005
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
6253c76
feat: add k8s.ingress endpoint
M0NsTeRRR 21f320e
fix: missing receivecreator config
M0NsTeRRR 8ade6ab
fix: validate ingress config
M0NsTeRRR 7181139
fix: receivercreator README typo ingress
M0NsTeRRR 7e1cf40
fix: add changelog
M0NsTeRRR 4bb5112
fix: CI lint
M0NsTeRRR 9154c50
fix: receivecreator config testdata
M0NsTeRRR 83bc198
chore(docs): add documentation example for k8s.ingress
M0NsTeRRR bdaf035
fix: remove useless kubeletstats
M0NsTeRRR 5ccecf5
Merge branch 'main' into main
M0NsTeRRR 6e5116a
fix: typo
M0NsTeRRR 56a8275
chore(docs): use scheme & port in example
M0NsTeRRR File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it makes sense to escape the backslashes here and be explicit with the ID format to avoid any kind of unlikely ID collision?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know; that was my initial question. How do other components deal with that ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We just need to ensure the uniqueness of the endpoint. I don't think the escaping is required. We shouldn't run into any conflicts without it