|
| 1 | +/* |
| 2 | +Copyright 2022. |
| 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 v1beta1 |
| 18 | + |
| 19 | +import ( |
| 20 | + "k8s.io/apimachinery/pkg/runtime" |
| 21 | + ctrl "sigs.k8s.io/controller-runtime" |
| 22 | + logf "sigs.k8s.io/controller-runtime/pkg/log" |
| 23 | + "sigs.k8s.io/controller-runtime/pkg/webhook" |
| 24 | +) |
| 25 | + |
| 26 | +// log is for logging in this package. |
| 27 | +var metricstoragelog = logf.Log.WithName("metricstorage-resource") |
| 28 | + |
| 29 | +// SetupWebhookWithManager sets up the webhook with the Manager |
| 30 | +func (r *MetricStorage) SetupWebhookWithManager(mgr ctrl.Manager) error { |
| 31 | + return ctrl.NewWebhookManagedBy(mgr). |
| 32 | + For(r). |
| 33 | + Complete() |
| 34 | +} |
| 35 | + |
| 36 | +//+kubebuilder:webhook:path=/mutate-telemetry-openstack-org-v1beta1-metricstorage,mutating=true,failurePolicy=fail,sideEffects=None,groups=telemetry.openstack.org,resources=metricstorages,verbs=create;update,versions=v1beta1,name=mmetricstorage.kb.io,admissionReviewVersions=v1 |
| 37 | + |
| 38 | +var _ webhook.Defaulter = &MetricStorage{} |
| 39 | + |
| 40 | +// Default implements webhook.Defaulter so a webhook will be registered for the type |
| 41 | +func (r *MetricStorage) Default() { |
| 42 | + r.Spec.Default() |
| 43 | +} |
| 44 | + |
| 45 | +// Default - set defaults for the MetricStorage spec |
| 46 | +func (spec *MetricStorageSpec) Default() { |
| 47 | + if spec.MonitoringStack == nil && spec.CustomMonitoringStack == nil { |
| 48 | + spec.MonitoringStack = &MonitoringStack{} |
| 49 | + // Set the AlertingEnabled to true here as the empty value means false |
| 50 | + // and we don't have a way of distinguishing if the value was left |
| 51 | + // empty or set to false by the user in the spec.MonitoringStack.Default() |
| 52 | + spec.MonitoringStack.AlertingEnabled = true |
| 53 | + } |
| 54 | + if spec.MonitoringStack != nil { |
| 55 | + spec.MonitoringStack.Default() |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +// Default - set defaults for the MonitoringStack field |
| 60 | +func (ms *MonitoringStack) Default() { |
| 61 | + if ms.ScrapeInterval == "" { |
| 62 | + ms.ScrapeInterval = "30s" |
| 63 | + } |
| 64 | + ms.Storage.Default() |
| 65 | +} |
| 66 | + |
| 67 | +// Default - set defaults for the Storage field |
| 68 | +func (storage *Storage) Default() { |
| 69 | + if storage.Strategy == "" { |
| 70 | + storage.Strategy = "persistent" |
| 71 | + } |
| 72 | + if storage.Retention == "" { |
| 73 | + storage.Retention = "24h" |
| 74 | + } |
| 75 | + if storage.Strategy == "persistent" { |
| 76 | + storage.Persistent.Default() |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +// Default - set defaults for the PersistentStorage field |
| 81 | +func (ps *PersistentStorage) Default() { |
| 82 | + if ps.PvcStorageRequest == "" { |
| 83 | + ps.PvcStorageRequest = "20G" |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +//+kubebuilder:webhook:path=/validate-telemetry-openstack-org-v1beta1-metricstorage,mutating=false,failurePolicy=fail,sideEffects=None,groups=telemetry.openstack.org,resources=metricstorages,verbs=create;update,versions=v1beta1,name=vmetricstorage.kb.io,admissionReviewVersions=v1 |
| 88 | + |
| 89 | +var _ webhook.Validator = &MetricStorage{} |
| 90 | + |
| 91 | +// ValidateCreate implements webhook.Validator so a webhook will be registered for the type |
| 92 | +func (r *MetricStorage) ValidateCreate() error { |
| 93 | + metricstoragelog.Info("validate create", "name", r.Name) |
| 94 | + |
| 95 | + // TODO(user): fill in your validation logic upon object creation. |
| 96 | + return nil |
| 97 | +} |
| 98 | + |
| 99 | +// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type |
| 100 | +func (r *MetricStorage) ValidateUpdate(old runtime.Object) error { |
| 101 | + metricstoragelog.Info("validate update", "name", r.Name) |
| 102 | + |
| 103 | + // TODO(user): fill in your validation logic upon object update. |
| 104 | + return nil |
| 105 | +} |
| 106 | + |
| 107 | +// ValidateDelete implements webhook.Validator so a webhook will be registered for the type |
| 108 | +func (r *MetricStorage) ValidateDelete() error { |
| 109 | + metricstoragelog.Info("validate delete", "name", r.Name) |
| 110 | + |
| 111 | + // TODO(user): fill in your validation logic upon object deletion. |
| 112 | + return nil |
| 113 | +} |
0 commit comments