-
Notifications
You must be signed in to change notification settings - Fork 322
Expand file tree
/
Copy pathfluentd-service.go
More file actions
102 lines (88 loc) · 2.35 KB
/
fluentd-service.go
File metadata and controls
102 lines (88 loc) · 2.35 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package operator
import (
"fmt"
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/v3/apis/fluentd/v1alpha1"
)
func MakeFluentdService(fd fluentdv1alpha1.Fluentd) *corev1.Service {
var name string
var labels map[string]string
if fd.Spec.Service.Name != "" {
name = fd.Spec.Service.Name
} else {
name = fd.Name
}
if len(fd.Spec.Service.Labels) != 0 {
labels = fd.Spec.Service.Labels
} else {
labels = map[string]string{
"app.kubernetes.io/name": name,
"app.kubernetes.io/instance": "fluentd",
"app.kubernetes.io/component": "fluentd",
}
}
serviceType := corev1.ServiceTypeClusterIP
if fd.Spec.Service.Type != nil {
serviceType = *fd.Spec.Service.Type
}
svc := corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: fd.Namespace,
Labels: labels,
},
Spec: corev1.ServiceSpec{
Selector: labels,
Type: serviceType,
},
}
// Read inputs definition from globalInputs
globalInputs := fd.Spec.GlobalInputs
firstForwardPort := true
firstHttpPort := true
for _, input := range globalInputs {
if input.Forward != nil {
forwardPort := *input.Forward.Port
if forwardPort == 0 {
forwardPort = DefaultForwardPort
}
forwardName := DefaultForwardName
if !firstForwardPort {
forwardName = fmt.Sprintf("%s-%d", DefaultForwardName, forwardPort)
}
forwardContainerPort := corev1.ServicePort{
Name: forwardName,
Port: forwardPort,
TargetPort: intstr.FromString(forwardName),
Protocol: corev1.ProtocolTCP,
}
svc.Spec.Ports = append(svc.Spec.Ports, forwardContainerPort)
firstForwardPort = false
continue
}
if input.Http != nil {
httpPort := *input.Http.Port
if httpPort == 0 {
httpPort = DefaultHttpPort
}
httpName := DefaultHttpName
if !firstHttpPort {
httpName = fmt.Sprintf("%s-%d", DefaultHttpName, httpPort)
}
httpContainerPort := corev1.ServicePort{
Name: httpName,
Port: httpPort,
TargetPort: intstr.FromString(httpName),
Protocol: corev1.ProtocolTCP,
}
svc.Spec.Ports = append(svc.Spec.Ports, httpContainerPort)
firstHttpPort = false
}
}
if len(fd.Spec.Service.Annotations) != 0 {
svc.Annotations = fd.Spec.Service.Annotations
}
return &svc
}