Skip to content

Commit 722af6c

Browse files
authored
Revamp volume configuration, fix issues introduced in 2.4.0 (#218)
* use emptydir volumes instead of hostpath defaults to avoid issues with readonly hosts * set default path for hostPath if nothing is set * make the chmod init container optional, fix the permission issues using fsgroup (see below) * add missing runAsGroup configuration to container securitypolicy * enable fsGroup 101 (fluentd) by default to get rid of the volume mount hack. the container will get 101 as a supplemental group and will be allowed to write on the volume * rbac fixes for podsecurity policy roles and proper use of service account name * generic KubernetesStorage implementation and use it for fluentd as well
1 parent c664a6a commit 722af6c

File tree

16 files changed

+733
-127
lines changed

16 files changed

+733
-127
lines changed

config/crd/bases/logging.banzaicloud.io_loggings.yaml

Lines changed: 530 additions & 2 deletions
Large diffs are not rendered by default.

config/samples/logging_v1alpha2_logging_default.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,3 @@ spec:
88
}
99
fluentbit: {}
1010
controlNamespace: default
11-
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
apiVersion: logging.banzaicloud.io/v1beta1
2+
kind: Logging
3+
metadata:
4+
name: defaultlogging
5+
spec:
6+
fluentd:
7+
security:
8+
podSecurityPolicyCreate: true
9+
fluentbit:
10+
security:
11+
podSecurityPolicyCreate: true
12+
controlNamespace: default
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
apiVersion: logging.banzaicloud.io/v1beta1
2+
kind: Logging
3+
metadata:
4+
name: defaultlogging
5+
spec:
6+
fluentd: {}
7+
fluentbit: {}
8+
controlNamespace: default

