From c7468bfd084c5391e159ca792add1d49aa50550a Mon Sep 17 00:00:00 2001 From: Alena Prokharchyk Date: Fri, 6 Jan 2017 10:23:06 -0800 Subject: [PATCH] skip inactive service from programming to lb --- controller/rancher/rancher.go | 13 +++ controller/rancher/rancher_test.go | 41 +++++++ controller/rancherglb/rancherglb.go | 11 +- controller/rancherglb/rancherglb_test.go | 104 ++++++++++++++++++ trash.conf | 2 +- .../go-rancher-metadata/metadata/types.go | 1 + 6 files changed, 170 insertions(+), 2 deletions(-) diff --git a/controller/rancher/rancher.go b/controller/rancher/rancher.go index a601b03..e2d8c63 100644 --- a/controller/rancher/rancher.go +++ b/controller/rancher/rancher.go @@ -205,6 +205,9 @@ func (lbc *LoadBalancerController) BuildConfigFromMetadata(lbName string, envUUI if err != nil { return nil, err } + if service == nil || !IsActiveService(service) { + continue + } eps, err := lbc.getServiceEndpoints(service, rule.TargetPort, true) if err != nil { return nil, err @@ -457,6 +460,16 @@ func (mf RMetaFetcher) GetServices() ([]metadata.Service, error) { return mf.MetadataClient.GetServices() } +func IsActiveService(svc *metadata.Service) bool { + inactiveStates := []string{"inactive", "deactivating", "removed", "removing"} + for _, state := range inactiveStates { + if strings.EqualFold(svc.State, state) { + return false + } + } + return true +} + func (mf RMetaFetcher) GetService(envUUID string, svcName string, stackName string) (*metadata.Service, error) { svcs, err := mf.MetadataClient.GetServices() if err != nil { diff --git a/controller/rancher/rancher_test.go b/controller/rancher/rancher_test.go index 6b91a60..a454118 100644 --- a/controller/rancher/rancher_test.go +++ b/controller/rancher/rancher_test.go @@ -207,6 +207,35 @@ func TestStoppedInstance(t *testing.T) { } } +func TestInactiveService(t *testing.T) { + portRules := []metadata.PortRule{} + port1 := metadata.PortRule{ + Protocol: "tcp", + Service: "default/inactive", + TargetPort: 44, + SourcePort: 45, + } + port2 := metadata.PortRule{ + Protocol: "tcp", + Service: "default/foo", + TargetPort: 46, + SourcePort: 45, + } + portRules = append(portRules, port1) + portRules = append(portRules, port2) + meta := &LBMetadata{ + PortRules: portRules, + } + + configs, _ := lbc.BuildConfigFromMetadata("test", "", meta) + + fe := configs[0].FrontendServices[0] + bes := fe.BackendServices + if len(bes) != 1 { + t.Fatalf("Invalid backends length, expected 1 for 1 active and 1 inactive service: [%v]", len(bes)) + } +} + func TestPriority(t *testing.T) { portRules := []metadata.PortRule{} port0 := metadata.PortRule{ @@ -521,6 +550,12 @@ func (mf tMetaFetcher) GetService(envUUID string, svcName string, stackName stri Kind: "externalService", Hostname: "google.com", } + } else if strings.EqualFold(svcName, "inactive") { + svc = &metadata.Service{ + Kind: "service", + State: "inactive", + Containers: getContainers("inactive"), + } } return svc, nil @@ -569,6 +604,12 @@ func getContainers(svcName string) []metadata.Container { State: "running", } containers = append(containers, c1) + } else if strings.EqualFold(svcName, "inactive") { + c1 := metadata.Container{ + PrimaryIp: "10.1.1.12", + State: "running", + } + containers = append(containers, c1) } return containers } diff --git a/controller/rancherglb/rancherglb.go b/controller/rancherglb/rancherglb.go index 17001b2..ff8c23d 100644 --- a/controller/rancherglb/rancherglb.go +++ b/controller/rancherglb/rancherglb.go @@ -141,9 +141,18 @@ func (lbc *glbController) GetGLBConfigs(glbSvc metadata.Service) ([]*config.Load if !rancher.IsSelectorMatch(glbRule.Selector, lbSvc.Labels) { continue } + + if !rancher.IsActiveService(&lbSvc) { + // cleanup public endpoints + eps := []client.PublicEndpoint{} + logrus.Debugf("cleaning up endpoints for inactive lb uuid [%v]", lbSvc.UUID) + if err := lbc.updateEndpoints(&lbSvc, eps); err != nil { + return nil, err + } + continue + } lbConfig := lbSvc.LBConfig if len(lbConfig.PortRules) == 0 { - logrus.Info("port rules is 0") continue } sourcePort := glbRule.SourcePort diff --git a/controller/rancherglb/rancherglb_test.go b/controller/rancherglb/rancherglb_test.go index 62dafd7..3d6d8ab 100644 --- a/controller/rancherglb/rancherglb_test.go +++ b/controller/rancherglb/rancherglb_test.go @@ -150,7 +150,47 @@ func TestTwoServicesMerge(t *testing.T) { } } } +} + +func TestInactiveLB(t *testing.T) { + var portRules []metadata.PortRule + portRule := metadata.PortRule{ + SourcePort: 80, + Protocol: "http", + Selector: "inactive=true", + } + portRules = append(portRules, portRule) + lbConfig := metadata.LBConfig{ + PortRules: portRules, + } + + glbSvc := metadata.Service{ + Kind: "loadBalancerService", + LBConfig: lbConfig, + Name: "glb", + StackName: "glb", + UUID: "glb", + } + configs, err := glb.GetGLBConfigs(glbSvc) + if err != nil { + t.Fatalf("Error getting configs: %s", err) + } + + if len(configs) != 1 { + t.Fatalf("Incorrect number of configs, expected 1, actual: %v", len(configs)) + } + + fes := configs[0].FrontendServices + if len(fes) != 1 { + t.Fatalf("Incorrect number of frontends, expected 1, actual: %v", len(fes)) + } + fe := fes[0] + + bes := fe.BackendServices + if len(bes) != 1 { + t.Fatalf("Incorrect number of backends, expected 1, actual: %v", len(bes)) + } } func (mf tMetaFetcher) GetServices() ([]metadata.Service, error) { @@ -189,6 +229,18 @@ func (mf tMetaFetcher) GetServices() ([]metadata.Service, error) { } svcs = append(svcs, *lbFooDup) + lbInactive, err := mf.GetService("", "lbinactive", "foo") + if err != nil { + return nil, err + } + svcs = append(svcs, *lbInactive) + + lbActive, err := mf.GetService("", "lbactive", "foo") + if err != nil { + return nil, err + } + svcs = append(svcs, *lbActive) + return svcs, nil } @@ -302,6 +354,58 @@ func (mf tMetaFetcher) GetService(envUUID string, svcName string, stackName stri StackName: "foo", } svc = &foo + } else if strings.EqualFold(svcName, "lbinactive") { + port := metadata.PortRule{ + SourcePort: 80, + Path: "/foo1", + Hostname: "foo1.com", + Service: "foo/foodup", + Protocol: "http", + TargetPort: 103, + } + var portRules []metadata.PortRule + portRules = append(portRules, port) + lbConfig := metadata.LBConfig{ + PortRules: portRules, + } + labels := make(map[string]string) + labels["inactive"] = "true" + lbfoodup := metadata.Service{ + Kind: "loadBalancerService", + LBConfig: lbConfig, + Name: "lbinactive", + UUID: "lbinactive", + StackName: "foo", + Labels: labels, + State: "deactivating", + } + svc = &lbfoodup + } else if strings.EqualFold(svcName, "lbactive") { + port := metadata.PortRule{ + SourcePort: 80, + Path: "/foo2", + Hostname: "foo2.com", + Service: "foo/foodup", + Protocol: "http", + TargetPort: 103, + } + var portRules []metadata.PortRule + portRules = append(portRules, port) + lbConfig := metadata.LBConfig{ + PortRules: portRules, + } + labels := make(map[string]string) + labels["inactive"] = "true" + lbfoodup := metadata.Service{ + Kind: "loadBalancerService", + LBConfig: lbConfig, + Name: "lbactive", + UUID: "lbactive", + StackName: "foo", + Labels: labels, + State: "active", + } + svc = &lbfoodup } return svc, nil diff --git a/trash.conf b/trash.conf index b33e745..a96125e 100644 --- a/trash.conf +++ b/trash.conf @@ -41,7 +41,7 @@ golang.org/x/sys a646d33 google.golang.org/appengine 267c27e google.golang.org/cloud 4f1a5ca gopkg.in/inf.v0 v0.9.0 -github.com/rancher/go-rancher-metadata 4c6aeec78cf34419855c88e47066f816b9361569 +github.com/rancher/go-rancher-metadata 2eae85d0ad3239e407eb6b86e5f9034f16fc4786 k8s.io/kubernetes v1.4.4 github.com/pkg/errors 1d2e60385a13aaa66134984235061c2f9302520e k8s.io/client-go/1.4 93fcd402979cfad8a7151f96e016416947c6a3cb diff --git a/vendor/github.com/rancher/go-rancher-metadata/metadata/types.go b/vendor/github.com/rancher/go-rancher-metadata/metadata/types.go index 628d2a9..22aa08f 100644 --- a/vendor/github.com/rancher/go-rancher-metadata/metadata/types.go +++ b/vendor/github.com/rancher/go-rancher-metadata/metadata/types.go @@ -39,6 +39,7 @@ type Service struct { PrimaryServiceName string `json:"primary_service_name"` LBConfig LBConfig `json:"lb_config"` EnvironmentUUID string `json:"environment_uuid"` + State string `json:"state"` } type Container struct {