Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"net/http"
"net/url"
"os"
"os/signal"
Expand All @@ -12,6 +13,7 @@ import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials/stscreds"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/heptiolabs/healthcheck"
log "github.com/sirupsen/logrus"
"github.com/zalando-incubator/kube-aws-iam-controller/pkg/clientset"
kingpin "gopkg.in/alecthomas/kingpin.v2"
Expand All @@ -23,6 +25,7 @@ const (
defaultRefreshLimit = "15m"
defaultEventQueueSize = "10"
defaultClientGOTimeout = 30 * time.Second
healthEndpointAddress = ":7979"
)

var (
Expand Down Expand Up @@ -104,13 +107,16 @@ func main() {

podsEventCh := make(chan *PodEvent, config.EventQueueSize)

healthReporter := healthcheck.NewHandler()

controller := NewSecretsController(
client,
config.Namespace,
config.Interval,
config.RefreshLimit,
credsGetter,
podsEventCh,
healthReporter,
)

podWatcher := NewPodWatcher(client, config.Namespace, podsEventCh)
Expand All @@ -128,6 +134,11 @@ func main() {
go awsIAMRoleController.Run(ctx)

podWatcher.Run(ctx)
go serveHealthz(healthEndpointAddress)

// Add the liveness endpoint at /healthz
http.HandleFunc("/healthz", controller.healthReporter.LiveEndpoint)

controller.Run(ctx)
}

Expand All @@ -139,3 +150,14 @@ func handleSigterm(cancelFunc func()) {
log.Info("Received Term signal. Terminating...")
cancelFunc()
}

// serve the HTTP endpoint for livenessProbe
func serveHealthz(address string) {
// Start the HTTP server
err := http.ListenAndServe(address, nil)
if err != nil {
log.Error(err)
} else {
log.Debug("Health server is running.")
}
}
31 changes: 12 additions & 19 deletions secrets_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"encoding/json"
"fmt"
"net/http"
"strings"
"time"

Expand Down Expand Up @@ -34,7 +33,6 @@ aws_expiration = %s
credential_process = cat /meta/aws-iam/credentials.json
`
credentialsJSONFileKey = "credentials.json"
healthEndpointAddress = ":8080"
)

var (
Expand All @@ -51,7 +49,7 @@ type SecretsController struct {
roleStore *RoleStore
podEvents <-chan *PodEvent
namespace string
HealthReporter healthcheck.Handler
healthReporter healthcheck.Handler
}

// ProcessCredentials defines the format expected from process credentials.
Expand All @@ -65,15 +63,16 @@ type ProcessCredentials struct {
}

// NewSecretsController initializes a new SecretsController.
func NewSecretsController(client kubernetes.Interface, namespace string, interval, refreshLimit time.Duration, creds CredentialsGetter, podEvents <-chan *PodEvent) *SecretsController {
func NewSecretsController(client kubernetes.Interface, namespace string, interval, refreshLimit time.Duration, creds CredentialsGetter, podEvents <-chan *PodEvent, healthReporter healthcheck.Handler) *SecretsController {
return &SecretsController{
client: client,
interval: interval,
refreshLimit: refreshLimit,
creds: creds,
roleStore: NewRoleStore(),
podEvents: podEvents,
namespace: namespace,
client: client,
interval: interval,
refreshLimit: refreshLimit,
creds: creds,
roleStore: NewRoleStore(),
podEvents: podEvents,
namespace: namespace,
healthReporter: healthReporter,
}
}

Expand Down Expand Up @@ -121,8 +120,8 @@ func (c *SecretsController) Run(ctx context.Context) {
var nextRefresh time.Time

// If the controller hasn't refreshed credentials in a while, fail liveness
c.HealthReporter.AddLivenessCheck("nextRefresh", func() error {
if time.Since(nextRefresh) > 5*c.interval {
c.healthReporter.AddLivenessCheck("nextRefresh", func() error {
if time.Since(nextRefresh) > 5*(c.interval) {
return fmt.Errorf("nextRefresh too old")
}
return nil
Expand All @@ -132,12 +131,6 @@ func (c *SecretsController) Run(ctx context.Context) {

nextRefresh = time.Now().Add(-c.interval)

// Add the liveness endpoint at /healthz
http.HandleFunc("/healthz", c.HealthReporter.LiveEndpoint)

// Start the HTTP server
http.ListenAndServe(healthEndpointAddress, nil)

for {
select {
case <-time.After(time.Until(nextRefresh)):
Expand Down
2 changes: 2 additions & 0 deletions secrets_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"
"time"

"github.com/heptiolabs/healthcheck"
"github.com/stretchr/testify/require"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -164,6 +165,7 @@ func TestRefresh(tt *testing.T) {
},
},
make(chan *PodEvent, 1),
healthcheck.NewHandler(),
)

// setup secrets
Expand Down