This repository has been archived by the owner on Oct 10, 2024. It is now read-only.
generated from cybozu-go/neco-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change the formula cat-gate uses to schedule pods
Signed-off-by: zeroalphat <taichi-takemura@cybozu.co.jp>
- Loading branch information
1 parent
67a5a1e
commit f4d12f8
Showing
6 changed files
with
174 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package runners | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/cybozu-go/cat-gate/internal/controller" | ||
"sigs.k8s.io/controller-runtime/pkg/log" | ||
) | ||
|
||
const levelDebug = -1 | ||
const gcIntervalHours = 24 | ||
|
||
var historyDeletionDuration = 24 * 60 * 60 // 1 day | ||
|
||
type GarbageCollector struct { | ||
} | ||
|
||
func (gc GarbageCollector) NeedLeaderElection() bool { | ||
return true | ||
} | ||
|
||
func (gc GarbageCollector) Start(ctx context.Context) error { | ||
ticker := time.NewTicker(time.Hour * gcIntervalHours) | ||
defer ticker.Stop() | ||
logger := log.FromContext(ctx) | ||
|
||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return nil | ||
case <-ticker.C: | ||
controller.GateRemovalHistories.Range(func(imageHash, value interface{}) bool { | ||
lastGateRemovalTime := time.UnixMilli(value.(int64)) | ||
// Delete history that has not been updated for a long time to prevent memory leaks. | ||
if time.Since(lastGateRemovalTime) > time.Second*time.Duration(historyDeletionDuration) { | ||
logger.V(levelDebug).Info("delete old history", "image hash", imageHash, "lastGateRemovalTime", lastGateRemovalTime) | ||
controller.GateRemovalHistories.Delete(imageHash) | ||
} | ||
return true | ||
}) | ||
} | ||
} | ||
} |