Skip to content

Commit

Permalink
chore: implemented unit tests for ingress for extensions
Browse files Browse the repository at this point in the history
Signed-off-by: Ankit152 <[email protected]>
  • Loading branch information
Ankit152 committed Nov 27, 2024
1 parent 1859ede commit 6e44119
Show file tree
Hide file tree
Showing 9 changed files with 338 additions and 21 deletions.
16 changes: 16 additions & 0 deletions .chloggen/ingress.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# 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. collector, target allocator, auto-instrumentation, opamp, github action)
component: collector

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: support for creating ingress for extensions that consumes the service.

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

# (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:
8 changes: 4 additions & 4 deletions apis/v1beta1/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,12 +323,12 @@ func (c *Config) GetExporterPorts(logger logr.Logger) ([]corev1.ServicePort, err
return c.getPortsForComponentKinds(logger, KindExporter)
}

func (c *Config) GetReceiverAndExporterPorts(logger logr.Logger) ([]corev1.ServicePort, error) {
return c.getPortsForComponentKinds(logger, KindReceiver, KindExporter)
func (c *Config) GetExtensionPorts(logger logr.Logger) ([]corev1.ServicePort, error) {
return c.getPortsForComponentKinds(logger, KindExtension)
}

func (c *Config) GetAllPorts(logger logr.Logger) ([]corev1.ServicePort, error) {
return c.getPortsForComponentKinds(logger, KindReceiver, KindExporter, KindExtension)
func (c *Config) GetReceiverAndExporterPorts(logger logr.Logger) ([]corev1.ServicePort, error) {
return c.getPortsForComponentKinds(logger, KindReceiver, KindExporter)
}

func (c *Config) GetAllPorts(logger logr.Logger) ([]corev1.ServicePort, error) {
Expand Down
5 changes: 5 additions & 0 deletions apis/v1beta1/opentelemetrycollector_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ type OpenTelemetryCollectorSpec struct {
// Valid modes are: deployment, daemonset and statefulset.
// +optional
Ingress Ingress `json:"ingress,omitempty"`
// ExtensionIngress is used to specify how OpenTelemetry Collector is exposed. This
// functionality is only available if one of the valid modes is set.
// Valid modes are: deployment, daemonset and statefulset.
// +optional
ExtensionIngress Ingress `json:"extensionIngress,omitempty"`
// Liveness config for the OpenTelemetry Collector except the probe handler which is auto generated from the health extension of the collector.
// It is only effective when healthcheckextension is configured in the OpenTelemetry Collector pipeline.
// +optional
Expand Down
1 change: 1 addition & 0 deletions apis/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 43 additions & 0 deletions config/crd/bases/opentelemetry.io_opentelemetrycollectors.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6042,6 +6042,49 @@ spec:
x-kubernetes-map-type: atomic
type: object
type: array
extensionIngress:
properties:
annotations:
additionalProperties:
type: string
type: object
hostname:
type: string
ingressClassName:
type: string
route:
properties:
termination:
enum:
- insecure
- edge
- passthrough
- reencrypt
type: string
type: object
ruleType:
enum:
- path
- subdomain
type: string
tls:
items:
properties:
hosts:
items:
type: string
type: array
x-kubernetes-list-type: atomic
secretName:
type: string
type: object
type: array
type:
enum:
- ingress
- route
type: string
type: object
hostNetwork:
type: boolean
image:
Expand Down
1 change: 1 addition & 0 deletions internal/manifests/collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func Build(params manifests.Params) ([]client.Object, error) {
manifests.Factory(MonitoringService),
manifests.Factory(ExtensionService),
manifests.Factory(Ingress),
manifests.Factory(ExtensionIngress),
}...)

if featuregate.CollectorUsesTargetAllocatorCR.IsEnabled() {
Expand Down
54 changes: 37 additions & 17 deletions internal/manifests/collector/ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ func Ingress(params manifests.Params) (*networkingv1.Ingress, error) {
var rules []networkingv1.IngressRule
switch params.OtelCol.Spec.Ingress.RuleType {
case v1beta1.IngressRuleTypePath, "":
rules = []networkingv1.IngressRule{createPathIngressRules(params.OtelCol.Name, params.OtelCol.Spec.Ingress.Hostname, ports)}
rules = []networkingv1.IngressRule{createPathIngressRules(params.OtelCol.Name, params.OtelCol.Spec.Ingress.Hostname, ports, "service")}
case v1beta1.IngressRuleTypeSubdomain:
rules = createSubdomainIngressRules(params.OtelCol.Name, params.OtelCol.Spec.Ingress.Hostname, ports)
rules = createSubdomainIngressRules(params.OtelCol.Name, params.OtelCol.Spec.Ingress.Hostname, ports, "service")
}

return &networkingv1.Ingress{
Expand All @@ -71,48 +71,60 @@ func Ingress(params manifests.Params) (*networkingv1.Ingress, error) {
}

func ExtensionIngress(params manifests.Params) (*networkingv1.Ingress, error) {
name := naming.Ingress(params.OtelCol.Name)
name := naming.ExtensionIngress(params.OtelCol.Name)
labels := manifestutils.Labels(params.OtelCol.ObjectMeta, name, params.OtelCol.Spec.Image, ComponentOpenTelemetryCollector, params.Config.LabelsFilter())

ports, err := extensionServicePortsFromCfg(params.Log, params.OtelCol)
if params.OtelCol.Spec.ExtensionIngress.Type != v1beta1.IngressTypeIngress {
return nil, nil
}

if err != nil {
ports, err := extensionServicePortsFromCfg(params.Log, params.OtelCol)
if err != nil || len(ports) == 0 {
return nil, err
}

// if there are no ports, no ingress required
if len(ports) == 0 {
return nil, nil
var rules []networkingv1.IngressRule
switch params.OtelCol.Spec.Ingress.RuleType {
case v1beta1.IngressRuleTypePath, "":
rules = []networkingv1.IngressRule{createPathIngressRules(params.OtelCol.Name, params.OtelCol.Spec.ExtensionIngress.Hostname, ports, "extension")}
case v1beta1.IngressRuleTypeSubdomain:
rules = createSubdomainIngressRules(params.OtelCol.Name, params.OtelCol.Spec.ExtensionIngress.Hostname, ports, "extension")
}

rules := createSubdomainIngressRules(name, "", ports)

return &networkingv1.Ingress{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: params.OtelCol.Namespace,
Annotations: params.OtelCol.Spec.Ingress.Annotations, // can the spec annotations be used?
Annotations: params.OtelCol.Spec.ExtensionIngress.Annotations,
Labels: labels,
},
Spec: networkingv1.IngressSpec{
TLS: params.OtelCol.Spec.Ingress.TLS,
TLS: params.OtelCol.Spec.ExtensionIngress.TLS,
Rules: rules,
IngressClassName: params.OtelCol.Spec.Ingress.IngressClassName,
IngressClassName: params.OtelCol.Spec.ExtensionIngress.IngressClassName,
},
}, nil
}

func createPathIngressRules(otelcol string, hostname string, ports []corev1.ServicePort) networkingv1.IngressRule {
func createPathIngressRules(otelcol string, hostname string, ports []corev1.ServicePort, serviceType string) networkingv1.IngressRule {
pathType := networkingv1.PathTypePrefix
paths := make([]networkingv1.HTTPIngressPath, len(ports))

var name string
if serviceType == "extension" {
name = naming.ExtensionService(otelcol)
} else {
name = naming.Service(otelcol)
}

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),
Name: name,
Port: networkingv1.ServiceBackendPort{
Name: portName,
},
Expand All @@ -130,9 +142,17 @@ func createPathIngressRules(otelcol string, hostname string, ports []corev1.Serv
}
}

func createSubdomainIngressRules(otelcol string, hostname string, ports []corev1.ServicePort) []networkingv1.IngressRule {
func createSubdomainIngressRules(otelcol string, hostname string, ports []corev1.ServicePort, serviceType string) []networkingv1.IngressRule {
var rules []networkingv1.IngressRule
pathType := networkingv1.PathTypePrefix

var name string
if serviceType == "extension" {
name = naming.ExtensionService(otelcol)
} else {
name = naming.Service(otelcol)
}

for _, port := range ports {
portName := naming.PortName(port.Name, port.Port)

Expand All @@ -151,7 +171,7 @@ func createSubdomainIngressRules(otelcol string, hostname string, ports []corev1
PathType: &pathType,
Backend: networkingv1.IngressBackend{
Service: &networkingv1.IngressServiceBackend{
Name: naming.Service(otelcol),
Name: name,
Port: networkingv1.ServiceBackendPort{
Name: portName,
},
Expand Down
Loading

0 comments on commit 6e44119

Please sign in to comment.