Skip to content

Commit

Permalink
Merge pull request #8 from gkarthiks/fix/additional
Browse files Browse the repository at this point in the history
fixing the addons
  • Loading branch information
gkarthiks authored Jul 28, 2020
2 parents 6ea35fe + 58b9725 commit a2a2f0f
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 18 deletions.
2 changes: 0 additions & 2 deletions globals/globalVars.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ var (
)

const (
HealthCheckPath = ":8200/v1/sys/seal-status"
ProxyPath = ":8200"
DefaultTimeOut = "1"
DefaultBalancerPort = 8000
)
2 changes: 2 additions & 0 deletions helper/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ func GetVaultIPsFromLabelSelectors(vaultPool *types.VaultPool) {
}
log.Infof("Finalized pods discovery process with label selector. Obtained the IP Address %v", reflect.ValueOf(globals.VaultIPList).MapKeys())
}

log.Printf("Vault Pool data at the end of GetVault IPs %v", vaultPool)
}

// GetAttemptsFromContext returns the attempts for a request
Expand Down
31 changes: 15 additions & 16 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,7 @@ import (
)

func init() {
log.SetFormatter(&log.JSONFormatter{
FieldMap: log.FieldMap{
"FieldKeyTime": "@timestamp",
"version": "@BuildVersion",
},
CallerPrettyfier: nil,
PrettyPrint: false,
})
log.SetFormatter(&log.JSONFormatter{})
log.Infof("Vault Balancer running version: `%v`", BuildVersion)

globals.K8s, _ = discovery.NewK8s()
Expand Down Expand Up @@ -58,30 +51,36 @@ func init() {
}

var (
VersionLogger = log.WithFields(log.Fields{"vlb_version": BuildVersion})
BuildVersion = "dev"
balancerPort int
vaultPool types.VaultPool
)

const (
HealthCheckPath = ":8200/v1/sys/seal-status"
ProxyPath = ":8200"
)

func main() {
go startRoutine()
go startRoutine(context.Background())

// start the balancer http service
server := http.Server{
Addr: fmt.Sprintf(":%d", balancerPort),
Handler: http.HandlerFunc(loadBalance),
}
//
log.Infof("Vault Balancer started and running at :%d", balancerPort)
VersionLogger.Infof("Vault Balancer started and running at :%d", balancerPort)
if err := server.ListenAndServe(); err != nil {
log.Fatalf("error while starting the load balance, %v", err)
VersionLogger.Fatalf("error while starting the load balance, %v", err)
}
}

// startRoutine starts the routine work of collecting IPs, setting up reverse
// proxies and doing health check.
func startRoutine() {
log.Info("Starting the routines for discovery, proxy setup and health check")
func startRoutine(context context.Context) {
VersionLogger.Info("Starting the routines for discovery, proxy setup and health check")
t := time.NewTicker(time.Second * 30)
for {
select {
Expand Down Expand Up @@ -115,19 +114,19 @@ func setUpProxies(vaultPool *types.VaultPool) {
log.Infof("Setting up the reverse proxy for %v", reflect.ValueOf(globals.VaultIPList).MapKeys())
for _, individualIP := range globals.VaultIPList {
sanitizedIP := strings.TrimSpace(individualIP)
vaultUrl, err := url.Parse("http://" + sanitizedIP + globals.ProxyPath)
vaultUrl, err := url.Parse("http://" + sanitizedIP + ProxyPath)
if err != nil {
log.Errorf("error occurred while converting string to URL for proxy path. error: %v", err)
}
healthUrl, _ := url.Parse("http://" + sanitizedIP + globals.HealthCheckPath)
healthUrl, _ := url.Parse("http://" + sanitizedIP + HealthCheckPath)

proxy := httputil.NewSingleHostReverseProxy(vaultUrl)
proxy.ErrorHandler = func(writer http.ResponseWriter, request *http.Request, e error) {
log.Infof("[%s] %s\n", vaultUrl.Host, e.Error())
retries := helper.GetRetryFromContext(request)
if retries < 3 {
select {
case <-time.After(10 * time.Millisecond):
case <-time.After(5 * time.Millisecond):
ctx := context.WithValue(request.Context(), helper.Retry, retries+1)
proxy.ServeHTTP(writer, request.WithContext(ctx))
}
Expand Down
1 change: 1 addition & 0 deletions types/vault_pool_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func (vp *VaultPool) AddBackend(vaultBackend *VaultBackend) {
func (vp *VaultPool) RetireBackend(obsoleteIP string) {
for index, currBackend := range vp.vaultBackends {
if currBackend.IP == obsoleteIP {
log.Infof("Retiring the backend %v from list of active IPs", obsoleteIP)
vp.vaultBackends = append(vp.vaultBackends[:index], vp.vaultBackends[index+1:]...)
}
}
Expand Down

0 comments on commit a2a2f0f

Please sign in to comment.