Skip to content

Commit

Permalink
Minor refactoring: consistent log.Fatalf usage, ordering of vars, d…
Browse files Browse the repository at this point in the history
…efaults as constants
  • Loading branch information
jtblin committed Apr 25, 2017
1 parent 6470cad commit 2336325
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 27 deletions.
10 changes: 5 additions & 5 deletions cmd/iam.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ import (
var cache = ccache.New(ccache.Configure())

const (
ttl = time.Minute * 15
maxSessNameLength = 64
fullArnPrefix = "arn:"
maxSessNameLength = 64
ttl = time.Minute * 15
)

type iam struct {
Expand All @@ -27,13 +27,13 @@ type iam struct {

// credentials represent the security credentials response.
type credentials struct {
AccessKeyID string `json:"AccessKeyId"`
Code string
Expiration string
LastUpdated string
Type string
AccessKeyID string `json:"AccessKeyId"`
SecretAccessKey string
Token string
Expiration string
Type string
}

func (iam *iam) roleARN(role string) string {
Expand Down
27 changes: 19 additions & 8 deletions cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ import (
"github.com/gorilla/mux"
)

const (
defaultAppPort = "8181"
defaultIAMRoleKey = "iam.amazonaws.com/role"
defaultMaxElapsedTime = 2 * time.Second
defaultMaxInterval = 1 * time.Second
defaultMetadataAddress = "169.254.169.254"
defaultNamespaceKey = "iam.amazonaws.com/allowed-roles"
)

// 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 {
Expand All @@ -28,18 +37,18 @@ type Server struct {
HostIP string
NamespaceKey string
AddIPTablesRule bool
AutoDiscoverBaseArn bool
AutoDiscoverDefaultRole bool
Debug bool
Insecure bool
NamespaceRestriction bool
Verbose bool
Version bool
NamespaceRestriction bool
AutoDiscoverBaseArn bool
AutoDiscoverDefaultRole bool
iam *iam
k8s *k8s
store *store
BackoffMaxInterval time.Duration
BackoffMaxElapsedTime time.Duration
BackoffMaxInterval time.Duration
}

type appHandler func(http.ResponseWriter, *http.Request)
Expand Down Expand Up @@ -208,9 +217,11 @@ func (s *Server) Run(host, token string, insecure bool) error {
// NewServer will create a new Server with default values.
func NewServer() *Server {
return &Server{
AppPort: "8181",
IAMRoleKey: "iam.amazonaws.com/role",
MetadataAddress: "169.254.169.254",
NamespaceKey: "iam.amazonaws.com/allowed-roles",
AppPort: defaultAppPort,
BackoffMaxElapsedTime: defaultMaxElapsedTime,
IAMRoleKey: defaultIAMRoleKey,
BackoffMaxInterval: defaultMaxInterval,
MetadataAddress: defaultMetadataAddress,
NamespaceKey: defaultNamespaceKey,
}
}
20 changes: 6 additions & 14 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"runtime"
"time"

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

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

func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
s := cmd.NewServer()
addFlags(s, pflag.CommandLine)
pflag.Parse()

// default to info or above (probably the default anyways)
log.SetLevel(log.InfoLevel)

if s.Verbose {
log.SetLevel(log.DebugLevel)
}
Expand All @@ -40,7 +32,7 @@ func main() {
}
arn, err := cmd.GetBaseArn()
if err != nil {
log.Fatal(err)
log.Fatalf("%s", err)
}
log.Infof("base ARN autodetected, %s", arn)
s.BaseRoleARN = arn
Expand All @@ -52,7 +44,7 @@ func main() {
}
arn, err := cmd.GetBaseArn()
if err != nil {
log.Fatal(err)
log.Fatalf("%s", err)
}
s.BaseRoleARN = arn
instanceIAMRole, err := cmd.GetInstanceIamRole()
Expand All @@ -65,12 +57,12 @@ func main() {

if s.AddIPTablesRule {
if err := iptables.AddRule(s.AppPort, s.MetadataAddress, s.HostInterface, s.HostIP); err != nil {
log.Fatal(err)
log.Fatalf("%s", err)
}
}

if err := s.Run(s.APIServer, s.APIToken, s.Insecure); err != nil {
log.Fatal(err)
log.Fatalf("%s", err)
}
}

Expand All @@ -92,8 +84,8 @@ func addFlags(s *cmd.Server, fs *pflag.FlagSet) {
fs.BoolVar(&s.NamespaceRestriction, "namespace-restrictions", false, "Enable namespace restrictions")
fs.StringVar(&s.NamespaceKey, "namespace-key", s.NamespaceKey, "Namespace annotation key used to retrieve the IAM roles allowed (value in annotation should be json array)")
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.DurationVar(&s.BackoffMaxInterval, "backoff-max-interval", s.BackoffMaxInterval, "Max interval for backoff when querying for role.")
fs.DurationVar(&s.BackoffMaxElapsedTime, "backoff-max-elapsed-time", s.BackoffMaxElapsedTime, "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 2336325

Please sign in to comment.