Skip to content

Commit 26d7723

Browse files
committed
webhook: adds an example of PrometheusRule validation with operator
This commits adds extra webhook, which is able to validate metricsQL expressions with PrometheusRule objects.
1 parent 7955c53 commit 26d7723

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed

internal/manager/manager.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,7 @@ func addWebhooks(mgr ctrl.Manager) error {
369369
webhookv1beta1.SetupVMScrapeConfigWebhookWithManager,
370370
webhookv1beta1.SetupVMStaticScrapeWebhookWithManager,
371371
webhookv1beta1.SetupVMProbeWebhookWithManager,
372+
webhookv1.SetupPromethuesRuleWebhookWithManager,
372373
})
373374
}
374375

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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+
"context"
21+
"encoding/json"
22+
"fmt"
23+
24+
promv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
25+
"k8s.io/apimachinery/pkg/runtime"
26+
ctrl "sigs.k8s.io/controller-runtime"
27+
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
28+
29+
vmv1beta1 "github.com/VictoriaMetrics/operator/api/operator/v1beta1"
30+
"github.com/VictoriaMetrics/operator/internal/config"
31+
"github.com/VictoriaMetrics/operator/internal/controller/operator/converter"
32+
)
33+
34+
// SetupVLClusterWebhookWithManager will setup the manager to manage the webhooks
35+
func SetupPromethuesRuleWebhookWithManager(mgr ctrl.Manager) error {
36+
println("setting up")
37+
return ctrl.NewWebhookManagedBy(mgr).
38+
For(&promv1.PrometheusRule{}).
39+
WithValidator(&PrometheusRuleValidator{}).
40+
Complete()
41+
}
42+
43+
type PrometheusRuleValidator struct{}
44+
45+
var _ admission.CustomValidator = (*PrometheusRuleValidator)(nil)
46+
47+
// ValidateCreate implements admission.CustomValidator so a webhook will be registered for the type
48+
func (*PrometheusRuleValidator) ValidateCreate(_ context.Context, obj runtime.Object) (admission.Warnings, error) {
49+
r, ok := obj.(*promv1.PrometheusRule)
50+
if !ok {
51+
return nil, fmt.Errorf("BUG: unexpected type: %T", obj)
52+
}
53+
54+
vmr := converter.ConvertPromRule(r, config.MustGetBaseConfig())
55+
56+
if err := vmr.Validate(); err != nil {
57+
return nil, err
58+
}
59+
60+
return nil, nil
61+
}
62+
63+
// ValidateUpdate implements admission.CustomValidator so a webhook will be registered for the type
64+
func (*PrometheusRuleValidator) ValidateUpdate(_ context.Context, _, newObj runtime.Object) (admission.Warnings, error) {
65+
r, ok := newObj.(*promv1.PrometheusRule)
66+
if !ok {
67+
return nil, fmt.Errorf("BUG: unexpected type: %T", newObj)
68+
}
69+
70+
vmr := converter.ConvertPromRule(r, config.MustGetBaseConfig())
71+
72+
if err := vmr.Validate(); err != nil {
73+
return nil, err
74+
}
75+
return nil, nil
76+
}
77+
78+
// ValidateDelete implements admission.CustomValidator so a webhook will be registered for the type
79+
func (*PrometheusRuleValidator) ValidateDelete(_ context.Context, _ runtime.Object) (admission.Warnings, error) {
80+
return nil, nil
81+
}
82+
83+
func promRuleSpecToVMRule(src *promv1.PrometheusRule) (*vmv1beta1.VMRule, error) {
84+
var vr vmv1beta1.VMRule
85+
specData, err := json.Marshal(src.Spec)
86+
if err != nil {
87+
return nil, err
88+
}
89+
90+
if err := json.Unmarshal(specData, &vr.Spec); err != nil {
91+
return nil, err
92+
}
93+
return &vr, nil
94+
}

0 commit comments

Comments
 (0)