/
vmuser_types.go
296 lines (264 loc) · 10.6 KB
/
vmuser_types.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
package v1beta1
import (
"fmt"
"strings"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/pointer"
"sigs.k8s.io/controller-runtime/pkg/client"
)
// VMUserSpec defines the desired state of VMUser
type VMUserSpec struct {
// Name of the VMUser object.
// +optional
Name *string `json:"name,omitempty"`
// UserName basic auth user name for accessing protected endpoint,
// will be replaced with metadata.name of VMUser if omitted.
// +optional
UserName *string `json:"username,omitempty"`
// Password basic auth password for accessing protected endpoint.
// +optional
Password *string `json:"password,omitempty"`
// PasswordRef allows fetching password from user-create secret by its name and key.
// +optional
PasswordRef *v1.SecretKeySelector `json:"passwordRef,omitempty"`
// TokenRef allows fetching token from user-created secrets by its name and key.
// +optional
TokenRef *v1.SecretKeySelector `json:"tokenRef,omitempty"`
// GeneratePassword instructs operator to generate password for user
// if spec.password if empty.
// +optional
GeneratePassword bool `json:"generatePassword,omitempty"`
// BearerToken Authorization header value for accessing protected endpoint.
// +optional
BearerToken *string `json:"bearerToken,omitempty"`
// TargetRefs - reference to endpoints, which user may access.
TargetRefs []TargetRef `json:"targetRefs"`
// DefaultURLs backend url for non-matching paths filter
// usually used for default backend with error message
// +optional
DefaultURLs []string `json:"default_url,omitempty"`
// IPFilters defines per target src ip filters
// supported only with enterprise version of vmauth
// https://docs.victoriametrics.com/vmauth.html#ip-filters
// +optional
IPFilters VMUserIPFilters `json:"ip_filters,omitempty"`
// Headers represent additional http headers, that vmauth uses
// in form of ["header_key: header_value"]
// multiple values for header key:
// ["header_key: value1,value2"]
// it's available since 1.68.0 version of vmauth
// +optional
Headers []string `json:"headers,omitempty"`
// ResponseHeaders represent additional http headers, that vmauth adds for request response
// in form of ["header_key: header_value"]
// multiple values for header key:
// ["header_key: value1,value2"]
// it's available since 1.93.0 version of vmauth
// +optional
ResponseHeaders []string `json:"response_headers,omitempty"`
// RetryStatusCodes defines http status codes in numeric format for request retries
// e.g. [429,503]
// +optional
RetryStatusCodes []int `json:"retry_status_codes,omitempty"`
// MaxConcurrentRequests defines max concurrent requests per user
// 300 is default value for vmauth
// +optional
MaxConcurrentRequests *int `json:"max_concurrent_requests,omitempty"`
// LoadBalancingPolicy defines load balancing policy to use for backend urls.
// Supported policies: least_loaded, first_available.
// See https://docs.victoriametrics.com/vmauth.html#load-balancing for more details (default "least_loaded")
// +optional
// +kubebuilder:validation:Enum=least_loaded;first_available
LoadBalancingPolicy *string `json:"load_balancing_policy,omitempty"`
// DropSrcPathPrefixParts is the number of `/`-delimited request path prefix parts to drop before proxying the request to backend.
// See https://docs.victoriametrics.com/vmauth.html#dropping-request-path-prefix for more details.
// +optional
DropSrcPathPrefixParts *int `json:"drop_src_path_prefix_parts,omitempty"`
// TLSInsecureSkipVerify - whether to skip TLS verification when connecting to backend over HTTPS.
// See https://docs.victoriametrics.com/vmauth.html#backend-tls-setup
// +optional
TLSInsecureSkipVerify bool `json:"tls_insecure_skip_verify,omitempty"`
// MetricLabels - additional labels for metrics exported by vmauth for given user.
// +optional
MetricLabels map[string]string `json:"metric_labels,omitempty"`
// DisableSecretCreation skips related secret creation for vmuser
DisableSecretCreation bool `json:"disable_secret_creation,omitempty"`
}
// TargetRef describes target for user traffic forwarding.
// one of target types can be chosen:
// crd or static per targetRef.
// user can define multiple targetRefs with different ref Types.
type TargetRef struct {
// CRD describes exist operator's CRD object,
// operator generates access url based on CRD params.
// +optional
CRD *CRDRef `json:"crd,omitempty"`
// Static - user defined url for traffic forward,
// for instance http://vmsingle:8429
// +optional
Static *StaticRef `json:"static,omitempty"`
// Paths - matched path to route.
// +optional
Paths []string `json:"paths,omitempty"`
Hosts []string `json:"hosts,omitempty"`
// QueryParams []string `json:"queryParams,omitempty"`
// TargetPathSuffix allows to add some suffix to the target path
// It allows to hide tenant configuration from user with crd as ref.
// it also may contain any url encoded params.
// +optional
TargetPathSuffix string `json:"target_path_suffix,omitempty"`
// Headers represent additional http headers, that vmauth uses
// in form of ["header_key: header_value"]
// multiple values for header key:
// ["header_key: value1,value2"]
// it's available since 1.68.0 version of vmauth
// +optional
Headers []string `json:"headers,omitempty"`
// ResponseHeaders represent additional http headers, that vmauth adds for request response
// in form of ["header_key: header_value"]
// multiple values for header key:
// ["header_key: value1,value2"]
// it's available since 1.93.0 version of vmauth
// +optional
ResponseHeaders []string `json:"response_headers,omitempty"`
// RetryStatusCodes defines http status codes in numeric format for request retries
// Can be defined per target or at VMUser.spec level
// e.g. [429,503]
// +optional
RetryStatusCodes []int `json:"retry_status_codes,omitempty"`
// LoadBalancingPolicy defines load balancing policy to use for backend urls.
// Supported policies: least_loaded, first_available.
// See https://docs.victoriametrics.com/vmauth.html#load-balancing for more details (default "least_loaded")
// +optional
// +kubebuilder:validation:Enum=least_loaded;first_available
LoadBalancingPolicy *string `json:"load_balancing_policy,omitempty"`
// DropSrcPathPrefixParts is the number of `/`-delimited request path prefix parts to drop before proxying the request to backend.
// See https://docs.victoriametrics.com/vmauth.html#dropping-request-path-prefix for more details.
// +optional
DropSrcPathPrefixParts *int `json:"drop_src_path_prefix_parts,omitempty"`
// TargetRefBasicAuth allow an target endpoint to authenticate over basic authentication
// +optional
TargetRefBasicAuth *TargetRefBasicAuth `json:"targetRefBasicAuth,omitempty"`
}
// VMUserIPFilters defines filters for IP addresses
// supported only with enterprise version of vmauth
// https://docs.victoriametrics.com/vmauth.html#ip-filters
type VMUserIPFilters struct {
DenyList []string `json:"deny_list,omitempty"`
AllowList []string `json:"allow_list,omitempty"`
}
// CRDRef describe CRD target reference.
type CRDRef struct {
// Kind one of:
// VMAgent VMAlert VMCluster VMSingle or VMAlertManager
Kind string `json:"kind"`
// Name target CRD object name
Name string `json:"name"`
// Namespace target CRD object namespace.
Namespace string `json:"namespace"`
}
// AddRefToObj adds reference to given object and return it.
func (cr *CRDRef) AddRefToObj(obj client.Object) client.Object {
obj.SetName(cr.Name)
obj.SetNamespace(cr.Namespace)
return obj
}
func (cr *CRDRef) AsKey() string {
return fmt.Sprintf("%s/%s/%s", cr.Kind, cr.Namespace, cr.Name)
}
// StaticRef - user-defined routing host address.
type StaticRef struct {
// URL http url for given staticRef.
URL string `json:"url,omitempty"`
// URLs allows setting multiple urls for load-balancing at vmauth-side.
// +optional
URLs []string `json:"urls,omitempty"`
}
// TargetRefBasicAuth target basic authentication
type TargetRefBasicAuth struct {
// The secret in the service scrape namespace that contains the username
// for authentication.
// It must be at them same namespace as CRD
Username v1.SecretKeySelector `json:"username"`
// The secret in the service scrape namespace that contains the password
// for authentication.
// It must be at them same namespace as CRD
Password v1.SecretKeySelector `json:"password"`
}
// VMUserStatus defines the observed state of VMUser
type VMUserStatus struct{}
// VMUser is the Schema for the vmusers API
// +kubebuilder:object:root=true
// +kubebuilder:subresource:status
// +genclient
type VMUser struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
Spec VMUserSpec `json:"spec,omitempty"`
Status VMUserStatus `json:"status,omitempty"`
}
// +kubebuilder:object:root=true
// VMUserList contains a list of VMUser
type VMUserList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []VMUser `json:"items"`
}
// SecretName builds secret name for VMUser.
func (cr *VMUser) SecretName() string {
return fmt.Sprintf("vmuser-%s", cr.Name)
}
// PasswordRefAsKey - builds key for passwordRef cache
func (cr *VMUser) PasswordRefAsKey() string {
return fmt.Sprintf("%s/%s/%s", cr.Namespace, cr.Spec.PasswordRef.Name, cr.Spec.PasswordRef.Key)
}
// TokenRefAsKey - builds key for passwordRef cache
func (cr *VMUser) TokenRefAsKey() string {
return fmt.Sprintf("%s/%s/%s", cr.Namespace, cr.Spec.TokenRef.Name, cr.Spec.TokenRef.Key)
}
func (cr *VMUser) AsOwner() []metav1.OwnerReference {
return []metav1.OwnerReference{
{
APIVersion: cr.APIVersion,
Kind: cr.Kind,
Name: cr.Name,
UID: cr.UID,
Controller: pointer.Bool(true),
BlockOwnerDeletion: pointer.Bool(true),
},
}
}
func (cr VMUser) AnnotationsFiltered() map[string]string {
annotations := make(map[string]string)
for annotation, value := range cr.ObjectMeta.Annotations {
if !strings.HasPrefix(annotation, "kubectl.kubernetes.io/") {
annotations[annotation] = value
}
}
return annotations
}
func (cr VMUser) SelectorLabels() map[string]string {
return map[string]string{
"app.kubernetes.io/name": "vmuser",
"app.kubernetes.io/instance": cr.Name,
"app.kubernetes.io/component": "monitoring",
"managed-by": "vm-operator",
}
}
func (cr VMUser) AllLabels() map[string]string {
labels := cr.SelectorLabels()
if cr.ObjectMeta.Labels != nil {
for label, value := range cr.ObjectMeta.Labels {
if _, ok := labels[label]; ok {
// forbid changes for selector labels
continue
}
labels[label] = value
}
}
return labels
}
func init() {
SchemeBuilder.Register(&VMUser{}, &VMUserList{})
}