代码拉取完成,页面将自动刷新
package kontainerdriver
import (
"github.com/rancher/norman/types"
v3 "github.com/rancher/types/apis/management.cattle.io/v3"
client "github.com/rancher/types/client/management/v3"
"github.com/rancher/types/config"
"github.com/sirupsen/logrus"
"k8s.io/client-go/tools/cache"
)
type Format struct {
ClusterIndexer cache.Indexer
}
func NewFormatter(manangement *config.ScaledContext) types.Formatter {
clusterInformer := manangement.Management.Clusters("").Controller().Informer()
// use an indexer instead of expensive k8s api calls
clusterInformer.AddIndexers(map[string]cache.IndexFunc{
clusterByKontainerDriverKey: clusterByKontainerDriver,
})
format := Format{
ClusterIndexer: clusterInformer.GetIndexer(),
}
return format.Formatter
}
const clusterByKontainerDriverKey = "clusterbyKontainerDriver"
func clusterByKontainerDriver(obj interface{}) ([]string, error) {
cluster, ok := obj.(*v3.Cluster)
if !ok {
return []string{}, nil
}
return []string{cluster.Status.Driver}, nil
}
func (f *Format) Formatter(request *types.APIContext, resource *types.RawResource) {
state, ok := resource.Values["state"].(string)
if ok {
if state == "active" {
resource.AddAction(request, "deactivate")
}
if state == "inactive" {
resource.AddAction(request, "activate")
}
}
// if cluster driver is a built-in, delete removal link from UI
if builtIn, _ := resource.Values[client.KontainerDriverFieldBuiltIn].(bool); builtIn {
delete(resource.Links, "remove")
}
resName := resource.Values[client.KontainerDriverFieldName]
// resName will be nil when first added
if resName != nil {
clustersWithKontainerDriver, err := f.ClusterIndexer.ByIndex(clusterByKontainerDriverKey, resName.(string))
if err != nil {
logrus.Warnf("failed to determine if kontainer driver %v was in use by a cluster : %v", resName.(string), err)
} else if len(clustersWithKontainerDriver) != 0 {
// if cluster driver in use, delete removal link from UI
delete(resource.Links, "remove")
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。