AngLi2 commented on a change in pull request #766:
URL: 
https://github.com/apache/servicecomb-service-center/pull/766#discussion_r532326507



##########
File path: server/service/gov/kie/kie_distributor.go
##########
@@ -0,0 +1,202 @@
+package kie
+
+import (
+       "context"
+       "encoding/json"
+       "fmt"
+       "github.com/apache/servicecomb-service-center/pkg/gov"
+       "github.com/apache/servicecomb-service-center/server/config"
+       svc "github.com/apache/servicecomb-service-center/server/service/gov"
+       "github.com/ghodss/yaml"
+       "github.com/go-chassis/kie-client"
+       "log"
+       "strings"
+)
+
+type Distributor struct {
+       lbPolicies map[string]*gov.LoadBalancer
+       name       string
+       client     *kie.Client
+}
+
+const PREFIX = "servicecomb."
+
+const EnableStatus = "enabled"
+
+const ValueType = "text"
+
+const AppKey = "app"
+
+const EnvironmentKey = "environment"
+
+var rule = Validator{}
+
+func (d *Distributor) Create(kind, project string, spec []byte) error {
+       p := &gov.LoadBalancer{}
+       err := json.Unmarshal(spec, p)
+       if err != nil {
+               return err
+       }
+       log.Println(fmt.Sprintf("create %v", &p))
+       key := toSnake(kind) + "." + p.Name
+       err = rule.Validate(kind, p.Spec)
+       if err != nil {
+               return err
+       }
+       yamlByte, err := yaml.Marshal(p.Spec)
+       if err != nil {
+               return err
+       }
+       kv := kie.KVRequest{
+               Key:       PREFIX + key,
+               Value:     string(yamlByte),
+               Status:    EnableStatus,
+               ValueType: ValueType,
+               Labels:    map[string]string{AppKey: p.Selector.App, 
EnvironmentKey: p.Selector.Environment},
+       }
+       _, err = d.client.Create(context.TODO(), kv, kie.WithProject(project))
+       if err != nil {
+               return err
+       }
+       d.lbPolicies[p.GovernancePolicy.Name] = p
+       return nil
+}
+
+func (d *Distributor) Update(id, kind, project string, spec []byte) error {
+       p := &gov.LoadBalancer{}
+       err := json.Unmarshal(spec, p)
+       if err != nil {
+               return err
+       }
+       log.Println(fmt.Sprintf("update %v", &p))
+       err = rule.Validate(kind, p.Spec)
+       if err != nil {
+               return err
+       }
+       yamlByte, err := yaml.Marshal(p.Spec)
+       if err != nil {
+               return err
+       }
+       kv := kie.KVRequest{
+               ID:     id,
+               Value:  string(yamlByte),
+               Status: p.Status,
+       }
+       _, err = d.client.Put(context.TODO(), kv, kie.WithProject(project))
+       if err != nil {
+               return err
+       }
+       d.lbPolicies[p.GovernancePolicy.Name] = p
+       return nil
+}
+
+func (d *Distributor) Delete(id, project string) error {
+       err := d.client.Delete(context.TODO(), id, kie.WithProject(project))
+       if err != nil {
+               return err
+       }
+       return nil
+}
+
+func (d *Distributor) List(kind, project, app, env string) ([]byte, error) {
+       list, _, err := d.client.List(context.TODO(),
+               kie.WithKey("beginWith("+PREFIX+toSnake(kind)+")"),
+               kie.WithLabels(map[string]string{AppKey: app, EnvironmentKey: 
env}),
+               kie.WithRevision(0),
+               kie.WithGetProject(project))
+       if err != nil {
+               return nil, err
+       }
+       var r []*gov.LoadBalancer

Review comment:
       建议明确len降低resize开销




----------------------------------------------------------------
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:
[email protected]


Reply via email to