diff --git a/pkg/controllers/node/node.go b/pkg/controllers/node/node.go index f8cefc4..ef5d889 100644 --- a/pkg/controllers/node/node.go +++ b/pkg/controllers/node/node.go @@ -78,14 +78,6 @@ func (c *NodeManager) nodeAdd(obj interface{}) { } defer client.Close() - // TODO: Login every 30 minutes - err = c.wattTimeClient.Login() - if err != nil { - log.Printf("failed to login: %v\n", err) - - return - } - index, err := c.wattTimeClient.Index() if err != nil { log.Printf("failed to get index: %v\n", err) diff --git a/pkg/controllers/pod/pod.go b/pkg/controllers/pod/pod.go index 58f713a..6e05665 100644 --- a/pkg/controllers/pod/pod.go +++ b/pkg/controllers/pod/pod.go @@ -44,13 +44,6 @@ func (c *PodManager) podAdd(obj interface{}) { return } - // TODO: Login every 30 minutes - err := c.wattTimeClient.Login() - if err != nil { - log.Printf("failed to login: %v\n", err) - return - } - index, err := c.wattTimeClient.Index() if err != nil { log.Printf("failed to get index: %v\n", err) diff --git a/pkg/plugins/emissions/emissions.go b/pkg/plugins/emissions/emissions.go index 66cda6a..312c01c 100644 --- a/pkg/plugins/emissions/emissions.go +++ b/pkg/plugins/emissions/emissions.go @@ -3,6 +3,7 @@ package emissions import ( "context" "fmt" + "log" "time" v1 "k8s.io/api/core/v1" @@ -54,6 +55,12 @@ func New(obj runtime.Object, h framework.Handle) (framework.Plugin, error) { } wattTimeClient := watttime.NewClient(args.WattTimeUsername, args.WattTimePassword, args.WattTimeBA) + err = wattTimeClient.Login() + if err != nil { + log.Printf("failed to login to WattTime: %v\n", err) + } + + go wattTimeLoginLoop(wattTimeClient) nodeFactory := informers.NewSharedInformerFactory(clientset, 5*time.Minute) nodeManager, err := node.NewNodeManager(nodeFactory, clientset, wattTimeClient) @@ -110,3 +117,16 @@ func (e *Emissions) PreFilter(ctx context.Context, state *framework.CycleState, func (e *Emissions) PreFilterExtensions() framework.PreFilterExtensions { return nil } + +func wattTimeLoginLoop(wattTimeClient *watttime.Client) { + tick := time.Tick(15 * time.Minute) + + for { + <-tick + + err := wattTimeClient.Login() + if err != nil { + log.Printf("failed to login to WattTime: %v\n", err) + } + } +}