summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHuabing Zhao <zhaohuabing@gmail.com>2018-08-06 07:50:34 +0000
committerGerrit Code Review <gerrit@onap.org>2018-08-06 07:50:34 +0000
commit23c8b92d3e365bac0f5ef57928cbe2d0df2d13ad (patch)
treee726ed9b11be08a1153c2124108e575522027974
parent7898ba2e11e4dfab3e55cf320f2a34dc83068079 (diff)
parent9d7f63fdc1906c10618915ef5033832ac3501ae8 (diff)
Merge "convert msb service to pilot default rule"
-rw-r--r--msb2pilot/src/msb2pilot/models/config.go1
-rw-r--r--msb2pilot/src/msb2pilot/pilot/msb.go108
2 files changed, 109 insertions, 0 deletions
diff --git a/msb2pilot/src/msb2pilot/models/config.go b/msb2pilot/src/msb2pilot/models/config.go
index 5773cf4..b8111a5 100644
--- a/msb2pilot/src/msb2pilot/models/config.go
+++ b/msb2pilot/src/msb2pilot/models/config.go
@@ -15,4 +15,5 @@ const (
EnvConsulAddress = "ConsulAddress" //http://localhost:8500
EnvK8sAddress = "K8sAddress"
EnvMsbAddress = "MsbAddress"
+ EnvApiGatewayName = "MsbApiGatewayName" // default value "apigateway"
)
diff --git a/msb2pilot/src/msb2pilot/pilot/msb.go b/msb2pilot/src/msb2pilot/pilot/msb.go
index c0a2dc9..9b6a977 100644
--- a/msb2pilot/src/msb2pilot/pilot/msb.go
+++ b/msb2pilot/src/msb2pilot/pilot/msb.go
@@ -11,10 +11,26 @@
*/
package pilot
+import (
+ "bytes"
+ "msb2pilot/log"
+ "msb2pilot/models"
+ "msb2pilot/msb"
+ "os"
+ "regexp"
+ "strings"
+
+ istioModel "istio.io/istio/pilot/pkg/model"
+)
+
var (
cachedServices []*models.MsbService
)
+const (
+ routerulePrefix = "msbcustom."
+)
+
func SyncMsbData(newServices []*models.MsbService) {
log.Log.Debug("sync msb rewrite rule to pilot")
createServices, updateServices, deleteServices := compareServices(cachedServices, newServices)
@@ -58,3 +74,95 @@ func toServiceMap(services []*models.MsbService) map[string]*models.MsbService {
return serviceMap
}
+
+func parseServiceToConfig(services []*models.MsbService) ([]istioModel.Config, error) {
+ publishServices := getPublishServiceMap()
+ apiGateway := os.Getenv(models.EnvApiGatewayName)
+ var buf bytes.Buffer
+ for _, service := range services {
+ if publishService, exist := publishServices[getPublishServiceKey(service)]; exist {
+
+ if service.ConsulLabels.BaseInfo != nil {
+ rule := createRouteRule(apiGateway, publishService.PublishUrl, service.ServiceName, service.ConsulLabels.BaseInfo.Url)
+ buf.WriteString(rule)
+ }
+ }
+ }
+ return ParseParam(buf.String())
+}
+
+func getPublishServiceKey(svc *models.MsbService) string {
+ res := svc.ServiceName
+
+ if svc.ConsulLabels.BaseInfo != nil {
+ res += svc.ConsulLabels.BaseInfo.Version
+ }
+
+ if svc.ConsulLabels.NameSpace != nil {
+ res += svc.ConsulLabels.NameSpace.NameSpace
+ }
+
+ return res
+}
+
+func getPublishServiceMap() map[string]*models.PublishService {
+ publishServices := msb.GetAllPublishServices()
+
+ res := make(map[string]*models.PublishService)
+
+ for _, svc := range publishServices {
+ key := svc.ServiceName + svc.Version + svc.NameSpace
+ res[key] = svc
+ }
+
+ return res
+}
+
+func createRouteRule(sourceService, sourcePath, targetService, targetPath string) string {
+ if sourcePath == "" {
+ sourcePath = "/"
+ }
+ if targetPath == "" {
+ targetPath = "/"
+ }
+ // rule name must consist of lower case alphanuberic charactoers, '-' or '.'. and must start and end with an alphanumberic charactore
+ r := regexp.MustCompile("[a-z0-9]([-a-z0-9]*[a-z0-9])?(\\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*")
+ strs := r.FindAllString(targetService, -1)
+ name := routerulePrefix + strings.Join(strs, "")
+ name = strings.ToLower(name)
+
+ rule := `{
+"apiVersion": "config.istio.io/v1alpha2",
+"kind": "RouteRule",
+"metadata": {
+ "name": "` + name + `"
+},
+"spec": {
+ "destination":{
+ "name":"` + sourceService + `"
+ },
+ "match":{
+ "request":{
+ "headers": {
+ "uri": {
+ "prefix": "` + sourcePath + `"
+ }
+ }
+ }
+ },
+ "rewrite": {
+ "uri": "` + targetPath + `"
+ },
+ "route":[
+ {
+ "destination":{
+ "name":"` + targetService + `"
+ }
+ }
+ ]
+}
+}
+
+`
+ return rule
+}