Skip to content

Commit 61596ee

Browse files
author
Noor Malik
committed
Fix segv due to bad initialization
1 parent f0f75bf commit 61596ee

File tree

3 files changed

+27
-16
lines changed

3 files changed

+27
-16
lines changed

main.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/aws/aws-sdk-go/aws"
1414
"github.com/aws/aws-sdk-go/aws/credentials/stscreds"
1515
"github.com/aws/aws-sdk-go/aws/session"
16+
"github.com/heptiolabs/healthcheck"
1617
log "github.com/sirupsen/logrus"
1718
"github.com/zalando-incubator/kube-aws-iam-controller/pkg/clientset"
1819
kingpin "gopkg.in/alecthomas/kingpin.v2"
@@ -106,13 +107,16 @@ func main() {
106107

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

110+
healthReporter := healthcheck.NewHandler()
111+
109112
controller := NewSecretsController(
110113
client,
111114
config.Namespace,
112115
config.Interval,
113116
config.RefreshLimit,
114117
credsGetter,
115118
podsEventCh,
119+
healthReporter,
116120
)
117121

118122
podWatcher := NewPodWatcher(client, config.Namespace, podsEventCh)
@@ -130,7 +134,11 @@ func main() {
130134
go awsIAMRoleController.Run(ctx)
131135

132136
podWatcher.Run(ctx)
133-
go serveHealthz(controller, healthEndpointAddress)
137+
go serveHealthz(healthEndpointAddress)
138+
139+
// Add the liveness endpoint at /healthz
140+
http.HandleFunc("/healthz", controller.healthReporter.LiveEndpoint)
141+
134142
controller.Run(ctx)
135143
}
136144

@@ -144,12 +152,11 @@ func handleSigterm(cancelFunc func()) {
144152
}
145153

146154
// serve the HTTP endpoint for livenessProbe
147-
func serveHealthz(controller *SecretsController, address string) {
148-
// Add the liveness endpoint at /healthz
149-
http.Handle("/healthz", controller.HealthReporter)
155+
func serveHealthz(address string) {
156+
println("Endpoint is live!")
150157

151158
// Start the HTTP server
152-
err := http.ListenAndServe(address, controller.HealthReporter)
159+
err := http.ListenAndServe(address, nil)
153160
if err != nil {
154161
log.Error(err)
155162
}

secrets_controller.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ type SecretsController struct {
4949
roleStore *RoleStore
5050
podEvents <-chan *PodEvent
5151
namespace string
52-
HealthReporter healthcheck.Handler
52+
healthReporter healthcheck.Handler
5353
}
5454

5555
// ProcessCredentials defines the format expected from process credentials.
@@ -63,15 +63,16 @@ type ProcessCredentials struct {
6363
}
6464

6565
// NewSecretsController initializes a new SecretsController.
66-
func NewSecretsController(client kubernetes.Interface, namespace string, interval, refreshLimit time.Duration, creds CredentialsGetter, podEvents <-chan *PodEvent) *SecretsController {
66+
func NewSecretsController(client kubernetes.Interface, namespace string, interval, refreshLimit time.Duration, creds CredentialsGetter, podEvents <-chan *PodEvent, healthReporter healthcheck.Handler) *SecretsController {
6767
return &SecretsController{
68-
client: client,
69-
interval: interval,
70-
refreshLimit: refreshLimit,
71-
creds: creds,
72-
roleStore: NewRoleStore(),
73-
podEvents: podEvents,
74-
namespace: namespace,
68+
client: client,
69+
interval: interval,
70+
refreshLimit: refreshLimit,
71+
creds: creds,
72+
roleStore: NewRoleStore(),
73+
podEvents: podEvents,
74+
namespace: namespace,
75+
healthReporter: healthReporter,
7576
}
7677
}
7778

@@ -115,12 +116,13 @@ func (c *SecretsController) getCreds(role string) (map[string][]byte, error) {
115116
// Run runs the secret controller loop. This will refresh secrets with AWS IAM
116117
// roles.
117118
func (c *SecretsController) Run(ctx context.Context) {
119+
println("Controller is running!")
118120
// Defining the liveness check
119121
var nextRefresh time.Time
120122

121123
// If the controller hasn't refreshed credentials in a while, fail liveness
122-
c.HealthReporter.AddLivenessCheck("nextRefresh", func() error {
123-
if time.Since(nextRefresh) > 5*c.interval {
124+
c.healthReporter.AddLivenessCheck("nextRefresh", func() error {
125+
if time.Since(nextRefresh) > 5*(c.interval) {
124126
return fmt.Errorf("nextRefresh too old")
125127
}
126128
return nil

secrets_controller_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"testing"
66
"time"
77

8+
"github.com/heptiolabs/healthcheck"
89
"github.com/stretchr/testify/require"
910
v1 "k8s.io/api/core/v1"
1011
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -164,6 +165,7 @@ func TestRefresh(tt *testing.T) {
164165
},
165166
},
166167
make(chan *PodEvent, 1),
168+
healthcheck.NewHandler(),
167169
)
168170

169171
// setup secrets

0 commit comments

Comments
 (0)