@@ -3,7 +3,6 @@ package handler
33import (
44 "context"
55 "fmt"
6- "sync"
76 "time"
87
98 "github.com/sirupsen/logrus"
@@ -15,11 +14,8 @@ import (
1514 "k8s.io/apimachinery/pkg/runtime"
1615)
1716
18- // Keeps track of currently active timers for paused deployments
19- var (
20- activeTimers = make (map [string ]* time.Timer )
21- timerLock = sync.Mutex {}
22- )
17+ // Keeps track of currently active timers
18+ var activeTimers = make (map [string ]* time.Timer )
2319
2420// Returns unique key for the activeTimers map
2521func getTimerKey (namespace , deploymentName string ) string {
@@ -98,9 +94,7 @@ func PauseDeployment(deployment *app.Deployment, clients kube.Clients, deploymen
9894
9995 // Check if a timer already exists for this deployment
10096 timerKey := getTimerKey (namespace , deploymentName )
101- timerLock .Lock ()
10297 _ , timerExists := activeTimers [timerKey ]
103- timerLock .Unlock ()
10498
10599 if ! timerExists {
106100 logrus .Warnf ("Timer does not exist for already paused deployment '%s' in namespace '%s', creating new one" ,
@@ -121,17 +115,17 @@ func HandleMissingTimer(deployment *app.Deployment, pauseDuration time.Duration,
121115 deploymentName , namespace , err )
122116 ResumeDeployment (deploymentName , namespace , clients )
123117 } else if pauseStartTime != nil {
124- elapsedTime := time .Since (* pauseStartTime )
125- remainingTime := pauseDuration - elapsedTime
118+ elapsedPauseTime := time .Since (* pauseStartTime )
119+ remainingPauseTime := pauseDuration - elapsedPauseTime
126120
127- if remainingTime <= 0 {
121+ if remainingPauseTime <= 0 {
128122 logrus .Infof ("Pause period for deployment '%s' in namespace '%s' has expired. Resuming immediately" ,
129123 deploymentName , namespace )
130124 ResumeDeployment (deploymentName , namespace , clients )
131125 } else {
132126 logrus .Infof ("Creating missing timer for already paused deployment '%s' in namespace '%s' with remaining time %s" ,
133- deploymentName , namespace , remainingTime )
134- CreateResumeTimer (deployment , clients , deploymentName , namespace , remainingTime )
127+ deploymentName , namespace , remainingPauseTime )
128+ CreateResumeTimer (deployment , clients , deploymentName , namespace , remainingPauseTime )
135129 }
136130 }
137131}
@@ -141,7 +135,6 @@ func CreateResumeTimer(deployment *app.Deployment, clients kube.Clients, deploym
141135 timerKey := getTimerKey (namespace , deploymentName )
142136
143137 // Check if there's an existing timer for this deployment
144- timerLock .Lock ()
145138 if _ , exists := activeTimers [timerKey ]; exists {
146139 logrus .Debugf ("Timer already exists for deployment '%s' in namespace '%s', Skipping creation" ,
147140 deploymentName , namespace )
@@ -155,7 +148,6 @@ func CreateResumeTimer(deployment *app.Deployment, clients kube.Clients, deploym
155148
156149 // Add the new timer to the map
157150 activeTimers [timerKey ] = timer
158- timerLock .Unlock ()
159151
160152 logrus .Debugf ("Created pause timer for deployment '%s' in namespace '%s' with duration %s" ,
161153 deploymentName , namespace , pauseDuration )
@@ -176,13 +168,11 @@ func ResumeDeployment(deploymentName, namespace string, clients kube.Clients) {
176168
177169 // Remove the timer itself
178170 timerKey := getTimerKey (namespace , deploymentName )
179- timerLock .Lock ()
180171 if timer , exists := activeTimers [timerKey ]; exists {
181172 timer .Stop ()
182173 delete (activeTimers , timerKey )
183174 logrus .Debugf ("Removed pause timer for deployment '%s' in namespace '%s'" , deploymentName , namespace )
184175 }
185- timerLock .Unlock ()
186176
187177 deployment .Spec .Paused = false
188178 delete (deployment .Annotations , options .PauseDeploymentTimeAnnotation )
0 commit comments