Merge pull request #3356 from wanjunlei/nm

support customize notification receiver
This commit is contained in:
KubeSphere CI Bot
2021-03-12 16:53:01 +08:00
committed by GitHub
65 changed files with 8900 additions and 2 deletions

View File

@@ -0,0 +1,149 @@
/*
Copyright 2020 The 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 v2beta1
import (
"github.com/emicklei/go-restful"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/client-go/kubernetes"
"k8s.io/klog"
"kubesphere.io/kubesphere/pkg/api"
"kubesphere.io/kubesphere/pkg/apiserver/query"
kubesphere "kubesphere.io/kubesphere/pkg/client/clientset/versioned"
"kubesphere.io/kubesphere/pkg/informers"
"kubesphere.io/kubesphere/pkg/models/notification"
servererr "kubesphere.io/kubesphere/pkg/server/errors"
)
type handler struct {
operator notification.Operator
}
func newNotificationHandler(
informers informers.InformerFactory,
k8sClient kubernetes.Interface,
ksClient kubesphere.Interface) *handler {
return &handler{
operator: notification.NewOperator(informers, k8sClient, ksClient),
}
}
func (h *handler) ListResource(req *restful.Request, resp *restful.Response) {
user := req.PathParameter("user")
resource := req.PathParameter("resources")
subresource := req.QueryParameter("type")
q := query.ParseQueryParameter(req)
if !h.operator.IsKnownResource(resource, subresource) {
api.HandleBadRequest(resp, req, servererr.New("unknown resource type %s/%s", resource, subresource))
return
}
objs, err := h.operator.List(user, resource, subresource, q)
handleResponse(req, resp, objs, err)
}
func (h *handler) GetResource(req *restful.Request, resp *restful.Response) {
user := req.PathParameter("user")
resource := req.PathParameter("resources")
name := req.PathParameter("name")
subresource := req.QueryParameter("type")
if !h.operator.IsKnownResource(resource, subresource) {
api.HandleBadRequest(resp, req, servererr.New("unknown resource type %s/%s", resource, subresource))
return
}
obj, err := h.operator.Get(user, resource, name, subresource)
handleResponse(req, resp, obj, err)
}
func (h *handler) CreateResource(req *restful.Request, resp *restful.Response) {
user := req.PathParameter("user")
resource := req.PathParameter("resources")
if !h.operator.IsKnownResource(resource, "") {
api.HandleBadRequest(resp, req, servererr.New("unknown resource type %s", resource))
return
}
obj := h.operator.GetObject(resource)
if err := req.ReadEntity(obj); err != nil {
api.HandleBadRequest(resp, req, err)
return
}
created, err := h.operator.Create(user, resource, obj)
handleResponse(req, resp, created, err)
}
func (h *handler) UpdateResource(req *restful.Request, resp *restful.Response) {
user := req.PathParameter("user")
resource := req.PathParameter("resources")
name := req.PathParameter("name")
if !h.operator.IsKnownResource(resource, "") {
api.HandleBadRequest(resp, req, servererr.New("unknown resource type %s", resource))
return
}
obj := h.operator.GetObject(resource)
if err := req.ReadEntity(obj); err != nil {
api.HandleBadRequest(resp, req, err)
return
}
updated, err := h.operator.Update(user, resource, name, obj)
handleResponse(req, resp, updated, err)
}
func (h *handler) DeleteResource(req *restful.Request, resp *restful.Response) {
user := req.PathParameter("user")
resource := req.PathParameter("resources")
name := req.PathParameter("name")
if !h.operator.IsKnownResource(resource, "") {
api.HandleBadRequest(resp, req, servererr.New("unknown resource type %s", resource))
return
}
handleResponse(req, resp, servererr.None, h.operator.Delete(user, resource, name))
}
func handleResponse(req *restful.Request, resp *restful.Response, obj interface{}, err error) {
if err != nil {
klog.Error(err)
if errors.IsNotFound(err) {
api.HandleNotFound(resp, req, err)
return
} else if errors.IsConflict(err) {
api.HandleConflict(resp, req, err)
return
}
api.HandleBadRequest(resp, req, err)
return
}
_ = resp.WriteEntity(obj)
}

View File

@@ -0,0 +1,153 @@
/*
Copyright 2020 The 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 v2beta1
import (
"github.com/emicklei/go-restful"
openapi "github.com/emicklei/go-restful-openapi"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/kubernetes"
"kubesphere.io/kubesphere/pkg/api"
"kubesphere.io/kubesphere/pkg/apiserver/query"
"kubesphere.io/kubesphere/pkg/apiserver/runtime"
kubesphere "kubesphere.io/kubesphere/pkg/client/clientset/versioned"
"kubesphere.io/kubesphere/pkg/constants"
"kubesphere.io/kubesphere/pkg/informers"
"kubesphere.io/kubesphere/pkg/server/errors"
"net/http"
)
const (
GroupName = "notification.kubesphere.io"
KeyOpenAPITags = openapi.KeyOpenAPITags
)
var GroupVersion = schema.GroupVersion{Group: GroupName, Version: "v2beta1"}
func AddToContainer(
container *restful.Container,
informers informers.InformerFactory,
k8sClient kubernetes.Interface,
ksClient kubesphere.Interface) error {
ws := runtime.NewWebService(GroupVersion)
h := newNotificationHandler(informers, k8sClient, ksClient)
// apis for global notification config, receiver, and secret
ws.Route(ws.GET("/{resources}").
To(h.ListResource).
Doc("list the notification configs or receivers").
Metadata(KeyOpenAPITags, []string{constants.NotificationTag}).
Param(ws.PathParameter("resources", "known values include configs, receivers, secrets")).
Param(ws.QueryParameter(query.ParameterName, "name used for filtering").Required(false)).
Param(ws.QueryParameter(query.ParameterLabelSelector, "label selector used for filtering").Required(false)).
Param(ws.QueryParameter("type", "config or receiver type, known values include dingtalk, email, slack, webhook, wechat").Required(false)).
Param(ws.QueryParameter(query.ParameterPage, "page").Required(false).DataFormat("page=%d").DefaultValue("page=1")).
Param(ws.QueryParameter(query.ParameterLimit, "limit").Required(false)).
Param(ws.QueryParameter(query.ParameterAscending, "sort parameters, e.g. ascending=false").Required(false).DefaultValue("ascending=false")).
Param(ws.QueryParameter(query.ParameterOrderBy, "sort parameters, e.g. orderBy=createTime")).
Returns(http.StatusOK, api.StatusOK, api.ListResult{Items: []interface{}{}}))
ws.Route(ws.GET("/{resources}/{name}").
To(h.GetResource).
Doc("get the specified notification config or receiver").
Metadata(KeyOpenAPITags, []string{constants.NotificationTag}).
Param(ws.PathParameter("resources", "known values include configs, receivers, secrets")).
Param(ws.PathParameter(query.ParameterName, "the name of the resource")).
Param(ws.QueryParameter("type", "config or receiver type, known values include dingtalk, email, slack, webhook, wechat").Required(false)).
Returns(http.StatusOK, api.StatusOK, nil))
ws.Route(ws.POST("/{resources}").
To(h.CreateResource).
Doc("create a notification config or receiver").
Metadata(KeyOpenAPITags, []string{constants.NotificationTag}).
Param(ws.PathParameter("resources", "known values include configs, receivers, secrets")).
Returns(http.StatusOK, api.StatusOK, nil))
ws.Route(ws.PUT("/{resources}/{name}").
To(h.UpdateResource).
Doc("update the specified notification config or receiver").
Metadata(KeyOpenAPITags, []string{constants.NotificationTag}).
Param(ws.PathParameter("resources", "known values include configs, receivers, secrets")).
Param(ws.PathParameter(query.ParameterName, "the name of the resource")).
Returns(http.StatusOK, api.StatusOK, nil))
ws.Route(ws.DELETE("/{resources}/{name}").
To(h.DeleteResource).
Doc("delete the specified notification config or receiver").
Metadata(KeyOpenAPITags, []string{constants.NotificationTag}).
Param(ws.PathParameter("resources", "known values include configs, receivers, secrets")).
Param(ws.PathParameter(query.ParameterName, "the name of the resource")).
Returns(http.StatusOK, api.StatusOK, errors.None))
// apis for tenant notification config and receiver
ws.Route(ws.GET("/users/{user}/{resources}").
To(h.ListResource).
Doc("list the notification configs or receivers").
Metadata(KeyOpenAPITags, []string{constants.NotificationTag}).
Param(ws.PathParameter("user", "user name")).
Param(ws.PathParameter("resources", "known values include configs, receivers, secrets")).
Param(ws.QueryParameter(query.ParameterName, "name used for filtering").Required(false)).
Param(ws.QueryParameter(query.ParameterLabelSelector, "label selector used for filtering").Required(false)).
Param(ws.QueryParameter("type", "config or receiver type, known values include dingtalk, email, slack, webhook, wechat").Required(false)).
Param(ws.QueryParameter(query.ParameterPage, "page").Required(false).DataFormat("page=%d").DefaultValue("page=1")).
Param(ws.QueryParameter(query.ParameterLimit, "limit").Required(false)).
Param(ws.QueryParameter(query.ParameterAscending, "sort parameters, e.g. ascending=false").Required(false).DefaultValue("ascending=false")).
Param(ws.QueryParameter(query.ParameterOrderBy, "sort parameters, e.g. orderBy=createTime")).
Returns(http.StatusOK, api.StatusOK, api.ListResult{Items: []interface{}{}}))
ws.Route(ws.GET("/users/{user}/{resources}/{name}").
To(h.GetResource).
Doc("get the specified notification config or receiver").
Metadata(KeyOpenAPITags, []string{constants.NotificationTag}).
Param(ws.PathParameter("user", "user name")).
Param(ws.PathParameter("resources", "known values include configs, receivers, secrets")).
Param(ws.PathParameter(query.ParameterName, "the name of the resource")).
Param(ws.QueryParameter("type", "config or receiver type, known values include dingtalk, email, slack, webhook, wechat").Required(false)).
Returns(http.StatusOK, api.StatusOK, nil))
ws.Route(ws.POST("/users/{user}/{resources}").
To(h.CreateResource).
Doc("create the specified notification config or receiver").
Metadata(KeyOpenAPITags, []string{constants.NotificationTag}).
Param(ws.PathParameter("user", "user name")).
Param(ws.PathParameter("resources", "known values include configs, receivers, secrets")).
Returns(http.StatusOK, api.StatusOK, nil))
ws.Route(ws.PUT("/users/{user}/{resources}/{name}").
To(h.UpdateResource).
Doc("update the specified notification config or receiver").
Metadata(KeyOpenAPITags, []string{constants.NotificationTag}).
Param(ws.PathParameter("user", "user name")).
Param(ws.PathParameter("resources", "known values include configs, receivers, secrets")).
Param(ws.PathParameter(query.ParameterName, "the name of the resource")).
Returns(http.StatusOK, api.StatusOK, nil))
ws.Route(ws.DELETE("/users/{user}/{resources}/{name}").
To(h.DeleteResource).
Doc("delete the specified notification config or receiver").
Metadata(KeyOpenAPITags, []string{constants.NotificationTag}).
Param(ws.PathParameter("user", "user name")).
Param(ws.PathParameter("resources", "known values include configs, receivers, secrets")).
Param(ws.PathParameter(query.ParameterName, "the name of the resource")).
Returns(http.StatusOK, api.StatusOK, errors.None))
container.Add(ws)
return nil
}