Skip to content

Commit

Permalink
Restrict handler to namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
JoelSpeed committed Mar 25, 2020
1 parent 1fc9cf6 commit 9ad5cf7
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
3 changes: 2 additions & 1 deletion cmd/termination-handler/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func main() {

pollIntervalSeconds := flag.Int64("poll-interval-seconds", 5, "interval in seconds at which termination notice endpoint should be checked (Default: 5)")
nodeName := flag.String("node-name", "", "name of the node that the termination handler is running on")
namespace := flag.String("namespace", "", "namespace that the machine for the node should live in. If unspecified, the look for machines across all namespaces.")
flag.Set("logtostderr", "true")
flag.Parse()

Expand All @@ -58,7 +59,7 @@ func main() {
pollInterval := time.Duration(*pollIntervalSeconds) * time.Second

// Construct a termination handler
handler, err := termination.NewHandler(logger, cfg, pollInterval, *nodeName)
handler, err := termination.NewHandler(logger, cfg, pollInterval, *namespace, *nodeName)
if err != nil {
logger.Error(err, "Error constructing termination handler")
return
Expand Down
10 changes: 6 additions & 4 deletions pkg/termination/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type Handler interface {
}

// NewHandler constructs a new Handler
func NewHandler(logger logr.Logger, cfg *rest.Config, pollInterval time.Duration, nodeName string) (Handler, error) {
func NewHandler(logger logr.Logger, cfg *rest.Config, pollInterval time.Duration, namespace, nodeName string) (Handler, error) {
machinev1.AddToScheme(scheme.Scheme)
c, err := client.New(cfg, client.Options{Scheme: scheme.Scheme})
if err != nil {
Expand All @@ -40,13 +40,14 @@ func NewHandler(logger logr.Logger, cfg *rest.Config, pollInterval time.Duration
panic(err)
}

logger = logger.WithValues("node", nodeName)
logger = logger.WithValues("node", nodeName, "namespace", namespace)

return &handler{
client: c,
pollURL: pollURL,
pollInterval: pollInterval,
nodeName: nodeName,
namespace: namespace,
log: logger,
}, nil
}
Expand All @@ -58,6 +59,7 @@ type handler struct {
pollURL *url.URL
pollInterval time.Duration
nodeName string
namespace string
log logr.Logger
}

Expand Down Expand Up @@ -92,7 +94,7 @@ func (h *handler) run(ctx context.Context, wg *sync.WaitGroup) error {
return fmt.Errorf("error fetching machine for node (%q): %v", h.nodeName, err)
}

logger := h.log.WithValues("namespace", machine.Namespace, "machine", machine.Name)
logger := h.log.WithValues("machine", machine.Name)
logger.V(1).Info("Monitoring node for machine")

if err := wait.PollImmediateUntil(h.pollInterval, func() (bool, error) {
Expand Down Expand Up @@ -128,7 +130,7 @@ func (h *handler) run(ctx context.Context, wg *sync.WaitGroup) error {
// getMachineForNodeName finds the Machine associated with the Node name given
func (h *handler) getMachineForNode(ctx context.Context) (*machinev1.Machine, error) {
machineList := &machinev1.MachineList{}
err := h.client.List(ctx, machineList)
err := h.client.List(ctx, machineList, client.InNamespace(h.namespace))
if err != nil {
return nil, fmt.Errorf("error listing machines: %v", err)
}
Expand Down

0 comments on commit 9ad5cf7

Please sign in to comment.