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

feat(api): Make PodTemplateSpec schemaless #77

Merged
merged 3 commits into from
May 3, 2022
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
5 changes: 5 additions & 0 deletions apis/execution/v1alpha1/job_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@ type StartPolicySpec struct {

// JobTemplate specifies how to create a Job with metadata.
type JobTemplate struct {
// Standard object's metadata that will be added to Job. More info:
// https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata
//
// +kubebuilder:validation:XPreserveUnknownFields
// +optional
metav1.ObjectMeta `json:"metadata,omitempty"`

// Specification of the desired behavior of the job.
Expand Down
38 changes: 32 additions & 6 deletions apis/execution/v1alpha1/task_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package v1alpha1

import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// TaskSpec describes a single task in the Job.
Expand Down Expand Up @@ -52,14 +53,39 @@ type TaskSpec struct {
// field must be specified.
type TaskTemplate struct {
// Describes how to create tasks as Pods.
// +optional
Pod *PodTemplateSpec `json:"pod,omitempty"`
}

// PodTemplateSpec describes the data a Pod should have when created from a template.
type PodTemplateSpec struct {
// Standard object's metadata that will be added to Pod. More info:
// https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata
//
// The following fields support context variable substitution:
// +kubebuilder:validation:XPreserveUnknownFields
// +optional
metav1.ObjectMeta `json:"metadata,omitempty"`

// Specification of the desired behavior of the pod. API docs:
// https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.23/#podspec-v1-core
//
// - .spec.containers.*.image
// - .spec.containers.*.command.*
// - .spec.containers.*.args.*
// - .spec.containers.*.env.*.value
// Supports context variable substitution in the following fields for containers
// and initContainers: image, command, args, env.value
//
// +kubebuilder:validation:Type=object
// +kubebuilder:validation:Schemaless
// +kubebuilder:validation:XPreserveUnknownFields
// +mapType=atomic
// +optional
Pod *corev1.PodTemplateSpec `json:"pod,omitempty"`
Spec corev1.PodSpec `json:"spec,omitempty"`
}

// ConvertToCoreSpec converts a PodTemplateSpec into the equivalent
// v1.PodTemplateSpec. This method is needed because we use a custom
// PodTemplateSpec type for the purposes of custom CRD documentation generation.
func (p *PodTemplateSpec) ConvertToCoreSpec() *corev1.PodTemplateSpec {
return &corev1.PodTemplateSpec{
ObjectMeta: p.ObjectMeta,
Spec: p.Spec,
}
}
20 changes: 18 additions & 2 deletions apis/execution/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4,053 changes: 8 additions & 4,045 deletions config/crd/bases/execution.furiko.io_jobconfigs.yaml

Large diffs are not rendered by default.

4,051 changes: 6 additions & 4,045 deletions config/crd/bases/execution.furiko.io_jobs.yaml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pkg/execution/controllers/croncontroller/control_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ var (
Template: &execution.JobTemplateSpec{
Task: execution.TaskSpec{
Template: execution.TaskTemplate{
Pod: &corev1.PodTemplateSpec{
Pod: &execution.PodTemplateSpec{
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Expand Down
4 changes: 2 additions & 2 deletions pkg/execution/controllers/jobcontroller/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ var (
},
}

podTemplate = &corev1.PodTemplateSpec{
podTemplate = &execution.PodTemplateSpec{
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Expand Down Expand Up @@ -223,7 +223,7 @@ var (
}()

// Pod that is to be created.
fakePod, _ = podtaskexecutor.NewPod(fakeJob, podTemplate, 1)
fakePod, _ = podtaskexecutor.NewPod(fakeJob, podTemplate.ConvertToCoreSpec(), 1)

// Pod that adds CreationTimestamp to mimic mutation on apiserver.
fakePodResult = func() *corev1.Pod {
Expand Down
2 changes: 1 addition & 1 deletion pkg/execution/mutation/mutation.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ func (m *Mutator) MutateJobTemplateSpec(spec *v1alpha1.JobTemplateSpec, fldPath
}

// MutatePodTemplateSpec mutates PodTemplateSpec in-place.
func (m *Mutator) MutatePodTemplateSpec(spec *corev1.PodTemplateSpec, fldPath *field.Path) *webhook.Result {
func (m *Mutator) MutatePodTemplateSpec(spec *v1alpha1.PodTemplateSpec, fldPath *field.Path) *webhook.Result {
result := webhook.NewResult()

if spec.Spec.RestartPolicy == "" {
Expand Down
6 changes: 3 additions & 3 deletions pkg/execution/mutation/mutation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ var (
},
}

podTemplateSpecBare = corev1.PodTemplateSpec{
podTemplateSpecBare = v1alpha1.PodTemplateSpec{
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Expand All @@ -123,7 +123,7 @@ var (
},
}

podTemplateSpecBasic = corev1.PodTemplateSpec{
podTemplateSpecBasic = v1alpha1.PodTemplateSpec{
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Expand All @@ -139,7 +139,7 @@ var (
},
}

podTemplateSpecBasic2 = corev1.PodTemplateSpec{
podTemplateSpecBasic2 = v1alpha1.PodTemplateSpec{
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Expand Down
2 changes: 1 addition & 1 deletion pkg/execution/mutation/patcher_jobs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func TestNewJobPatcher(t *testing.T) {
Template: &v1alpha1.JobTemplateSpec{
Task: v1alpha1.TaskSpec{
Template: v1alpha1.TaskTemplate{
Pod: &corev1.PodTemplateSpec{
Pod: &v1alpha1.PodTemplateSpec{
Spec: corev1.PodSpec{
Containers: podTemplateSpecBare.Spec.Containers,
RestartPolicy: corev1.RestartPolicyNever,
Expand Down
4 changes: 2 additions & 2 deletions pkg/execution/taskexecutor/podtaskexecutor/pod_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ func (p *PodTaskClient) CreateIndex(ctx context.Context, index int64) (tasks.Tas
var template *corev1.PodTemplateSpec

// Get pod template
if jobTemplate := p.rj.Spec.Template; jobTemplate != nil {
template = jobTemplate.Task.Template.Pod
if jobTemplate := p.rj.Spec.Template; jobTemplate != nil && jobTemplate.Task.Template.Pod != nil {
template = jobTemplate.Task.Template.Pod.ConvertToCoreSpec()
}
if template == nil {
return nil, errors.New("pod template cannot be empty")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ var (
Template: &execution.JobTemplateSpec{
Task: execution.TaskSpec{
Template: execution.TaskTemplate{
Pod: &corev1.PodTemplateSpec{},
Pod: &execution.PodTemplateSpec{},
},
},
},
Expand Down
9 changes: 5 additions & 4 deletions pkg/execution/validation/corev1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,18 @@ import (
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/validation/field"

"github.com/furiko-io/furiko/apis/execution/v1alpha1"
"github.com/furiko-io/furiko/pkg/execution/validation"
)

var (
podTemplateSpecEmpty = corev1.PodTemplateSpec{
podTemplateSpecEmpty = v1alpha1.PodTemplateSpec{
Spec: corev1.PodSpec{
Containers: []corev1.Container{},
},
}

podTemplateSpecBasic = corev1.PodTemplateSpec{
podTemplateSpecBasic = v1alpha1.PodTemplateSpec{
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Expand All @@ -57,12 +58,12 @@ func TestValidatePodTemplateSpec(t *testing.T) {
}{
{
name: "empty spec",
spec: &podTemplateSpecEmpty,
spec: podTemplateSpecEmpty.ConvertToCoreSpec(),
wantErr: "[].spec.containers: Required value",
},
{
name: "basic spec",
spec: &podTemplateSpecBasic,
spec: podTemplateSpecBasic.ConvertToCoreSpec(),
},
}
for _, tt := range tests {
Expand Down
24 changes: 21 additions & 3 deletions pkg/execution/validation/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -438,10 +438,28 @@ func (v *Validator) ValidateTaskTemplate(spec *v1alpha1.TaskTemplate, fldPath *f
return allErrs
}

// ValidatePodTaskTemplateSpec validates a *corev1.PodTemplateSpec.
func (v *Validator) ValidatePodTaskTemplateSpec(spec *corev1.PodTemplateSpec, fldPath *field.Path) field.ErrorList {
// ValidatePodTaskTemplateSpec validates a *v1alpha1.PodTemplateSpec.
func (v *Validator) ValidatePodTaskTemplateSpec(spec *v1alpha1.PodTemplateSpec, fldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
allErrs = append(allErrs, ValidatePodTemplateSpec(spec, fldPath)...)

// Convert to corev1.PodTemplateSpec.
podTemplateSpec := &corev1.PodTemplateSpec{
ObjectMeta: spec.ObjectMeta,
Spec: spec.Spec,
}

// Invoke the Kubernetes core validator for the PodTemplateSpec here.
//
// Note that even though we do not embed the schema for PodTemplateSpec into our
// CRD, we should still validate here to catch any invalid PodSpec during
// creation of the JobConfig/Job, instead of when the Job is about to create the
// task. We also assume that the Kubernetes core API will not break backwards
// compatibility, and if we happen to invoke an older version of the Kubernetes
// core validator against a newer API, it SHOULD NOT break (i.e. cannot save the
// JobConfig). If a newer validation rule was added in a newer K8s version, then
// unfortunately we cannot catch it in this scenario, and we will fall back to
// failing with AdmissionRefusedError only at task creation time.
allErrs = append(allErrs, ValidatePodTemplateSpec(podTemplateSpec, fldPath)...)

// Cannot use Always for restartPolicy.
if restartPolicy := spec.Spec.RestartPolicy; restartPolicy == corev1.RestartPolicyAlways {
Expand Down
4 changes: 2 additions & 2 deletions pkg/execution/validation/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ func TestValidateJobConfig(t *testing.T) {
Spec: v1alpha1.JobTemplateSpec{
Task: v1alpha1.TaskSpec{
Template: v1alpha1.TaskTemplate{
Pod: &corev1.PodTemplateSpec{
Pod: &v1alpha1.PodTemplateSpec{
Spec: corev1.PodSpec{
Containers: []corev1.Container{{Name: "container", Image: "alpine"}},
RestartPolicy: corev1.RestartPolicyAlways,
Expand Down Expand Up @@ -594,7 +594,7 @@ func TestValidateJob(t *testing.T) {
Template: &v1alpha1.JobTemplateSpec{
Task: v1alpha1.TaskSpec{
Template: v1alpha1.TaskTemplate{
Pod: &corev1.PodTemplateSpec{
Pod: &v1alpha1.PodTemplateSpec{
Spec: corev1.PodSpec{
Containers: []corev1.Container{{Name: "container", Image: "alpine"}},
RestartPolicy: corev1.RestartPolicyAlways,
Expand Down