Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removed repeated validation and unused code #183

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 0 additions & 68 deletions pkg/admission/admission_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,74 +84,6 @@ func ToAdmissionResponse(err error) *v1beta1.AdmissionResponse {
}
}

func CheckPolicyDuplicate(policies []v1alpha1.LifecyclePolicy) (string, bool) {
policyEvents := map[v1alpha1.Event]v1alpha1.Event{}
hasDuplicate := false
var duplicateInfo string

for _, policy := range policies {
if _, found := policyEvents[policy.Event]; found {
hasDuplicate = true
duplicateInfo = fmt.Sprintf("%v", policy.Event)
break
} else {
policyEvents[policy.Event] = policy.Event
}
}

if _, found := policyEvents[v1alpha1.AnyEvent]; found && len(policyEvents) > 1 {
hasDuplicate = true
duplicateInfo = "if there's * here, no other policy should be here"
}

return duplicateInfo, hasDuplicate
}

func ValidatePolicies(policies []v1alpha1.LifecyclePolicy) error {
var err error
policyEvents := map[v1alpha1.Event]struct{}{}
exitCodes := map[int32]struct{}{}

for _, policy := range policies {
if policy.Event != "" && policy.ExitCode != nil {
err = multierror.Append(err, fmt.Errorf("must not specify event and exitCode simultaneously"))
break
}

if policy.Event == "" && policy.ExitCode == nil {
err = multierror.Append(err, fmt.Errorf("either event and exitCode should be specified"))
break
}

if policy.Event != "" {
// TODO: check event is in supported Event
if _, found := policyEvents[policy.Event]; found {
err = multierror.Append(err, fmt.Errorf("duplicate event %v", policy.Event))
break
} else {
policyEvents[policy.Event] = struct{}{}
}
} else {
if *policy.ExitCode == 0 {
err = multierror.Append(err, fmt.Errorf("0 is not a valid error code"))
break
}
if _, found := exitCodes[*policy.ExitCode]; found {
err = multierror.Append(err, fmt.Errorf("duplicate exitCode %v", *policy.ExitCode))
break
} else {
exitCodes[*policy.ExitCode] = struct{}{}
}
}
}

if _, found := policyEvents[v1alpha1.AnyEvent]; found && len(policyEvents) > 1 {
err = multierror.Append(err, fmt.Errorf("if there's * here, no other policy should be here"))
}

return err
}

func DecodeJob(object runtime.RawExtension, resource metav1.GroupVersionResource) (v1alpha1.Job, error) {
jobResource := metav1.GroupVersionResource{Group: v1alpha1.SchemeGroupVersion.Group, Version: v1alpha1.SchemeGroupVersion.Version, Resource: "jobs"}
raw := object.Raw
Expand Down
23 changes: 1 addition & 22 deletions pkg/admission/admit_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package admission

import (
"fmt"
"reflect"
"strings"

"github.com/golang/glog"
Expand All @@ -32,7 +31,7 @@ import (
k8scorev1 "k8s.io/kubernetes/pkg/apis/core/v1"
k8scorevalid "k8s.io/kubernetes/pkg/apis/core/validation"

v1alpha1 "volcano.sh/volcano/pkg/apis/batch/v1alpha1"
"volcano.sh/volcano/pkg/apis/batch/v1alpha1"
"volcano.sh/volcano/pkg/controllers/job/plugins"
)

Expand Down Expand Up @@ -117,11 +116,6 @@ func validateJob(job v1alpha1.Job, reviewResponse *v1beta1.AdmissionResponse) st
taskNames[task.Name] = task.Name
}

//duplicate task event policies
if duplicateInfo, ok := CheckPolicyDuplicate(task.Policies); ok {
msg = msg + fmt.Sprintf(" duplicated task event policies: %s;", duplicateInfo)
}

if err := validatePolicies(task.Policies, field.NewPath("spec.tasks.policies")); err != nil {
msg = msg + err.Error() + fmt.Sprintf(" valid events are %v, valid actions are %v",
getValidEvents(), getValidActions())
Expand All @@ -134,11 +128,6 @@ func validateJob(job v1alpha1.Job, reviewResponse *v1beta1.AdmissionResponse) st
msg = msg + " 'minAvailable' should not be greater than total replicas in tasks;"
}

//duplicate job event policies
if duplicateInfo, ok := CheckPolicyDuplicate(job.Spec.Policies); ok {
msg = msg + fmt.Sprintf(" duplicated job event policies: %s;", duplicateInfo)
}

if err := validatePolicies(job.Spec.Policies, field.NewPath("spec.policies")); err != nil {
msg = msg + err.Error() + fmt.Sprintf(" valid events are %v, valid actions are %v;",
getValidEvents(), getValidActions())
Expand All @@ -164,16 +153,6 @@ func validateJob(job v1alpha1.Job, reviewResponse *v1beta1.AdmissionResponse) st
return msg
}

func specDeepEqual(newJob v1alpha1.Job, oldJob v1alpha1.Job, reviewResponse *v1beta1.AdmissionResponse) string {
var msg string
if !reflect.DeepEqual(newJob.Spec, oldJob.Spec) {
reviewResponse.Allowed = false
msg = "job.spec is not allowed to modify when update jobs;"
}

return msg
}

func validateTaskTemplate(task v1alpha1.TaskSpec, job v1alpha1.Job, index int) string {
var v1PodTemplate v1.PodTemplate
v1PodTemplate.Template = *task.Template.DeepCopy()
Expand Down