Skip to content
This repository has been archived by the owner on Mar 25, 2024. It is now read-only.

skip inactive service from programming to lb #48

Merged
merged 1 commit into from
Jan 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions controller/rancher/rancher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
41 changes: 41 additions & 0 deletions controller/rancher/rancher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
11 changes: 10 additions & 1 deletion controller/rancherglb/rancherglb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
104 changes: 104 additions & 0 deletions controller/rancherglb/rancherglb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion trash.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.