/
fluentd-service.go
71 lines (61 loc) · 1.68 KB
/
fluentd-service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package operator
import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
fluentdv1alpha1 "github.com/fluent/fluent-operator/apis/fluentd/v1alpha1"
)
const (
FluentdForwardPortName = "forward"
FluentdHttpPortName = "http"
)
func MakeFluentdService(fd fluentdv1alpha1.Fluentd) corev1.Service {
labels := map[string]string{
"app.kubernetes.io/name": fd.Name,
"app.kubernetes.io/instance": "fluentd",
"app.kubernetes.io/component": "fluentd",
}
svc := corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: fd.Name,
Namespace: fd.Namespace,
Labels: labels,
},
Spec: corev1.ServiceSpec{
Selector: labels,
Type: corev1.ServiceTypeClusterIP,
},
}
// read inputs definition from globalInputs
globalInputs := fd.Spec.GlobalInputs
for _, input := range globalInputs {
if input.Forward != nil {
forwardPort := *input.Forward.Port
if forwardPort == 0 {
forwardPort = DefaultForwardPort
}
forwardContainerPort := corev1.ServicePort{
Name: DefaultForwardName,
Port: forwardPort,
TargetPort: intstr.FromString(FluentdForwardPortName),
Protocol: corev1.ProtocolTCP,
}
svc.Spec.Ports = append(svc.Spec.Ports, forwardContainerPort)
continue
}
if input.Http != nil {
httpPort := *input.Http.Port
if httpPort == 0 {
httpPort = DefaultHttpPort
}
httpContainerPort := corev1.ServicePort{
Name: DefaultHttpName,
Port: httpPort,
TargetPort: intstr.FromString(FluentdHttpPortName),
Protocol: corev1.ProtocolTCP,
}
svc.Spec.Ports = append(svc.Spec.Ports, httpContainerPort)
}
}
return svc
}