代码拉取完成,页面将自动刷新
package providerrefresh
import (
"context"
"fmt"
"time"
"github.com/pkg/errors"
"github.com/rancher/rancher/pkg/auth/tokens"
"github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/rancher/types/config"
"github.com/robfig/cron"
"github.com/sirupsen/logrus"
)
var (
ref *refresher
c = cron.New()
)
func StartRefreshDaemon(ctx context.Context, scaledContext *config.ScaledContext, mgmtContext *config.ManagementContext, refreshCronTime string, maxAge string) {
ref = &refresher{
tokenLister: mgmtContext.Management.Tokens("").Controller().Lister(),
tokens: mgmtContext.Management.Tokens(""),
userLister: mgmtContext.Management.Users("").Controller().Lister(),
tokenMGR: tokens.NewManager(ctx, scaledContext),
userAttributes: mgmtContext.Management.UserAttributes(""),
userAttributeLister: mgmtContext.Management.UserAttributes("").Controller().Lister(),
settingLister: mgmtContext.Management.Settings("").Controller().Lister(),
}
UpdateRefreshMaxAge(maxAge)
UpdateRefreshCronTime(refreshCronTime)
}
func UpdateRefreshCronTime(refreshCronTime string) {
if ref == nil {
return
}
parsed, err := ParseCron(refreshCronTime)
if err != nil {
logrus.Errorf("%v", err)
return
}
c.Stop()
c = cron.New()
if parsed != nil {
job := cron.FuncJob(RefreshAllForCron)
c.Schedule(parsed, job)
c.Start()
}
}
func UpdateRefreshMaxAge(maxAge string) {
if ref == nil {
return
}
ref.ensureMaxAgeUpToDate(maxAge)
}
func RefreshAllForCron() {
if ref == nil {
return
}
logrus.Debug("Triggering auth refresh cron")
ref.refreshAll(false)
}
func RefreshAttributes(attribs *v3.UserAttribute) (*v3.UserAttribute, error) {
if ref == nil {
return nil, errors.Errorf("refresh daemon not yet initialized")
}
logrus.Debugf("Starting refresh process for %v", attribs.Name)
modified, err := ref.refreshAttributes(attribs)
if err != nil {
return nil, err
}
logrus.Debugf("Finished refresh process for %v", attribs.Name)
modified.LastRefresh = time.Now().UTC().Format(time.RFC3339)
modified.NeedsRefresh = false
return modified, nil
}
func ParseMaxAge(setting string) (time.Duration, error) {
durString := fmt.Sprintf("%vs", setting)
dur, err := time.ParseDuration(durString)
if err != nil {
return 0, fmt.Errorf("Error parsing auth refresh max age: %v", err)
}
return dur, nil
}
func ParseCron(setting string) (cron.Schedule, error) {
if setting == "" {
return nil, nil
}
schedule, err := cron.ParseStandard(setting)
if err != nil {
return nil, fmt.Errorf("Error parsing auth refresh cron: %v", err)
}
return schedule, nil
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。