|
| 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