37 Star 396 Fork 71

GVPrancher/rancher

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
podsecuritypolicytemplate.go 3.94 KB
一键复制 编辑 原始数据 按行查看 历史
Nathan Jenan 提交于 2018-04-20 10:20 . Fixing missing removal links
package podsecuritypolicytemplate
import (
"fmt"
"github.com/rancher/norman/types"
"github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/rancher/types/apis/management.cattle.io/v3/schema"
"github.com/rancher/types/client/management/v3"
"github.com/rancher/types/config"
"github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/client-go/tools/cache"
)
type Store struct {
types.Store
}
func (s *Store) Delete(apiContext *types.APIContext, schema *types.Schema, id string) (map[string]interface{}, error) {
projectHasPSPT, err := projectHasPSPTAssigned(apiContext)
if err != nil {
return nil, fmt.Errorf("error checking if PSPT is assigned to projects: %v", err)
}
if projectHasPSPT {
return nil, errors.NewBadRequest("PSPT is assigned to one or more projects, remove PSPT from those " +
"projects before deleting")
}
clusterHasPSPT, err := clusterHasPSPTAssigned(apiContext)
if err != nil {
return nil, fmt.Errorf("error checking if PSPT is assigned to clusters: %v", err)
}
if clusterHasPSPT {
return nil, errors.NewBadRequest("PSPT is assigned to one or more clusters, remove PSPT from those " +
"clusters before deleting")
}
return s.Store.Delete(apiContext, schema, id)
}
const clusterByPSPTKey = "clusterByPSPT"
const projectByPSPTKey = "projectByPSPT"
func NewFormatter(management *config.ScaledContext) types.Formatter {
clusterInformer := management.Management.Clusters("").Controller().Informer()
clusterInformer.AddIndexers(map[string]cache.IndexFunc{
clusterByPSPTKey: clusterByPSPT,
})
projectInformer := management.Management.Projects("").Controller().Informer()
projectInformer.AddIndexers(map[string]cache.IndexFunc{
projectByPSPTKey: projectByPSPT,
})
format := Format{
ClusterIndexer: clusterInformer.GetIndexer(),
ProjectIndexer: projectInformer.GetIndexer(),
}
return format.Formatter
}
func clusterByPSPT(obj interface{}) ([]string, error) {
cluster, ok := obj.(*v3.Cluster)
if !ok {
return []string{}, nil
}
return []string{cluster.Spec.DefaultPodSecurityPolicyTemplateName}, nil
}
func projectByPSPT(obj interface{}) ([]string, error) {
project, ok := obj.(*v3.Project)
if !ok {
return []string{}, nil
}
return []string{project.Status.PodSecurityPolicyTemplateName}, nil
}
type Format struct {
ClusterIndexer cache.Indexer
ProjectIndexer cache.Indexer
}
func (f *Format) Formatter(apiContext *types.APIContext, resource *types.RawResource) {
// check if PSPT is assigned to a cluster or project
projectsWithPSPT, err := f.ProjectIndexer.ByIndex(projectByPSPTKey, resource.ID)
if err != nil {
logrus.Warn("failed to determine if PSPT was assigned to a project: %v", err)
return
}
if len(projectsWithPSPT) != 0 {
// remove delete link
delete(resource.Links, "remove")
return
}
clustersWithPSPT, err := f.ClusterIndexer.ByIndex(clusterByPSPTKey, resource.ID)
if err != nil {
logrus.Warnf("failed to determine if a PSPT was assigned to a cluster: %v", err)
return
}
if len(clustersWithPSPT) != 0 {
// remove delete link
delete(resource.Links, "remove")
return
}
}
func projectHasPSPTAssigned(apiContext *types.APIContext) (bool, error) {
projectSchema := apiContext.Schemas.Schema(&schema.Version, client.ProjectType)
projects, err := projectSchema.Store.List(apiContext, projectSchema, &types.QueryOptions{
Conditions: []*types.QueryCondition{
types.NewConditionFromString(client.ProjectFieldPodSecurityPolicyTemplateName, types.ModifierEQ,
apiContext.ID),
},
})
return len(projects) != 0, err
}
func clusterHasPSPTAssigned(apiContext *types.APIContext) (bool, error) {
clusterSchema := apiContext.Schemas.Schema(&schema.Version, client.ClusterType)
clusters, err := clusterSchema.Store.List(apiContext, clusterSchema, &types.QueryOptions{
Conditions: []*types.QueryCondition{
types.NewConditionFromString(client.ClusterFieldDefaultPodSecurityPolicyTemplateId, types.ModifierEQ,
apiContext.ID),
},
})
return len(clusters) != 0, err
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/rancher/rancher.git
git@gitee.com:rancher/rancher.git
rancher
rancher
rancher
v2.0.1

搜索帮助

344bd9b3 5694891 D2dac590 5694891