Skip to content

Commit 35d0984

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

File tree

14 files changed

+183
-115
lines changed

14 files changed

+183
-115
lines changed

api/operator/v1/vlagent_types.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,17 @@ type VLAgentSpec struct {
8888
type VLAgentK8sCollector struct {
8989
// Enabled switches VLAgent to log collection mode.
9090
// Note, for this purpose operator uses DaemonSet, while by default VLAgent uses StatefulSet.
91-
// It means that switching this option will drop all persisted data.
91+
// Switching this option will drop all persisted data.
9292
Enabled bool `json:"enabled,omitempty"`
9393

9494
// LogsPath configures root for logs path
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 path where logs checkpoints are stored.
99+
// By default it emptyDir is used as a volume for data path.
100+
// To guarantee checkpoints persistence during pods recreation consider explicitly setting this option to destination, where hostPath volume is mounted.
101+
DataPath string `json:"dataPath,omitempty"`
100102

101103
// TenantID defines default tenant ID to use for logs collected from pods in format: <accountID>:<projectID>
102104
TenantID string `json:"tenantID,omitempty"`
@@ -334,7 +336,7 @@ func (cr *VLAgent) FinalAnnotations() map[string]string {
334336
}
335337

336338
// AsCRDOwner implements interface
337-
func (*VLAgent) AsCRDOwner() []metav1.OwnerReference {
339+
func (*VLAgent) AsCRDOwner() *metav1.OwnerReference {
338340
return vmv1beta1.GetCRDAsOwner(vmv1beta1.VLAgentCRD)
339341
}
340342

api/operator/v1beta1/owner.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ const (
1818
)
1919

2020
func (c CRDName) String() string {
21-
return []string{"vmagents.operator.victoriametrics.com", "vmalerts.operator.victoriametrics.com", "vmsingles.operator.victoriametrics.com", "vmclusters.operator.victoriametrics.com", "vmauths.operator.victoriametrics.com", "vmalertmanagers.operator.victoriametrics.com"}[c]
21+
return []string{
22+
"vmagents.operator.victoriametrics.com",
23+
"vlagents.operator.victoriametrics.com",
24+
}[c]
2225
}
2326

2427
type crdInfo struct {
@@ -57,17 +60,15 @@ func Init(ctx context.Context, rclient client.Client) error {
5760

5861
// GetCRDAsOwner returns owner references with global CustomResourceDefinition object as owner
5962
// useful for non-namespaced objects, like clusterRole
60-
func GetCRDAsOwner(name CRDName) []metav1.OwnerReference {
63+
func GetCRDAsOwner(name CRDName) *metav1.OwnerReference {
6164
crdData := crdCache[name]
6265
if crdData == nil {
6366
return nil
6467
}
65-
return []metav1.OwnerReference{
66-
{
67-
Name: name.String(),
68-
UID: crdData.uuid,
69-
Kind: "CustomResourceDefinition",
70-
APIVersion: crdData.apiVersion,
71-
},
68+
return &metav1.OwnerReference{
69+
Name: name.String(),
70+
UID: crdData.uuid,
71+
Kind: "CustomResourceDefinition",
72+
APIVersion: crdData.apiVersion,
7273
}
7374
}

api/operator/v1beta1/vmagent_types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -787,7 +787,7 @@ func (cr *VMAgent) AsURL() string {
787787
}
788788

789789
// AsCRDOwner implements interface
790-
func (*VMAgent) AsCRDOwner() []metav1.OwnerReference {
790+
func (*VMAgent) AsCRDOwner() *metav1.OwnerReference {
791791
return GetCRDAsOwner(VMAgentCRD)
792792
}
793793

config/crd/overlay/crd.yaml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -740,9 +740,11 @@ spec:
740740
description: K8sCollector configures VLAgent logs collection from
741741
K8s pods
742742
properties:
743-
checkpointsPath:
744-
description: CheckpointsPath configures path where logs checkpoints
745-
are stored
743+
dataPath:
744+
description: |-
745+
DataPath configures path where logs checkpoints are stored.
746+
By default it emptyDir is used as a volume for data path.
747+
To guarantee checkpoints persistence during pods recreation consider explicitly setting this option to destination, where hostPath volume is mounted.
746748
type: string
747749
decolorizeFields:
748750
description: DecolorizeFields defines fields to remove ANSI color
@@ -754,7 +756,7 @@ spec:
754756
description: |-
755757
Enabled switches VLAgent to log collection mode.
756758
Note, for this purpose operator uses DaemonSet, while by default VLAgent uses StatefulSet.
757-
It means that switching this option will drop all persisted data.
759+
Switching this option will drop all persisted data.
758760
type: boolean
759761
extraFields:
760762
description: ExtraFields defines extra fields to add to each collected

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
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
apiVersion: operator.victoriametrics.com/v1
2+
kind: VLAgent
3+
metadata:
4+
name: example
5+
spec:
6+
resources:
7+
requests:
8+
cpu: "50m"
9+
memory: "350Mi"
10+
limits:
11+
cpu: "500m"
12+
memory: "850Mi"
13+
k8sCollector:
14+
enabled: true
15+
dataPath: /var/lib/vl-collector
16+
volumes:
17+
- name: data
18+
hostPath:
19+
path: /var/lib/vl-collector
20+
volumeMounts:
21+
- name: data
22+
mountPath: /var/lib/vl-collector
23+
remoteWrite:
24+
- url: "http://vlsingle-example-0.default.svc:9428/internal/insert"

docs/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ aliases:
1414
## tip
1515

1616
* FEATURE: [vmagent](https://docs.victoriametrics.com/operator/resources/vmagent/): support `namespace` parameter in `attach_metadata` section for all scrape configurations. See [#1654](https://github.com/VictoriaMetrics/operator/issues/1654).
17-
* FEATURE: [vlagen](https://docs.victoriametrics.com/operator/resources/vlagent): support logs collection. See [#1501](https://github.com/VictoriaMetrics/operator/issues/1501).
17+
* FEATURE: [vlagent](https://docs.victoriametrics.com/operator/resources/vlagent): support logs collection. See [#1501](https://github.com/VictoriaMetrics/operator/issues/1501).
1818

1919
## [v0.66.1](https://github.com/VictoriaMetrics/operator/releases/tag/v0.66.1)
2020

docs/api.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,9 @@ 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+
| dataPath<a href="#vlagentk8scollector-datapath" id="vlagentk8scollector-datapath">#</a><br/>_string_ | _(Required)_<br/>DataPath configures path where logs checkpoints are stored.<br />By default it emptyDir is used as a volume for data path.<br />To guarantee checkpoints persistence during pods recreation consider explicitly setting this option to destination, where hostPath volume is mounted. |
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 |
185-
| 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. |
185+
| 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 />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 |
187187
| ignoreFields<a href="#vlagentk8scollector-ignorefields" id="vlagentk8scollector-ignorefields">#</a><br/>_string array_ | _(Required)_<br/>IgnoreFields defines fields to ignore across logs ingested from Kubernetes |
188188
| logsPath<a href="#vlagentk8scollector-logspath" id="vlagentk8scollector-logspath">#</a><br/>_string_ | _(Required)_<br/>LogsPath configures root for logs path<br />By default VLAgent collects logs from /var/log/containers |

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: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,24 @@ import (
1616
)
1717

1818
var (
19-
policyRules = []rbacv1.PolicyRule{
20-
{
21-
APIGroups: []string{""},
22-
Verbs: []string{
23-
"get",
24-
"list",
25-
"watch",
26-
},
27-
Resources: []string{
28-
"pods",
29-
"namespaces",
30-
"nodes",
31-
},
19+
policyRules = []rbacv1.PolicyRule{{
20+
APIGroups: []string{""},
21+
Verbs: []string{
22+
"get",
23+
"list",
24+
"watch",
3225
},
33-
}
26+
Resources: []string{
27+
"pods",
28+
"namespaces",
29+
"nodes",
30+
},
31+
}}
3432
)
3533

3634
// createK8sAPIAccess - creates RBAC access rules for vlagent
3735
func createK8sAPIAccess(ctx context.Context, rclient client.Client, cr, prevCR *vmv1.VLAgent) error {
38-
if config.IsClusterWideAccessAllowed() {
36+
if !config.IsClusterWideAccessAllowed() {
3937
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))
4038
return nil
4139
}
@@ -65,16 +63,13 @@ func ensureCRBExist(ctx context.Context, rclient client.Client, cr, prevCR *vmv1
6563
}
6664

6765
func buildCRB(cr *vmv1.VLAgent) *rbacv1.ClusterRoleBinding {
68-
return &rbacv1.ClusterRoleBinding{
66+
r := &rbacv1.ClusterRoleBinding{
6967
ObjectMeta: metav1.ObjectMeta{
7068
Name: cr.GetClusterRoleName(),
7169
Namespace: cr.GetNamespace(),
7270
Labels: cr.FinalLabels(),
7371
Annotations: cr.FinalAnnotations(),
7472
Finalizers: []string{vmv1beta1.FinalizerName},
75-
// Kubernetes does not allow namespace-scoped resources to own cluster-scoped resources,
76-
// use crd instead
77-
OwnerReferences: cr.AsCRDOwner(),
7873
},
7974
Subjects: []rbacv1.Subject{
8075
{
@@ -89,20 +84,31 @@ func buildCRB(cr *vmv1.VLAgent) *rbacv1.ClusterRoleBinding {
8984
Kind: "ClusterRole",
9085
},
9186
}
87+
owner := cr.AsCRDOwner()
88+
if owner != nil {
89+
// Kubernetes does not allow namespace-scoped resources to own cluster-scoped resources,
90+
// use crd instead
91+
r.OwnerReferences = []metav1.OwnerReference{*owner}
92+
}
93+
return r
9294
}
9395

9496
func buildCR(cr *vmv1.VLAgent) *rbacv1.ClusterRole {
95-
return &rbacv1.ClusterRole{
97+
r := &rbacv1.ClusterRole{
9698
ObjectMeta: metav1.ObjectMeta{
9799
Name: cr.GetClusterRoleName(),
98100
Namespace: cr.GetNamespace(),
99101
Labels: cr.FinalLabels(),
100102
Annotations: cr.FinalAnnotations(),
101103
Finalizers: []string{vmv1beta1.FinalizerName},
102-
// Kubernetes does not allow namespace-scoped resources to own cluster-scoped resources,
103-
// use crd instead
104-
OwnerReferences: cr.AsCRDOwner(),
105104
},
106105
Rules: policyRules,
107106
}
107+
owner := cr.AsCRDOwner()
108+
if owner != nil {
109+
// Kubernetes does not allow namespace-scoped resources to own cluster-scoped resources,
110+
// use crd instead
111+
r.OwnerReferences = []metav1.OwnerReference{*owner}
112+
}
113+
return r
108114
}

0 commit comments

Comments
 (0)