37 Star 407 Fork 74

GVPrancher/rancher

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
template.go 2.09 KB
一键复制 编辑 原始数据 按行查看 历史
package podsecuritypolicy
import (
"fmt"
"github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/rancher/types/config"
"k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/tools/cache"
)
const psptpbByPSPTNameIndex = "something.something.psptpb/pspt-name"
func Register(management *config.ManagementContext) {
psptpbInformer := management.Management.PodSecurityPolicyTemplateProjectBindings("").Controller().Informer()
psptpbIndexers := map[string]cache.IndexFunc{
psptpbByPSPTNameIndex: PSPTPBByPSPTName,
}
psptpbInformer.AddIndexers(psptpbIndexers)
lifecycle := &lifecycle{
psptpbs: management.Management.PodSecurityPolicyTemplateProjectBindings(""),
psptpbLister: management.Management.PodSecurityPolicyTemplateProjectBindings("").Controller().Lister(),
psptpbIndexer: psptpbInformer.GetIndexer(),
}
management.Management.PodSecurityPolicyTemplates("").AddLifecycle("mgmt-pspt-lfc-handler", lifecycle)
}
type lifecycle struct {
psptpbs v3.PodSecurityPolicyTemplateProjectBindingInterface
psptpbLister v3.PodSecurityPolicyTemplateProjectBindingLister
psptpbIndexer cache.Indexer
}
func (l *lifecycle) Create(obj *v3.PodSecurityPolicyTemplate) (*v3.PodSecurityPolicyTemplate, error) {
return obj, nil
}
func (l *lifecycle) Updated(obj *v3.PodSecurityPolicyTemplate) (*v3.PodSecurityPolicyTemplate, error) {
return obj, nil
}
func (l *lifecycle) Remove(obj *v3.PodSecurityPolicyTemplate) (*v3.PodSecurityPolicyTemplate, error) {
psptpbs, err := l.psptpbIndexer.ByIndex(psptpbByPSPTNameIndex, obj.Name)
if err != nil {
return nil, fmt.Errorf("error getting psptpbs: %v", err)
}
for _, rawPSPTPB := range psptpbs {
psptpb := rawPSPTPB.(*v3.PodSecurityPolicyTemplateProjectBinding)
err := l.psptpbs.Delete(psptpb.Name, &v1.DeleteOptions{})
if err != nil {
return nil, fmt.Errorf("error deleting psptpb: %v", err)
}
}
return obj, nil
}
func PSPTPBByPSPTName(obj interface{}) ([]string, error) {
psptpb, ok := obj.(*v3.PodSecurityPolicyTemplateProjectBinding)
if !ok {
return []string{}, nil
}
return []string{psptpb.PodSecurityPolicyTemplateName}, nil
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/rancher/rancher.git
git@gitee.com:rancher/rancher.git
rancher
rancher
rancher
v2.0.16-rc1

搜索帮助