Documentation ¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ValidationParams ¶
type ValidationParams struct { Object runtime.Object OldObject runtime.Object ParamObj runtime.Object NamespaceObj *corev1.Namespace UserInfo user.Info }
ValidationParams contains the parameters required to evaluate a ValidatingAdmissionPolicy.
func (ValidationParams) Operation ¶
func (p ValidationParams) Operation() admission.Operation
type Validator ¶
type Validator struct {
// contains filtered or unexported fields
}
func NewValidator ¶
func NewValidator(policy *v1.ValidatingAdmissionPolicy) *Validator
NewValidator compiles the provided ValidatingAdmissionPolicy and generates Validator.
func (*Validator) EvalMatchCondition ¶
func (v *Validator) EvalMatchCondition(p ValidationParams) *matchconditions.MatchResult
EvalMatchCondition evaluates ValidatingAdmissionPolicies' match conditions. It returns the result of the matchCondition evaluation to tell the caller which one is evaluated as 'false'. This is a hack to be able to check the name of failed expressions in matchCondition.
TODO: Remove this func after k/k's Validate func outputs the name of the failed matchCondition.
Example ¶
package main import ( "fmt" "github.com/pfnet/kaptest" v1 "k8s.io/api/admissionregistration/v1" appsv1 "k8s.io/api/apps/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) func main() { samplePolicy := v1.ValidatingAdmissionPolicy{ Spec: v1.ValidatingAdmissionPolicySpec{ MatchConditions: []v1.MatchCondition{ {Name: "is-mutable", Expression: "oldObject.?metadata.?labels['immutable'].orValue('') != 'true'"}, }, }, } sampleDeployment := &appsv1.Deployment{ ObjectMeta: metav1.ObjectMeta{ Name: "simple", Labels: map[string]string{"immutable": "true"}, }, // Spec: appsv1.DeploymentSpec{...} } validator := kaptest.NewValidator(&samplePolicy) result := validator.EvalMatchCondition(kaptest.ValidationParams{OldObject: sampleDeployment}) fmt.Printf("match: %t, condition: %q\n", result.Matches, result.FailedConditionName) }
Output: match: false, condition: "is-mutable"
func (*Validator) Validate ¶
func (v *Validator) Validate(p ValidationParams) (*validating.ValidateResult, error)
Validate evaluates ValidationAdmissionPolicies' validations. ValidationResult contains the result of each validation(Admit, Deny, Error) and the reason if it is evaluated as Deny or Error.
Example ¶
package main import ( "fmt" "github.com/pfnet/kaptest" v1 "k8s.io/api/admissionregistration/v1" appsv1 "k8s.io/api/apps/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/utils/ptr" ) func main() { samplePolicy := v1.ValidatingAdmissionPolicy{ Spec: v1.ValidatingAdmissionPolicySpec{ Validations: []v1.Validation{ {Expression: "object.spec.replicas < 5", Message: "spec.replicas should be less than 5"}, }, }, } sampleDeployment := &appsv1.Deployment{ ObjectMeta: metav1.ObjectMeta{Name: "simple"}, Spec: appsv1.DeploymentSpec{ Replicas: ptr.To(int32(6)), // LabelSelector, PodTemplateSpec... }, } validator := kaptest.NewValidator(&samplePolicy) result, _ := validator.Validate(kaptest.ValidationParams{Object: sampleDeployment}) fmt.Println(result.Decisions[0].Evaluation) }
Output: deny
type ValidatorInterface ¶
type ValidatorInterface interface { EvalMatchCondition(p ValidationParams) *matchconditions.MatchResult Validate(p ValidationParams) (*validating.ValidateResult, error) }
ValidatorInterface is an interface to evaluate ValidatingAdmissionPolicy.