github.com/polarismesh/polaris@v1.17.8/test/integrate/resource/circuitbreaker_config.go (about)

     1  /**
     2   * Tencent is pleased to support the open source community by making Polaris available.
     3   *
     4   * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
     5   *
     6   * Licensed under the BSD 3-Clause License (the "License");
     7   * you may not use this file except in compliance with the License.
     8   * You may obtain a copy of the License at
     9   *
    10   * https://opensource.org/licenses/BSD-3-Clause
    11   *
    12   * Unless required by applicable law or agreed to in writing, software distributed
    13   * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
    14   * CONDITIONS OF ANY KIND, either express or implied. See the License for the
    15   * specific language governing permissions and limitations under the License.
    16   */
    17  
    18  package resource
    19  
    20  import (
    21  	"fmt"
    22  
    23  	"github.com/golang/protobuf/ptypes/duration"
    24  	"github.com/golang/protobuf/ptypes/wrappers"
    25  	apifault "github.com/polarismesh/specification/source/go/api/v1/fault_tolerance"
    26  	apimodel "github.com/polarismesh/specification/source/go/api/v1/model"
    27  	apiservice "github.com/polarismesh/specification/source/go/api/v1/service_manage"
    28  
    29  	"github.com/polarismesh/polaris/common/utils"
    30  )
    31  
    32  const (
    33  	circuitBreakerName      = "test-name-%v"
    34  	circuitBreakerNamespace = "test-namespace-%v"
    35  )
    36  
    37  /**
    38   * @brief 创建测试熔断规则
    39   */
    40  func CreateCircuitBreakers(namespace *apimodel.Namespace) []*apifault.CircuitBreaker {
    41  	var circuitBreakers []*apifault.CircuitBreaker
    42  	for index := 0; index < 2; index++ {
    43  		circuitBreaker := &apifault.CircuitBreaker{
    44  			Name:       utils.NewStringValue(fmt.Sprintf(circuitBreakerName, index)),
    45  			Namespace:  namespace.GetName(),
    46  			Business:   utils.NewStringValue("test"),
    47  			Department: utils.NewStringValue("test"),
    48  			Owners:     utils.NewStringValue("test"),
    49  			Comment:    utils.NewStringValue("test"),
    50  		}
    51  		ruleNum := 2
    52  		// 填充source规则
    53  		sources := make([]*apifault.SourceMatcher, 0, ruleNum)
    54  		for i := 0; i < ruleNum; i++ {
    55  			source := &apifault.SourceMatcher{
    56  				Service:   utils.NewStringValue(fmt.Sprintf("service-test-%d", i)),
    57  				Namespace: utils.NewStringValue(fmt.Sprintf("namespace-test-%d", i)),
    58  				Labels: map[string]*apimodel.MatchString{
    59  					fmt.Sprintf("name-%d", i): {
    60  						Type:  apimodel.MatchString_EXACT,
    61  						Value: utils.NewStringValue(fmt.Sprintf("value-%d", i)),
    62  					},
    63  					fmt.Sprintf("name-%d", i+1): {
    64  						Type:  apimodel.MatchString_REGEX,
    65  						Value: utils.NewStringValue(fmt.Sprintf("value-%d", i+1)),
    66  					},
    67  				},
    68  			}
    69  			sources = append(sources, source)
    70  		}
    71  
    72  		// 填充destination规则
    73  		destinations := make([]*apifault.DestinationSet, 0, ruleNum)
    74  		for i := 0; i < ruleNum; i++ {
    75  			destination := &apifault.DestinationSet{
    76  				Service:   utils.NewStringValue(fmt.Sprintf("service-test-%d", i)),
    77  				Namespace: utils.NewStringValue(fmt.Sprintf("namespace-test-%d", i)),
    78  				Metadata: map[string]*apimodel.MatchString{
    79  					fmt.Sprintf("name-%d", i): {
    80  						Type:  apimodel.MatchString_EXACT,
    81  						Value: utils.NewStringValue(fmt.Sprintf("value-%d", i)),
    82  					},
    83  					fmt.Sprintf("name-%d", i+1): {
    84  						Type:  apimodel.MatchString_REGEX,
    85  						Value: utils.NewStringValue(fmt.Sprintf("value-%d", i+1)),
    86  					},
    87  				},
    88  				Resource: 0,
    89  				Type:     0,
    90  				Scope:    0,
    91  				MetricWindow: &duration.Duration{
    92  					Seconds: int64(i),
    93  				},
    94  				MetricPrecision: utils.NewUInt32Value(uint32(i)),
    95  				UpdateInterval: &duration.Duration{
    96  					Seconds: int64(i),
    97  				},
    98  			}
    99  			destinations = append(destinations, destination)
   100  		}
   101  
   102  		// 填充inbound规则
   103  		inbounds := make([]*apifault.CbRule, 0, ruleNum)
   104  		for i := 0; i < ruleNum; i++ {
   105  			inbound := &apifault.CbRule{
   106  				Sources:      sources,
   107  				Destinations: destinations,
   108  			}
   109  			inbounds = append(inbounds, inbound)
   110  		}
   111  		// 填充outbound规则
   112  		outbounds := make([]*apifault.CbRule, 0, ruleNum)
   113  		for i := 0; i < ruleNum; i++ {
   114  			outbound := &apifault.CbRule{
   115  				Sources:      sources,
   116  				Destinations: destinations,
   117  			}
   118  			outbounds = append(outbounds, outbound)
   119  		}
   120  		circuitBreaker.Inbounds = inbounds
   121  		circuitBreaker.Outbounds = outbounds
   122  		circuitBreakers = append(circuitBreakers, circuitBreaker)
   123  	}
   124  	return circuitBreakers
   125  }
   126  
   127  /**
   128   * @brief 更新测试熔断规则
   129   */
   130  func UpdateCircuitBreakers(circuitBreakers []*apifault.CircuitBreaker) {
   131  	for _, item := range circuitBreakers {
   132  		item.Inbounds = []*apifault.CbRule{
   133  			{
   134  				Sources: []*apifault.SourceMatcher{
   135  					{
   136  						Service: &wrappers.StringValue{
   137  							Value: "testSvc",
   138  						},
   139  					},
   140  				},
   141  			},
   142  		}
   143  		item.Outbounds = []*apifault.CbRule{
   144  			{
   145  				Sources: []*apifault.SourceMatcher{
   146  					{
   147  						Service: &wrappers.StringValue{
   148  							Value: "testSvc1",
   149  						},
   150  					},
   151  				},
   152  			},
   153  		}
   154  	}
   155  }
   156  
   157  /**
   158   * @brief 创建熔断规则版本
   159   */
   160  func CreateCircuitBreakerVersions(circuitBreakers []*apifault.CircuitBreaker) []*apifault.CircuitBreaker {
   161  	var newCircuitBreakers []*apifault.CircuitBreaker
   162  
   163  	for index, item := range circuitBreakers {
   164  		newCircuitBreaker := &apifault.CircuitBreaker{
   165  			Id:        item.GetId(),
   166  			Name:      item.GetName(),
   167  			Namespace: item.GetNamespace(),
   168  			Token:     item.GetToken(),
   169  			Version:   utils.NewStringValue(fmt.Sprintf("test-version-%d", index)),
   170  		}
   171  		newCircuitBreakers = append(newCircuitBreakers, newCircuitBreaker)
   172  	}
   173  
   174  	return newCircuitBreakers
   175  }
   176  
   177  /**
   178   * @brief 创建测试发布熔断规则
   179   */
   180  func CreateConfigRelease(services []*apiservice.Service, circuitBreakers []*apifault.CircuitBreaker) []*apiservice.ConfigRelease {
   181  	var configReleases []*apiservice.ConfigRelease
   182  	for index := 0; index < 2; index++ {
   183  		configRelease := &apiservice.ConfigRelease{
   184  			Service:        services[index],
   185  			CircuitBreaker: circuitBreakers[index],
   186  		}
   187  		configReleases = append(configReleases, configRelease)
   188  	}
   189  	return configReleases
   190  }