/
logging.go
70 lines (61 loc) · 2.09 KB
/
logging.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
// Copyright (C) 2023 Red Hat
// SPDX-License-Identifier: Apache-2.0
/*
Package logging provides various utility functions regarding
optional service log collection for the sf-operator:
* create fluent bit sidecar
*/
package logging
import (
_ "embed"
"strconv"
v1 "github.com/softwarefactory-project/sf-operator/api/v1"
"github.com/softwarefactory-project/sf-operator/controllers/libs/base"
apiv1 "k8s.io/api/core/v1"
)
type FluentBitLabel struct {
Key string
Value string
}
type PythonTemplateLoggingParams struct {
LogLevel string
ForwardLogs bool
BaseURL string
}
func CreateForwarderEnvVars(name string, extraLabels []FluentBitLabel) []apiv1.EnvVar {
forwarderEnvVars := []apiv1.EnvVar{
base.MkEnvVarFromFieldRef("K8S_NODENAME", "spec.nodeName"),
base.MkEnvVarFromFieldRef("K8S_PODNAME", "metadata.name"),
base.MkEnvVarFromFieldRef("K8S_NAMESPACE", "metadata.namespace"),
base.MkEnvVarFromFieldRef("K8S_PODIP", "status.podIP"),
base.MkEnvVar("K8S_LABELS_RUN", name),
}
for i := range extraLabels {
var v = base.MkEnvVar("K8S_"+extraLabels[i].Key, extraLabels[i].Value)
forwarderEnvVars = append(forwarderEnvVars, v)
}
return forwarderEnvVars
}
func SetupLogForwarding(serviceName string, forwarderSpec *v1.FluentBitForwarderSpec, extraLabels []FluentBitLabel, annotations map[string]string) []apiv1.EnvVar {
if forwarderSpec != nil {
annotations["log-forwarding"] = forwarderSpec.HTTPInputHost + ":" + strconv.Itoa(int(forwarderSpec.HTTPInputPort))
return CreateForwarderEnvVars(serviceName, extraLabels)
} else {
annotations["log-forwarding"] = "disabled"
return []apiv1.EnvVar{}
}
}
func CreateFluentBitSideCarContainer(serviceName string, extraLabels []FluentBitLabel, volumeMounts []apiv1.VolumeMount, debug bool) apiv1.Container {
var img = base.FluentBitImage(debug)
container := base.MkContainer("fluentbit", img)
container.Env = CreateForwarderEnvVars(serviceName, extraLabels)
ports := []apiv1.ContainerPort{
{
Name: "fb-http-server",
ContainerPort: 2020,
},
}
container.Ports = ports
container.VolumeMounts = volumeMounts
return container
}