Skip to content

Commit 95b46b7

Browse files
Merge pull request #273 from vyzigold/metricstorage-webhook
Add defaulting webhook for MetricStorage
2 parents 9348e8b + ce6a0df commit 95b46b7

13 files changed

+226
-36
lines changed

PROJECT

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# Code generated by tool. DO NOT EDIT.
2+
# This file is used to track the info used to scaffold your project
3+
# and allow the plugins properly work.
4+
# More info: https://book.kubebuilder.io/reference/project-config.html
15
domain: openstack.org
26
layout:
37
- go.kubebuilder.io/v3
@@ -61,4 +65,8 @@ resources:
6165
kind: MetricStorage
6266
path: github.com/openstack-k8s-operators/telemetry-operator/api/v1beta1
6367
version: v1beta1
68+
webhooks:
69+
defaulting: true
70+
validation: true
71+
webhookVersion: v1
6472
version: "3"

api/bases/telemetry.openstack.org_metricstorages.yaml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,13 +1109,6 @@ spec:
11091109
- persistent
11101110
type: string
11111111
type: object
1112-
type:
1113-
default: prometheus
1114-
description: Type defines the type of the storage. Can only be
1115-
"prometheus" at the moment
1116-
enum:
1117-
- prometheus
1118-
type: string
11191112
type: object
11201113
type: object
11211114
status:

api/bases/telemetry.openstack.org_telemetries.yaml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1537,13 +1537,6 @@ spec:
15371537
- persistent
15381538
type: string
15391539
type: object
1540-
type:
1541-
default: prometheus
1542-
description: Type defines the type of the storage. Can only
1543-
be "prometheus" at the moment
1544-
enum:
1545-
- prometheus
1546-
type: string
15471540
type: object
15481541
type: object
15491542
type: object

api/v1beta1/metricstorage_types.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,6 @@ type Storage struct {
6161

6262
// MonitoringStack defines the options for a Red Hat supported metric storage
6363
type MonitoringStack struct {
64-
// Type defines the type of the storage.
65-
// Can only be "prometheus" at the moment
66-
// +kubebuilder:validation:Optional
67-
// +kubebuilder:validation:Enum=prometheus
68-
// +kubebuilder:default=prometheus
69-
Type string `json:"type"`
70-
7164
// AlertingEnabled allows to enable or disable alertmanager
7265
// +kubebuilder:validation:Optional
7366
// +kubebuilder:default=true
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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+
}

api/v1beta1/webhook_suite_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ var _ = BeforeSuite(func() {
102102
err = (&Ceilometer{}).SetupWebhookWithManager(mgr)
103103
Expect(err).NotTo(HaveOccurred())
104104

105+
err = (&MetricStorage{}).SetupWebhookWithManager(mgr)
106+
Expect(err).NotTo(HaveOccurred())
107+
105108
//+kubebuilder:scaffold:webhook
106109

107110
go func() {

config/crd/bases/telemetry.openstack.org_metricstorages.yaml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,13 +1109,6 @@ spec:
11091109
- persistent
11101110
type: string
11111111
type: object
1112-
type:
1113-
default: prometheus
1114-
description: Type defines the type of the storage. Can only be
1115-
"prometheus" at the moment
1116-
enum:
1117-
- prometheus
1118-
type: string
11191112
type: object
11201113
type: object
11211114
status:

config/crd/bases/telemetry.openstack.org_telemetries.yaml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1537,13 +1537,6 @@ spec:
15371537
- persistent
15381538
type: string
15391539
type: object
1540-
type:
1541-
default: prometheus
1542-
description: Type defines the type of the storage. Can only
1543-
be "prometheus" at the moment
1544-
enum:
1545-
- prometheus
1546-
type: string
15471540
type: object
15481541
type: object
15491542
type: object

config/samples/telemetry_v1beta1_metricstorage.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ metadata:
1010
name: metricstorage-sample
1111
spec:
1212
monitoringStack:
13-
type: prometheus
1413
alertingEnabled: true
1514
scrapeInterval: 30s
1615
storage:

config/webhook/manifests.yaml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,26 @@ webhooks:
4545
resources:
4646
- ceilometers
4747
sideEffects: None
48+
- admissionReviewVersions:
49+
- v1
50+
clientConfig:
51+
service:
52+
name: webhook-service
53+
namespace: system
54+
path: /mutate-telemetry-openstack-org-v1beta1-metricstorage
55+
failurePolicy: Fail
56+
name: mmetricstorage.kb.io
57+
rules:
58+
- apiGroups:
59+
- telemetry.openstack.org
60+
apiVersions:
61+
- v1beta1
62+
operations:
63+
- CREATE
64+
- UPDATE
65+
resources:
66+
- metricstorages
67+
sideEffects: None
4868
- admissionReviewVersions:
4969
- v1
5070
clientConfig:
@@ -112,6 +132,26 @@ webhooks:
112132
resources:
113133
- ceilometers
114134
sideEffects: None
135+
- admissionReviewVersions:
136+
- v1
137+
clientConfig:
138+
service:
139+
name: webhook-service
140+
namespace: system
141+
path: /validate-telemetry-openstack-org-v1beta1-metricstorage
142+
failurePolicy: Fail
143+
name: vmetricstorage.kb.io
144+
rules:
145+
- apiGroups:
146+
- telemetry.openstack.org
147+
apiVersions:
148+
- v1beta1
149+
operations:
150+
- CREATE
151+
- UPDATE
152+
resources:
153+
- metricstorages
154+
sideEffects: None
115155
- admissionReviewVersions:
116156
- v1
117157
clientConfig:

0 commit comments

Comments
 (0)