Skip to content

Commit 4573011

Browse files
introduce VMAnomalyModel and VMAnomalyScheduler
1 parent 1b40e31 commit 4573011

34 files changed

+2560
-164
lines changed

PROJECT

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,30 @@ resources:
237237
webhooks:
238238
validation: true
239239
webhookVersion: v1
240+
- api:
241+
crdVersion: v1
242+
namespaced: true
243+
controller: true
244+
domain: victoriametrics.com
245+
group: operator
246+
kind: VMAnomalyModel
247+
path: github.com/VictoriaMetrics/operator/api/operator/v1
248+
version: v1
249+
webhooks:
250+
validation: true
251+
webhookVersion: v1
252+
- api:
253+
crdVersion: v1
254+
namespaced: true
255+
controller: true
256+
domain: victoriametrics.com
257+
group: operator
258+
kind: VMAnomalyScheduler
259+
path: github.com/VictoriaMetrics/operator/api/operator/v1
260+
version: v1
261+
webhooks:
262+
validation: true
263+
webhookVersion: v1
240264
- api:
241265
crdVersion: v1
242266
namespaced: true

api/operator/v1/common.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"net/url"
77

88
corev1 "k8s.io/api/core/v1"
9+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
910
)
1011

1112
const (
@@ -89,6 +90,20 @@ func (o *OAuth2) Validate() error {
8990
return nil
9091
}
9192

93+
// Selector defines object and namespace selectors
94+
type Selector struct {
95+
// ObjectSelector defines object to be selected for discovery.
96+
// +optional
97+
ObjectSelector *metav1.LabelSelector `json:"objectSelector,omitempty"`
98+
// NamespaceSelector defines namespaces to be selected for object discovery.
99+
// +optional
100+
NamespaceSelector *metav1.LabelSelector `json:"namespaceSelector,omitempty"`
101+
}
102+
103+
func (s *Selector) IsUnmanaged() bool {
104+
return s == nil || (s.ObjectSelector == nil && s.NamespaceSelector == nil)
105+
}
106+
92107
// TLSConfig specifies TLS configuration parameters
93108
// with optional references to secrets with corresponding sensitive values
94109
type TLSConfig struct {

api/operator/v1/vmanomaly_types.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,12 @@ 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 VMAnomalyModel's object and namespace selectors.
98+
// +optional
99+
ModelSelector *Selector `json:"modelSelector,omitempty"`
100+
// SchedulerSelector defines VMAnomalyScheduler's object and namespace selectors.
101+
// +optional
102+
SchedulerSelector *Selector `json:"schedulerSelector,omitempty"`
97103
// License allows to configure license key to be used for enterprise features.
98104
// Using license key is supported starting from VictoriaMetrics v1.94.0.
99105
// See [here](https://docs.victoriametrics.com/victoriametrics/enterprise/)
@@ -457,6 +463,11 @@ func (cr *VMAnomalySpec) UnmarshalJSON(src []byte) error {
457463
return nil
458464
}
459465

466+
// IsUnmanaged checks if object should managed any config objects
467+
func (cr *VMAnomaly) IsUnmanaged() bool {
468+
return cr.Spec.ModelSelector.IsUnmanaged() && cr.Spec.SchedulerSelector.IsUnmanaged()
469+
}
470+
460471
// +kubebuilder:object:root=true
461472

462473
// VMAnomalyList contains a list of VMAnomaly.
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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+
// AsKey returns unique key for object
60+
func (cr *VMAnomalyModel) AsKey(_ bool) string {
61+
return cr.Namespace + "/" + cr.Name
62+
}
63+
64+
// +kubebuilder:object:root=true
65+
// +kubebuilder:subresource:status
66+
67+
// VMAnomalyModel is the Schema for the vmanomalymodels API.
68+
type VMAnomalyModel struct {
69+
metav1.TypeMeta `json:",inline"`
70+
metav1.ObjectMeta `json:"metadata,omitempty"`
71+
72+
Spec VMAnomalyModelSpec `json:"spec,omitempty"`
73+
Status VMAnomalyModelStatus `json:"status,omitempty"`
74+
}
75+
76+
// +kubebuilder:object:root=true
77+
78+
// VMAnomalyModelList contains a list of VMAnomalyModel.
79+
type VMAnomalyModelList struct {
80+
metav1.TypeMeta `json:",inline"`
81+
metav1.ListMeta `json:"metadata,omitempty"`
82+
Items []VMAnomalyModel `json:"items"`
83+
}
84+
85+
func init() {
86+
SchemeBuilder.Register(&VMAnomalyModel{}, &VMAnomalyModelList{})
87+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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+
// VMAnomalySchedulerSpec defines the desired state of VMAnomalyScheduler.
30+
type VMAnomalySchedulerSpec 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 scheduler class
34+
Class string `json:"class" yaml:"class"`
35+
// Params defines anomaly detection scheduler params
36+
Params runtime.RawExtension `json:"params,omitempty" yaml:"params,omitempty"`
37+
}
38+
39+
// VMAnomalySchedulerStatus defines the observed state of VMAnomalyScheduler.
40+
type VMAnomalySchedulerStatus struct {
41+
vmv1beta1.StatusMetadata `json:",inline"`
42+
}
43+
44+
// GetStatusMetadata returns metadata for object status
45+
func (cr *VMAnomalyScheduler) GetStatusMetadata() *vmv1beta1.StatusMetadata {
46+
return &cr.Status.StatusMetadata
47+
}
48+
49+
// AsKey returns unique key for object
50+
func (cr *VMAnomalyScheduler) AsKey(_ bool) string {
51+
return cr.Namespace + "/" + cr.Name
52+
}
53+
54+
// UnmarshalJSON implements json.Unmarshaler interface
55+
func (cr *VMAnomalySchedulerSpec) UnmarshalJSON(src []byte) error {
56+
type pcr VMAnomalySchedulerSpec
57+
if err := json.Unmarshal(src, (*pcr)(cr)); err != nil {
58+
cr.ParsingError = fmt.Sprintf("cannot parse spec: %s, err: %s", string(src), err)
59+
return nil
60+
}
61+
return nil
62+
}
63+
64+
// +kubebuilder:object:root=true
65+
// +kubebuilder:subresource:status
66+
67+
// VMAnomalyScheduler is the Schema for the vmanomalyschedulers API.
68+
type VMAnomalyScheduler struct {
69+
metav1.TypeMeta `json:",inline"`
70+
metav1.ObjectMeta `json:"metadata,omitempty"`
71+
72+
Spec VMAnomalySchedulerSpec `json:"spec,omitempty"`
73+
Status VMAnomalySchedulerStatus `json:"status,omitempty"`
74+
}
75+
76+
// +kubebuilder:object:root=true
77+
78+
// VMAnomalySchedulerList contains a list of VMAnomalyScheduler.
79+
type VMAnomalySchedulerList struct {
80+
metav1.TypeMeta `json:",inline"`
81+
metav1.ListMeta `json:"metadata,omitempty"`
82+
Items []VMAnomalyScheduler `json:"items"`
83+
}
84+
85+
func init() {
86+
SchemeBuilder.Register(&VMAnomalyScheduler{}, &VMAnomalySchedulerList{})
87+
}

0 commit comments

Comments
 (0)