Skip to content

Commit

Permalink
Make backoff MaxInterval and MaxElapsedTime configurable (#33)
Browse files Browse the repository at this point in the history
This makes the max interval and max elapsed time configurabe for the
exponential backoff used when getting a role based on source IP.

The defaults are still the same e.g. 1 minute for MaxInterval and 15
minutes for the MaxElapsedTime.

Fix #28

* Use sane max interval & max elapsed time defaults
  • Loading branch information
mikkeloscar authored and jtblin committed Dec 4, 2016
1 parent d55bfe9 commit 2734a63
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
41 changes: 24 additions & 17 deletions cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http"
"net/http/httputil"
"strings"
"time"

log "github.com/Sirupsen/logrus"
"github.com/cenk/backoff"
Expand All @@ -15,22 +16,24 @@ import (
// Server encapsulates all of the parameters necessary for starting up
// the server. These can either be set via command line or directly.
type Server struct {
APIServer string
APIToken string
AppPort string
BaseRoleARN string
DefaultIAMRole string
IAMRoleKey string
MetadataAddress string
HostInterface string
HostIP string
AddIPTablesRule bool
Insecure bool
Verbose bool
Version bool
iam *iam
k8s *k8s
store *store
APIServer string
APIToken string
AppPort string
BaseRoleARN string
DefaultIAMRole string
IAMRoleKey string
MetadataAddress string
HostInterface string
HostIP string
BackoffMaxInterval time.Duration
BackoffMaxElapsedTime time.Duration
AddIPTablesRule bool
Insecure bool
Verbose bool
Version bool
iam *iam
k8s *k8s
store *store
}

type appHandler func(http.ResponseWriter, *http.Request)
Expand Down Expand Up @@ -63,7 +66,11 @@ func (s *Server) getRole(IP string) (string, error) {
return err
}

err = backoff.Retry(operation, backoff.NewExponentialBackOff())
expBackoff := backoff.NewExponentialBackOff()
expBackoff.MaxInterval = s.BackoffMaxInterval
expBackoff.MaxElapsedTime = s.BackoffMaxElapsedTime

err = backoff.Retry(operation, expBackoff)
if err != nil {
return "", err
}
Expand Down
8 changes: 8 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"runtime"
"time"

log "github.com/Sirupsen/logrus"
"github.com/spf13/pflag"
Expand All @@ -11,6 +12,11 @@ import (
"github.com/jtblin/kube2iam/version"
)

const (
defaultMaxInterval = 1 * time.Second
defaultMaxElapsedTime = 2 * time.Second
)

func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
s := cmd.NewServer()
Expand Down Expand Up @@ -49,6 +55,8 @@ func addFlags(s *cmd.Server, fs *pflag.FlagSet) {
fs.BoolVar(&s.AddIPTablesRule, "iptables", false, "Add iptables rule (also requires --host-ip)")
fs.StringVar(&s.HostInterface, "host-interface", "docker0", "Host interface for proxying AWS metadata")
fs.StringVar(&s.HostIP, "host-ip", s.HostIP, "IP address of host")
fs.DurationVar(&s.BackoffMaxInterval, "backoff-max-interval", defaultMaxInterval, "Max interval for backoff when querying for role.")
fs.DurationVar(&s.BackoffMaxElapsedTime, "backoff-max-elapsed-time", defaultMaxElapsedTime, "Max elapsed time for backoff when querying for role.")
fs.BoolVar(&s.Verbose, "verbose", false, "Verbose")
fs.BoolVar(&s.Version, "version", false, "Print the version and exits")
}

0 comments on commit 2734a63

Please sign in to comment.