Skip to content

Commit

Permalink
Merge pull request #247 from ltagliamonte-dd/master
Browse files Browse the repository at this point in the history
make configurable the cache refresh interval
  • Loading branch information
Jacobious52 committed Dec 19, 2019
2 parents b4d482c + 2e6efc1 commit 47b54eb
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 15 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,7 @@ Usage of kube2iam:
--metadata-addr string Address for the ec2 metadata (default "169.254.169.254")
--metrics-port string Metrics server http port (default: same as kube2iam server port) (default "8181")
--namespace-key string Namespace annotation key used to retrieve the IAM roles allowed (value in annotation should be json array) (default "iam.amazonaws.com/allowed-roles")
--cache-resync-period Refresh interval for pod and namespace caches
--namespace-restriction-format string Namespace Restriction Format (glob/regexp) (default "glob")
--namespace-restrictions Enable namespace restrictions
--node string Name of the node where kube2iam is running
Expand Down
1 change: 1 addition & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func addFlags(s *server.Server, fs *pflag.FlagSet) {
fs.BoolVar(&s.NamespaceRestriction, "namespace-restrictions", false, "Enable namespace restrictions")
fs.StringVar(&s.NamespaceRestrictionFormat, "namespace-restriction-format", s.NamespaceRestrictionFormat, "Namespace Restriction Format (glob/regexp)")
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.DurationVar(&s.CacheResyncPeriod, "cache-resync-period", s.CacheResyncPeriod, "Kubernetes caches resync period")
fs.StringVar(&s.HostIP, "host-ip", s.HostIP, "IP address of host")
fs.StringVar(&s.NodeName, "node", s.NodeName, "Name of the node where kube2iam is running")
fs.DurationVar(&s.BackoffMaxInterval, "backoff-max-interval", s.BackoffMaxInterval, "Max interval for backoff when querying for role.")
Expand Down
2 changes: 1 addition & 1 deletion iam/iam.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func (iam *Client) AssumeRole(roleARN, externalID string, remoteIP string, sessi
RoleSessionName: aws.String(sessionName(roleARN, remoteIP)),
}
// Only inject the externalID if one was provided with the request
if (externalID != "") {
if externalID != "" {
assumeRoleInput.SetExternalId(externalID)
}
resp, err := svc.AssumeRole(&assumeRoleInput)
Expand Down
8 changes: 3 additions & 5 deletions k8s/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/jtblin/kube2iam"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/pkg/api/v1"
v1 "k8s.io/client-go/pkg/api/v1"
selector "k8s.io/client-go/pkg/fields"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/cache"
Expand All @@ -16,8 +16,6 @@ import (
const (
podIPIndexName = "byPodIP"
namespaceIndexName = "byName"
// Resync period for the kube controller loop.
resyncPeriod = 30 * time.Minute
)

// Client represents a kubernetes client.
Expand All @@ -40,7 +38,7 @@ func (k8s *Client) createPodLW() *cache.ListWatch {
}

// WatchForPods watches for pod changes.
func (k8s *Client) WatchForPods(podEventLogger cache.ResourceEventHandler) cache.InformerSynced {
func (k8s *Client) WatchForPods(podEventLogger cache.ResourceEventHandler, resyncPeriod time.Duration) cache.InformerSynced {
k8s.podIndexer, k8s.podController = cache.NewIndexerInformer(
k8s.createPodLW(),
&v1.Pod{},
Expand All @@ -58,7 +56,7 @@ func (k8s *Client) createNamespaceLW() *cache.ListWatch {
}

// WatchForNamespaces watches for namespaces changes.
func (k8s *Client) WatchForNamespaces(nsEventLogger cache.ResourceEventHandler) cache.InformerSynced {
func (k8s *Client) WatchForNamespaces(nsEventLogger cache.ResourceEventHandler, resyncPeriod time.Duration) cache.InformerSynced {
k8s.namespaceIndexer, k8s.namespaceController = cache.NewIndexerInformer(
k8s.createNamespaceLW(),
&v1.Namespace{},
Expand Down
14 changes: 7 additions & 7 deletions mappings/mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,13 @@ func (r *RoleMapper) DumpDebugInfo() map[string]interface{} {
// NewRoleMapper returns a new RoleMapper for use.
func NewRoleMapper(roleKey string, externalIDKey string, defaultRole string, namespaceRestriction bool, namespaceKey string, iamInstance *iam.Client, kubeStore store, namespaceRestrictionFormat string) *RoleMapper {
return &RoleMapper{
defaultRoleARN: iamInstance.RoleARN(defaultRole),
iamRoleKey: roleKey,
iamExternalIDKey: externalIDKey,
namespaceKey: namespaceKey,
namespaceRestriction: namespaceRestriction,
iam: iamInstance,
store: kubeStore,
defaultRoleARN: iamInstance.RoleARN(defaultRole),
iamRoleKey: roleKey,
iamExternalIDKey: externalIDKey,
namespaceKey: namespaceKey,
namespaceRestriction: namespaceRestriction,
iam: iamInstance,
store: kubeStore,
namespaceRestrictionFormat: namespaceRestrictionFormat,
}
}
8 changes: 6 additions & 2 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const (
defaultMaxInterval = 1 * time.Second
defaultMetadataAddress = "169.254.169.254"
defaultNamespaceKey = "iam.amazonaws.com/allowed-roles"
defaultCacheResyncPeriod = 30 * time.Minute
defaultNamespaceRestrictionFormat = "glob"
healthcheckInterval = 30 * time.Second
)
Expand All @@ -60,6 +61,7 @@ type Server struct {
HostIP string
NodeName string
NamespaceKey string
CacheResyncPeriod time.Duration
LogLevel string
LogFormat string
NamespaceRestrictionFormat string
Expand Down Expand Up @@ -375,8 +377,9 @@ func (s *Server) Run(host, token, nodeName string, insecure bool) error {
s.iam = iam.NewClient(s.BaseRoleARN, s.UseRegionalStsEndpoint)
log.Debugln("Caches have been synced. Proceeding with server.")
s.roleMapper = mappings.NewRoleMapper(s.IAMRoleKey, s.IAMExternalID, s.DefaultIAMRole, s.NamespaceRestriction, s.NamespaceKey, s.iam, s.k8s, s.NamespaceRestrictionFormat)
podSynched := s.k8s.WatchForPods(kube2iam.NewPodHandler(s.IAMRoleKey))
namespaceSynched := s.k8s.WatchForNamespaces(kube2iam.NewNamespaceHandler(s.NamespaceKey))
log.Debugf("Starting pod and namespace sync jobs with %s resync period", s.CacheResyncPeriod.String())
podSynched := s.k8s.WatchForPods(kube2iam.NewPodHandler(s.IAMRoleKey), s.CacheResyncPeriod)
namespaceSynched := s.k8s.WatchForNamespaces(kube2iam.NewNamespaceHandler(s.NamespaceKey), s.CacheResyncPeriod)

synced := false
for i := 0; i < defaultCacheSyncAttempts && !synced; i++ {
Expand Down Expand Up @@ -435,6 +438,7 @@ func NewServer() *Server {
LogFormat: defaultLogFormat,
MetadataAddress: defaultMetadataAddress,
NamespaceKey: defaultNamespaceKey,
CacheResyncPeriod: defaultCacheResyncPeriod,
NamespaceRestrictionFormat: defaultNamespaceRestrictionFormat,
HealthcheckFailReason: "Healthcheck not yet performed",
IAMRoleSessionTTL: defaultIAMRoleSessionTTL,
Expand Down

0 comments on commit 47b54eb

Please sign in to comment.