37 Star 411 Fork 76

GVPrancher/rancher

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
catalog_common.go 4.69 KB
一键复制 编辑 原始数据 按行查看 历史
Erik Wilson 提交于 2018-11-06 18:04 . Store templateContent cache on disk
package manager
import (
"fmt"
"github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/rancher/types/client/management/v3"
"github.com/sirupsen/logrus"
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
)
func hasAllUpdates(catalog *v3.Catalog) bool {
upgraded := v3.CatalogConditionUpgraded.IsTrue(catalog)
diskCached := v3.CatalogConditionDiskCached.IsTrue(catalog)
return upgraded && diskCached
}
func isUpToDate(commit string, catalog *v3.Catalog) bool {
commitsEqual := commit == catalog.Status.Commit
updated := hasAllUpdates(catalog)
return commitsEqual && updated
}
func setRefreshed(catalog *v3.Catalog) bool {
logrus.Debugf("Catalog %s is already up to date", catalog.Name)
if !v3.CatalogConditionRefreshed.IsTrue(catalog) {
v3.CatalogConditionRefreshed.True(catalog)
v3.CatalogConditionRefreshed.Reason(catalog, "")
v3.CatalogConditionRefreshed.Message(catalog, "")
return true
}
return false
}
func setRefreshedError(catalog *v3.Catalog, err error) {
v3.CatalogConditionRefreshed.False(catalog)
v3.CatalogConditionRefreshed.ReasonAndMessageFromError(catalog, err)
}
func (m *Manager) deleteTemplates(key string, namespace string) error {
templates, err := m.getTemplateMap(key, namespace)
if err != nil {
return err
}
tvToDelete := map[string]struct{}{}
for _, t := range templates {
tvs, err := m.getTemplateVersion(t.Name, namespace)
if err != nil {
return err
}
for k := range tvs {
tvToDelete[k] = struct{}{}
}
}
go func() {
for {
for k := range templates {
if err := m.templateClient.DeleteNamespaced(namespace, k, &metav1.DeleteOptions{}); err != nil && !kerrors.IsNotFound(err) {
logrus.Warnf("Deleting template %v doesn't succeed. Continue loop", k)
continue
}
}
for k := range tvToDelete {
if err := m.templateVersionClient.DeleteNamespaced(namespace, k, &metav1.DeleteOptions{}); err != nil && !kerrors.IsNotFound(err) {
logrus.Warnf("Deleting templateVersion %v doesn't succeed. Continue loop", k)
continue
}
}
break
}
}()
return nil
}
func getCatalogType(cmt *CatalogInfo) string {
if cmt.projectCatalog == nil && cmt.clusterCatalog == nil {
return client.CatalogType
} else if cmt.projectCatalog != nil {
return client.ProjectCatalogType
} else {
return client.ClusterCatalogType
}
}
func (m *Manager) updateCatalogInfo(cmt *CatalogInfo, catalogType string, templateName string, condition bool, updateOnly bool) (*CatalogInfo, error) {
var obj runtime.Object
if condition {
switch catalogType {
case client.CatalogType:
obj = runtime.Object(cmt.catalog)
case client.ProjectCatalogType:
obj = runtime.Object(cmt.projectCatalog)
case client.ClusterCatalogType:
obj = runtime.Object(cmt.clusterCatalog)
default:
return cmt, fmt.Errorf("incorrect catalog type")
}
v3.CatalogConditionRefreshed.Unknown(obj)
if templateName != "" {
v3.CatalogConditionRefreshed.Message(obj, fmt.Sprintf("syncing template %v", templateName))
} else {
v3.CatalogConditionRefreshed.Message(obj, fmt.Sprintf(""))
}
}
if updateOnly {
switch catalogType {
case client.CatalogType:
if _, err := m.catalogClient.Update(cmt.catalog); err != nil {
return nil, err
}
case client.ProjectCatalogType:
if _, err := m.projectCatalogClient.Update(cmt.projectCatalog); err != nil {
return nil, err
}
case client.ClusterCatalogType:
if _, err := m.clusterCatalogClient.Update(cmt.clusterCatalog); err != nil {
return nil, err
}
default:
return cmt, fmt.Errorf("incorrect catalog type")
}
return cmt, nil
}
switch catalogType {
case client.CatalogType:
catalog := cmt.catalog
if newCatalog, err := m.catalogClient.Update(cmt.catalog); err == nil {
catalog = newCatalog
} else {
catalog, _ = m.catalogClient.Get(catalog.Name, metav1.GetOptions{})
}
cmt.catalog = catalog
case client.ProjectCatalogType:
projectCatalog := cmt.projectCatalog
if newCatalog, err := m.projectCatalogClient.Update(projectCatalog); err == nil {
projectCatalog = newCatalog
} else {
projectCatalog, _ = m.projectCatalogClient.Get(projectCatalog.Name, metav1.GetOptions{})
}
cmt.catalog = &projectCatalog.Catalog
cmt.projectCatalog = projectCatalog
case client.ClusterCatalogType:
clusterCatalog := cmt.clusterCatalog
if newCatalog, err := m.clusterCatalogClient.Update(clusterCatalog); err == nil {
clusterCatalog = newCatalog
} else {
clusterCatalog, _ = m.clusterCatalogClient.Get(clusterCatalog.Name, metav1.GetOptions{})
}
cmt.catalog = &clusterCatalog.Catalog
cmt.clusterCatalog = clusterCatalog
default:
return cmt, fmt.Errorf("incorrect catalog type")
}
return cmt, nil
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/rancher/rancher.git
git@gitee.com:rancher/rancher.git
rancher
rancher
rancher
v2.2.2-patch1-rc1

搜索帮助

0d507c66 1850385 C8b1a773 1850385