robocanic commented on code in PR #1353:
URL: https://github.com/apache/dubbo-admin/pull/1353#discussion_r2505955403
##########
pkg/console/service/application.go:
##########
@@ -267,3 +273,400 @@ func buildApplicationSearchResp(appResource
*meshresource.ApplicationResource, m
RegistryClusters: []string{mesh},
}
}
+
+func isAppAccessLogConfig(conf *meshproto.OverrideConfig, appName string) bool
{
+ if conf.Side != consts.SideProvider ||
+ conf.Parameters == nil ||
+ conf.Match == nil ||
+ conf.Match.Application == nil ||
+ conf.Match.Application.Oneof == nil ||
+ len(conf.Match.Application.Oneof) != 1 ||
+ conf.Match.Application.Oneof[0].Exact != appName {
+ return false
+ } else if _, ok := conf.Parameters[`accesslog`]; !ok {
+ return false
+ }
+ return true
+}
+
+func UpInsertAppAccessLog(ctx consolectx.Context, appName string,
openAccessLog bool, mesh string) error {
+ // check app exists
+ data, err := GetApplicationDetail(ctx,
&model.ApplicationDetailReq{AppName: appName})
+ if err != nil {
+ return err
+ }
+ if data == nil {
+ return bizerror.NewBizError(bizerror.AppNotFound,
fmt.Sprintf("%s is not exists", appName))
+ }
+ // check app configurator exists
+ appConfiguratorName := appName + consts.ConfiguratorRuleSuffix
+ res, err := GetConfigurator(ctx, appConfiguratorName, mesh)
+ if err != nil {
+ return err
+ }
+ // if not exists, create one configurator with access log enable
+ if res == nil {
+ return insertConfiguratorWithAccessLog(ctx, res, openAccessLog,
appConfiguratorName, appName, mesh)
+ }
+ // else we update the configurator
+ return updateConfiguratorWithAccessLog(ctx, res, openAccessLog,
appConfiguratorName, appName, mesh)
+}
+
+func insertConfiguratorWithAccessLog(ctx consolectx.Context, res
*meshresource.DynamicConfigResource, openAccessLog bool,
+ appConfiguratorName, appName, mesh string) error {
+ // configurator is nil, accessLog is already closed
+ if !openAccessLog {
+ return nil
+ }
+ res =
meshresource.NewDynamicConfigResourceWithAttributes(appConfiguratorName, mesh)
+ res.Spec = &meshproto.DynamicConfig{
+ Key: appName,
+ Scope: consts.ScopeApplication,
+ ConfigVersion: consts.ConfiguratorVersionV3,
+ Enabled: true,
+ Configs: make([]*meshproto.OverrideConfig, 0),
+ }
+ res.Spec.Configs = append(res.Spec.Configs,
newAccessLogEnabledConfig(appName))
+ err := CreateConfigurator(ctx, appConfiguratorName, res)
+ if err != nil {
+ logger.Errorf("create configurator failed when open accesslog,
resourceKey: %s, openAccessLog: %t, err: %s",
+ coremodel.BuildResourceKey(mesh, appName),
openAccessLog, err)
+ return err
+ }
+ return nil
+}
+
+func updateConfiguratorWithAccessLog(ctx consolectx.Context, res
*meshresource.DynamicConfigResource, openAccessLog bool,
+ appConfiguratorName, appName, mesh string) error {
+ var accessLogConfig *meshproto.OverrideConfig
+ res.Spec.RangeConfig(func(conf *meshproto.OverrideConfig) (isStop bool)
{
+ if isAppAccessLogConfig(conf, appName) {
+ accessLogConfig = conf
+ return true
+ }
+ return false
+ })
+ // access log config not found
+ if accessLogConfig == nil {
+ // access log is need to be closed and already closed
+ if !openAccessLog {
+ return nil
+ }
+ // insert a access log enabled config
+ res.Spec.Configs = append(res.Spec.Configs,
newAccessLogEnabledConfig(appName))
+ } else {
+ // access log config found and status is the same as needed
+ if accessLogConfig.Enabled == openAccessLog {
+ return nil
+ }
+ // update the access log enabled status as needed
+ accessLogConfig.Enabled = openAccessLog
+ }
+ err := UpdateConfigurator(ctx, appConfiguratorName, res)
+ if err != nil {
+ logger.Errorf("update configurator failed when open accesslog,
resourceKey: %s, openAccessLog: %t, err: %s",
+ coremodel.BuildResourceKey(mesh, appName),
openAccessLog, err)
+ return err
+ }
+ return nil
+}
+
+func newAccessLogEnabledConfig(appName string) *meshproto.OverrideConfig {
+ return &meshproto.OverrideConfig{
+ Side: consts.SideProvider,
+ Parameters: map[string]string{`accesslog`: `true`},
+ Enabled: true,
+ Match: &meshproto.ConditionMatch{
+ Application: &meshproto.ListStringMatch{
+ Oneof: []*meshproto.StringMatch{
+ {
+ Exact: appName,
+ },
+ }}},
+ XGenerateByCp: true,
+ }
+}
+
+func GetAppAccessLog(ctx consolectx.Context, appName string, mesh string)
(*model.AppAccessLogConfigResp, error) {
+ appConfiguratorName := appName + consts.ConfiguratorRuleSuffix
+ res, err := GetConfigurator(ctx, appConfiguratorName, mesh)
+ resp := &model.AppAccessLogConfigResp{
+ AccessLog: false,
+ }
+ if err != nil {
+ logger.Errorf("get configurator failed when get app accesslog,
resourceKey: %s, err: %s",
+ coremodel.BuildResourceKey(mesh, appName), err)
+ return nil, err
+ }
+ if res == nil {
+ return resp, nil
+ }
+ var appAccessLogConfig *meshproto.OverrideConfig
+ res.Spec.RangeConfig(func(conf *meshproto.OverrideConfig) (isStop bool)
{
+ if isAppAccessLogConfig(conf, appName) {
+ appAccessLogConfig = conf
+ return true
+ }
+ return false
+ })
+ if appAccessLogConfig == nil {
+ return resp, nil
+ }
+ resp.AccessLog = appAccessLogConfig.Enabled
+ return resp, nil
+}
+
+func GetAppFlowWeight(ctx consolectx.Context, appName string, mesh string)
(*model.AppFlowWeightConfigResp, error) {
+ resp := &model.AppFlowWeightConfigResp{
+ FlowWeightSets: []model.FlowWeightSet{},
+ }
+ appConfiguratorName := appName + consts.ConfiguratorRuleSuffix
+ res, err := GetConfigurator(ctx, appConfiguratorName, mesh)
+ if err != nil {
+ logger.Errorf("get configurator failed when get app flow
weight, resourceKey: %s, err: %s",
+ coremodel.BuildResourceKey(mesh, appName), err)
+ return nil, err
+ }
+ if res == nil {
+ return resp, nil
+ }
+
+ weight := 0
+ res.Spec.RangeConfig(func(conf *meshproto.OverrideConfig) (isStop bool)
{
+ if isFlowWeightConfig(conf) {
+ weight, err = strconv.Atoi(conf.Parameters[`weight`])
+ if err != nil {
+ logger.Error("parse weight failed", err)
+ return true
Review Comment:
ignored
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]