AlexStocks commented on a change in pull request #1187:
URL: https://github.com/apache/dubbo-go/pull/1187#discussion_r627500030



##########
File path: cluster/router/v3router/router_chain.go
##########
@@ -0,0 +1,247 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package v3router
+
+import (
+       "encoding/json"
+       "fmt"
+       "io"
+       "strings"
+)
+
+import (
+       "gopkg.in/yaml.v2"
+)
+
+import (
+       "github.com/apache/dubbo-go/cluster/router"
+       "github.com/apache/dubbo-go/cluster/router/v3router/k8s_api"
+       "github.com/apache/dubbo-go/common"
+       "github.com/apache/dubbo-go/common/logger"
+       "github.com/apache/dubbo-go/config"
+       "github.com/apache/dubbo-go/config_center"
+       "github.com/apache/dubbo-go/protocol"
+       "github.com/apache/dubbo-go/remoting"
+)
+
+// RouterChain contains all uniform router logic
+// it has UniformRouter list,
+type RouterChain struct {
+       routers                    []*UniformRouter
+       virtualServiceConfigBytes  []byte
+       destinationRuleConfigBytes []byte
+       notify                     chan struct{}
+}
+
+// NewUniformRouterChain return
+func NewUniformRouterChain(virtualServiceConfig, destinationRuleConfig []byte, 
notify chan struct{}) (router.PriorityRouter, error) {
+       fromFileConfig := true
+       uniformRouters, err := parseFromConfigToRouters(virtualServiceConfig, 
destinationRuleConfig, notify)
+       if err != nil {
+               fromFileConfig = false
+               logger.Warnf("parse router config form local file failed, error 
= %+v", err)
+       }
+       r := &RouterChain{
+               virtualServiceConfigBytes:  virtualServiceConfig,
+               destinationRuleConfigBytes: destinationRuleConfig,
+               routers:                    uniformRouters,
+               notify:                     notify,
+       }
+       if err := k8s_api.SetK8sEventListener(r); err != nil {
+               logger.Warnf("try listen K8s router config failed, error = 
%+v", err)
+               if !fromFileConfig {
+                       panic("No config file from both local file and k8s")
+               }
+       }
+       return r, nil
+}
+
+// Route route invokers using RouterChain's routers one by one
+func (r *RouterChain) Route(invokers []protocol.Invoker, url *common.URL, 
invocation protocol.Invocation) []protocol.Invoker {
+       for _, v := range r.routers {
+               invokers = v.Route(invokers, url, invocation)
+       }
+       return invokers
+}
+
+func (r *RouterChain) Process(event *config_center.ConfigChangeEvent) {
+       logger.Debugf("on processed event = %+v\n", *event)
+       if event.ConfigType == remoting.EventTypeAdd || event.ConfigType == 
remoting.EventTypeUpdate {
+               switch event.Key {
+               case k8s_api.VirtualServiceEventKey:
+                       logger.Debug("virtul service event")
+                       newVSValue, ok := 
event.Value.(*config.VirtualServiceConfig)
+                       if !ok {
+                               logger.Error("event.Value assertion error")
+                               return
+                       }
+
+                       newVSJsonValue, ok := 
newVSValue.ObjectMeta.Annotations["kubectl.kubernetes.io/last-applied-configuration"]
+                       if !ok {
+                               logger.Error("newVSValue.ObjectMeta.Annotations 
has no key named kubectl.kubernetes.io/last-applied-configuration")
+                               return
+                       }
+                       logger.Debugf("json file = %v\n", newVSJsonValue)
+                       newVirtualServiceConfig := 
&config.VirtualServiceConfig{}
+                       if err := json.Unmarshal([]byte(newVSJsonValue), 
newVirtualServiceConfig); err != nil {
+                               logger.Error("on process json data unmarshal 
error = ", err)
+                               return
+                       }
+                       newVirtualServiceConfig.YamlAPIVersion = 
newVirtualServiceConfig.APIVersion
+                       newVirtualServiceConfig.YamlKind = 
newVirtualServiceConfig.Kind
+                       newVirtualServiceConfig.MetaData.Name = 
newVirtualServiceConfig.ObjectMeta.Name
+                       fmt.Printf("get event after asseration = %+v\n", 
newVirtualServiceConfig)
+                       data, err := yaml.Marshal(newVirtualServiceConfig)
+                       if err != nil {
+                               logger.Error("Process change of virtual 
service: event.Value marshal error:", err)
+                               return
+                       }
+                       r.routers, err = parseFromConfigToRouters(data, 
r.destinationRuleConfigBytes, r.notify)
+                       if err != nil {
+                               logger.Error("Process change of virtual 
service: parseFromConfigToRouters:", err)
+                               return
+                       }
+               case k8s_api.DestinationRuleEventKey:
+                       logger.Debug("handling dest rule event")
+                       newDRValue, ok := 
event.Value.(*config.DestinationRuleConfig)
+                       if !ok {
+                               logger.Error("event.Value assertion error")
+                               return
+                       }
+
+                       newDRJsonValue, ok := 
newDRValue.ObjectMeta.Annotations["kubectl.kubernetes.io/last-applied-configuration"]
+                       if !ok {
+                               logger.Error("newVSValue.ObjectMeta.Annotations 
has no key named kubectl.kubernetes.io/last-applied-configuration")
+                               return
+                       }
+                       newDestRuleConfig := &config.DestinationRuleConfig{}
+                       if err := json.Unmarshal([]byte(newDRJsonValue), 
newDestRuleConfig); err != nil {
+                               logger.Error("on process json data unmarshal 
error = ", err)
+                               return
+                       }
+                       newDestRuleConfig.YamlAPIVersion = 
newDestRuleConfig.APIVersion
+                       newDestRuleConfig.YamlKind = newDestRuleConfig.Kind
+                       newDestRuleConfig.MetaData.Name = 
newDestRuleConfig.ObjectMeta.Name
+                       fmt.Printf("get event after asseration = %+v\n", 
newDestRuleConfig)

Review comment:
       logger.Debugf




-- 
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.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscr...@dubbo.apache.org
For additional commands, e-mail: notifications-h...@dubbo.apache.org

Reply via email to