2015-08-18 14:39:49 +00:00
|
|
|
/*
|
2016-06-03 00:25:58 +00:00
|
|
|
Copyright 2015 The Kubernetes Authors.
|
2015-08-18 14:39:49 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package job
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"k8s.io/kubernetes/pkg/api"
|
2016-04-18 15:44:19 +00:00
|
|
|
"k8s.io/kubernetes/pkg/apis/batch"
|
|
|
|
"k8s.io/kubernetes/pkg/apis/batch/validation"
|
2016-12-03 19:06:03 +00:00
|
|
|
metav1 "k8s.io/kubernetes/pkg/apis/meta/v1"
|
2015-08-18 14:39:49 +00:00
|
|
|
"k8s.io/kubernetes/pkg/fields"
|
|
|
|
"k8s.io/kubernetes/pkg/labels"
|
|
|
|
"k8s.io/kubernetes/pkg/registry/generic"
|
|
|
|
"k8s.io/kubernetes/pkg/runtime"
|
2016-08-23 03:41:21 +00:00
|
|
|
"k8s.io/kubernetes/pkg/storage"
|
2015-11-06 23:30:52 +00:00
|
|
|
"k8s.io/kubernetes/pkg/util/validation/field"
|
2015-08-18 14:39:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// jobStrategy implements verification logic for Replication Controllers.
|
|
|
|
type jobStrategy struct {
|
|
|
|
runtime.ObjectTyper
|
|
|
|
api.NameGenerator
|
|
|
|
}
|
|
|
|
|
|
|
|
// Strategy is the default logic that applies when creating and updating Replication Controller objects.
|
|
|
|
var Strategy = jobStrategy{api.Scheme, api.SimpleNameGenerator}
|
|
|
|
|
|
|
|
// NamespaceScoped returns true because all jobs need to be within a namespace.
|
|
|
|
func (jobStrategy) NamespaceScoped() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// PrepareForCreate clears the status of a job before creation.
|
2016-08-08 20:15:33 +00:00
|
|
|
func (jobStrategy) PrepareForCreate(ctx api.Context, obj runtime.Object) {
|
2016-04-18 15:44:19 +00:00
|
|
|
job := obj.(*batch.Job)
|
|
|
|
job.Status = batch.JobStatus{}
|
2015-08-18 14:39:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// PrepareForUpdate clears fields that are not allowed to be set by end users on update.
|
2016-08-08 20:15:33 +00:00
|
|
|
func (jobStrategy) PrepareForUpdate(ctx api.Context, obj, old runtime.Object) {
|
2016-04-18 15:44:19 +00:00
|
|
|
newJob := obj.(*batch.Job)
|
|
|
|
oldJob := old.(*batch.Job)
|
2015-08-18 14:39:49 +00:00
|
|
|
newJob.Status = oldJob.Status
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate validates a new job.
|
2015-11-06 23:30:52 +00:00
|
|
|
func (jobStrategy) Validate(ctx api.Context, obj runtime.Object) field.ErrorList {
|
2016-04-18 15:44:19 +00:00
|
|
|
job := obj.(*batch.Job)
|
Added Selector Generation to Job.
Added selector generation to Job's
strategy.Validate, right before validation.
Can't do in defaulting since UID is not known.
Added a validation to Job to ensure that the generated
labels and selector are correct when generation was requested.
This happens right after generation, but validation is in a better
place to return an error.
Adds "manualSelector" field to batch/v1 Job to control selector generation.
Adds same field to extensions/__internal. Conversion between those two
is automatic.
Adds "autoSelector" field to extensions/v1beta1 Job. Used for storing batch/v1 Jobs
- Default for v1 is to do generation.
- Default for v1beta1 is to not do it.
- In both cases, unset == false == do the default thing.
Release notes:
Added batch/v1 group, which contains just Job, and which is the next
version of extensions/v1beta1 Job.
The changes from the previous version are:
- Users no longer need to ensure labels on their pod template are unique to the enclosing
job (but may add labels as needed for categorization).
- In v1beta1, job.spec.selector was defaulted from pod labels, with the user responsible for uniqueness.
In v1, a unique label is generated and added to the pod template, and used as the selector (other
labels added by user stay on pod template, but need not be used by selector).
- a new field called "manualSelector" field exists to control whether the new behavior is used,
versus a more error-prone but more flexible "manual" (not generated) seletor. Most users
will not need to use this field and should leave it unset.
Users who are creating extensions.Job go objects and then posting them using the go client
will see a change in the default behavior. They need to either stop providing a selector (relying on
selector generation) or else specify "spec.manualSelector" until they are ready to do the former.
2016-02-08 23:55:40 +00:00
|
|
|
// TODO: move UID generation earlier and do this in defaulting logic?
|
|
|
|
if job.Spec.ManualSelector == nil || *job.Spec.ManualSelector == false {
|
|
|
|
generateSelector(job)
|
|
|
|
}
|
2015-08-18 14:39:49 +00:00
|
|
|
return validation.ValidateJob(job)
|
|
|
|
}
|
|
|
|
|
Added Selector Generation to Job.
Added selector generation to Job's
strategy.Validate, right before validation.
Can't do in defaulting since UID is not known.
Added a validation to Job to ensure that the generated
labels and selector are correct when generation was requested.
This happens right after generation, but validation is in a better
place to return an error.
Adds "manualSelector" field to batch/v1 Job to control selector generation.
Adds same field to extensions/__internal. Conversion between those two
is automatic.
Adds "autoSelector" field to extensions/v1beta1 Job. Used for storing batch/v1 Jobs
- Default for v1 is to do generation.
- Default for v1beta1 is to not do it.
- In both cases, unset == false == do the default thing.
Release notes:
Added batch/v1 group, which contains just Job, and which is the next
version of extensions/v1beta1 Job.
The changes from the previous version are:
- Users no longer need to ensure labels on their pod template are unique to the enclosing
job (but may add labels as needed for categorization).
- In v1beta1, job.spec.selector was defaulted from pod labels, with the user responsible for uniqueness.
In v1, a unique label is generated and added to the pod template, and used as the selector (other
labels added by user stay on pod template, but need not be used by selector).
- a new field called "manualSelector" field exists to control whether the new behavior is used,
versus a more error-prone but more flexible "manual" (not generated) seletor. Most users
will not need to use this field and should leave it unset.
Users who are creating extensions.Job go objects and then posting them using the go client
will see a change in the default behavior. They need to either stop providing a selector (relying on
selector generation) or else specify "spec.manualSelector" until they are ready to do the former.
2016-02-08 23:55:40 +00:00
|
|
|
// generateSelector adds a selector to a job and labels to its template
|
|
|
|
// which can be used to uniquely identify the pods created by that job,
|
|
|
|
// if the user has requested this behavior.
|
2016-04-18 15:44:19 +00:00
|
|
|
func generateSelector(obj *batch.Job) {
|
Added Selector Generation to Job.
Added selector generation to Job's
strategy.Validate, right before validation.
Can't do in defaulting since UID is not known.
Added a validation to Job to ensure that the generated
labels and selector are correct when generation was requested.
This happens right after generation, but validation is in a better
place to return an error.
Adds "manualSelector" field to batch/v1 Job to control selector generation.
Adds same field to extensions/__internal. Conversion between those two
is automatic.
Adds "autoSelector" field to extensions/v1beta1 Job. Used for storing batch/v1 Jobs
- Default for v1 is to do generation.
- Default for v1beta1 is to not do it.
- In both cases, unset == false == do the default thing.
Release notes:
Added batch/v1 group, which contains just Job, and which is the next
version of extensions/v1beta1 Job.
The changes from the previous version are:
- Users no longer need to ensure labels on their pod template are unique to the enclosing
job (but may add labels as needed for categorization).
- In v1beta1, job.spec.selector was defaulted from pod labels, with the user responsible for uniqueness.
In v1, a unique label is generated and added to the pod template, and used as the selector (other
labels added by user stay on pod template, but need not be used by selector).
- a new field called "manualSelector" field exists to control whether the new behavior is used,
versus a more error-prone but more flexible "manual" (not generated) seletor. Most users
will not need to use this field and should leave it unset.
Users who are creating extensions.Job go objects and then posting them using the go client
will see a change in the default behavior. They need to either stop providing a selector (relying on
selector generation) or else specify "spec.manualSelector" until they are ready to do the former.
2016-02-08 23:55:40 +00:00
|
|
|
if obj.Spec.Template.Labels == nil {
|
|
|
|
obj.Spec.Template.Labels = make(map[string]string)
|
|
|
|
}
|
|
|
|
// The job-name label is unique except in cases that are expected to be
|
|
|
|
// quite uncommon, and is more user friendly than uid. So, we add it as
|
|
|
|
// a label.
|
|
|
|
_, found := obj.Spec.Template.Labels["job-name"]
|
|
|
|
if found {
|
|
|
|
// User asked us to not automatically generate a selector and labels,
|
|
|
|
// but set a possibly conflicting value. If there is a conflict,
|
|
|
|
// we will reject in validation.
|
|
|
|
} else {
|
|
|
|
obj.Spec.Template.Labels["job-name"] = string(obj.ObjectMeta.Name)
|
|
|
|
}
|
|
|
|
// The controller-uid label makes the pods that belong to this job
|
|
|
|
// only match this job.
|
|
|
|
_, found = obj.Spec.Template.Labels["controller-uid"]
|
|
|
|
if found {
|
|
|
|
// User asked us to automatically generate a selector and labels,
|
|
|
|
// but set a possibly conflicting value. If there is a conflict,
|
|
|
|
// we will reject in validation.
|
|
|
|
} else {
|
|
|
|
obj.Spec.Template.Labels["controller-uid"] = string(obj.ObjectMeta.UID)
|
|
|
|
}
|
|
|
|
// Select the controller-uid label. This is sufficient for uniqueness.
|
|
|
|
if obj.Spec.Selector == nil {
|
2016-12-03 18:57:26 +00:00
|
|
|
obj.Spec.Selector = &metav1.LabelSelector{}
|
Added Selector Generation to Job.
Added selector generation to Job's
strategy.Validate, right before validation.
Can't do in defaulting since UID is not known.
Added a validation to Job to ensure that the generated
labels and selector are correct when generation was requested.
This happens right after generation, but validation is in a better
place to return an error.
Adds "manualSelector" field to batch/v1 Job to control selector generation.
Adds same field to extensions/__internal. Conversion between those two
is automatic.
Adds "autoSelector" field to extensions/v1beta1 Job. Used for storing batch/v1 Jobs
- Default for v1 is to do generation.
- Default for v1beta1 is to not do it.
- In both cases, unset == false == do the default thing.
Release notes:
Added batch/v1 group, which contains just Job, and which is the next
version of extensions/v1beta1 Job.
The changes from the previous version are:
- Users no longer need to ensure labels on their pod template are unique to the enclosing
job (but may add labels as needed for categorization).
- In v1beta1, job.spec.selector was defaulted from pod labels, with the user responsible for uniqueness.
In v1, a unique label is generated and added to the pod template, and used as the selector (other
labels added by user stay on pod template, but need not be used by selector).
- a new field called "manualSelector" field exists to control whether the new behavior is used,
versus a more error-prone but more flexible "manual" (not generated) seletor. Most users
will not need to use this field and should leave it unset.
Users who are creating extensions.Job go objects and then posting them using the go client
will see a change in the default behavior. They need to either stop providing a selector (relying on
selector generation) or else specify "spec.manualSelector" until they are ready to do the former.
2016-02-08 23:55:40 +00:00
|
|
|
}
|
|
|
|
if obj.Spec.Selector.MatchLabels == nil {
|
|
|
|
obj.Spec.Selector.MatchLabels = make(map[string]string)
|
|
|
|
}
|
|
|
|
if _, found := obj.Spec.Selector.MatchLabels["controller-uid"]; !found {
|
|
|
|
obj.Spec.Selector.MatchLabels["controller-uid"] = string(obj.ObjectMeta.UID)
|
|
|
|
}
|
|
|
|
// If the user specified matchLabel controller-uid=$WRONGUID, then it should fail
|
|
|
|
// in validation, either because the selector does not match the pod template
|
|
|
|
// (controller-uid=$WRONGUID does not match controller-uid=$UID, which we applied
|
|
|
|
// above, or we will reject in validation because the template has the wrong
|
|
|
|
// labels.
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: generalize generateSelector so it can work for other controller
|
|
|
|
// objects such as ReplicaSet. Can use pkg/api/meta to generically get the
|
|
|
|
// UID, but need some way to generically access the selector and pod labels
|
|
|
|
// fields.
|
|
|
|
|
2015-11-13 05:13:16 +00:00
|
|
|
// Canonicalize normalizes the object after validation.
|
|
|
|
func (jobStrategy) Canonicalize(obj runtime.Object) {
|
|
|
|
}
|
|
|
|
|
2015-08-18 14:39:49 +00:00
|
|
|
func (jobStrategy) AllowUnconditionalUpdate() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// AllowCreateOnUpdate is false for jobs; this means a POST is needed to create one.
|
|
|
|
func (jobStrategy) AllowCreateOnUpdate() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// ValidateUpdate is the default update validation for an end user.
|
2015-11-06 23:30:52 +00:00
|
|
|
func (jobStrategy) ValidateUpdate(ctx api.Context, obj, old runtime.Object) field.ErrorList {
|
2016-04-18 15:44:19 +00:00
|
|
|
validationErrorList := validation.ValidateJob(obj.(*batch.Job))
|
|
|
|
updateErrorList := validation.ValidateJobUpdate(obj.(*batch.Job), old.(*batch.Job))
|
2015-08-18 14:39:49 +00:00
|
|
|
return append(validationErrorList, updateErrorList...)
|
|
|
|
}
|
|
|
|
|
2015-09-22 08:05:54 +00:00
|
|
|
type jobStatusStrategy struct {
|
|
|
|
jobStrategy
|
|
|
|
}
|
|
|
|
|
|
|
|
var StatusStrategy = jobStatusStrategy{Strategy}
|
|
|
|
|
2016-08-08 20:15:33 +00:00
|
|
|
func (jobStatusStrategy) PrepareForUpdate(ctx api.Context, obj, old runtime.Object) {
|
2016-04-18 15:44:19 +00:00
|
|
|
newJob := obj.(*batch.Job)
|
|
|
|
oldJob := old.(*batch.Job)
|
2015-09-22 08:05:54 +00:00
|
|
|
newJob.Spec = oldJob.Spec
|
|
|
|
}
|
|
|
|
|
2015-11-06 23:30:52 +00:00
|
|
|
func (jobStatusStrategy) ValidateUpdate(ctx api.Context, obj, old runtime.Object) field.ErrorList {
|
2016-04-18 15:44:19 +00:00
|
|
|
return validation.ValidateJobUpdateStatus(obj.(*batch.Job), old.(*batch.Job))
|
2015-09-22 08:05:54 +00:00
|
|
|
}
|
|
|
|
|
2015-08-18 14:39:49 +00:00
|
|
|
// JobSelectableFields returns a field set that represents the object for matching purposes.
|
2016-04-18 15:44:19 +00:00
|
|
|
func JobToSelectableFields(job *batch.Job) fields.Set {
|
2016-08-23 14:34:02 +00:00
|
|
|
objectMetaFieldsSet := generic.ObjectMetaFieldsSet(&job.ObjectMeta, true)
|
2015-10-15 23:53:26 +00:00
|
|
|
specificFieldsSet := fields.Set{
|
2016-04-27 04:35:14 +00:00
|
|
|
"status.successful": strconv.Itoa(int(job.Status.Succeeded)),
|
2015-08-18 14:39:49 +00:00
|
|
|
}
|
2015-10-15 23:53:26 +00:00
|
|
|
return generic.MergeFieldsSets(objectMetaFieldsSet, specificFieldsSet)
|
2015-08-18 14:39:49 +00:00
|
|
|
}
|
|
|
|
|
2016-11-16 09:19:55 +00:00
|
|
|
// GetAttrs returns labels and fields of a given object for filtering purposes.
|
|
|
|
func GetAttrs(obj runtime.Object) (labels.Set, fields.Set, error) {
|
|
|
|
job, ok := obj.(*batch.Job)
|
|
|
|
if !ok {
|
|
|
|
return nil, nil, fmt.Errorf("Given object is not a job.")
|
|
|
|
}
|
|
|
|
return labels.Set(job.ObjectMeta.Labels), JobToSelectableFields(job), nil
|
|
|
|
}
|
|
|
|
|
2015-08-18 14:39:49 +00:00
|
|
|
// MatchJob is the filter used by the generic etcd backend to route
|
|
|
|
// watch events from etcd to clients of the apiserver only interested in specific
|
|
|
|
// labels/fields.
|
2016-08-23 03:41:21 +00:00
|
|
|
func MatchJob(label labels.Selector, field fields.Selector) storage.SelectionPredicate {
|
|
|
|
return storage.SelectionPredicate{
|
2016-11-16 09:19:55 +00:00
|
|
|
Label: label,
|
|
|
|
Field: field,
|
|
|
|
GetAttrs: GetAttrs,
|
2015-08-18 14:39:49 +00:00
|
|
|
}
|
|
|
|
}
|