@@ -178,48 +178,58 @@ func UpdateInstanceTrafficStatus(ctx consolectx.Context, mesh string, appName st
178178 logger .Errorf ("get condition rule for %s failed, cause: %s" , appName , err )
179179 return err
180180 }
181- // if no condition rule for application exists
182181 if conditionRuleRes == nil || conditionRuleRes .Spec == nil {
183- // if disable traffic is false, and rule doesn't exist, directly return
184- if ! newDisabled {
185- return nil
186- }
187- conditionRoute := generateDefaultConditionV3 (true , true , true , appName , constants .ScopeApplication )
188- conditionRoute .Conditions = append (conditionRoute .Conditions , disableExpression (instanceIP ))
189- resName := appName + constants .ConditionRuleDotSuffix
190- conditionRuleRes = meshresource .NewConditionRouteResourceWithAttributes (resName , mesh )
191- conditionRuleRes .Spec = conditionRoute
192- err := CreateConditionRule (ctx , conditionRuleRes )
193- if err != nil {
194- logger .Errorf ("create condition rule for app %s failed, cause: %s" , appName , err )
195- return err
196- }
182+ return createTrafficConditionRuleIfNeeded (ctx , mesh , appName , instanceIP , newDisabled )
183+ }
184+ return updateExistingTrafficConditionRule (ctx , conditionRuleRes , appName , instanceIP , newDisabled )
185+ }
186+
187+ func createTrafficConditionRuleIfNeeded (
188+ ctx consolectx.Context ,
189+ mesh string ,
190+ appName string ,
191+ instanceIP string ,
192+ newDisabled bool ) error {
193+ if ! newDisabled {
197194 return nil
198195 }
199- // otherwise, checkout condition one by one
200- for i , condition := range conditionRuleRes .Spec .Conditions {
201- oldDisabled := isInstanceTrafficDisabled (condition , instanceIP )
202- // if user needs to disable traffic and instance's traffic is already disabled, skip updating, directly return
203- if newDisabled && oldDisabled {
196+ conditionRoute := generateDefaultConditionV3 (true , true , true , appName , constants .ScopeApplication )
197+ conditionRoute .Conditions = append (conditionRoute .Conditions , disableExpression (instanceIP ))
198+ resName := appName + constants .ConditionRuleDotSuffix
199+ conditionRuleRes := meshresource .NewConditionRouteResourceWithAttributes (resName , mesh )
200+ conditionRuleRes .Spec = conditionRoute
201+ if err := CreateConditionRule (ctx , conditionRuleRes ); err != nil {
202+ logger .Errorf ("create condition rule for app %s failed, cause: %s" , appName , err )
203+ return err
204+ }
205+ return nil
206+ }
207+
208+ func updateExistingTrafficConditionRule (
209+ ctx consolectx.Context ,
210+ conditionRuleRes * meshresource.ConditionRouteResource ,
211+ appName string ,
212+ instanceIP string ,
213+ newDisabled bool ) error {
214+ disabledIndex , disabledFound := findDisabledTrafficConditionIndex (conditionRuleRes .Spec .Conditions , instanceIP )
215+ if disabledFound {
216+ if newDisabled {
204217 logger .Warnf ("The instance %s has been disabled, skip updating condition rule" , instanceIP )
205218 return nil
206219 }
207- // if user needs to enable traffic while instance's traffic is disabled, update condition rule
208- if ! newDisabled && oldDisabled {
209- conditionRuleRes .Spec .Conditions = append (conditionRuleRes .Spec .Conditions [:i ], conditionRuleRes .Spec .Conditions [i + 1 :]... )
210- if err = UpdateConditionRule (ctx , conditionRuleRes ); err != nil {
211- logger .Errorf ("update condition rule for app %s failed, cause: %s" , appName , err )
212- return err
213- }
214- return nil
220+ conditionRuleRes .Spec .Conditions = append (
221+ conditionRuleRes .Spec .Conditions [:disabledIndex ],
222+ conditionRuleRes .Spec .Conditions [disabledIndex + 1 :]... )
223+ if err := UpdateConditionRule (ctx , conditionRuleRes ); err != nil {
224+ logger .Errorf ("update condition rule for app %s failed, cause: %s" , appName , err )
225+ return err
215226 }
227+ return nil
216228 }
217- // if user needs to enable traffic while instance's traffic is already enabled, directly return
218229 if ! newDisabled {
219230 logger .Warnf ("the instance %s has been enabled, skip updating condition rule" , instanceIP )
220231 return nil
221232 }
222- // else user needs ato disable traffic, add a disable condition
223233 conditionRuleRes .Spec .Conditions = append (conditionRuleRes .Spec .Conditions , disableExpression (instanceIP ))
224234 if err := UpdateConditionRule (ctx , conditionRuleRes ); err != nil {
225235 logger .Errorf ("update condition rule for app %s failed, cause: %s" , appName , err )
@@ -228,6 +238,15 @@ func UpdateInstanceTrafficStatus(ctx consolectx.Context, mesh string, appName st
228238 return nil
229239}
230240
241+ func findDisabledTrafficConditionIndex (conditions []string , instanceIP string ) (int , bool ) {
242+ for i , condition := range conditions {
243+ if isInstanceTrafficDisabled (condition , instanceIP ) {
244+ return i , true
245+ }
246+ }
247+ return - 1 , false
248+ }
249+
231250func GetInstanceTrafficStatus (ctx consolectx.Context , mesh string , appName string , instanceIP string ) (bool , error ) {
232251 resName := appName + constants .ConditionRuleDotSuffix
233252 res , err := GetConditionRule (ctx , resName , mesh )
@@ -319,76 +338,89 @@ func UpdateInstanceAccessLogOpenStatus(
319338 logger .Errorf ("get configurator for %s failed, cause: %s" , appConfiguratorName , err )
320339 return err
321340 }
322- // if configurator doesn't exist
323341 if res == nil || res .Spec == nil {
324- // if user needs to disable accesslog, directly return
325- if ! openStatus {
326- logger .Warnf ("the instance %s accesslog is disabled, skip updating configurator" , instanceIP )
327- return nil
328- }
329- // otherwise create a new configurator with accesslog opened
330- res = meshresource .NewDynamicConfigResourceWithAttributes (appConfiguratorName , mesh )
331- res .Spec = & meshproto.DynamicConfig {
332- Key : appName ,
333- Scope : constants .ScopeApplication ,
334- ConfigVersion : constants .ConfiguratorVersionV3 ,
335- Enabled : true ,
336- Configs : []* meshproto.OverrideConfig {
337- {
338- Side : constants .SideProvider ,
339- Match : & meshproto.ConditionMatch {Address : & meshproto.AddressMatch {Wildcard : instanceIP + `:*` }},
340- Parameters : map [string ]string {`accesslog` : `true` },
341- XGenerateByCp : true ,
342- },
343- },
344- }
345- err := CreateConfigurator (ctx , res )
346- if err != nil {
347- logger .Errorf ("create configurator for instance %s%s failed, cause: %s" , appName , instanceIP , err )
348- return err
349- }
342+ return createAccessLogConfiguratorIfNeeded (ctx , mesh , appName , instanceIP , openStatus )
343+ }
344+ return updateExistingAccessLogConfigurator (ctx , res , appName , instanceIP , openStatus )
345+ }
346+
347+ func createAccessLogConfiguratorIfNeeded (
348+ ctx consolectx.Context ,
349+ mesh string ,
350+ appName string ,
351+ instanceIP string ,
352+ openStatus bool ) error {
353+ if ! openStatus {
354+ logger .Warnf ("the instance %s accesslog is disabled, skip updating configurator" , instanceIP )
350355 return nil
351356 }
357+ res := meshresource .NewDynamicConfigResourceWithAttributes (appName + constants .ConfiguratorRuleDotSuffix , mesh )
358+ res .Spec = & meshproto.DynamicConfig {
359+ Key : appName ,
360+ Scope : constants .ScopeApplication ,
361+ ConfigVersion : constants .ConfiguratorVersionV3 ,
362+ Enabled : true ,
363+ Configs : []* meshproto.OverrideConfig {
364+ {
365+ Side : constants .SideProvider ,
366+ Match : & meshproto.ConditionMatch {Address : & meshproto.AddressMatch {Wildcard : instanceIP + `:*` }},
367+ Parameters : map [string ]string {`accesslog` : `true` },
368+ XGenerateByCp : true ,
369+ },
370+ },
371+ }
372+ if err := CreateConfigurator (ctx , res ); err != nil {
373+ logger .Errorf ("create configurator for instance %s%s failed, cause: %s" , appName , instanceIP , err )
374+ return err
375+ }
376+ return nil
377+ }
352378
353- // otherwise we need to match config one by one
354- for i , config := range res .Spec .Configs {
355- accessLogOpened := isInstanceAccessLogOpen (config , instanceIP )
356- // if user needs to open accesslog and accesslog is already opened, directly return
357- if openStatus && accessLogOpened {
379+ func updateExistingAccessLogConfigurator (
380+ ctx consolectx.Context ,
381+ res * meshresource.DynamicConfigResource ,
382+ appName string ,
383+ instanceIP string ,
384+ openStatus bool ) error {
385+ configIndex , accessLogOpened := findAccessLogConfigIndex (res .Spec .Configs , instanceIP )
386+ if accessLogOpened {
387+ if openStatus {
358388 logger .Warnf ("the instance %s accesslog is already opened, skip updating configurator" , instanceIP )
359389 return nil
360390 }
361- // if user needs to close accesslog and accesslog is opened, remove the config and return
362- if ! openStatus && accessLogOpened {
363- res .Spec .Configs = slice .Concat (res .Spec .Configs [:i ], res .Spec .Configs [i + 1 :])
364- err := UpdateConfigurator (ctx , res )
365- if err != nil {
366- logger .Errorf ("update configurator for instance %s%s failed, cause: %s" , appName , instanceIP , err )
367- return err
368- }
369- return nil
391+ res .Spec .Configs = slice .Concat (res .Spec .Configs [:configIndex ], res .Spec .Configs [configIndex + 1 :])
392+ if err := UpdateConfigurator (ctx , res ); err != nil {
393+ logger .Errorf ("update configurator for instance %s%s failed, cause: %s" , appName , instanceIP , err )
394+ return err
370395 }
396+ return nil
371397 }
372- // if user needs to close accesslog and accesslog is not opened, directly return
373398 if ! openStatus {
374399 logger .Warnf ("the instance %s accesslog is already disabled, skip updating configurator" , instanceIP )
375400 return nil
376401 }
377- // otherwise we need to add a new config to open accesslog
378402 res .Spec .Configs = append (res .Spec .Configs , & meshproto.OverrideConfig {
379403 Side : constants .SideProvider ,
380404 Match : & meshproto.ConditionMatch {Address : & meshproto.AddressMatch {Wildcard : instanceIP + `:*` }},
381405 Parameters : map [string ]string {`accesslog` : `true` },
382406 XGenerateByCp : true ,
383407 })
384- err = UpdateConfigurator (ctx , res )
385- if err != nil {
408+ if err := UpdateConfigurator (ctx , res ); err != nil {
386409 logger .Errorf ("update configurator for instance %s%s failed, cause: %s" , appName , instanceIP , err )
387410 return err
388411 }
389412 return nil
390413}
391414
415+ func findAccessLogConfigIndex (configs []* meshproto.OverrideConfig , instanceIP string ) (int , bool ) {
416+ for i , config := range configs {
417+ if isInstanceAccessLogOpen (config , instanceIP ) {
418+ return i , true
419+ }
420+ }
421+ return - 1 , false
422+ }
423+
392424func isInstanceAccessLogOpen (conf * meshproto.OverrideConfig , IP string ) bool {
393425 if conf != nil &&
394426 conf .Match != nil &&
@@ -423,9 +455,7 @@ func fetchMetricsData(ip string, port int64) ([]model.Metric, error) {
423455// parsePrometheusData parses Prometheus text format data and converts it to a slice of Metrics.
424456func parsePrometheusData (data string ) ([]model.Metric , error ) {
425457 var metrics []model.Metric
426- lines := strings .Split (data , "\n " )
427-
428- for _ , line := range lines {
458+ for line := range strings .SplitSeq (data , "\n " ) {
429459 line = strings .TrimSpace (line )
430460 if line == "" || strings .HasPrefix (line , "#" ) {
431461 continue
@@ -449,7 +479,7 @@ func parsePrometheusData(data string) ([]model.Metric, error) {
449479 labelsPart := strings .TrimSuffix (nameAndLabels [1 ], "}" )
450480
451481 labels := make (map [string ]string )
452- for _ , label := range strings .Split (labelsPart , "," ) {
482+ for label := range strings .SplitSeq (labelsPart , "," ) {
453483 if label == "" {
454484 continue
455485 }
0 commit comments