Skip to content

Commit bc5cd97

Browse files
committed
refactor: remove clusternodemonitoring webhook
Remove the ClusterNodeMonitoring validating webhook, in favor of CRD/CEL validation.
1 parent ef84205 commit bc5cd97

File tree

5 files changed

+0
-159
lines changed

5 files changed

+0
-159
lines changed

charts/operator/templates/validatingwebhookconfiguration.yaml

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,28 +22,6 @@ metadata:
2222
{{- include "prometheus-engine.labels" . | nindent 4 }}
2323
{{- end }}
2424
webhooks:
25-
- name: validate.clusternodemonitorings.gmp-operator.gmp-system.monitoring.googleapis.com
26-
admissionReviewVersions:
27-
- v1
28-
clientConfig:
29-
# caBundle populated by operator.
30-
service:
31-
name: gmp-operator
32-
namespace: {{.Values.namespace.system}}
33-
port: 443
34-
path: /validate/monitoring.googleapis.com/v1/clusternodemonitorings
35-
failurePolicy: Fail
36-
rules:
37-
- resources:
38-
- clusternodemonitorings
39-
apiGroups:
40-
- monitoring.googleapis.com
41-
apiVersions:
42-
- v1
43-
operations:
44-
- CREATE
45-
- UPDATE
46-
sideEffects: None
4725
- name: validate.rules.gmp-operator.gmp-system.monitoring.googleapis.com
4826
admissionReviewVersions:
4927
- v1

manifests/operator.yaml

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,28 +1041,6 @@ kind: ValidatingWebhookConfiguration
10411041
metadata:
10421042
name: gmp-operator.gmp-system.monitoring.googleapis.com
10431043
webhooks:
1044-
- name: validate.clusternodemonitorings.gmp-operator.gmp-system.monitoring.googleapis.com
1045-
admissionReviewVersions:
1046-
- v1
1047-
clientConfig:
1048-
# caBundle populated by operator.
1049-
service:
1050-
name: gmp-operator
1051-
namespace: gmp-system
1052-
port: 443
1053-
path: /validate/monitoring.googleapis.com/v1/clusternodemonitorings
1054-
failurePolicy: Fail
1055-
rules:
1056-
- resources:
1057-
- clusternodemonitorings
1058-
apiGroups:
1059-
- monitoring.googleapis.com
1060-
apiVersions:
1061-
- v1
1062-
operations:
1063-
- CREATE
1064-
- UPDATE
1065-
sideEffects: None
10661044
- name: validate.rules.gmp-operator.gmp-system.monitoring.googleapis.com
10671045
admissionReviewVersions:
10681046
- v1

pkg/operator/apis/monitoring/v1/node_types.go

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package v1
1616

