From 3a0e089129ac285b1f4b5440b768e44df6a000f2 Mon Sep 17 00:00:00 2001 From: junotx Date: Tue, 19 Jan 2021 10:50:56 +0800 Subject: [PATCH] add request err with api not enabled to alerting Signed-off-by: junotx --- pkg/api/alerting/v2alpha1/types.go | 1 + pkg/kapis/alerting/v2alpha1/register.go | 18 ++++++++++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/pkg/api/alerting/v2alpha1/types.go b/pkg/api/alerting/v2alpha1/types.go index b2a895709..d1dbc1ca3 100644 --- a/pkg/api/alerting/v2alpha1/types.go +++ b/pkg/api/alerting/v2alpha1/types.go @@ -39,6 +39,7 @@ var ( ErrThanosRulerNotEnabled = errors.New("The request operation to custom alerting rule could not be done because thanos ruler is not enabled") ErrAlertingRuleNotFound = errors.New("The alerting rule was not found") ErrAlertingRuleAlreadyExists = errors.New("The alerting rule already exists") + ErrAlertingAPIV2NotEnabled = errors.New("The alerting v2 API was not enabled") ruleLabelNameMatcher = regexp.MustCompile(`[a-zA-Z_][a-zA-Z0-9_]*`) ) diff --git a/pkg/kapis/alerting/v2alpha1/register.go b/pkg/kapis/alerting/v2alpha1/register.go index 3ae5f543a..3fb055d8e 100644 --- a/pkg/kapis/alerting/v2alpha1/register.go +++ b/pkg/kapis/alerting/v2alpha1/register.go @@ -41,10 +41,24 @@ func AddToContainer(container *restful.Container, informers informers.InformerFa promResourceClient promresourcesclient.Interface, ruleClient alerting.RuleClient, option *alerting.Options) error { - handler := newHandler(informers, promResourceClient, ruleClient, option) - ws := runtime.NewWebService(GroupVersion) + if informers == nil || promResourceClient == nil || ruleClient == nil || option == nil { + h := func(req *restful.Request, resp *restful.Response) { + ksapi.HandleBadRequest(resp, nil, alertingv2alpha1.ErrAlertingAPIV2NotEnabled) + return + } + ws.Route(ws.GET("/{path:*}").To(h).Returns(http.StatusOK, ksapi.StatusOK, nil)) + ws.Route(ws.PUT("/{path:*}").To(h).Returns(http.StatusOK, ksapi.StatusOK, nil)) + ws.Route(ws.POST("/{path:*}").To(h).Returns(http.StatusOK, ksapi.StatusOK, nil)) + ws.Route(ws.DELETE("/{path:*}").To(h).Returns(http.StatusOK, ksapi.StatusOK, nil)) + ws.Route(ws.PATCH("/{path:*}").To(h).Returns(http.StatusOK, ksapi.StatusOK, nil)) + container.Add(ws) + return nil + } + + handler := newHandler(informers, promResourceClient, ruleClient, option) + ws.Route(ws.GET("/rules"). To(handler.handleListCustomAlertingRules). Doc("list the cluster-level custom alerting rules").