37 Star 403 Fork 75

GVPrancher/rancher

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
validator.go 1.14 KB
一键复制 编辑 原始数据 按行查看 历史
dax 提交于 2019-02-19 13:36 . PSPT Validation
package podsecuritypolicytemplate
import (
"errors"
"strings"
"github.com/rancher/norman/httperror"
"github.com/rancher/norman/types"
"github.com/rancher/norman/types/convert"
"k8s.io/apimachinery/pkg/util/validation/field"
"k8s.io/kubernetes/pkg/apis/policy"
"k8s.io/kubernetes/pkg/apis/policy/validation"
)
// Validator uses k8s Pod Security Policy validation to prevent creation of pod security policy templates that are invalid
func Validator(request *types.APIContext, schema *types.Schema, data map[string]interface{}) error {
var spec policy.PodSecurityPolicySpec // k8s psp
if err := convert.ToObj(data, &spec); err != nil {
return httperror.WrapAPIError(err, httperror.InvalidBodyContent, "Pod Security Policy spec conversion error")
}
var allErrs field.ErrorList
allErrs = validation.ValidatePodSecurityPolicySpec(&spec, &field.Path{})
if len(allErrs) > 0 { // concatenate all errors to present in UI
strs := make([]string, len(allErrs))
for i, v := range allErrs {
strs[i] = v.Detail
}
return httperror.WrapAPIError(errors.New(allErrs[0].Type.String()), httperror.InvalidBodyContent, strings.Join(strs, ", "))
}
return nil
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/rancher/rancher.git
git@gitee.com:rancher/rancher.git
rancher
rancher
rancher
v2.3.0-alpha1

搜索帮助