/
agent_pipeline_webhook.go
153 lines (137 loc) · 4.63 KB
/
agent_pipeline_webhook.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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package v1beta
import (
"fmt"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/validation"
"k8s.io/apimachinery/pkg/util/validation/field"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/webhook"
)
func (r *VectorAgentPipeline) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(r).
Complete()
}
// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
//+kubebuilder:webhook:path=/mutate-logging-vlo-io-v1beta-vectoragentpipeline,mutating=true,failurePolicy=fail,sideEffects=None,groups=logging.vlo.io,resources=vectoragentpipelines,verbs=create;update,versions={v1,v1beta},name=mvectoragentpipeline.kb.io,admissionReviewVersions={v1,v1beta}
var _ webhook.Defaulter = &VectorAgentPipeline{}
func (r *VectorAgentPipeline) Default() {
log.Info("default", "name", r.Name)
for id, sinks := range r.Spec.Sinks {
if sinks.S3.Name == "" {
sinks.S3.Name = fmt.Sprint("s3_", id)
}
if sinks.S3.Compression == "" {
sinks.S3.Compression = "gzip"
}
if sinks.S3.ContentType == "" {
sinks.S3.ContentType = "text/x-log"
}
if sinks.S3.KeyPrefix == "" {
sinks.S3.KeyPrefix = "date=%F/"
}
if sinks.S3.Encoding == "" {
sinks.S3.Encoding = "text"
}
if sinks.Console.Name == "" {
sinks.Console.Name = fmt.Sprint("console_", id)
}
if sinks.Console.Encoding == "" {
sinks.Console.Encoding = "text"
}
if sinks.Console.Target == "" {
sinks.Console.Target = "stdout"
}
if sinks.File.Name == "" {
sinks.File.Name = fmt.Sprint("file_", id)
}
if sinks.File.Compression == "" {
sinks.File.Compression = "none"
}
if sinks.File.Encoding == "" {
sinks.File.Encoding = "text"
}
if sinks.Elasticsearch.Name == "" {
sinks.Elasticsearch.Name = fmt.Sprint("elasticsearch_", id)
}
if sinks.Elasticsearch.Compression == "" {
sinks.Elasticsearch.Compression = "none"
}
if sinks.Elasticsearch.Mode == "" {
sinks.Elasticsearch.Mode = "bulk"
}
if sinks.HTTP.Encoding == "" {
sinks.HTTP.Encoding = "text"
}
if sinks.HTTP.Name == "" {
sinks.HTTP.Name = fmt.Sprint("http_", id)
}
if sinks.HTTP.Compression == "" {
sinks.HTTP.Compression = "none"
}
if sinks.HTTP.Method == "" {
sinks.HTTP.Method = "POST"
}
if sinks.Kafka.Name == "" {
sinks.Kafka.Name = fmt.Sprint("kafka_", id)
}
if sinks.Kafka.Compression == "" {
sinks.Kafka.Compression = "none"
}
if sinks.Loki.Name == "" {
sinks.Loki.Name = fmt.Sprint("loki_", id)
}
if sinks.Loki.Compression == "" {
sinks.Loki.Compression = "snappy"
}
if sinks.Loki.Encoding == "" {
sinks.Loki.Encoding = "text"
}
if sinks.Vector.Name == "" {
sinks.Vector.Name = fmt.Sprint("vector_", id)
}
if sinks.Vector.Compression == "" {
sinks.Vector.Compression = "false"
}
}
}
// TODO(user): change verbs to "verbs=create;update;delete" if you want to enable deletion validation.
//+kubebuilder:webhook:path=/validate-logging-vlo-io-v1beta-vectoragentpipeline,mutating=false,failurePolicy=fail,sideEffects=None,groups=logging.vlo.io,resources=vectoragentpipelines,verbs=create;update,versions={v1,v1beta},name=vvectoragentpipeline.kb.io,admissionReviewVersions={v1,v1beta}
var _ webhook.Validator = &VectorAgentPipeline{}
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
func (r *VectorAgentPipeline) ValidateCreate() error {
log.Info("validate create", "name", r.Name)
return r.validateVectorAgent()
}
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
func (r *VectorAgentPipeline) ValidateUpdate(old runtime.Object) error {
log.Info("validate update", "name", r.Name)
return r.validateVectorAgent()
}
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
func (r *VectorAgentPipeline) ValidateDelete() error {
log.Info("validate delete", "name", r.Name)
// TODO(user): fill in your validation logic upon object deletion.
return nil
}
func (r *VectorAgentPipeline) validateVectorAgent() error {
var allErrs field.ErrorList
if err := r.validateVectorAgentName(); err != nil {
allErrs = append(allErrs, err)
}
//todo: validation Spec
if len(allErrs) == 0 {
return nil
}
return apierrors.NewInvalid(
schema.GroupKind{Group: "vlo.io", Kind: "VectorAgent"},
r.Name, allErrs)
}
func (r *VectorAgentPipeline) validateVectorAgentName() *field.Error {
if len(r.ObjectMeta.Name) > validation.DNS1123LabelMaxLength {
return field.Invalid(field.NewPath("metadata").Child("name"), r.Name, "must be no more than 63 characters")
}
return nil
}