convert msb service to pilot default rule 99/59099/1
authorLvbo163 <lv.bo163@zte.com.cn>
Mon, 6 Aug 2018 07:45:19 +0000 (15:45 +0800)
committerLvbo163 <lv.bo163@zte.com.cn>
Mon, 6 Aug 2018 07:45:19 +0000 (15:45 +0800)
Issue-ID: MSB-260

Change-Id: I0ae233102f293d2c50c2e4c4a509dd880d0aab15
Signed-off-by: Lvbo163 <lv.bo163@zte.com.cn>
msb2pilot/src/msb2pilot/models/config.go
msb2pilot/src/msb2pilot/pilot/msb.go

index 5773cf4..b8111a5 100644 (file)
@@ -15,4 +15,5 @@ const (
        EnvConsulAddress  = "ConsulAddress" //http://localhost:8500
        EnvK8sAddress     = "K8sAddress"
        EnvMsbAddress     = "MsbAddress"
+       EnvApiGatewayName = "MsbApiGatewayName" // default value "apigateway"
 )
index c0a2dc9..9b6a977 100644 (file)
  */
 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
+}