From e7c4fe3f1d5edf17e5319dea8e3e666952cc10ad Mon Sep 17 00:00:00 2001 From: panfeng Date: Thu, 17 Aug 2023 18:34:33 +0800 Subject: [PATCH 1/2] add error code definition --- edge/pkg/appsd/model/err.go | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 edge/pkg/appsd/model/err.go diff --git a/edge/pkg/appsd/model/err.go b/edge/pkg/appsd/model/err.go new file mode 100644 index 00000000000..39ad1c0a67e --- /dev/null +++ b/edge/pkg/appsd/model/err.go @@ -0,0 +1,31 @@ +package model + +import "fmt" + +type Error struct { + Status int `json:"-"` + Code string `json:"code"` + Message string `json:"msg"` +} + +func (e *Error) Error() string { + return fmt.Sprintf("error:status=%v code=%s, message=%s", e.Status, e.Code, e.Message) +} + +func New(status int, code, message string) *Error { + return &Error{ + Status: status, + Code: code, + Message: message, + } +} + +var ( + Success = New(200, "1000", "Success") + ErrInvalidParam = New(400, "1002", "Invalid parameter") + ErrCertEmpty = New(403, "1112", "Domain has no cert") + ErrRequestMethod = New(405, "1113", "Request method error") + ErrInternalServer = New(500, "1001", "Internal server error") + ErrJsonUnmarshal = New(500, "1107", "Json unmarshal error") + ErrFormatResponse = New(500, "1108", "Format http response error") +) \ No newline at end of file From 7e5c3c0e692d0258f5daea1e03e0f3cecd8c7dfd Mon Sep 17 00:00:00 2001 From: panfeng Date: Thu, 17 Aug 2023 18:40:33 +0800 Subject: [PATCH 2/2] modify manage app by supervisord and appsd http error code response --- common/constants/default.go | 1 + edge/pkg/appsd/appsd.go | 25 ++++++++++++++++--------- edge/pkg/appsd/util/http.go | 28 ++++++++++++++-------------- edge/pkg/edged/edged.go | 8 +++++--- 4 files changed, 36 insertions(+), 26 deletions(-) diff --git a/common/constants/default.go b/common/constants/default.go index d84f3b07fd9..bbc273192d2 100644 --- a/common/constants/default.go +++ b/common/constants/default.go @@ -90,6 +90,7 @@ const ( CurrentSupportK8sVersion = "v1.24.14" AppType = "appType" + AppName = "appName" Pod = "pod" Native = "native" diff --git a/edge/pkg/appsd/appsd.go b/edge/pkg/appsd/appsd.go index ff10fce07fa..947e2d9884a 100644 --- a/edge/pkg/appsd/appsd.go +++ b/edge/pkg/appsd/appsd.go @@ -41,6 +41,7 @@ import ( appsdconfig "github.com/kubeedge/kubeedge/edge/pkg/appsd/config" edgedconfig "github.com/kubeedge/kubeedge/edge/pkg/edged/config" "github.com/kubeedge/kubeedge/edge/pkg/appsd/util" + appsdmodel "github.com/kubeedge/kubeedge/edge/pkg/appsd/model" "github.com/kubeedge/kubeedge/edge/pkg/common/message" "github.com/kubeedge/kubeedge/edge/pkg/common/modules" "github.com/kubeedge/kubeedge/pkg/apis/componentconfig/edgecore/v1alpha2" @@ -148,7 +149,7 @@ func server(stopChan <-chan struct{}) { func queryConfigHandler(w http.ResponseWriter, req *http.Request) { if req.Method != http.MethodGet { msg := "only support get request method" - util.ResponseError(http.StatusBadRequest, msg, w) + util.ResponseError(w, msg, appsdmodel.ErrRequestMethod) return } query := req.URL.Query() @@ -157,29 +158,29 @@ func queryConfigHandler(w http.ResponseWriter, req *http.Request) { domain := query.Get("domain") if configType == "" { msg := "request param must have type field, the value is configmap or secert" - util.ResponseError(http.StatusBadRequest, msg, w) + util.ResponseError(w, msg, appsdmodel.ErrInvalidParam) return } if domain == "" && appName == "" { msg := "request param must have appname, when query domain cert also includes domain" - util.ResponseError(http.StatusBadRequest, msg, w) + util.ResponseError(w, msg, appsdmodel.ErrInvalidParam) return } responseMessage, err := queryConfigFromMetaManager(configType, appName, domain) if err != nil { - util.ResponseError(http.StatusBadRequest, err.Error(), w) + util.ResponseError(w, err.Error(), appsdmodel.ErrInternalServer) return } resp, err := responseMessage.GetContentData() if err != nil { - util.ResponseError(http.StatusInternalServerError, err.Error(), w) + util.ResponseError(w, err.Error(), appsdmodel.ErrInternalServer) return } var data []string err = json.Unmarshal(resp, &data) if err != nil { - util.ResponseError(http.StatusInternalServerError, err.Error(), w) + util.ResponseError(w, err.Error(), appsdmodel.ErrJsonUnmarshal) return } @@ -193,11 +194,11 @@ func queryConfigHandler(w http.ResponseWriter, req *http.Request) { klog.Errorf("configType is not configmap or secret: configType is %s", configType) } if err != nil { - util.ResponseError(http.StatusInternalServerError, err.Error(), w) + util.ResponseError(w, err.Error(), appsdmodel.ErrFormatResponse) return } - util.ResponseSuccess(respData, w) + util.ResponseSuccess(w, respData) } func (a *appsd) handleApp(msg *model.Message) { @@ -397,6 +398,11 @@ func getNativeAppConfig(appName, configKey string) (string, error) { klog.Errorf("unmarshal data failed: %v", err) return "", err } + // It is also allowed that the app config is not created by using configmap, just use local config + if data == nil || len(data) == 0 { + klog.Warning("the native app config is not created by using configmap, will use local config") + return "", nil + } appConfigs, err := formatConfigmapResp(data) if err != nil { klog.Errorf("format configmap resp failed: %v", err) @@ -404,7 +410,8 @@ func getNativeAppConfig(appName, configKey string) (string, error) { } configItem, ok := appConfigs[configKey] if !ok { - return "", fmt.Errorf("cannot find config: %v", configKey) + klog.Warning("the native app supervisor config is not created by using configmap, will use local config") + return "", nil } return configItem, nil } diff --git a/edge/pkg/appsd/util/http.go b/edge/pkg/appsd/util/http.go index 97ae80a9d15..983ec105157 100644 --- a/edge/pkg/appsd/util/http.go +++ b/edge/pkg/appsd/util/http.go @@ -3,33 +3,33 @@ package util import ( "encoding/json" "net/http" -) -const ( - SUCCESS = "success" + appsdmodel "github.com/kubeedge/kubeedge/edge/pkg/appsd/model" ) type serverResponse struct { - Code int `json:"code"` - Msg string `json:"msg"` - Body interface{} `json:"body"` + Code string `json:"code"` + Message string `json:"message"` + Body interface{} `json:"body"` } -func ResponseError(code int, msg string, w http.ResponseWriter) { +func ResponseError(w http.ResponseWriter, msg string, err *appsdmodel.Error) { resp := serverResponse{ - Code: code, - Msg: msg, - Body: nil, + Code: err.Code, + Message: msg, + Body: nil, } + w.WriteHeader(err.Status) w.Write(marshalResult(&resp)) } -func ResponseSuccess(data interface{}, w http.ResponseWriter) { +func ResponseSuccess(w http.ResponseWriter, data interface{}) { resp := serverResponse{ - Code: http.StatusOK, - Msg: SUCCESS, - Body: data, + Code: appsdmodel.Success.Code, + Message: appsdmodel.Success.Message, + Body: data, } + w.WriteHeader(http.StatusOK) w.Write(marshalResult(&resp)) } diff --git a/edge/pkg/edged/edged.go b/edge/pkg/edged/edged.go index 69512f097e1..20113e23285 100644 --- a/edge/pkg/edged/edged.go +++ b/edge/pkg/edged/edged.go @@ -293,14 +293,16 @@ func (e *edged) handlePod(op string, content []byte, updatesChan chan<- interfac var info *model.Message if filterPodByNodeName(&pod, e.nodeName) { labels := pods[0].ObjectMeta.Labels - appType := "" + appType, appName := "", "" if labels != nil { if val, ok := labels[constants.AppType]; ok { appType = val } + if val, ok := labels[constants.AppName]; ok { + appName = val + } } - if appType == constants.Native && pods[0].Spec.Containers[0].Args != nil && - len(pods[0].Spec.Containers[0].Args) > 0 { + if appType == constants.Native && appName != "" { switch op { case model.InsertOperation: klog.V(4).InfoS("Receive message of adding new pods", "pods", klog.KObjs(pods))