Skip to content

Commit 9e5ab0f

Browse files
introduce VMAnomalyModel
1 parent cb76e6a commit 9e5ab0f

33 files changed

+1730
-88
lines changed

PROJECT

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,4 +249,16 @@ resources:
249249
webhooks:
250250
validation: true
251251
webhookVersion: v1
252+
- api:
253+
crdVersion: v1
254+
namespaced: true
255+
controller: true
256+
domain: victoriametrics.com
257+
group: operator
258+
kind: VMAnomalyModel
259+
path: github.com/VictoriaMetrics/operator/api/operator/v1
260+
version: v1
261+
webhooks:
262+
validation: true
263+
webhookVersion: v1
252264
version: "3"

api/operator/v1/vmanomaly_types.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,18 @@ type VMAnomalySpec struct {
9494
// Monitoring configures how expose anomaly metrics
9595
// See https://docs.victoriametrics.com/anomaly-detection/components/monitoring/
9696
Monitoring *VMAnomalyMonitoringSpec `json:"monitoring,omitempty"`
97+
// ModelSelector defines VMAnomalyModels to be selected for anomaly detection.
98+
// Works in combination with ModelNamespaceSelector.
99+
// ModelNamespaceSelector nil - only objects at VMAnomaly namespace.
100+
// Selector nil - only objects at ModelNamespaceSelector namespaces.
101+
// +optional
102+
ModelSelector *metav1.LabelSelector `json:"modelSelector,omitempty"`
103+
// ModelNamespaceSelector Namespaces to be selected for VMAnomalyModel discovery.
104+
// Works in combination with ModelSelector.
105+
// ModelNamespaceSelector nil - only objects at VMAnomaly namespace.
106+
// Selector nil - only objects at ModelNamespaceSelector namespaces.
107+
// +optional
108+
ModelNamespaceSelector *metav1.LabelSelector `json:"modelNamespaceSelector,omitempty"`
97109
// License allows to configure license key to be used for enterprise features.
98110
// Using license key is supported starting from VictoriaMetrics v1.94.0.
99111
// See [here](https://docs.victoriametrics.com/enterprise)
@@ -103,6 +115,7 @@ type VMAnomalySpec struct {
103115
// +optional
104116
ServiceAccountName string `json:"serviceAccountName,omitempty"`
105117
vmv1beta1.CommonDefaultableParams `json:",inline,omitempty"`
118+
vmv1beta1.CommonConfigReloaderParams `json:",inline,omitempty"`
106119
vmv1beta1.CommonApplicationDeploymentParams `json:",inline,omitempty"`
107120
}
108121

@@ -340,6 +353,11 @@ func (cr *VMAnomaly) GetServiceAccountName() string {
340353
return cr.Spec.ServiceAccountName
341354
}
342355

356+
// AsCRDOwner implements interface
357+
func (*VMAnomaly) AsCRDOwner() []metav1.OwnerReference {
358+
return vmv1beta1.GetCRDAsOwner(vmv1beta1.Anomaly)
359+
}
360+
343361
// IsOwnsServiceAccount checks if ServiceAccountName is set explicitly
344362
func (cr *VMAnomaly) IsOwnsServiceAccount() bool {
345363
return cr.Spec.ServiceAccountName == ""
@@ -454,6 +472,10 @@ func (cr *VMAnomaly) UnmarshalJSON(src []byte) error {
454472
return nil
455473
}
456474

475+
func (cr *VMAnomaly) GetClusterRoleName() string {
476+
return fmt.Sprintf("monitoring:%s:vmanomaly-%s", cr.Namespace, cr.Name)
477+
}
478+
457479
// UnmarshalJSON implements json.Unmarshaler interface
458480
func (cr *VMAnomalySpec) UnmarshalJSON(src []byte) error {
459481
type pcr VMAnomalySpec
@@ -464,6 +486,16 @@ func (cr *VMAnomalySpec) UnmarshalJSON(src []byte) error {
464486
return nil
465487
}
466488

489+
// IsUnmanaged checks if object should managed any config objects
490+
func (cr *VMAnomaly) IsUnmanaged() bool {
491+
return cr.IsModelUnmanaged()
492+
}
493+
494+
// IsModelUnmanaged checks if object should managed model config objects
495+
func (cr *VMAnomaly) IsModelUnmanaged() bool {
496+
return cr.Spec.ModelSelector == nil && cr.Spec.ModelNamespaceSelector == nil
497+
}
498+
467499
// +kubebuilder:object:root=true
468500

469501
// VMAnomalyList contains a list of VMAnomaly.
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package v1
18+
19+
import (
20+
"encoding/json"
21+
"fmt"
22+
23+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
24+
"k8s.io/apimachinery/pkg/runtime"
25+
26+
vmv1beta1 "github.com/VictoriaMetrics/operator/api/operator/v1beta1"
27+
)
28+
29+
// VMAnomalyModelSpec defines the desired state of VMAnomalyModel.
30+
type VMAnomalyModelSpec struct {
31+
// ParsingError contents error with context if operator was failed to parse json object from kubernetes api server
32+
ParsingError string `json:"-" yaml:"-"`
33+
// Class defines anomaly detection model class
34+
Class string `json:"class" yaml:"class"`
35+
// Params defines anomaly detection model params
36+
Params runtime.RawExtension `json:"params,omitempty" yaml:"params,omitempty"`
37+
}
38+
39+
// UnmarshalJSON implements json.Unmarshaler interface
40+
func (cr *VMAnomalyModelSpec) UnmarshalJSON(src []byte) error {
41+
type pcr VMAnomalyModelSpec
42+
if err := json.Unmarshal(src, (*pcr)(cr)); err != nil {
43+
cr.ParsingError = fmt.Sprintf("cannot parse spec: %s, err: %s", string(src), err)
44+
return nil
45+
}
46+
return nil
47+
}
48+
49+
// VMAnomalyModelStatus defines the observed state of VMAnomalyModel.
50+
type VMAnomalyModelStatus struct {
51+
vmv1beta1.StatusMetadata `json:",inline"`
52+
}
53+
54+
// GetStatusMetadata returns metadata for object status
55+
func (cr *VMAnomalyModel) GetStatusMetadata() *vmv1beta1.StatusMetadata {
56+
return &cr.Status.StatusMetadata
57+
}
58+
59+
// +kubebuilder:object:root=true
60+
// +kubebuilder:subresource:status
61+
62+
// VMAnomalyModel is the Schema for the vmanomalymodels API.
63+
type VMAnomalyModel struct {
64+
metav1.TypeMeta `json:",inline"`
65+
metav1.ObjectMeta `json:"metadata,omitempty"`
66+
67+
Spec VMAnomalyModelSpec `json:"spec,omitempty"`
68+
Status VMAnomalyModelStatus `json:"status,omitempty"`
69+
}
70+
71+
// +kubebuilder:object:root=true
72+
73+
// VMAnomalyModelList contains a list of VMAnomalyModel.
74+
type VMAnomalyModelList struct {
75+
metav1.TypeMeta `json:",inline"`
76+
metav1.ListMeta `json:"metadata,omitempty"`
77+
Items []VMAnomalyModel `json:"items"`
78+
}
79+
80+
func init() {
81+
SchemeBuilder.Register(&VMAnomalyModel{}, &VMAnomalyModelList{})
82+
}

api/operator/v1/zz_generated.deepcopy.go

Lines changed: 104 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/operator/v1beta1/owner.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ type CRDName int
1414

1515
const (
1616
Agent CRDName = iota
17+
Anomaly
1718
)
1819

1920
func (c CRDName) String() string {
@@ -40,6 +41,8 @@ func Init(ctx context.Context, rclient client.Client) error {
4041
switch item.Name {
4142
case "vmagents.operator.victoriametrics.com":
4243
n = Agent
44+
case "vmanomalies.operator.victoriametrics.com":
45+
n = Anomaly
4346
default:
4447
continue
4548
}

config/crd/kustomization.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ resources:
2222
- bases/operator.victoriametrics.com_vlsingles.yaml
2323
- bases/operator.victoriametrics.com_vlclusters.yaml
2424
- bases/operator.victoriametrics.com_vmanomalies.yaml
25+
- bases/operator.victoriametrics.com_vmanomalymodels.yaml
2526
patches:
2627
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix.
2728
# patches here are for enabling the conversion webhook for each CRD

0 commit comments

Comments
 (0)