Skip to content

Commit

Permalink
Make sure OTLP export can report data to OTLP ingress/route without a…
Browse files Browse the repository at this point in the history
…dditional configuration.

Signed-off-by: Pavol Loffay <[email protected]>
  • Loading branch information
pavolloffay committed Aug 15, 2023
1 parent 4558192 commit 26b7994
Show file tree
Hide file tree
Showing 27 changed files with 479 additions and 40 deletions.
21 changes: 21 additions & 0 deletions .chloggen/1967-ingress-path.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: breaking

# The name of the component, or a single word describing the area of concern, (e.g. operator, target allocator, github action)
component: operator

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Make sure OTLP export can report data to OTLP ingress/route without additional configuration

# One or more tracking issues related to the change
issues: [1967]

# (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: |
The ingress can be configured to create a single host with multiple paths or
multiple hosts with subdomains (one per receiver port).
The path from OpenShift route was removed.
The port names are truncate to 15 characters. Users with custom receivers
which create ports with longer name might need to update their configuration.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -484,4 +484,4 @@ catalog-build: opm bundle-build bundle-push ## Build a catalog image.
# Push the catalog image.
.PHONY: catalog-push
catalog-push: ## Push a catalog image.
docker push $(CATALOG_IMG)
docker push $(CATALOG_IMG)
15 changes: 15 additions & 0 deletions apis/v1alpha1/ingress_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,18 @@ const (
// and re-encrypt using a new certificate.
TLSRouteTerminationTypeReencrypt TLSRouteTerminationType = "reencrypt"
)

// IngressRuleType defines how the collector receivers will be exposed in the Ingress.
//
// +kubebuilder:validation:Enum=path;subdomain
type IngressRuleType string

