Skip to content

Commit

Permalink
Changes to make the suffix to add
Browse files Browse the repository at this point in the history
on external lb targets configurable.

Changes to skip a service using the same lb_endpoint as some other
service.

Also changes to update the lb_endpoint when the mapped service changes.

Some minor code changes.
  • Loading branch information
Prachi Damle authored and Prachi Damle committed Apr 22, 2016
1 parent 9c632a8 commit 871ede3
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 40 deletions.
12 changes: 8 additions & 4 deletions external-lb.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@ func getProviderLBConfigs() (map[string]model.LBConfig, error) {
return nil, err
}
rancherConfigs := make(map[string]model.LBConfig, len(allConfigs))
suffix := "_rancher.internal"
for _, value := range allConfigs {
if strings.HasSuffix(value.LBTargetName, suffix) {
if strings.HasSuffix(value.LBTargetPoolName, targetRancherSuffix) {
rancherConfigs[value.LBEndpoint] = value
}
}
Expand Down Expand Up @@ -77,8 +76,9 @@ func updateExistingConfigs(metadataConfigs map[string]model.LBConfig, providerCo
mLBConfig := metadataConfigs[key]
pLBConfig := providerConfigs[key]
var update bool
//check that the targetName and targets match
if mLBConfig.LBTargetName == pLBConfig.LBTargetName {

//check that the targetPoolName and targets match
if strings.EqualFold(mLBConfig.LBTargetPoolName, pLBConfig.LBTargetPoolName) {
if len(mLBConfig.LBTargets) != len(pLBConfig.LBTargets) {
update = true
}
Expand All @@ -97,6 +97,10 @@ func updateExistingConfigs(metadataConfigs map[string]model.LBConfig, providerCo
break
}
}
} else {
//targetPool should be changed
logrus.Debugf("The LBEndPoint %s will be updated to map to a new LBTargetPoolName %s", key, mLBConfig.LBTargetPoolName)
update = true
}

if update {
Expand Down
4 changes: 2 additions & 2 deletions healthcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ func healthcheck(w http.ResponseWriter, req *http.Request) {
http.Error(w, "Failed to reach metadata server", http.StatusInternalServerError)
} else {
// 2) test provider
ok, err := provider.TestConnection()
if !ok {
err := provider.TestConnection()
if err != nil {
logrus.Errorf("Healthcheck failed: unable to reach a provider, error:%v", err)
http.Error(w, "Failed to reach an external provider ", http.StatusInternalServerError)
} else {
Expand Down
15 changes: 12 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ var (
debug = flag.Bool("debug", false, "Debug")
logFile = flag.String("log", "", "Log file")

provider providers.Provider
m *metadata.MetadataClient
provider providers.Provider
m *metadata.MetadataClient
lbEndpointServiceLabel string
targetRancherSuffix string
)

func setEnv() {
Expand Down Expand Up @@ -60,6 +62,13 @@ func setEnv() {
}
m = mClient

targetRancherSuffix = os.Getenv("LB_TARGET_RANCHER_SUFFIX")
if len(targetRancherSuffix) == 0 {
logrus.Info("LB_TARGET_RANCHER_SUFFIX is not set, using default suffix '_rancher.internal'")
targetRancherSuffix = "_rancher.internal"
}

lbEndpointServiceLabel = "io.rancher.service.external_lb_endpoint"
}

func main() {
Expand Down Expand Up @@ -92,7 +101,7 @@ func main() {
if update {
// get records from metadata
logrus.Debugf("Reading metadata LB Configs")
metadataLBConfigs, err := m.GetMetadataLBConfigs()
metadataLBConfigs, err := m.GetMetadataLBConfigs(lbEndpointServiceLabel, targetRancherSuffix)
if err != nil {
logrus.Errorf("Error reading metadata lb entries: %v", err)
}
Expand Down
30 changes: 17 additions & 13 deletions metadata/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ import (
)

const (
metadataUrl = "http://rancher-metadata/latest"
metadataUrl = "http://rancher-metadata/2015-12-19"
)

type MetadataClient struct {
MetadataClient *metadata.Client
EnvironmentName string
MetadataClient *metadata.Client
}

func getEnvironmentName(m *metadata.Client) (string, error) {
Expand All @@ -40,22 +39,21 @@ func NewMetadataClient() (*MetadataClient, error) {
logrus.Fatalf("Failed to configure rancher-metadata: %v", err)
}

envName, err := getEnvironmentName(m)
_, err = getEnvironmentName(m)
if err != nil {
logrus.Fatalf("Error reading stack info: %v", err)
logrus.Fatalf("Error reading stack metadata info: %v", err)
}

return &MetadataClient{
MetadataClient: m,
EnvironmentName: envName,
MetadataClient: m,
}, nil
}

func (m *MetadataClient) GetVersion() (string, error) {
return m.MetadataClient.GetVersion()
}

func (m *MetadataClient) GetMetadataLBConfigs() (map[string]model.LBConfig, error) {
func (m *MetadataClient) GetMetadataLBConfigs(lbEndpointServiceLabel string, targetRancherSuffix string) (map[string]model.LBConfig, error) {
lbConfigs := make(map[string]model.LBConfig)

services, err := m.MetadataClient.GetServices()
Expand All @@ -64,15 +62,21 @@ func (m *MetadataClient) GetMetadataLBConfigs() (map[string]model.LBConfig, erro
logrus.Infof("Error reading services %v", err)
} else {
for _, service := range services {
lb_endpoint, ok := service.Labels["io.rancher.service.external_lb_endpoint"]
lb_endpoint, ok := service.Labels[lbEndpointServiceLabel]
if ok {
//label exists, configure external LB
logrus.Debugf("label exists for service : %v", service)
// Configure this service only if this endpoint is already not used by some other service so far
_, ok = lbConfigs[lb_endpoint]
if ok {
logrus.Errorf("LB Endpoint already used by another service, will skip this service : %v", service.Name)
continue
}

logrus.Debugf("LB label exists for service : %v", service)
lbConfig := model.LBConfig{}
lbConfig.LBEndpoint = lb_endpoint
lbConfig.LBTargetName = service.StackName + "." + service.Name + "_rancher.internal"
err = m.getContainerLBTargets(&lbConfig, service)
if err != nil {
lbConfig.LBTargetPoolName = service.UUID + targetRancherSuffix
if err = m.getContainerLBTargets(&lbConfig, service); err != nil {
continue
}
lbConfigs[lb_endpoint] = lbConfig
Expand Down
2 changes: 1 addition & 1 deletion model/lb.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package model

type LBConfig struct {
LBEndpoint string
LBTargetName string
LBTargetPoolName string
LBTargets []LBTarget
}

Expand Down
2 changes: 1 addition & 1 deletion packaging/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ FROM alpine:3.2
MAINTAINER Rancher Labs, Inc.
RUN apk add --update ca-certificates

ENV EXT_DNS_RELEASE v0.1.0
ENV EXT_LB_RELEASE v0.1.0
ADD external-lb /usr/bin/external-lb


Expand Down
5 changes: 2 additions & 3 deletions providers/external_lb_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package providers

import (
"fmt"
//"github.com/Sirupsen/logrus"
"github.com/rancher/external-lb/model"
)

Expand All @@ -12,7 +11,7 @@ type Provider interface {
RemoveLBConfig(config model.LBConfig) error
UpdateLBConfig(config model.LBConfig) error
GetLBConfigs() ([]model.LBConfig, error)
TestConnection() (bool, error)
TestConnection() error
}

var (
Expand All @@ -31,7 +30,7 @@ func RegisterProvider(name string, provider Provider) error {
providers = make(map[string]Provider)
}
if _, exists := providers[name]; exists {
return fmt.Errorf("provider already registered")
return fmt.Errorf("provider %s already registered", name)
}
providers[name] = provider
return nil
Expand Down
22 changes: 9 additions & 13 deletions providers/f5/f5_bigip.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package f5

import (
//"fmt"
"github.com/Sirupsen/logrus"
"github.com/rancher/external-lb/model"
"github.com/rancher/external-lb/providers"
Expand Down Expand Up @@ -36,8 +35,8 @@ func init() {
}

client = bigip.NewSession(f5_host, f5_admin, f5_pwd)
works, err := checkF5Connection()
if !works {
err := checkF5Connection()
if err != nil {
logrus.Fatalf("Connecting to f5 host %v does not work, error: %v", f5_host, err)
return
}
Expand Down Expand Up @@ -83,7 +82,7 @@ func (*F5BigIPHandler) AddLBConfig(config model.LBConfig) error {
}

//Create our pool if does not exist
poolName := config.LBTargetName
poolName := config.LBTargetPoolName
if !poolExists(poolName) {
err = client.CreatePool(poolName)
if err != nil {
Expand Down Expand Up @@ -196,7 +195,7 @@ func (*F5BigIPHandler) RemoveLBConfig(config model.LBConfig) error {
return err
}

poolName := config.LBTargetName
poolName := config.LBTargetPoolName

poolMembers, err := client.PoolMembers(poolName)
var nodes []model.LBTarget
Expand Down Expand Up @@ -253,7 +252,7 @@ func (f *F5BigIPHandler) UpdateLBConfig(config model.LBConfig) error {
func (*F5BigIPHandler) GetLBConfigs() ([]model.LBConfig, error) {
//list all virtualServers
// for each vs -> LBEndpoint
// get the pool -> LBTargetName
// get the pool -> LBTargetPoolName
// pool members -> LB Targets hostIP : Port
var lbConfigs []model.LBConfig

Expand All @@ -272,7 +271,7 @@ func (*F5BigIPHandler) GetLBConfigs() ([]model.LBConfig, error) {
}
lbConfig := model.LBConfig{}
lbConfig.LBEndpoint = vServer.Name
lbConfig.LBTargetName = pool.Name
lbConfig.LBTargetPoolName = pool.Name

var nodes []model.LBTarget

Expand Down Expand Up @@ -302,20 +301,17 @@ func (*F5BigIPHandler) GetLBConfigs() ([]model.LBConfig, error) {

}

func (*F5BigIPHandler) TestConnection() (bool, error) {
func (*F5BigIPHandler) TestConnection() error {
return checkF5Connection()
}


func checkF5Connection() (bool, error) {
var works bool
func checkF5Connection() error {
_, err := client.Pools()
if err != nil {
logrus.Errorf("f5 TestConnection: Error listing f5 pool: %v\n", err)
works = false
} else {
logrus.Infof("f5 TestConnection check passed")
works = true
}
return works, err
return err
}

0 comments on commit 871ede3

Please sign in to comment.