docs/crds.md

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,11 @@ spec:
125125
| tolerations | [Toleration](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.12/#toleration-v1-core) | {} | Pod toleration |
126126
| metrics | [Metrics](./logging-operator-monitoring.md#metrics-variables) | {} | Metrics defines the service monitor endpoints |
127127
| security | [Security](./security#security-variables) | {} | Security defines Fluentd, Fluentbit deployment security properties |
128-
| position_db | [KubernetesStorage](#KubernetesStorage) | nil | Add position db storage support. If nothing is configured a `hostPath` volume is used with the path `/opt/fluent-bit/<name of the logging CR>/pos`. |
128+
| position_db | [KubernetesStorage](#KubernetesStorage) | nil | Add position db storage support. If nothing is configured an emptyDir volume will be used. |
129129
| inputTail | [InputTail](./fluentbit.md#tail-inputtail) | {} | The tail input plugin allows to monitor one or several text files. |
130130
| filterKubernetes | [FilterKubernetes](./fluentbit.md#kubernetes-filterkubernetes) | {} | Fluent Bit Kubernetes Filter allows to enrich your log files with Kubernetes metadata. |
131131
| bufferStorage | [BufferStorage](./fluentbit.md#bufferstorage) | | Buffer Storage configures persistent buffer to avoid losing data in case of a failure |
132-
| bufferStorageVolume | [KubernetesStorage](#KubernetesStorage) | nil | Volume definition for the Buffer Storage. If nothing is configured a `hostPath` volume is used with the path `/opt/fluent-bit/<name of the logging CR>/buf`. |
132+
| bufferStorageVolume | [KubernetesStorage](#KubernetesStorage) | nil | Volume definition for the Buffer Storage. If nothing is configured an emptydir volume will be used. |
133133
| customConfigSecret | string | "" | Custom secret to use as fluent-bit config.<br /> It must include all the config files necessary to run fluent-bit (_fluent-bit.conf_, _parsers*.conf_) |
134134

135135
**`logging` with custom fluent-bit annotations**
@@ -214,10 +214,20 @@ Define Kubernetes storage
214214

215215
| Name | Type | Default | Description |
216216
|-----------|------|---------|-------------|
217-
| host_path | [HostPathVolumeSource](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.12/#hostpathvolumesource-v1-core) | - | Represents a host path mapped into a pod. |
217+
| host_path | [HostPathVolumeSource](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.12/#hostpathvolumesource-v1-core) | - | Represents a host path mapped into a pod. If path is empty, it will automatically be set to "/opt/logging-operator/<name of the logging CR>/<name of the volume>" |
218+
| emptyDir | [EmptyDirVolumeSource](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.12/#emptydirvolumesource-v1-core) | - | Represents an empty directory for a pod. |
219+
| pvc | [PersistentVolumeClaim](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.12/#persistentvolumeclaim-v1-core) | - | A PersistentVolumeClaim (PVC) is a request for storage by a user. |
218220

221+
#### Persistent Volume Claim
219222

220-
## outputs, clusteroutputs
223+
| Name | Type | Default | Description |
224+
|-----------|------|---------|-------------|
225+
| spec | [PersistentVolumeClaimSpec](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.12/#persistentvolumeclaimspec-v1-core) | - | Spec defines the desired characteristics of a volume requested by a pod author. |
226+
| source | [PersistentVolumeClaimVolumeSource](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.12/#persistentvolumeclaimvolumesource-v1-core) | - | PersistentVolumeClaimVolumeSource references the user's PVC in the same namespace.  |
227+
228+
The Persistent Volume Claim should be created with the given `spec` and with the `name` defined in the `source`'s `claimName`.
229+
230+
## Outputs, Clusteroutputs
221231

222232
Outputs are the final stage for a `logging flow`. You can define multiple `outputs` and attach them to multiple `flows`.
223233

docs/security/README.md

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,6 @@ helm install --namespace logging --name nginx-demo banzaicloud-stable/nginx-logg
103103
- kind: ServiceAccount
104104
name: nginx-demo-nginx-logging-demo-logging-fluentd
105105
namespace: logging
106-
107-
108106
```
109107

110108
#### Fluentbit ClusterRole & ClusterRoleBinding Output
@@ -212,19 +210,23 @@ helm install --namespace logging --name nginx-demo banzaicloud-stable/nginx-logg
212210

213211
#### Fluentd PSP+Role Output
214212
```
213+
apiVersion: rbac.authorization.k8s.io/v1
215214
kind: Role
216215
metadata:
217216
name: nginx-demo-nginx-logging-demo-logging-fluentd-psp
218-
namespace: logging
219217
rules:
220218
- apiGroups:
221219
- policy
220+
- extensions
222221
resources:
222+
- podsecuritypolicies
223+
resourceNames:
223224
- nginx-demo-nginx-logging-demo-logging-fluentd
224225
verbs:
225226
- use
226227
227228
---
229+
apiVersion: policy/v1beta1
228230
kind: PodSecurityPolicy
229231
metadata:
230232
name: nginx-demo-nginx-logging-demo-logging-fluentd
@@ -251,12 +253,14 @@ spec:
251253
- configMap
252254
- emptyDir
253255
- secret
254-
- persistentVolumeClaim
256+
- hostPath
257+
255258
256259
257260
```
258261
#### Fluentbit PSP+ClusterRole Output
259262
```
263+
apiVersion: rbac.authorization.k8s.io/v1
260264
kind: ClusterRole
261265
metadata:
262266
name: nginx-demo-nginx-logging-demo-logging-fluentbit-psp
@@ -268,6 +272,7 @@ rules:
268272
verbs:
269273
- use
270274
---
275+
apiVersion: policy/v1beta1
271276
kind: PodSecurityPolicy
272277
metadata:
273278
name: nginx-demo-nginx-logging-demo-logging-fluentbit
@@ -323,14 +328,14 @@ spec:
323328
allowPrivilegeEscalation: false
324329
readOnlyRootFilesystem: false
325330
podSecurityContext:
326-
fsGroup: 65533
331+
fsGroup: 101
327332
fluentbit:
328333
security:
329334
securityContext:
330335
allowPrivilegeEscalation: false
331336
readOnlyRootFilesystem: true
332337
podSecurityContext:
333-
fsGroup: 65533
338+
fsGroup: 101
334339
controlNamespace: logging
335340
EOF
336341
```
@@ -339,10 +344,10 @@ EOF
339344
helm install --namespace logging --name nginx-demo banzaicloud-stable/nginx-logging-demo \
340345
--set=loggingOperator.fluentd.security.securityContext.allowPrivilegeEscalation=False \
341346
--set=loggingOperator.fluentd.security.securityContext.readOnlyRootFilesystem=False \
342-
--set=loggingOperator.fluentd.security.podSecurityContext.fsGroup=65533 \
347+
--set=loggingOperator.fluentd.security.podSecurityContext.fsGroup=101 \
343348
--set=loggingOperator.fluentbit.security.securityContext.allowPrivilegeEscalation=False \
344349
--set=loggingOperator.fluentbit.security.securityContext.readOnlyRootFilesystem=True \
345-
--set=loggingOperator.fluentbit.security.podSecurityContext.fsGroup=65533
350+
--set=loggingOperator.fluentbit.security.podSecurityContext.fsGroup=101
346351
```
347352

348353
### Example Manifest Generated by the operator
@@ -364,7 +369,7 @@ spec:
364369
...
365370
schedulerName: default-scheduler
366371
securityContext:
367-
fsGroup: 65533
372+
fsGroup: 101
368373
serviceAccount: nginx-demo-nginx-logging-demo-logging-fluentd
369374
...
370375
```

pkg/resources/fluentbit/daemonset.go

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020

2121
"github.com/banzaicloud/logging-operator/pkg/k8sutil"
2222
"github.com/banzaicloud/logging-operator/pkg/resources/templates"
23-
"github.com/banzaicloud/logging-operator/pkg/sdk/api/v1beta1"
2423
"github.com/banzaicloud/logging-operator/pkg/sdk/util"
2524
appsv1 "k8s.io/api/apps/v1"
2625
corev1 "k8s.io/api/core/v1"
@@ -197,25 +196,7 @@ func (r *Reconciler) generateVolume() (v []corev1.Volume) {
197196
}
198197
v = append(v, tlsRelatedVolume)
199198
}
200-
v = append(v, GetVolumeFromKubernetesStorage(r.Logging.Spec.FluentbitSpec.PositionDB, TailPositionVolume))
201-
v = append(v, GetVolumeFromKubernetesStorage(r.Logging.Spec.FluentbitSpec.BufferStorageVolume, BufferStorageVolume))
199+
v = append(v, r.Logging.Spec.FluentbitSpec.PositionDB.GetVolume(r.Logging.Name, TailPositionVolume))
200+
v = append(v, r.Logging.Spec.FluentbitSpec.BufferStorageVolume.GetVolume(r.Logging.Name, BufferStorageVolume))
202201
return
203202
}
204-
205-
func GetVolumeFromKubernetesStorage(storage *v1beta1.KubernetesStorage, name string) corev1.Volume {
206-
volume := corev1.Volume{
207-
Name: name,
208-
}
209-
if storage != nil {
210-
if storage.HostPath != nil {
211-
volume.VolumeSource = corev1.VolumeSource{
212-
HostPath: storage.HostPath,
213-
}
214-
}
215-
} else {
216-
volume.VolumeSource = corev1.VolumeSource{
217-
EmptyDir: &corev1.EmptyDirVolumeSource{},
218-
}
219-
}
220-
return volume
221-
}

pkg/resources/fluentbit/psp.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,10 @@ func (r *Reconciler) pspClusterRole() (runtime.Object, k8sutil.DesiredState) {
8080
r.Logging.QualifiedName(clusterRoleName+"-psp"), r.Logging.Labels, r.Logging),
8181
Rules: []rbacv1.PolicyRule{
8282
{
83-
APIGroups: []string{"policy"},
84-
Resources: []string{r.Logging.QualifiedName(fluentbitPodSecurityPolicyName)},
85-
Verbs: []string{"use"},
83+
APIGroups: []string{"policy"},
84+
Resources: []string{"podsecuritypolicies"},
85+
ResourceNames: []string{r.Logging.QualifiedName(fluentbitPodSecurityPolicyName)},
86+
Verbs: []string{"use"},
8687
},
8788
},
8889
}, k8sutil.StatePresent
@@ -108,7 +109,7 @@ func (r *Reconciler) pspClusterRoleBinding() (runtime.Object, k8sutil.DesiredSta
108109
Subjects: []rbacv1.Subject{
109110
{
110111
Kind: "ServiceAccount",
111-
Name: r.Logging.QualifiedName(defaultServiceAccountName),
112+
Name: r.getServiceAccount(),
112113
Namespace: r.Logging.Spec.ControlNamespace,
113114
},
114115
},

pkg/resources/fluentd/psp.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ func (r *Reconciler) clusterPodSecurityPolicy() (runtime.Object, k8sutil.Desired
3535
"configMap",
3636
"emptyDir",
3737
"secret",
38+
"hostPath",
3839
"persistentVolumeClaim"},
3940
SELinux: policyv1beta1.SELinuxStrategyOptions{
4041
Rule: policyv1beta1.SELinuxStrategyRunAsAny,
@@ -68,9 +69,10 @@ func (r *Reconciler) pspRole() (runtime.Object, k8sutil.DesiredState) {
6869
ObjectMeta: templates.FluentdObjectMeta(r.Logging.QualifiedName(roleName+"-psp"), r.Logging.Labels, r.Logging),
6970
Rules: []rbacv1.PolicyRule{
7071
{
71-
APIGroups: []string{"policy"},
72-
Resources: []string{r.Logging.QualifiedName(PodSecurityPolicyName)},
73-
Verbs: []string{"use"},
72+
APIGroups: []string{"policy"},
73+
Resources: []string{"podsecuritypolicies"},
74+
ResourceNames: []string{r.Logging.QualifiedName(PodSecurityPolicyName)},
75+
Verbs: []string{"use"},
7476
},
7577
},
7678
}, k8sutil.StatePresent
@@ -95,7 +97,7 @@ func (r *Reconciler) pspRoleBinding() (runtime.Object, k8sutil.DesiredState) {
9597
Subjects: []rbacv1.Subject{
9698
{
9799
Kind: "ServiceAccount",
98-
Name: r.Logging.QualifiedName(defaultServiceAccountName),
100+
Name: r.getServiceAccount(),
99101
Namespace: r.Logging.Spec.ControlNamespace,
100102
},
101103
},

pkg/resources/fluentd/statefulset.go

Lines changed: 25 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,11 @@ func (r *Reconciler) statefulset() (runtime.Object, k8sutil.DesiredState) {
3232
spec.VolumeClaimTemplates = []corev1.PersistentVolumeClaim{
3333
{
3434
ObjectMeta: templates.FluentdObjectMeta(
35-
r.Logging.QualifiedName(bufferVolumeName), util.MergeLabels(r.Logging.Labels, r.getFluentdLabels()), r.Logging),
36-
Spec: r.Logging.Spec.FluentdSpec.FluentdPvcSpec,
35+
r.Logging.Spec.FluentdSpec.BufferStorageVolume.PersistentVolumeClaim.PersistentVolumeSource.ClaimName,
36+
util.MergeLabels(r.Logging.Labels, r.getFluentdLabels()),
37+
r.Logging,
38+
),
39+
Spec: r.Logging.Spec.FluentdSpec.BufferStorageVolume.PersistentVolumeClaim.PersistentVolumeClaimSpec,
3740
Status: corev1.PersistentVolumeClaimStatus{
3841
Phase: corev1.ClaimPending,
3942
},
@@ -48,6 +51,23 @@ func (r *Reconciler) statefulset() (runtime.Object, k8sutil.DesiredState) {
4851
}
4952

5053
func (r *Reconciler) statefulsetSpec() *appsv1.StatefulSetSpec {
54+
initContainers := make([]corev1.Container, 0)
55+
56+
if r.Logging.Spec.FluentdSpec.VolumeMountChmod {
57+
initContainers = append(initContainers, corev1.Container{
58+
Name: "volume-mount-hack",
59+
Image: r.Logging.Spec.FluentdSpec.VolumeModImage.Repository + ":" + r.Logging.Spec.FluentdSpec.VolumeModImage.Tag,
60+
ImagePullPolicy: corev1.PullPolicy(r.Logging.Spec.FluentdSpec.VolumeModImage.PullPolicy),
61+
Command: []string{"sh", "-c", "chmod -R 777 /buffers"},
62+
VolumeMounts: []corev1.VolumeMount{
63+
{
64+
Name: r.Logging.QualifiedName(bufferVolumeName),
65+
MountPath: "/buffers",
66+
},
67+
},
68+
})
69+
}
70+
5171
return &appsv1.StatefulSetSpec{
5272
Replicas: util.IntPointer(cast.ToInt32(r.Logging.Spec.FluentdSpec.Scaling.Replicas)),
5373
Selector: &metav1.LabelSelector{
@@ -58,20 +78,7 @@ func (r *Reconciler) statefulsetSpec() *appsv1.StatefulSetSpec {
5878
Spec: corev1.PodSpec{
5979
Volumes: r.generateVolume(),
6080
ServiceAccountName: r.getServiceAccount(),
61-
InitContainers: []corev1.Container{
62-
{
63-
Name: "volume-mount-hack",
64-
Image: r.Logging.Spec.FluentdSpec.VolumeModImage.Repository + ":" + r.Logging.Spec.FluentdSpec.VolumeModImage.Tag,
65-
ImagePullPolicy: corev1.PullPolicy(r.Logging.Spec.FluentdSpec.VolumeModImage.PullPolicy),
66-
Command: []string{"sh", "-c", "chmod -R 777 /buffers"},
67-
VolumeMounts: []corev1.VolumeMount{
68-
{
69-
Name: r.Logging.QualifiedName(bufferVolumeName),
70-
MountPath: "/buffers",
71-
},
72-
},
73-
},
74-
},
81+
InitContainers: initContainers,
7582
Containers: []corev1.Container{
7683
*r.fluentContainer(),
7784
*newConfigMapReloader(r.Logging.Spec.FluentdSpec.ConfigReloaderImage),
@@ -98,6 +105,7 @@ func (r *Reconciler) fluentContainer() *corev1.Container {
98105
Resources: r.Logging.Spec.FluentdSpec.Resources,
99106
SecurityContext: &corev1.SecurityContext{
100107
RunAsUser: r.Logging.Spec.FluentdSpec.Security.SecurityContext.RunAsUser,
108+
RunAsGroup: r.Logging.Spec.FluentdSpec.Security.SecurityContext.RunAsGroup,
101109
ReadOnlyRootFilesystem: r.Logging.Spec.FluentdSpec.Security.SecurityContext.ReadOnlyRootFilesystem,
102110
AllowPrivilegeEscalation: r.Logging.Spec.FluentdSpec.Security.SecurityContext.AllowPrivilegeEscalation,
103111
Privileged: r.Logging.Spec.FluentdSpec.Security.SecurityContext.Privileged,
@@ -219,26 +227,7 @@ func (r *Reconciler) generateVolume() (v []corev1.Volume) {
219227
},
220228
},
221229
}
222-
if !r.Logging.Spec.FluentdSpec.DisablePvc {
223-
bufferVolume := corev1.Volume{
224-
Name: r.Logging.QualifiedName(bufferVolumeName),
225-
VolumeSource: corev1.VolumeSource{
226-
PersistentVolumeClaim: &corev1.PersistentVolumeClaimVolumeSource{
227-
ClaimName: r.Logging.QualifiedName(bufferVolumeName),
228-
ReadOnly: false,
229-
},
230-
},
231-
}
232-
v = append(v, bufferVolume)
233-
} else {
234-
bufferVolume := corev1.Volume{
235-
Name: r.Logging.QualifiedName(bufferVolumeName),
236-
VolumeSource: corev1.VolumeSource{
237-
EmptyDir: &corev1.EmptyDirVolumeSource{},
238-
},
239-
}
240-
v = append(v, bufferVolume)
241-
}
230+
v = append(v, r.Logging.Spec.FluentdSpec.BufferStorageVolume.GetVolume(r.Logging.Name, r.Logging.QualifiedName(bufferVolumeName)))
242231
if r.Logging.Spec.FluentdSpec.TLS.Enabled {
243232
tlsRelatedVolume := corev1.Volume{
244233
Name: "fluentd-tls",

0 commit comments

Comments
 (0)