37 Star 403 Fork 75

GVPrancher/rancher

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
jailsync.go 1.91 KB
一键复制 编辑 原始数据 按行查看 历史
Dan Ramich 提交于 2019-06-12 13:38 . goimport linting changes
package cron
import (
"fmt"
"io/ioutil"
"os"
"path"
"github.com/sirupsen/logrus"
v3 "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)
}
}
}
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/rancher/rancher.git
git@gitee.com:rancher/rancher.git
rancher
rancher
rancher
v2.2.11-rc1

搜索帮助