const (
// IngressRuleTypePath configures Ingress to use single host with multiple paths.
// This configuration might require additional ingress setting to rewrite paths.
IngressRuleTypePath IngressRuleType = "path"

// IngressRuleTypeSubdomain configures Ingress to use multiple hosts - one for each exposed
// receiver port. The port name is used as a subdomain for the host defined in the Ingress e.g. otlp-http.example.com.
IngressRuleTypeSubdomain IngressRuleType = "subdomain"
)
8 changes: 7 additions & 1 deletion apis/v1alpha1/opentelemetrycollector_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,15 @@ const (
// SEE: OpenTelemetryCollector.spec.ports[index].
type Ingress struct {
// Type default value is: ""
// Supported types are: ingress
// Supported types are: ingress, route
Type IngressType `json:"type,omitempty"`

// RuleType defines how Ingress exposes collector receivers.
// IngressRuleTypePath ("path") exposes each receiver port on a unique path on single domain defined in Hostname.
// IngressRuleTypeSubdomain ("subdomain") exposes each receiver port on a unique subdomain of Hostname.
// Default is IngressRuleTypePath ("path").
RuleType IngressRuleType `json:"ruleType,omitempty"`

// Hostname by which the ingress proxy can be reached.
// +optional
Hostname string `json:"hostname,omitempty"`
Expand Down
6 changes: 6 additions & 0 deletions apis/v1alpha1/opentelemetrycollector_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ func (r *OpenTelemetryCollector) Default() {
if r.Spec.Ingress.Type == IngressTypeRoute && r.Spec.Ingress.Route.Termination == "" {
r.Spec.Ingress.Route.Termination = TLSRouteTerminationTypeEdge
}
if r.Spec.Ingress.Type == IngressTypeNginx && r.Spec.Ingress.RuleType == "" {
r.Spec.Ingress.RuleType = IngressRuleTypePath
}
}

// +kubebuilder:webhook:verbs=create;update,path=/validate-opentelemetry-io-v1alpha1-opentelemetrycollector,mutating=false,failurePolicy=fail,groups=opentelemetry.io,resources=opentelemetrycollectors,versions=v1alpha1,name=vopentelemetrycollectorcreateupdate.kb.io,sideEffects=none,admissionReviewVersions=v1
Expand Down Expand Up @@ -248,6 +251,9 @@ func (r *OpenTelemetryCollector) validateCRDSpec() error {
ModeDeployment, ModeDaemonSet, ModeStatefulSet,
)
}
if r.Spec.Ingress.RuleType == IngressRuleTypeSubdomain && (r.Spec.Ingress.Hostname == "" || r.Spec.Ingress.Hostname == "*") {
return fmt.Errorf("a valid Ingress hostname has to be defined for subdomain ruleType")
}

if r.Spec.LivenessProbe != nil {
if r.Spec.LivenessProbe.InitialDelaySeconds != nil && *r.Spec.LivenessProbe.InitialDelaySeconds < 0 {
Expand Down
11 changes: 11 additions & 0 deletions apis/v1alpha1/opentelemetrycollector_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,17 @@ func TestOTELColValidatingWebhook(t *testing.T) {
},
expectedErr: "the OpenTelemetry Collector mode is set to sidecar, which does not support the attribute 'AdditionalContainers'",
},
{
name: "missing ingress hostname for subdomain ruleType",
otelcol: OpenTelemetryCollector{
Spec: OpenTelemetryCollectorSpec{
Ingress: Ingress{
RuleType: IngressRuleTypeSubdomain,
},
},
},
expectedErr: "a valid Ingress hostname has to be defined for subdomain ruleType",
},
}

for _, test := range tests {
Expand Down
13 changes: 12 additions & 1 deletion bundle/manifests/opentelemetry.io_opentelemetrycollectors.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2653,6 +2653,16 @@ spec:
- reencrypt
type: string
type: object
ruleType:
description: RuleType defines how Ingress exposes collector receivers.
IngressRuleTypePath ("path") exposes each receiver port on a
unique path on single domain defined in Hostname. IngressRuleTypeSubdomain
("subdomain") exposes each receiver port on a unique subdomain
of Hostname. Default is IngressRuleTypePath ("path").
enum:
- path
- subdomain
type: string
tls:
description: TLS configuration.
items:
Expand Down Expand Up @@ -2681,7 +2691,8 @@ spec:
type: object
type: array
type:
description: 'Type default value is: "" Supported types are: ingress'
description: 'Type default value is: "" Supported types are: ingress,
route'
enum:
- ingress
- route
Expand Down
13 changes: 12 additions & 1 deletion config/crd/bases/opentelemetry.io_opentelemetrycollectors.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2650,6 +2650,16 @@ spec:
- reencrypt
type: string
type: object
ruleType:
description: RuleType defines how Ingress exposes collector receivers.
IngressRuleTypePath ("path") exposes each receiver port on a
unique path on single domain defined in Hostname. IngressRuleTypeSubdomain
("subdomain") exposes each receiver port on a unique subdomain
of Hostname. Default is IngressRuleTypePath ("path").
enum:
- path
- subdomain
type: string
tls:
description: TLS configuration.
items:
Expand Down Expand Up @@ -2678,7 +2688,8 @@ spec:
type: object
type: array
type:
description: 'Type default value is: "" Supported types are: ingress'
description: 'Type default value is: "" Supported types are: ingress,
route'
enum:
- ingress
- route
Expand Down
11 changes: 10 additions & 1 deletion docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -8438,6 +8438,15 @@ Ingress is used to specify how OpenTelemetry Collector is exposed. This function
Route is an OpenShift specific section that is only considered when type "route" is used.<br/>
</td>
<td>false</td>
</tr><tr>
<td><b>ruleType</b></td>
<td>enum</td>
<td>
RuleType defines how Ingress exposes collector receivers. IngressRuleTypePath ("path") exposes each receiver port on a unique path on single domain defined in Hostname. IngressRuleTypeSubdomain ("subdomain") exposes each receiver port on a unique subdomain of Hostname. Default is IngressRuleTypePath ("path").<br/>
<br/>
<i>Enum</i>: path, subdomain<br/>
</td>
<td>false</td>
</tr><tr>
<td><b><a href="#opentelemetrycollectorspecingresstlsindex">tls</a></b></td>
<td>[]object</td>
Expand All @@ -8449,7 +8458,7 @@ Ingress is used to specify how OpenTelemetry Collector is exposed. This function
<td><b>type</b></td>
<td>enum</td>
<td>
Type default value is: "" Supported types are: ingress<br/>
Type default value is: "" Supported types are: ingress, route<br/>
<br/>
<i>Enum</i>: ingress, route<br/>
</td>
Expand Down
95 changes: 70 additions & 25 deletions internal/manifests/collector/ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,12 @@ func Ingress(cfg config.Config, logger logr.Logger, otelcol v1alpha1.OpenTelemet
return nil
}

pathType := networkingv1.PathTypePrefix
paths := make([]networkingv1.HTTPIngressPath, len(ports))
for i, p := range ports {
paths[i] = networkingv1.HTTPIngressPath{
Path: "/" + p.Name,
PathType: &pathType,
Backend: networkingv1.IngressBackend{
Service: &networkingv1.IngressServiceBackend{
Name: naming.Service(otelcol.Name),
Port: networkingv1.ServiceBackendPort{
// Valid names must be non-empty and no more than 15 characters long.
Name: naming.Truncate(p.Name, 15),
},
},
},
}
var rules []networkingv1.IngressRule
switch otelcol.Spec.Ingress.RuleType {
case v1alpha1.IngressRuleTypePath, "":
rules = []networkingv1.IngressRule{createPathIngressRules(otelcol.Name, otelcol.Spec.Ingress.Hostname, ports)}
case v1alpha1.IngressRuleTypeSubdomain:
rules = createSubdomainIngressRules(otelcol.Name, otelcol.Spec.Ingress.Hostname, ports)
}

return &networkingv1.Ingress{
Expand All @@ -75,22 +65,77 @@ func Ingress(cfg config.Config, logger logr.Logger, otelcol v1alpha1.OpenTelemet
},
},
Spec: networkingv1.IngressSpec{
TLS: otelcol.Spec.Ingress.TLS,
Rules: []networkingv1.IngressRule{
{
Host: otelcol.Spec.Ingress.Hostname,
IngressRuleValue: networkingv1.IngressRuleValue{
HTTP: &networkingv1.HTTPIngressRuleValue{
Paths: paths,
},
TLS: otelcol.Spec.Ingress.TLS,
Rules: rules,
IngressClassName: otelcol.Spec.Ingress.IngressClassName,
},
}
}

func createPathIngressRules(otelcol string, hostname string, ports []corev1.ServicePort) networkingv1.IngressRule {
pathType := networkingv1.PathTypePrefix
paths := make([]networkingv1.HTTPIngressPath, len(ports))
for i, port := range ports {
portName := naming.PortName(port.Name, port.Port)
paths[i] = networkingv1.HTTPIngressPath{
Path: "/" + port.Name,
PathType: &pathType,
Backend: networkingv1.IngressBackend{
Service: &networkingv1.IngressServiceBackend{
Name: naming.Service(otelcol),
Port: networkingv1.ServiceBackendPort{
Name: portName,
},
},
},
IngressClassName: otelcol.Spec.Ingress.IngressClassName,
}
}
return networkingv1.IngressRule{
Host: hostname,
IngressRuleValue: networkingv1.IngressRuleValue{
HTTP: &networkingv1.HTTPIngressRuleValue{
Paths: paths,
},
},
}
}

func createSubdomainIngressRules(otelcol string, hostname string, ports []corev1.ServicePort) []networkingv1.IngressRule {
var rules []networkingv1.IngressRule
pathType := networkingv1.PathTypePrefix
for _, port := range ports {
portName := naming.PortName(port.Name, port.Port)

host := fmt.Sprintf("%s.%s", portName, hostname)
// This should not happen due to validation in the webhook.
if hostname == "" || hostname == "*" {
host = portName
}
rules = append(rules, networkingv1.IngressRule{
Host: host,
IngressRuleValue: networkingv1.IngressRuleValue{
HTTP: &networkingv1.HTTPIngressRuleValue{
Paths: []networkingv1.HTTPIngressPath{
{
Path: "/",
PathType: &pathType,
Backend: networkingv1.IngressBackend{
Service: &networkingv1.IngressServiceBackend{
Name: naming.Service(otelcol),
Port: networkingv1.ServiceBackendPort{
Name: portName,
},
},
},
},
},
},
},
})
}
return rules
}

// TODO: Update this to properly return an error https://github.com/open-telemetry/opentelemetry-operator/issues/1972
func servicePortsFromCfg(logger logr.Logger, otelcol v1alpha1.OpenTelemetryCollector) []corev1.ServicePort {
configFromString, err := adapters.ConfigFromString(otelcol.Spec.Config)
Expand Down
Loading

0 comments on commit 26b7994

Please sign in to comment.