Skip to content

Commit

Permalink
feat: add ability to customize metrics port (#587)
Browse files Browse the repository at this point in the history
* feat: add ability to customize metrics port

This change allows a user to specify a metrics port as part of their `FluentBit` custom resource.  This is useful for those who need to use a port other than 2020.

An example of a partial FluentBit resource with this new property might look like this:

```
apiVersion: fluentbit.fluent.io/v1alpha2
kind: FluentBit
metadata:
  name: fluent-bit
  namespace: fluent
  labels:
    app.kubernetes.io/name: fluent-bit
spec:
  image: kubesphere/fluent-bit:v2.0.9
  metricsPort: 32020
.
.
.
```

Signed-off-by: Dale Hille <dh185221@ncr.com>

* docs: note the link between MetricsPort/HttpPort

Signed-off-by: Dale Hille <dh185221@ncr.com>

---------

Signed-off-by: Dale Hille <dh185221@ncr.com>
  • Loading branch information
dh185221 committed Feb 27, 2023
1 parent 47eecd2 commit 72ed3c1
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 3 deletions.
2 changes: 2 additions & 0 deletions apis/fluentbit/v1alpha2/fluentbit_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ type FluentBitSpec struct {
// Set DNS policy for the pod. Defaults to "ClusterFirst". Valid values are
// 'ClusterFirstWithHostNet', 'ClusterFirst', 'Default' or 'None'.
DNSPolicy corev1.DNSPolicy `json:"dnsPolicy,omitempty"`
// MetricsPort is the port used by the metrics server. If this option is set, HttpPort from ClusterFluentBitConfig needs to match this value. Default is 2020.
MetricsPort int32 `json:"metricsPort,omitempty"`
}

// FluentBitStatus defines the observed state of FluentBit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2399,6 +2399,10 @@ spec:
format: int32
type: integer
type: object
metricsPort:
description: Set the port used by the metrics server
format: int32
type: integer
nodeSelector:
additionalProperties:
type: string
Expand Down
4 changes: 4 additions & 0 deletions config/crd/bases/fluentbit.fluent.io_fluentbits.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2399,6 +2399,10 @@ spec:
format: int32
type: integer
type: object
metricsPort:
description: Set the port used by the metrics server
format: int32
type: integer
nodeSelector:
additionalProperties:
type: string
Expand Down
1 change: 1 addition & 0 deletions docs/fluentbit.md
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ FluentBitSpec defines the desired state of FluentBit
| ports | Ports represents the pod's ports. | []corev1.ContainerPort |
| rbacRules | RBACRules represents additional rbac rules which will be applied to the fluent-bit clusterrole. | []rbacv1.PolicyRule |
| dnsPolicy | Set DNS policy for the pod. Defaults to \"ClusterFirst\". Valid values are 'ClusterFirstWithHostNet', 'ClusterFirst', 'Default' or 'None'. | corev1.DNSPolicy |
| metricsPort | MetricsPort is the port used by the metrics server. If this option is set, HttpPort from ClusterFluentBitConfig needs to match this value. Default is 2020. | int32 |

[Back to TOC](#table-of-contents)
# InputSpec
Expand Down
10 changes: 9 additions & 1 deletion pkg/operator/daemonset.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ func MakeDaemonSet(fb fluentbitv1alpha2.FluentBit, logPath string) appsv1.Daemon
} else {
labels = fb.Labels
}

var metricsPort int32
if fb.Spec.MetricsPort != 0 {
metricsPort = fb.Spec.MetricsPort
} else {
metricsPort = 2020
}

ds := appsv1.DaemonSet{
ObjectMeta: metav1.ObjectMeta{
Name: fb.Name,
Expand Down Expand Up @@ -81,7 +89,7 @@ func MakeDaemonSet(fb fluentbitv1alpha2.FluentBit, logPath string) appsv1.Daemon
Ports: []corev1.ContainerPort{
{
Name: "metrics",
ContainerPort: 2020,
ContainerPort: metricsPort,
Protocol: "TCP",
},
},
Expand Down
11 changes: 9 additions & 2 deletions pkg/operator/fluent-bit-service.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,18 @@ import (

const (
FluentBitMetricsPortName = "metrics"
FluentBitMetricsPort = 2020
FluentBitTCPProtocolName = "TCP"
)

func MakeFluentbitService(fb fluentbitv1alpha2.FluentBit) corev1.Service {

var FluentBitMetricsPort int32
if fb.Spec.MetricsPort != 0 {
FluentBitMetricsPort = fb.Spec.MetricsPort
} else {
FluentBitMetricsPort = 2020
}

svc := corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: fb.Name,
Expand All @@ -29,7 +36,7 @@ func MakeFluentbitService(fb fluentbitv1alpha2.FluentBit) corev1.Service {
Name: FluentBitMetricsPortName,
Port: FluentBitMetricsPort,
Protocol: FluentBitTCPProtocolName,
TargetPort: intstr.FromInt(FluentBitMetricsPort),
TargetPort: intstr.FromInt(int(FluentBitMetricsPort)),
},
},
},
Expand Down

0 comments on commit 72ed3c1

Please sign in to comment.