Skip to content

Commit

Permalink
modify manage app by supervisord and appsd http error code response
Browse files Browse the repository at this point in the history
  • Loading branch information
codefarmer009 committed Aug 17, 2023
1 parent e7c4fe3 commit 7e5c3c0
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 26 deletions.
1 change: 1 addition & 0 deletions common/constants/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ const (
CurrentSupportK8sVersion = "v1.24.14"

AppType = "appType"
AppName = "appName"
Pod = "pod"
Native = "native"

Expand Down
25 changes: 16 additions & 9 deletions edge/pkg/appsd/appsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()
Expand All @@ -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
}

Expand All @@ -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) {
Expand Down Expand Up @@ -397,14 +398,20 @@ 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)
return "", err
}
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
}
Expand Down
28 changes: 14 additions & 14 deletions edge/pkg/appsd/util/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

Expand Down
8 changes: 5 additions & 3 deletions edge/pkg/edged/edged.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit 7e5c3c0

Please sign in to comment.