forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jailsync.go
85 lines (71 loc) · 1.91 KB
/
jailsync.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package cron
import (
"fmt"
"io/ioutil"
"os"
"path"
"github.com/sirupsen/logrus"
"github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/rancher/types/config"
"github.com/robfig/cron"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
const (
cronSchedule = "0 0 * * *" // every 24 hours
jailPath = "/opt/jail"
)
type jailSync struct {
clusters v3.ClusterInterface
}
// StartJailSyncCron for cleaning up the /opt/jail dir
func StartJailSyncCron(scaledContext *config.ScaledContext) error {
ref := &jailSync{
clusters: scaledContext.Management.Clusters(""),
}
schedule, err := cron.ParseStandard(cronSchedule)
if err != nil {
return fmt.Errorf("error parsing auth refresh cron: %v", err)
}
c := cron.New()
job := cron.FuncJob(ref.syncJails)
c.Schedule(schedule, job)
c.Start()
return nil
}
// syncJails removes any unneeded jails from old clusters.
func (j *jailSync) syncJails() {
// Get the clusters from the api to ensure we are up to date
clusters, err := j.clusters.List(metav1.ListOptions{})
if err != nil {
logrus.Warnf("Error listing clusters for jail cleanup: %v", err)
}
clusterMap := make(map[string]v3.Cluster)
for _, cluster := range clusters.Items {
clusterMap[cluster.Name] = cluster
}
files, err := ioutil.ReadDir(jailPath)
if err != nil {
if !os.IsNotExist(err) {
logrus.Warnf("Error attempting to get files for jail cleanup: %v", err)
}
// The dir doesn't exist, nothing to do
return
}
for _, file := range files {
if file.IsDir() {
dirName := file.Name()
// Don't drop the KE driver jail
if dirName == "driver-jail" {
continue
}
// If the dir doesn't have a corresponding cluster delete it
if _, ok := clusterMap[dirName]; !ok {
clusterPath := path.Join(jailPath, dirName)
err = os.RemoveAll(clusterPath)
if err != nil {
logrus.Warnf("Error attempting to delete jail %v: %v", clusterPath, err)
}
}
}
}
}