-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.go
53 lines (49 loc) · 1.56 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package main
import (
"dynamic-pv-scaling/api"
"dynamic-pv-scaling/logger"
"dynamic-pv-scaling/pkg"
"dynamic-pv-scaling/utils"
"fmt"
"github.com/jasonlvhit/gocron"
log "github.com/sirupsen/logrus"
"strconv"
"time"
)
var (
nameSpace string
scalePercentage int
thresholdPercentage int
pvcName string
)
// ResizeFunction : Is the main driver code checks for PV usage and scales up the PV when and as defined.
func ResizeFunction() {
infos := utils.GetConfigurations()
logger.LogStdout()
for _, info := range infos {
nameSpace = fmt.Sprintf("%v", info["namespace"])
scalePercentage, _ = strconv.Atoi(fmt.Sprintf("%v", info["scale_percentage"]))
thresholdPercentage, _ = strconv.Atoi(fmt.Sprintf("%v", info["threshold_percentage"]))
pvcName = fmt.Sprintf("%v", info["pvc_name"])
if (100 - api.GetPersistentVolumeList(nameSpace, pvcName).Value) >= thresholdPercentage {
updatedSize := utils.CalculateUpdatedSize(api.GetPeristentVolumeUsage(nameSpace, pvcName).Value, scalePercentage)
pkg.ResizePersistentVolume(pvcName, nameSpace, updatedSize)
for _, pod := range pkg.ListPods(nameSpace) {
if pod.PersistentVolumeName == pvcName {
time.Sleep(100 * time.Second)
pkg.DeletePod(pod.PodName, nameSpace)
}
}
} else {
log.WithFields(log.Fields{
"namespace": nameSpace,
"persistent_volume_name": pvcName,
}).Info("Threshold is under control, no need of resizing")
}
}
}
func main() {
scheduler := gocron.NewScheduler()
scheduler.Every(100).Seconds().Do(ResizeFunction)
<-scheduler.Start()
}