36 Star 396 Fork 71

GVPrancher / rancher

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
template.go 2.15 KB
一键复制 编辑 原始数据 按行查看 历史
dax 提交于 2019-01-24 09:13 . Fix for incorrect PSPTPB deletion
package podsecuritypolicy
import (
"context"
"fmt"
"k8s.io/apimachinery/pkg/runtime"
"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(ctx context.Context, 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(ctx, "mgmt-pspt-lfc-handler", lifecycle)
}
type lifecycle struct {
psptpbs v3.PodSecurityPolicyTemplateProjectBindingInterface
psptpbLister v3.PodSecurityPolicyTemplateProjectBindingLister
psptpbIndexer cache.Indexer
}
func (l *lifecycle) Create(obj *v3.PodSecurityPolicyTemplate) (runtime.Object, error) {
return obj, nil
}
func (l *lifecycle) Updated(obj *v3.PodSecurityPolicyTemplate) (runtime.Object, error) {
return obj, nil
}
func (l *lifecycle) Remove(obj *v3.PodSecurityPolicyTemplate) (runtime.Object, 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.DeleteNamespaced(psptpb.Namespace, 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.2.2-rc14

搜索帮助

344bd9b3 5694891 D2dac590 5694891