1717
import (
18-
"errors"
1918
"fmt"
2019
"strings"
2120

@@ -26,8 +25,6 @@ import (
2625
discoverykube "github.com/prometheus/prometheus/discovery/kubernetes"
2726
"github.com/prometheus/prometheus/model/relabel"
2827
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
29-
"k8s.io/apimachinery/pkg/runtime"
30-
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
3128
)
3229

3330
// ScrapeNodeEndpoint specifies a Prometheus metrics endpoint on a node to scrape.
@@ -118,26 +115,6 @@ func (c *ClusterNodeMonitoring) GetMonitoringStatus() *MonitoringStatus {
118115
return &c.Status
119116
}
120117

121-
func (c *ClusterNodeMonitoring) ValidateCreate() (admission.Warnings, error) {
122-
if len(c.Spec.Endpoints) == 0 {
123-
return nil, errors.New("at least one endpoint is required")
124-
}
125-
// TODO(freinartz): extract validator into dedicated object (like defaulter). For now using
126-
// example values has no adverse effects.
127-
_, err := c.ScrapeConfigs("test_project", "test_location", "test_cluster")
128-
return nil, err
129-
}
130-
131-
func (c *ClusterNodeMonitoring) ValidateUpdate(runtime.Object) (admission.Warnings, error) {
132-
// Validity does not depend on state changes.
133-
return c.ValidateCreate()
134-
}
135-
136-
func (*ClusterNodeMonitoring) ValidateDelete() (admission.Warnings, error) {
137-
// Deletions are always valid.
138-
return nil, nil
139-
}
140-
141118
func (c *ClusterNodeMonitoring) ScrapeConfigs(projectID, location, cluster string) (res []*promconfig.ScrapeConfig, err error) {
142119
for i, ep := range c.Spec.Endpoints {
143120
sc, err := c.endpointScrapeConfig(&ep, projectID, location, cluster)

pkg/operator/apis/monitoring/v1/node_types_test.go

Lines changed: 0 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -15,101 +15,13 @@
1515
package v1
1616

1717
import (
18-
"strings"
1918
"testing"
2019

2120
"github.com/google/go-cmp/cmp"
2221
yaml "gopkg.in/yaml.v2"
2322
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2423
)
2524

26-
func TestValidateClusterNodeMonitoring(t *testing.T) {
27-
cases := []struct {
28-
desc string
29-
pm ClusterNodeMonitoringSpec
30-
eps []ScrapeNodeEndpoint
31-
fail bool
32-
errContains string
33-
}{
34-
{
35-
desc: "OK metadata labels",
36-
eps: []ScrapeNodeEndpoint{
37-
{
38-
Interval: "10s",
39-
},
40-
},
41-
},
42-
{
43-
desc: "Scrape interval missing",
44-
eps: []ScrapeNodeEndpoint{
45-
{},
46-
},
47-
fail: true,
48-
errContains: "empty duration string",
49-
},
50-
{
51-
desc: "scrape interval malformed",
52-
eps: []ScrapeNodeEndpoint{
53-
{
54-
Interval: "foo",
55-
},
56-
},
57-
fail: true,
58-
errContains: "invalid scrape interval: not a valid duration string",
59-
},
60-
{
61-
desc: "scrape timeout greater than interval",
62-
eps: []ScrapeNodeEndpoint{
63-
{
64-
Interval: "1s",
65-
Timeout: "2s",
66-
},
67-
},
68-
fail: true,
69-
errContains: "scrape timeout 2s must not be greater than scrape interval 1s",
70-
},
71-
{
72-
// Regression test for https://github.com/GoogleCloudPlatform/prometheus-engine/issues/479
73-
desc: "Duplicated job name",
74-
eps: []ScrapeNodeEndpoint{
75-
{
76-
Interval: "10s",
77-
},
78-
{
79-
Interval: "10000ms",
80-
},
81-
},
82-
fail: true,
83-
errContains: "/r1/metrics for endpoints with index 0 and 1;consider creating a separate custom resource (PodMonitoring, etc.) for endpoints that share the same resource name, namespace and port name",
84-
},
85-
}
86-
87-
for _, c := range cases {
88-
t.Run(c.desc+"", func(t *testing.T) {
89-
nm := &ClusterNodeMonitoring{
90-
ObjectMeta: metav1.ObjectMeta{
91-
Name: "r1",
92-
},
93-
Spec: ClusterNodeMonitoringSpec{
94-
Endpoints: c.eps,
95-
},
96-
}
97-
_, err := nm.ValidateCreate()
98-
t.Log(err)
99-
100-
if err == nil && c.fail {
101-
t.Fatalf("expected failure but passed")
102-
}
103-
if err != nil && !c.fail {
104-
t.Fatalf("unexpected failure: %s", err)
105-
}
106-
if err != nil && c.fail && !strings.Contains(err.Error(), c.errContains) {
107-
t.Fatalf("expected error to contain %q but got %q", c.errContains, err)
108-
}
109-
})
110-
}
111-
}
112-
11325
func TestClusterNodeMonitoring_ScrapeConfig(t *testing.T) {
11426
// Generate YAML for one complex scrape config and make sure everything
11527
// adds up. This primarily verifies that everything is included and marshalling

pkg/operator/webhook.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,6 @@ func setupAdmissionWebhooks(ctx context.Context, logger logr.Logger, kubeClient
5656
scheme := kubeClient.Scheme()
5757

5858
// Validating webhooks.
59-
webhookServer.Register(
60-
validatePath(monitoringv1.ClusterNodeMonitoringResource()),
61-
admission.ValidatingWebhookFor(scheme, &monitoringv1.ClusterNodeMonitoring{}),
62-
)
6359
webhookServer.Register(
6460
validatePath(monitoringv1.OperatorConfigResource()),
6561
admission.WithCustomValidator(scheme, &monitoringv1.OperatorConfig{}, &monitoringv1.OperatorConfigValidator{

0 commit comments

Comments
 (0)