285
pkg/kapis/customalerting/v1alpha1/handler.go
Normal file
285
pkg/kapis/customalerting/v1alpha1/handler.go
Normal file
@@ -0,0 +1,285 @@
|
||||
/*
|
||||
Copyright 2020 KubeSphere Authors
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package v1alpha1
|
||||
|
||||
import (
|
||||
promresourcesclient "github.com/prometheus-operator/prometheus-operator/pkg/client/versioned"
|
||||
"github.com/emicklei/go-restful"
|
||||
"k8s.io/klog"
|
||||
ksapi "kubesphere.io/kubesphere/pkg/api"
|
||||
"kubesphere.io/kubesphere/pkg/api/customalerting/v1alpha1"
|
||||
"kubesphere.io/kubesphere/pkg/informers"
|
||||
customalertingmodels "kubesphere.io/kubesphere/pkg/models/customalerting"
|
||||
"kubesphere.io/kubesphere/pkg/simple/client/customalerting"
|
||||
)
|
||||
|
||||
type handler struct {
|
||||
operator customalertingmodels.Operator
|
||||
}
|
||||
|
||||
func newHandler(informers informers.InformerFactory,
|
||||
promResourceClient promresourcesclient.Interface, ruleClient customalerting.RuleClient,
|
||||
option *customalerting.Options) *handler {
|
||||
return &handler{
|
||||
operator: customalertingmodels.NewOperator(
|
||||
informers, promResourceClient, ruleClient, option),
|
||||
}
|
||||
}
|
||||
|
||||
func (h *handler) handleListCustomAlertingRules(req *restful.Request, resp *restful.Response) {
|
||||
namespace := req.PathParameter("namespace")
|
||||
query, err := v1alpha1.ParseAlertingRuleQueryParams(req)
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
ksapi.HandleBadRequest(resp, nil, err)
|
||||
return
|
||||
}
|
||||
|
||||
rules, err := h.operator.ListCustomAlertingRules(req.Request.Context(), namespace, query)
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
switch {
|
||||
case err == v1alpha1.ErrThanosRulerNotEnabled:
|
||||
ksapi.HandleBadRequest(resp, nil, err)
|
||||
default:
|
||||
ksapi.HandleInternalError(resp, nil, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
resp.WriteEntity(rules)
|
||||
}
|
||||
|
||||
func (h *handler) handleListCustomRulesAlerts(req *restful.Request, resp *restful.Response) {
|
||||
namespace := req.PathParameter("namespace")
|
||||
query, err := v1alpha1.ParseAlertQueryParams(req)
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
ksapi.HandleBadRequest(resp, nil, err)
|
||||
return
|
||||
}
|
||||
|
||||
alerts, err := h.operator.ListCustomRulesAlerts(req.Request.Context(), namespace, query)
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
switch {
|
||||
case err == v1alpha1.ErrThanosRulerNotEnabled:
|
||||
ksapi.HandleBadRequest(resp, nil, err)
|
||||
default:
|
||||
ksapi.HandleInternalError(resp, nil, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
resp.WriteEntity(alerts)
|
||||
}
|
||||
|
||||
func (h *handler) handleGetCustomAlertingRule(req *restful.Request, resp *restful.Response) {
|
||||
namespace := req.PathParameter("namespace")
|
||||
ruleName := req.PathParameter("rule_name")
|
||||
|
||||
rule, err := h.operator.GetCustomAlertingRule(req.Request.Context(), namespace, ruleName)
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
switch {
|
||||
case err == v1alpha1.ErrThanosRulerNotEnabled:
|
||||
ksapi.HandleBadRequest(resp, nil, err)
|
||||
case err == v1alpha1.ErrAlertingRuleNotFound:
|
||||
ksapi.HandleNotFound(resp, nil, err)
|
||||
default:
|
||||
ksapi.HandleInternalError(resp, nil, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
if rule == nil {
|
||||
ksapi.HandleNotFound(resp, nil, err)
|
||||
return
|
||||
}
|
||||
resp.WriteEntity(rule)
|
||||
}
|
||||
|
||||
func (h *handler) handleListCustomSpecifiedRuleAlerts(req *restful.Request, resp *restful.Response) {
|
||||
namespace := req.PathParameter("namespace")
|
||||
ruleName := req.PathParameter("rule_name")
|
||||
|
||||
alerts, err := h.operator.ListCustomSpecifiedRuleAlerts(req.Request.Context(), namespace, ruleName)
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
switch {
|
||||
case err == v1alpha1.ErrThanosRulerNotEnabled:
|
||||
ksapi.HandleBadRequest(resp, nil, err)
|
||||
case err == v1alpha1.ErrAlertingRuleNotFound:
|
||||
ksapi.HandleNotFound(resp, nil, err)
|
||||
default:
|
||||
ksapi.HandleInternalError(resp, nil, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
resp.WriteEntity(alerts)
|
||||
}
|
||||
|
||||
func (h *handler) handleCreateCustomAlertingRule(req *restful.Request, resp *restful.Response) {
|
||||
namespace := req.PathParameter("namespace")
|
||||
|
||||
var rule v1alpha1.PostableAlertingRule
|
||||
if err := req.ReadEntity(&rule); err != nil {
|
||||
klog.Error(err)
|
||||
ksapi.HandleBadRequest(resp, nil, err)
|
||||
return
|
||||
}
|
||||
if err := rule.Validate(); err != nil {
|
||||
klog.Error(err)
|
||||
ksapi.HandleBadRequest(resp, nil, err)
|
||||
return
|
||||
}
|
||||
|
||||
err := h.operator.CreateCustomAlertingRule(req.Request.Context(), namespace, &rule)
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
switch {
|
||||
case err == v1alpha1.ErrThanosRulerNotEnabled:
|
||||
ksapi.HandleBadRequest(resp, nil, err)
|
||||
case err == v1alpha1.ErrAlertingRuleAlreadyExists:
|
||||
ksapi.HandleConflict(resp, nil, err)
|
||||
default:
|
||||
ksapi.HandleInternalError(resp, nil, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (h *handler) handleUpdateCustomAlertingRule(req *restful.Request, resp *restful.Response) {
|
||||
namespace := req.PathParameter("namespace")
|
||||
ruleName := req.PathParameter("rule_name")
|
||||
|
||||
var rule v1alpha1.PostableAlertingRule
|
||||
if err := req.ReadEntity(&rule); err != nil {
|
||||
klog.Error(err)
|
||||
ksapi.HandleBadRequest(resp, nil, err)
|
||||
return
|
||||
}
|
||||
if err := rule.Validate(); err != nil {
|
||||
klog.Error(err)
|
||||
ksapi.HandleBadRequest(resp, nil, err)
|
||||
return
|
||||
}
|
||||
|
||||
err := h.operator.UpdateCustomAlertingRule(req.Request.Context(), namespace, ruleName, &rule)
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
switch {
|
||||
case err == v1alpha1.ErrThanosRulerNotEnabled:
|
||||
ksapi.HandleBadRequest(resp, nil, err)
|
||||
case err == v1alpha1.ErrAlertingRuleNotFound:
|
||||
ksapi.HandleNotFound(resp, nil, err)
|
||||
default:
|
||||
ksapi.HandleInternalError(resp, nil, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (h *handler) handleDeleteCustomAlertingRule(req *restful.Request, resp *restful.Response) {
|
||||
namespace := req.PathParameter("namespace")
|
||||
name := req.PathParameter("rule_name")
|
||||
|
||||
err := h.operator.DeleteCustomAlertingRule(req.Request.Context(), namespace, name)
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
switch {
|
||||
case err == v1alpha1.ErrThanosRulerNotEnabled:
|
||||
ksapi.HandleBadRequest(resp, nil, err)
|
||||
case err == v1alpha1.ErrAlertingRuleNotFound:
|
||||
ksapi.HandleNotFound(resp, nil, err)
|
||||
default:
|
||||
ksapi.HandleInternalError(resp, nil, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (h *handler) handleListBuiltinAlertingRules(req *restful.Request, resp *restful.Response) {
|
||||
query, err := v1alpha1.ParseAlertingRuleQueryParams(req)
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
ksapi.HandleBadRequest(resp, nil, err)
|
||||
return
|
||||
}
|
||||
|
||||
rules, err := h.operator.ListBuiltinAlertingRules(req.Request.Context(), query)
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
ksapi.HandleInternalError(resp, nil, err)
|
||||
return
|
||||
}
|
||||
resp.WriteEntity(rules)
|
||||
}
|
||||
|
||||
func (h *handler) handleListBuiltinRulesAlerts(req *restful.Request, resp *restful.Response) {
|
||||
query, err := v1alpha1.ParseAlertQueryParams(req)
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
ksapi.HandleBadRequest(resp, nil, err)
|
||||
return
|
||||
}
|
||||
|
||||
alerts, err := h.operator.ListBuiltinRulesAlerts(req.Request.Context(), query)
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
ksapi.HandleInternalError(resp, nil, err)
|
||||
return
|
||||
}
|
||||
resp.WriteEntity(alerts)
|
||||
}
|
||||
|
||||
func (h *handler) handleGetBuiltinAlertingRule(req *restful.Request, resp *restful.Response) {
|
||||
ruleId := req.PathParameter("rule_id")
|
||||
|
||||
rule, err := h.operator.GetBuiltinAlertingRule(req.Request.Context(), ruleId)
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
switch {
|
||||
case err == v1alpha1.ErrAlertingRuleNotFound:
|
||||
ksapi.HandleNotFound(resp, nil, err)
|
||||
default:
|
||||
ksapi.HandleInternalError(resp, nil, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
if rule == nil {
|
||||
ksapi.HandleNotFound(resp, nil, err)
|
||||
return
|
||||
}
|
||||
|
||||
resp.WriteEntity(rule)
|
||||
}
|
||||
|
||||
func (h *handler) handleListBuiltinSpecifiedRuleAlerts(req *restful.Request, resp *restful.Response) {
|
||||
ruleId := req.PathParameter("rule_id")
|
||||
|
||||
alerts, err := h.operator.ListBuiltinSpecifiedRuleAlerts(req.Request.Context(), ruleId)
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
switch {
|
||||
case err == v1alpha1.ErrAlertingRuleNotFound:
|
||||
ksapi.HandleNotFound(resp, nil, err)
|
||||
default:
|
||||
ksapi.HandleInternalError(resp, nil, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
resp.WriteEntity(alerts)
|
||||
}
|
||||
199
pkg/kapis/customalerting/v1alpha1/register.go
Normal file
199
pkg/kapis/customalerting/v1alpha1/register.go
Normal file
@@ -0,0 +1,199 @@
|
||||
/*
|
||||
Copyright 2020 KubeSphere Authors
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package v1alpha1
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
promresourcesclient "github.com/prometheus-operator/prometheus-operator/pkg/client/versioned"
|
||||
"github.com/emicklei/go-restful"
|
||||
restfulspec "github.com/emicklei/go-restful-openapi"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
ksapi "kubesphere.io/kubesphere/pkg/api"
|
||||
customalertingv1alpha1 "kubesphere.io/kubesphere/pkg/api/customalerting/v1alpha1"
|
||||
"kubesphere.io/kubesphere/pkg/apiserver/runtime"
|
||||
"kubesphere.io/kubesphere/pkg/constants"
|
||||
"kubesphere.io/kubesphere/pkg/informers"
|
||||
"kubesphere.io/kubesphere/pkg/simple/client/customalerting"
|
||||
)
|
||||
|
||||
const (
|
||||
groupName = "custom.alerting.kubesphere.io"
|
||||
)
|
||||
|
||||
var GroupVersion = schema.GroupVersion{Group: groupName, Version: "v1alpha1"}
|
||||
|
||||
func AddToContainer(container *restful.Container, informers informers.InformerFactory,
|
||||
promResourceClient promresourcesclient.Interface, ruleClient customalerting.RuleClient,
|
||||
option *customalerting.Options) error {
|
||||
|
||||
handler := newHandler(informers, promResourceClient, ruleClient, option)
|
||||
|
||||
ws := runtime.NewWebService(GroupVersion)
|
||||
|
||||
ws.Route(ws.GET("/rules").
|
||||
To(handler.handleListCustomAlertingRules).
|
||||
Doc("list the cluster-level custom alerting rules").
|
||||
Param(ws.QueryParameter("name", "rule name")).
|
||||
Param(ws.QueryParameter("state", "state of a rule based on its alerts, one of `firing`, `pending`, `inactive`")).
|
||||
Param(ws.QueryParameter("health", "health state of a rule based on the last execution, one of `ok`, `err`, `unknown`")).
|
||||
Param(ws.QueryParameter("label_filters", "label filters, concatenating multiple filters with commas, equal symbol for exact query, wave symbol for fuzzy query e.g. name~a").DataFormat("key=%s,key~%s")).
|
||||
Param(ws.QueryParameter("sort_field", "sort field, one of `name`, `lastEvaluation`, `evaluationTime`")).
|
||||
Param(ws.QueryParameter("sort_type", "sort type, one of `asc`, `desc`")).
|
||||
Param(ws.QueryParameter("offset", "offset of the result set").DataType("integer").DefaultValue("0")).
|
||||
Param(ws.QueryParameter("limit", "limit size of the result set").DataType("integer").DefaultValue("10")).
|
||||
Returns(http.StatusOK, ksapi.StatusOK, customalertingv1alpha1.GettableAlertingRuleList{}).
|
||||
Metadata(restfulspec.KeyOpenAPITags, []string{constants.CustomAlertingTag}))
|
||||
|
||||
ws.Route(ws.GET("/alerts").
|
||||
To(handler.handleListCustomRulesAlerts).
|
||||
Doc("list the alerts of the cluster-level custom alerting rules").
|
||||
Param(ws.QueryParameter("state", "state, one of `firing`, `pending`, `inactive`")).
|
||||
Param(ws.QueryParameter("label_filters", "label filters, concatenating multiple filters with commas, equal symbol for exact query, wave symbol for fuzzy query e.g. name~a").DataFormat("key=%s,key~%s")).
|
||||
Param(ws.QueryParameter("offset", "offset of the result set").DataType("integer").DefaultValue("0")).
|
||||
Param(ws.QueryParameter("limit", "limit size of the result set").DataType("integer").DefaultValue("10")).
|
||||
Returns(http.StatusOK, ksapi.StatusOK, customalertingv1alpha1.AlertList{}).
|
||||
Metadata(restfulspec.KeyOpenAPITags, []string{constants.CustomAlertingTag}))
|
||||
|
||||
ws.Route(ws.GET("/rules/{rule_name}").
|
||||
To(handler.handleGetCustomAlertingRule).
|
||||
Doc("get the cluster-level custom alerting rule with the specified name").
|
||||
Returns(http.StatusOK, ksapi.StatusOK, customalertingv1alpha1.GettableAlertingRule{}).
|
||||
Metadata(restfulspec.KeyOpenAPITags, []string{constants.CustomAlertingTag}))
|
||||
|
||||
ws.Route(ws.GET("/rules/{rule_name}/alerts").
|
||||
To(handler.handleListCustomSpecifiedRuleAlerts).
|
||||
Doc("list the alerts of the cluster-level custom alerting rule with the specified name").
|
||||
Returns(http.StatusOK, ksapi.StatusOK, []customalertingv1alpha1.Alert{}).
|
||||
Metadata(restfulspec.KeyOpenAPITags, []string{constants.CustomAlertingTag}))
|
||||
|
||||
ws.Route(ws.POST("/rules").
|
||||
To(handler.handleCreateCustomAlertingRule).
|
||||
Doc("create a cluster-level custom alerting rule").
|
||||
Reads(customalertingv1alpha1.PostableAlertingRule{}).
|
||||
Returns(http.StatusOK, ksapi.StatusOK, nil).
|
||||
Metadata(restfulspec.KeyOpenAPITags, []string{constants.CustomAlertingTag}))
|
||||
|
||||
ws.Route(ws.PUT("/rules/{rule_name}").
|
||||
To(handler.handleUpdateCustomAlertingRule).
|
||||
Doc("update the cluster-level custom alerting rule with the specified name").
|
||||
Reads(customalertingv1alpha1.PostableAlertingRule{}).
|
||||
Returns(http.StatusOK, ksapi.StatusOK, nil).
|
||||
Metadata(restfulspec.KeyOpenAPITags, []string{constants.CustomAlertingTag}))
|
||||
|
||||
ws.Route(ws.DELETE("/rules/{rule_name}").
|
||||
To(handler.handleDeleteCustomAlertingRule).
|
||||
Doc("delete the cluster-level custom alerting rule with the specified name").
|
||||
Returns(http.StatusOK, ksapi.StatusOK, nil).
|
||||
Metadata(restfulspec.KeyOpenAPITags, []string{constants.CustomAlertingTag}))
|
||||
|
||||
ws.Route(ws.GET("/namespaces/{namespace}/rules").
|
||||
To(handler.handleListCustomAlertingRules).
|
||||
Doc("list the custom alerting rules in the specified namespace").
|
||||
Param(ws.QueryParameter("name", "rule name")).
|
||||
Param(ws.QueryParameter("state", "state of a rule based on its alerts, one of `firing`, `pending`, `inactive`")).
|
||||
Param(ws.QueryParameter("health", "health state of a rule based on the last execution, one of `ok`, `err`, `unknown`")).
|
||||
Param(ws.QueryParameter("label_filters", "label filters, concatenating multiple filters with commas, equal symbol for exact query, wave symbol for fuzzy query e.g. name~a").DataFormat("key=%s,key~%s")).
|
||||
Param(ws.QueryParameter("sort_field", "sort field, one of `name`, `lastEvaluation`, `evaluationTime`")).
|
||||
Param(ws.QueryParameter("sort_type", "sort type, one of `asc`, `desc`")).
|
||||
Param(ws.QueryParameter("offset", "offset of the result set").DataType("integer").DefaultValue("0")).
|
||||
Param(ws.QueryParameter("limit", "limit size of the result set").DataType("integer").DefaultValue("10")).
|
||||
Returns(http.StatusOK, ksapi.StatusOK, customalertingv1alpha1.GettableAlertingRuleList{}).
|
||||
Metadata(restfulspec.KeyOpenAPITags, []string{constants.CustomAlertingTag}))
|
||||
|
||||
ws.Route(ws.GET("/namespaces/{namespace}/alerts").
|
||||
To(handler.handleListCustomRulesAlerts).
|
||||
Doc("list the alerts of the custom alerting rules in the specified namespace.").
|
||||
Param(ws.QueryParameter("state", "state, one of `firing`, `pending`, `inactive`")).
|
||||
Param(ws.QueryParameter("label_filters", "label filters, concatenating multiple filters with commas, equal symbol for exact query, wave symbol for fuzzy query e.g. name~a").DataFormat("key=%s,key~%s")).
|
||||
Param(ws.QueryParameter("offset", "offset of the result set").DataType("integer").DefaultValue("0")).
|
||||
Param(ws.QueryParameter("limit", "limit size of the result set").DataType("integer").DefaultValue("10")).
|
||||
Returns(http.StatusOK, ksapi.StatusOK, customalertingv1alpha1.AlertList{}).
|
||||
Metadata(restfulspec.KeyOpenAPITags, []string{constants.CustomAlertingTag}))
|
||||
|
||||
ws.Route(ws.GET("/namespaces/{namespace}/rules/{rule_name}").
|
||||
To(handler.handleGetCustomAlertingRule).
|
||||
Doc("get the custom alerting rule with the specified name in the specified namespace").
|
||||
Returns(http.StatusOK, ksapi.StatusOK, customalertingv1alpha1.GettableAlertingRule{}).
|
||||
Metadata(restfulspec.KeyOpenAPITags, []string{constants.CustomAlertingTag}))
|
||||
|
||||
ws.Route(ws.GET("/namespaces/{namespace}/rules/{rule_name}/alerts").
|
||||
To(handler.handleListCustomSpecifiedRuleAlerts).
|
||||
Doc("get the alerts of the custom alerting rule with the specified name in the specified namespace").
|
||||
Returns(http.StatusOK, ksapi.StatusOK, []customalertingv1alpha1.Alert{}).
|
||||
Metadata(restfulspec.KeyOpenAPITags, []string{constants.CustomAlertingTag}))
|
||||
|
||||
ws.Route(ws.POST("/namespaces/{namespace}/rules").
|
||||
To(handler.handleCreateCustomAlertingRule).
|
||||
Doc("create a custom alerting rule in the specified namespace").
|
||||
Reads(customalertingv1alpha1.PostableAlertingRule{}).
|
||||
Returns(http.StatusOK, ksapi.StatusOK, "").
|
||||
Metadata(restfulspec.KeyOpenAPITags, []string{constants.CustomAlertingTag}))
|
||||
|
||||
ws.Route(ws.PUT("/namespaces/{namespace}/rules/{rule_name}").
|
||||
To(handler.handleUpdateCustomAlertingRule).
|
||||
Doc("update the custom alerting rule with the specified name in the specified namespace").
|
||||
Reads(customalertingv1alpha1.PostableAlertingRule{}).
|
||||
Returns(http.StatusOK, ksapi.StatusOK, "").
|
||||
Metadata(restfulspec.KeyOpenAPITags, []string{constants.CustomAlertingTag}))
|
||||
|
||||
ws.Route(ws.DELETE("/namespaces/{namespace}/rules/{rule_name}").
|
||||
To(handler.handleDeleteCustomAlertingRule).
|
||||
Doc("delete the custom alerting rule with the specified rule name in the specified namespace").
|
||||
Returns(http.StatusOK, ksapi.StatusOK, nil).
|
||||
Metadata(restfulspec.KeyOpenAPITags, []string{constants.CustomAlertingTag}))
|
||||
|
||||
ws.Route(ws.GET("/builtin/rules").
|
||||
To(handler.handleListBuiltinAlertingRules).
|
||||
Doc("list the builtin(non-custom) alerting rules").
|
||||
Param(ws.QueryParameter("name", "rule name")).
|
||||
Param(ws.QueryParameter("state", "state of a rule based on its alerts, one of `firing`, `pending`, `inactive`")).
|
||||
Param(ws.QueryParameter("health", "health state of a rule based on the last execution, one of `ok`, `err`, `unknown`")).
|
||||
Param(ws.QueryParameter("label_filters", "label filters, concatenating multiple filters with commas, equal symbol for exact query, wave symbol for fuzzy query e.g. name~a").DataFormat("key=%s,key~%s")).
|
||||
Param(ws.QueryParameter("sort_field", "sort field, one of `name`, `lastEvaluation`, `evaluationTime`")).
|
||||
Param(ws.QueryParameter("sort_type", "sort type, one of `asc`, `desc`")).
|
||||
Param(ws.QueryParameter("offset", "offset of the result set").DataType("integer").DefaultValue("0")).
|
||||
Param(ws.QueryParameter("limit", "limit size of the result set").DataType("integer").DefaultValue("10")).
|
||||
Returns(http.StatusOK, ksapi.StatusOK, customalertingv1alpha1.GettableAlertingRuleList{}).
|
||||
Metadata(restfulspec.KeyOpenAPITags, []string{constants.CustomAlertingTag}))
|
||||
|
||||
ws.Route(ws.GET("/builtin/alerts").
|
||||
To(handler.handleListBuiltinRulesAlerts).
|
||||
Doc("list the alerts of the builtin(non-custom) rules").
|
||||
Param(ws.QueryParameter("state", "state, one of `firing`, `pending`, `inactive`")).
|
||||
Param(ws.QueryParameter("label_filters", "label filters, concatenating multiple filters with commas, equal symbol for exact query, wave symbol for fuzzy query e.g. name~a").DataFormat("key=%s,key~%s")).
|
||||
Param(ws.QueryParameter("offset", "offset of the result set").DataType("integer").DefaultValue("0")).
|
||||
Param(ws.QueryParameter("limit", "limit size of the result set").DataType("integer").DefaultValue("10")).
|
||||
Returns(http.StatusOK, ksapi.StatusOK, customalertingv1alpha1.AlertList{}).
|
||||
Metadata(restfulspec.KeyOpenAPITags, []string{constants.CustomAlertingTag}))
|
||||
|
||||
ws.Route(ws.GET("/builtin/rules/{rule_id}").
|
||||
To(handler.handleGetBuiltinAlertingRule).
|
||||
Doc("get the builtin(non-custom) alerting rule with specified id").
|
||||
Returns(http.StatusOK, ksapi.StatusOK, customalertingv1alpha1.GettableAlertingRule{}).
|
||||
Metadata(restfulspec.KeyOpenAPITags, []string{constants.CustomAlertingTag}))
|
||||
|
||||
ws.Route(ws.GET("/builtin/rules/{rule_id}/alerts").
|
||||
To(handler.handleListBuiltinSpecifiedRuleAlerts).
|
||||
Doc("list the alerts of the builtin(non-custom) alerting rule with the specified id").
|
||||
Returns(http.StatusOK, ksapi.StatusOK, []customalertingv1alpha1.Alert{}).
|
||||
Metadata(restfulspec.KeyOpenAPITags, []string{constants.CustomAlertingTag}))
|
||||
|
||||
container.Add(ws)
|
||||
|
||||
return nil
|
||||
}
|
||||
147
pkg/kapis/customalerting/v1alpha1/test_test.go
Normal file
147
pkg/kapis/customalerting/v1alpha1/test_test.go
Normal file
@@ -0,0 +1,147 @@
|
||||
package v1alpha1
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/emicklei/go-restful"
|
||||
restfulspec "github.com/emicklei/go-restful-openapi"
|
||||
"github.com/go-openapi/spec"
|
||||
promresourcesclient "github.com/prometheus-operator/prometheus-operator/pkg/client/versioned"
|
||||
"github.com/prometheus/prometheus/promql/parser"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
"k8s.io/client-go/kubernetes"
|
||||
"k8s.io/client-go/tools/clientcmd"
|
||||
"kubesphere.io/kubesphere/pkg/apiserver/runtime"
|
||||
"kubesphere.io/kubesphere/pkg/constants"
|
||||
"kubesphere.io/kubesphere/pkg/informers"
|
||||
"kubesphere.io/kubesphere/pkg/simple/client/customalerting"
|
||||
)
|
||||
|
||||
func TestTest(t *testing.T) {
|
||||
expr := `increase((max by(job) (etcd_server_leader_changes_seen_total{job=~".*etcd.*"}) or 0 * absent(etcd_server_leader_changes_seen_total{job=~".*etcd.*"}))[15m:1m]) >= 3`
|
||||
//expr = `absent_over_time(sum(nonexistent{job="myjob"})[1h:])`
|
||||
|
||||
pexpr, err := parser.ParseExpr(expr)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Println(pexpr.String())
|
||||
}
|
||||
|
||||
func TestApis(t *testing.T) {
|
||||
kubeconfig := "D:/ks/conf/ks3-config"
|
||||
|
||||
config, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
k8sClient := kubernetes.NewForConfigOrDie(config)
|
||||
promResourcesClient := promresourcesclient.NewForConfigOrDie(config)
|
||||
|
||||
option := &customalerting.Options{
|
||||
PrometheusEndpoint: "http://139.198.112.79:39090/",
|
||||
ThanosRulerEndpoint: "http://139.198.112.79:39091/",
|
||||
ThanosRuleResourceLabels: "role=thanos-alerting-rules,thanosruler:thanos-ruler",
|
||||
}
|
||||
ruleClient, err := customalerting.NewRuleClient(option)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
stopCh := make(chan struct{})
|
||||
|
||||
informerFactory := informers.NewInformerFactories(k8sClient, nil, nil, nil, nil, promResourcesClient)
|
||||
k8sGVRs := []schema.GroupVersionResource{
|
||||
{Group: "", Version: "v1", Resource: "namespaces"},
|
||||
}
|
||||
for _, gvr := range k8sGVRs {
|
||||
_, err = informerFactory.KubernetesSharedInformerFactory().ForResource(gvr)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
prometheusGVRs := []schema.GroupVersionResource{
|
||||
{Group: "monitoring.coreos.com", Version: "v1", Resource: "prometheuses"},
|
||||
{Group: "monitoring.coreos.com", Version: "v1", Resource: "prometheusrules"},
|
||||
{Group: "monitoring.coreos.com", Version: "v1", Resource: "thanosrulers"},
|
||||
}
|
||||
for _, gvr := range prometheusGVRs {
|
||||
_, err = informerFactory.PrometheusSharedInformerFactory().ForResource(gvr)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
informerFactory.Start(stopCh)
|
||||
informerFactory.KubernetesSharedInformerFactory().WaitForCacheSync(stopCh)
|
||||
informerFactory.PrometheusSharedInformerFactory().WaitForCacheSync(stopCh)
|
||||
informerFactory.Start(stopCh)
|
||||
|
||||
container := restful.NewContainer()
|
||||
AddToContainer(container, informerFactory, promResourcesClient, ruleClient, option)
|
||||
server := &http.Server{}
|
||||
server.Handler = container
|
||||
if err := server.ListenAndServe(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGenSwaggerJson(t *testing.T) {
|
||||
container := runtime.Container
|
||||
|
||||
informerFactory := informers.NewNullInformerFactory()
|
||||
|
||||
AddToContainer(container, informerFactory, nil, nil, nil)
|
||||
|
||||
swagger := restfulspec.BuildSwagger(restfulspec.Config{
|
||||
WebServices: container.RegisteredWebServices(),
|
||||
PostBuildSwaggerObjectHandler: enrichSwaggerObject,
|
||||
})
|
||||
|
||||
swagger.Info.Extensions = make(spec.Extensions)
|
||||
swagger.Info.Extensions.Add("x-tagGroups", []struct {
|
||||
Name string `json:"name"`
|
||||
Tags []string `json:"tags"`
|
||||
}{
|
||||
{
|
||||
Name: "Custom Alerting",
|
||||
Tags: []string{constants.CustomAlertingTag},
|
||||
},
|
||||
})
|
||||
|
||||
enc := json.NewEncoder(os.Stdout)
|
||||
enc.SetIndent("", "\t")
|
||||
enc.Encode(swagger)
|
||||
}
|
||||
|
||||
func enrichSwaggerObject(swo *spec.Swagger) {
|
||||
swo.Info = &spec.Info{
|
||||
InfoProps: spec.InfoProps{
|
||||
Title: "KubeSphere",
|
||||
Description: "KubeSphere OpenAPI",
|
||||
Contact: &spec.ContactInfo{
|
||||
ContactInfoProps: spec.ContactInfoProps{
|
||||
Name: "KubeSphere",
|
||||
URL: "https://kubesphere.io/",
|
||||
Email: "kubesphere@yunify.com",
|
||||
},
|
||||
},
|
||||
License: &spec.License{
|
||||
LicenseProps: spec.LicenseProps{
|
||||
Name: "Apache 2.0",
|
||||
URL: "https://www.apache.org/licenses/LICENSE-2.0.html",
|
||||
},
|
||||
},
|
||||
Version: "0.1.0",
|
||||
}}
|
||||
|
||||
// setup security definitions
|
||||
swo.SecurityDefinitions = map[string]*spec.SecurityScheme{
|
||||
"jwt": spec.APIKeyAuth("Authorization", "header"),
|
||||
}
|
||||
swo.Security = []map[string][]string{{"jwt": []string{}}}
|
||||
}
|
||||
Reference in New Issue
Block a user