Files
kubesphere/pkg/kapis/application/v2/errors.go
inksnw bac3bc62f9 Support manual triggering of a repository update. (#6280)
* Support manual triggering of a repository update.

* cherry pick add api for workload template (#1982)

* cherry pick (add operator application (#1970))

* Modify routing implementation to improve readability

* cherry pick from kse dfc40e5adf5aa2e67d1

* Filter by Routing Parameter Namespace (#1990)

* add doc for workloadtemplates
2024-11-25 10:56:35 +08:00

71 lines
1.9 KiB
Go

/*
* Please refer to the LICENSE file in the root directory of the project.
* https://github.com/kubesphere/kubesphere/blob/master/LICENSE
*/
package v2
import (
"fmt"
goruntime "runtime"
"github.com/emicklei/go-restful/v3"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/klog/v2"
"sigs.k8s.io/controller-runtime/pkg/client"
runtimeclient "sigs.k8s.io/controller-runtime/pkg/client"
"kubesphere.io/kubesphere/pkg/api"
)
func (h *appHandler) conflictedDone(req *restful.Request, resp *restful.Response, pathParam string, obj client.Object) bool {
existed, err := h.checkConflicted(req, pathParam, obj)
if err != nil {
api.HandleInternalError(resp, nil, err)
return true
}
if existed {
kind := obj.GetObjectKind().GroupVersionKind().Kind
api.HandleConflict(resp, req, fmt.Errorf("%s %s already exists", kind, obj.GetName()))
return true
}
return false
}
func (h *appHandler) checkConflicted(req *restful.Request, pathParam string, obj client.Object) (bool, error) {
key := runtimeclient.ObjectKey{Name: obj.GetName(), Namespace: obj.GetNamespace()}
if req.PathParameter(pathParam) != "" {
//if route like /repos/{repo} and request path has repo, then it's update, filling obj
err := h.client.Get(req.Request.Context(), key, obj)
if err != nil {
return false, err
}
return false, nil
}
//if route like /repos, then it's create
err := h.client.Get(req.Request.Context(), key, obj)
if err != nil && apierrors.IsNotFound(err) {
return false, nil
}
if err != nil {
return false, err
}
return true, nil
}
func requestDone(err error, resp *restful.Response) bool {
_, file, line, _ := goruntime.Caller(1)
if err != nil {
if apierrors.IsNotFound(err) {
api.HandleNotFound(resp, nil, err)
return true
}
klog.Errorf("%s:%d request done with error: %v", file, line, err)
api.HandleInternalError(resp, nil, err)
return true
}
return false
}