Skip to content

Commit

Permalink
Merge pull request #16 from pf93/feature-keruntime-monitor-edge-app
Browse files Browse the repository at this point in the history
MIKU-40 Feature keruntime monitor edge app
  • Loading branch information
qidu authored Aug 17, 2023
2 parents 4369cba + 7e5c3c0 commit 4a10ed1
Show file tree
Hide file tree
Showing 5 changed files with 67 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
31 changes: 31 additions & 0 deletions edge/pkg/appsd/model/err.go
Original file line number Diff line number Diff line change
@@ -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")
)
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 4a10ed1

Please sign in to comment.