Skip to content

Commit 5906c87

Browse files
introduce VMAnomalyModel
1 parent 4255587 commit 5906c87

34 files changed

+2516
-154
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 (
@@ -93,6 +94,20 @@ func (o *OAuth2) Validate() error {
9394
return nil
9495
}
9596

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

api/operator/v1/vmanomaly_types.go

Lines changed: 16 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/)
@@ -338,6 +344,11 @@ func (cr *VMAnomaly) GetServiceAccountName() string {
338344
return cr.Spec.ServiceAccountName
339345
}
340346

347+
// AsCRDOwner implements interface
348+
func (*VMAnomaly) AsCRDOwner() []metav1.OwnerReference {
349+
return vmv1beta1.GetCRDAsOwner(vmv1beta1.Anomaly)
350+
}
351+
341352
// IsOwnsServiceAccount checks if ServiceAccountName is set explicitly
342353
func (cr *VMAnomaly) IsOwnsServiceAccount() bool {
343354
return cr.Spec.ServiceAccountName == ""
@@ -462,6 +473,11 @@ func (cr *VMAnomalySpec) UnmarshalJSON(src []byte) error {
462473
return nil
463474
}
464475

476+
// IsUnmanaged checks if object should managed any config objects
477+
func (cr *VMAnomaly) IsUnmanaged() bool {
478+
return cr.Spec.ModelSelector.IsUnmanaged() && cr.Spec.SchedulerSelector.IsUnmanaged()
479+
}
480+
465481
// +kubebuilder:object:root=true
466482

467483
// 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+
}
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+
// 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+
// UnmarshalJSON implements json.Unmarshaler interface
50+
func (cr *VMAnomalySchedulerSpec) UnmarshalJSON(src []byte) error {
51+
type pcr VMAnomalySchedulerSpec
52+
if err := json.Unmarshal(src, (*pcr)(cr)); err != nil {
53+
cr.ParsingError = fmt.Sprintf("cannot parse spec: %s, err: %s", string(src), err)
54+
return nil
55+
}
56+
return nil
57+
}
58+
59+
// +kubebuilder:object:root=true
60+
// +kubebuilder:subresource:status
61+
62+
// VMAnomalyScheduler is the Schema for the vmanomalyschedulers API.
63+
type VMAnomalyScheduler struct {
64+
metav1.TypeMeta `json:",inline"`
65+
metav1.ObjectMeta `json:"metadata,omitempty"`
66+
67+
Spec VMAnomalySchedulerSpec `json:"spec,omitempty"`
68+
Status VMAnomalySchedulerStatus `json:"status,omitempty"`
69+
}
70+
71+
// +kubebuilder:object:root=true
72+
73+
// VMAnomalySchedulerList contains a list of VMAnomalyScheduler.
74+
type VMAnomalySchedulerList struct {
75+
metav1.TypeMeta `json:",inline"`
76+
metav1.ListMeta `json:"metadata,omitempty"`
77+
Items []VMAnomalyScheduler `json:"items"`
78+
}
79+
80+
func init() {
81+
SchemeBuilder.Register(&VMAnomalyScheduler{}, &VMAnomalySchedulerList{})
82+
}

0 commit comments

Comments
 (0)