Skip to content

Commit 133e89d

Browse files
added dataPath and logsPath, store pq data and checkpoints at dataPath by default
1 parent d8757fe commit 133e89d

File tree

9 files changed

+108
-64
lines changed

9 files changed

+108
-64
lines changed

api/operator/v1/vlagent_types.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,9 @@ type VLAgentK8sCollector struct {
9595
// By default VLAgent collects logs from /var/log/containers
9696
LogsPath string `json:"logsPath,omitempty"`
9797

98-
// CheckpointsPath configures path where logs checkpoints are stored
99-
CheckpointsPath string `json:"checkpointsPath,omitempty"`
98+
// DataPath configures host path where logs checkpoints are stored.
99+
// By default it also contains persistent queue
100+
DataPath string `json:"checkpointsPath,omitempty"`
100101

101102
// TenantID defines default tenant ID to use for logs collected from pods in format: <accountID>:<projectID>
102103
TenantID string `json:"tenantID,omitempty"`

config/base-with-webhook/kustomization.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,9 @@ replacements:
8080
group: cert-manager.io
8181
kind: Certificate
8282
version: v1
83+
apiVersion: kustomize.config.k8s.io/v1beta1
84+
kind: Kustomization
85+
images:
86+
- name: manager
87+
newName: localhost:5001/victoriametrics/operator
88+
newTag: heads-vlagent-logs-collection-0-gd8757fec-dirty-60c84815

config/crd/overlay/crd.yaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -741,8 +741,9 @@ spec:
741741
K8s pods
742742
properties:
743743
checkpointsPath:
744-
description: CheckpointsPath configures path where logs checkpoints
745-
are stored
744+
description: |-
745+
DataPath configures host path where logs checkpoints are stored.
746+
By default it also contains persistent queue
746747
type: string
747748
decolorizeFields:
748749
description: DecolorizeFields defines fields to remove ANSI color

config/examples/vlagent-collector.yaml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,14 @@ kind: VLAgent
33
metadata:
44
name: example
55
spec:
6-
replicaCount: 2
76
resources:
87
requests:
98
cpu: "50m"
109
memory: "350Mi"
1110
limits:
1211
cpu: "500m"
1312
memory: "850Mi"
14-
persistentVolumeClaimRetentionPolicy:
15-
whenDeleted: Delete
1613
k8sCollector:
1714
enabled: true
1815
remoteWrite:
1916
- url: "http://vlsingle-example-0.default.svc:9428/internal/insert"
20-
maxDiskUsage: 10GB
21-
remoteWriteSettings:
22-
# ~ 5GB in bytes
23-
maxBlockSize: 30MB

docs/api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ Appears in: [VLAgentSpec](#vlagentspec)
180180

181181
| Field | Description |
182182
| --- | --- |
183-
| checkpointsPath<a href="#vlagentk8scollector-checkpointspath" id="vlagentk8scollector-checkpointspath">#</a><br/>_string_ | _(Required)_<br/>CheckpointsPath configures path where logs checkpoints are stored |
183+
| checkpointsPath<a href="#vlagentk8scollector-checkpointspath" id="vlagentk8scollector-checkpointspath">#</a><br/>_string_ | _(Required)_<br/>DataPath configures host path where logs checkpoints are stored.<br />By default it also contains persistent queue |
184184
| decolorizeFields<a href="#vlagentk8scollector-decolorizefields" id="vlagentk8scollector-decolorizefields">#</a><br/>_string array_ | _(Required)_<br/>DecolorizeFields defines fields to remove ANSI color codes across logs ingested from Kubernetes |
185185
| enabled<a href="#vlagentk8scollector-enabled" id="vlagentk8scollector-enabled">#</a><br/>_boolean_ | _(Required)_<br/>Enabled switches VLAgent to log collection mode.<br />Note, for this purpose operator uses DaemonSet, while by default VLAgent uses StatefulSet.<br />It means that switching this option will drop all persisted data. |
186186
| extraFields<a href="#vlagentk8scollector-extrafields" id="vlagentk8scollector-extrafields">#</a><br/>_string_ | _(Required)_<br/>ExtraFields defines extra fields to add to each collected log line |

internal/controller/operator/factory/finalize/vlagent.go

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,24 @@ import (
55

66
appsv1 "k8s.io/api/apps/v1"
77
corev1 "k8s.io/api/core/v1"
8+
rbacv1 "k8s.io/api/rbac/v1"
9+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
810
"sigs.k8s.io/controller-runtime/pkg/client"
911

1012
vmv1 "github.com/VictoriaMetrics/operator/api/operator/v1"
13+
"github.com/VictoriaMetrics/operator/internal/config"
1114
)
1215

1316
// OnVLAgentDelete deletes all vlagent related resources
1417
func OnVLAgentDelete(ctx context.Context, rclient client.Client, cr *vmv1.VLAgent) error {
15-
if err := removeFinalizeObjByName(ctx, rclient, &appsv1.StatefulSet{}, cr.PrefixedName(), cr.Namespace); err != nil {
16-
return err
18+
if cr.Spec.K8sCollector.Enabled {
19+
if err := removeFinalizeObjByName(ctx, rclient, &appsv1.DaemonSet{}, cr.PrefixedName(), cr.Namespace); err != nil {
20+
return err
21+
}
22+
} else {
23+
if err := removeFinalizeObjByName(ctx, rclient, &appsv1.StatefulSet{}, cr.PrefixedName(), cr.Namespace); err != nil {
24+
return err
25+
}
1726
}
1827
// check service
1928
if err := removeFinalizeObjByName(ctx, rclient, &corev1.Service{}, cr.PrefixedName(), cr.Namespace); err != nil {
@@ -39,5 +48,20 @@ func OnVLAgentDelete(ctx context.Context, rclient client.Client, cr *vmv1.VLAgen
3948
if err := deleteSA(ctx, rclient, cr); err != nil {
4049
return err
4150
}
51+
if config.IsClusterWideAccessAllowed() {
52+
if err := removeFinalizeObjByName(ctx, rclient, &rbacv1.ClusterRoleBinding{}, cr.GetClusterRoleName(), cr.GetNamespace()); err != nil {
53+
return err
54+
}
55+
if err := removeFinalizeObjByName(ctx, rclient, &rbacv1.ClusterRole{}, cr.GetClusterRoleName(), cr.GetNamespace()); err != nil {
56+
return err
57+
}
58+
if err := SafeDelete(ctx, rclient, &rbacv1.ClusterRoleBinding{ObjectMeta: metav1.ObjectMeta{Name: cr.GetClusterRoleName(), Namespace: cr.GetNamespace()}}); err != nil {
59+
return err
60+
}
61+
62+
if err := SafeDelete(ctx, rclient, &rbacv1.ClusterRole{ObjectMeta: metav1.ObjectMeta{Name: cr.GetClusterRoleName(), Namespace: cr.GetNamespace()}}); err != nil {
63+
return err
64+
}
65+
}
4266
return nil
4367
}

internal/controller/operator/factory/vlagent/rbac.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ var (
3535

3636
// createK8sAPIAccess - creates RBAC access rules for vlagent
3737
func createK8sAPIAccess(ctx context.Context, rclient client.Client, cr, prevCR *vmv1.VLAgent) error {
38-
if config.IsClusterWideAccessAllowed() {
38+
if !config.IsClusterWideAccessAllowed() {
3939
logger.WithContext(ctx).Info(fmt.Sprintf("skipping cluster role and binding for vlagent=%s/%s since operator has WATCH_NAMESPACE set", cr.Namespace, cr.Name))
4040
return nil
4141
}

internal/controller/operator/factory/vlagent/vlagent.go

Lines changed: 61 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ const (
3131
persistentQueueSTSDir = "/vlagent_pq/vlagent-remotewrite-data"
3232
persistentQueueMountName = "persistent-queue-data"
3333

34+
defaultLogsPath = "/var/log/containers"
35+
defaultDataPath = "/var/lib/vlagent"
36+
dataVolumeName = "data"
37+
3438
remoteWriteAssetsMounthPath = "/etc/vl/remote-write-assets"
3539
tlsServerConfigMountPath = "/etc/vl/tls-server-secrets"
3640
)
@@ -78,9 +82,6 @@ func CreateOrUpdate(ctx context.Context, cr *vmv1.VLAgent, rclient client.Client
7882
if cr.ParsedLastAppliedSpec != nil {
7983
prevCR = cr.DeepCopy()
8084
prevCR.Spec = *cr.ParsedLastAppliedSpec
81-
if err := deleteOrphaned(ctx, rclient, cr); err != nil {
82-
return fmt.Errorf("cannot delete objects from prev state: %w", err)
83-
}
8485
}
8586
if cr.IsOwnsServiceAccount() {
8687
var prevSA *corev1.ServiceAccount
@@ -293,54 +294,70 @@ func newPodSpec(cr *vmv1.VLAgent) (*corev1.PodSpec, error) {
293294
if len(cr.Spec.K8sCollector.ExtraFields) > 0 {
294295
args = append(args, fmt.Sprintf("-kubernetesCollector.extraField=%q", cr.Spec.K8sCollector.ExtraFields))
295296
}
296-
if len(cr.Spec.K8sCollector.CheckpointsPath) > 0 {
297-
args = append(args, fmt.Sprintf("-kubernetesCollector.checkpointsPath=%q", cr.Spec.K8sCollector.CheckpointsPath))
298-
}
299-
if len(cr.Spec.K8sCollector.LogsPath) > 0 {
300-
args = append(args, fmt.Sprintf("-kubernetesCollector.logsPath=%q", cr.Spec.K8sCollector.LogsPath))
301-
}
302-
volumes = append(volumes, corev1.Volume{
303-
Name: "varlog",
304-
VolumeSource: corev1.VolumeSource{
305-
HostPath: &corev1.HostPathVolumeSource{
306-
Path: "/var/log",
307-
},
308-
},
309-
}, corev1.Volume{
310-
Name: "varlib",
311-
VolumeSource: corev1.VolumeSource{
312-
HostPath: &corev1.HostPathVolumeSource{
313-
Path: "/var/lib",
297+
298+
if len(cr.Spec.K8sCollector.LogsPath) == 0 || cr.Spec.K8sCollector.LogsPath == defaultLogsPath {
299+
logVolumeName := "varlog"
300+
logVolumePath := "/var/log"
301+
libVolumeName := "varlib"
302+
libVolumePath := "/var/lib"
303+
volumes = append(volumes, corev1.Volume{
304+
Name: logVolumeName,
305+
VolumeSource: corev1.VolumeSource{
306+
HostPath: &corev1.HostPathVolumeSource{
307+
Path: logVolumePath,
308+
},
314309
},
315-
},
316-
}, corev1.Volume{
317-
Name: "vl-collector-data",
318-
VolumeSource: corev1.VolumeSource{
319-
HostPath: &corev1.HostPathVolumeSource{
320-
Path: "/var/lib/vl-collector",
310+
}, corev1.Volume{
311+
Name: libVolumeName,
312+
VolumeSource: corev1.VolumeSource{
313+
HostPath: &corev1.HostPathVolumeSource{
314+
Path: libVolumePath,
315+
},
321316
},
322-
},
323-
})
324-
if cr.Spec.RemoteWriteSettings == nil || cr.Spec.RemoteWriteSettings.TmpDataPath == nil {
317+
})
318+
agentVolumeMounts = append(agentVolumeMounts, corev1.VolumeMount{
319+
Name: logVolumeName,
320+
MountPath: logVolumePath,
321+
ReadOnly: true,
322+
}, corev1.VolumeMount{
323+
Name: libVolumeName,
324+
MountPath: libVolumePath,
325+
ReadOnly: true,
326+
})
327+
} else {
328+
args = append(args, fmt.Sprintf("-kubernetesCollector.logsPath=%q", cr.Spec.K8sCollector.LogsPath))
329+
}
330+
dataPath := defaultDataPath
331+
var pqSrc corev1.VolumeSource
332+
if len(cr.Spec.K8sCollector.DataPath) == 0 || cr.Spec.K8sCollector.DataPath == defaultDataPath {
325333
volumes = append(volumes, corev1.Volume{
326-
Name: persistentQueueMountName,
334+
Name: dataVolumeName,
327335
VolumeSource: corev1.VolumeSource{
328-
EmptyDir: &corev1.EmptyDirVolumeSource{},
336+
HostPath: &corev1.HostPathVolumeSource{
337+
Path: dataPath,
338+
},
329339
},
330340
})
341+
agentVolumeMounts = append(agentVolumeMounts, corev1.VolumeMount{
342+
Name: dataVolumeName,
343+
MountPath: dataPath,
344+
})
345+
pqSrc.HostPath = &corev1.HostPathVolumeSource{
346+
Path: path.Join(dataPath, persistentQueueSTSDir),
347+
}
348+
} else {
349+
dataPath = cr.Spec.K8sCollector.DataPath
350+
pqSrc.EmptyDir = &corev1.EmptyDirVolumeSource{}
351+
}
352+
checkpointsPath := path.Join(dataPath, "checkpoints.json")
353+
args = append(args, fmt.Sprintf("-kubernetesCollector.checkpointsPath=%q", checkpointsPath))
354+
355+
if cr.Spec.RemoteWriteSettings == nil || cr.Spec.RemoteWriteSettings.TmpDataPath == nil {
356+
volumes = append(volumes, corev1.Volume{
357+
Name: persistentQueueMountName,
358+
VolumeSource: pqSrc,
359+
})
331360
}
332-
agentVolumeMounts = append(agentVolumeMounts, corev1.VolumeMount{
333-
Name: "varlog",
334-
MountPath: "/var/log",
335-
ReadOnly: true,
336-
}, corev1.VolumeMount{
337-
Name: "varlib",
338-
MountPath: "/var/lib",
339-
ReadOnly: true,
340-
}, corev1.VolumeMount{
341-
Name: "vl-collector-data",
342-
MountPath: "/vl-collector",
343-
})
344361
}
345362

346363
var envs []corev1.EnvVar

internal/controller/operator/factory/vlagent/vlagent_test.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,6 +1081,7 @@ containers:
10811081
args:
10821082
- -httpListenAddr=:9425
10831083
- -kubernetesCollector
1084+
- -kubernetesCollector.checkpointsPath="/var/lib/vlagent/checkpoints.json"
10841085
- -kubernetesCollector.msgField="msg,message"
10851086
- -remoteWrite.maxDiskUsagePerURL=10GB,10GB,
10861087
- -remoteWrite.tmpDataPath=/vlagent_pq/vlagent-remotewrite-data
@@ -1100,8 +1101,8 @@ containers:
11001101
- name: varlib
11011102
readonly: true
11021103
mountpath: /var/lib
1103-
- name: vl-collector-data
1104-
mountpath: /vl-collector
1104+
- name: data
1105+
mountpath: /var/lib/vlagent
11051106
- name: persistent-queue-data
11061107
readonly: false
11071108
mountpath: /vlagent_pq/vlagent-remotewrite-data
@@ -1140,13 +1141,14 @@ volumes:
11401141
volumesource:
11411142
hostpath:
11421143
path: /var/lib
1143-
- name: vl-collector-data
1144+
- name: data
11441145
volumesource:
11451146
hostpath:
1146-
path: /var/lib/vl-collector
1147+
path: /var/lib/vlagent
11471148
- name: persistent-queue-data
11481149
volumesource:
1149-
emptydir: {}
1150+
hostpath:
1151+
path: /var/lib/vlagent/vlagent_pq/vlagent-remotewrite-data
11501152
11511153
`)
11521154

0 commit comments

Comments
 (0)