代码拉取完成,页面将自动刷新
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() {
// If the dir doesn't have a corresponding cluster delete it
if _, ok := clusterMap[file.Name()]; !ok {
clusterPath := path.Join(jailPath, file.Name())
err = os.RemoveAll(clusterPath)
if err != nil {
logrus.Warnf("Error attempting to delete jail %v: %v", clusterPath, err)
}
}
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。