Skip to content

Commit

Permalink
Merge pull request #968 from k8up-io/add/pod_spec
Browse files Browse the repository at this point in the history
Add full podSpec to all job types
  • Loading branch information
Kidswiss committed May 15, 2024
2 parents 2ddb6ee + c8e9202 commit 212a033
Show file tree
Hide file tree
Showing 39 changed files with 8,418 additions and 41 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,7 @@ jobs:
${{ runner.os }}-go-
- name: Run tests
run: make test

- name: Run integration tests
run: make integration-test
9 changes: 9 additions & 0 deletions api/v1/archive_types.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package v1

import (
"context"
"reflect"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
)

// ArchiveSpec defines the desired state of Archive.
Expand Down Expand Up @@ -87,6 +89,13 @@ func (a *Archive) GetSuccessfulJobsHistoryLimit() *int {
return a.Spec.KeepJobs
}

func (a *Archive) GetPodConfig(ctx context.Context, c client.Client) (*PodConfig, error) {
if a.Spec.RunnableSpec.PodConfigRef == nil {
return nil, nil
}
return NewPodConfig(ctx, a.Spec.RunnableSpec.PodConfigRef.Name, a.GetNamespace(), c)
}

// GetJobObjects returns a sortable list of jobs
func (a *ArchiveList) GetJobObjects() JobObjectList {
items := make(JobObjectList, len(a.Items))
Expand Down
9 changes: 9 additions & 0 deletions api/v1/backup_types.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package v1

import (
"context"
"reflect"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
)

// BackupSpec defines a single backup. It must contain all information to connect to
Expand Down Expand Up @@ -127,6 +129,13 @@ func (b *Backup) GetSuccessfulJobsHistoryLimit() *int {
return b.Spec.KeepJobs
}

func (b *Backup) GetPodConfig(ctx context.Context, c client.Client) (*PodConfig, error) {
if b.Spec.RunnableSpec.PodConfigRef == nil {
return nil, nil
}
return NewPodConfig(ctx, b.Spec.RunnableSpec.PodConfigRef.Name, b.GetNamespace(), c)
}

// GetJobObjects returns a sortable list of jobs
func (b *BackupList) GetJobObjects() JobObjectList {
items := make(JobObjectList, len(b.Items))
Expand Down
9 changes: 9 additions & 0 deletions api/v1/check_types.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package v1

import (
"context"
"reflect"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
)

// CheckSpec defines the desired state of Check. It needs to contain the repository
Expand Down Expand Up @@ -106,6 +108,13 @@ func (c *Check) GetSuccessfulJobsHistoryLimit() *int {
return c.Spec.KeepJobs
}

func (b *Check) GetPodConfig(ctx context.Context, c client.Client) (*PodConfig, error) {
if b.Spec.RunnableSpec.PodConfigRef == nil {
return nil, nil
}
return NewPodConfig(ctx, b.Spec.RunnableSpec.PodConfigRef.Name, b.GetNamespace(), c)
}

// GetJobObjects returns a sortable list of jobs
func (c *CheckList) GetJobObjects() JobObjectList {
items := make(JobObjectList, len(c.Items))
Expand Down
4 changes: 4 additions & 0 deletions api/v1/job_object.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package v1

import (
"context"

corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
)
Expand All @@ -19,6 +21,8 @@ type JobObject interface {
GetPodSecurityContext() *corev1.PodSecurityContext
// GetActiveDeadlineSeconds returns the specified active deadline seconds timeout.
GetActiveDeadlineSeconds() *int64
// GetPodConfig returns the defined PodSpec
GetPodConfig(context.Context, client.Client) (*PodConfig, error)
}

// +k8s:deepcopy-gen=false
Expand Down
60 changes: 60 additions & 0 deletions api/v1/podconfig_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package v1

import (
"context"

corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
)

// +kubebuilder:rbac:groups=k8up.io,resources=podconfigs,verbs=get;list;watch

// PodConfigSpec contains the podTemplate definition.
type PodConfigSpec struct {
Template corev1.PodTemplateSpec `json:"template,omitempty"`
}

// PodConfigStatus defines the observed state of Snapshot
type PodConfigStatus struct {
}

// +kubebuilder:object:root=true
// +kubebuilder:subresource:status

// PodConfig is the Schema for the PodConcig API
// Any annotations and labels set on this object will also be set on
// the final pod.
type PodConfig struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec PodConfigSpec `json:"spec,omitempty"`
Status PodConfigStatus `json:"status,omitempty"`
}

// +kubebuilder:object:root=true

// SnapshotList contains a list of Snapshot
type PodConfigList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []PodConfig `json:"items"`
}

func NewPodConfig(ctx context.Context, name, namespace string, c client.Client) (*PodConfig, error) {
config := &PodConfig{}
err := c.Get(ctx, client.ObjectKey{Name: name, Namespace: namespace}, config)
if err != nil {
if apierrors.IsNotFound(err) {
return nil, nil
}
return nil, err
}
return config, nil
}

func init() {
SchemeBuilder.Register(&PodConfig{}, &PodConfigList{})
}
9 changes: 9 additions & 0 deletions api/v1/prune_types.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package v1

import (
"context"
"reflect"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
)

// PruneSpec needs to contain the repository information as well as the desired
Expand Down Expand Up @@ -115,6 +117,13 @@ func (p *Prune) GetSuccessfulJobsHistoryLimit() *int {
return p.Spec.KeepJobs
}

func (p *Prune) GetPodConfig(ctx context.Context, c client.Client) (*PodConfig, error) {
if p.Spec.RunnableSpec.PodConfigRef == nil {
return nil, nil
}
return NewPodConfig(ctx, p.Spec.RunnableSpec.PodConfigRef.Name, p.GetNamespace(), c)
}

// GetJobObjects returns a sortable list of jobs
func (p *PruneList) GetJobObjects() JobObjectList {
items := make(JobObjectList, len(p.Items))
Expand Down
9 changes: 9 additions & 0 deletions api/v1/restore_types.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package v1

import (
"context"
"reflect"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
)

// RestoreSpec can either contain an S3 restore point or a local one. For the local
Expand Down Expand Up @@ -107,6 +109,13 @@ func (r *Restore) GetSuccessfulJobsHistoryLimit() *int {
return r.Spec.KeepJobs
}

func (r *Restore) GetPodConfig(ctx context.Context, c client.Client) (*PodConfig, error) {
if r.Spec.RunnableSpec.PodConfigRef == nil {
return nil, nil
}
return NewPodConfig(ctx, r.Spec.RunnableSpec.PodConfigRef.Name, r.GetNamespace(), c)
}

// GetJobObjects returns a sortable list of jobs
func (r *RestoreList) GetJobObjects() JobObjectList {
items := make(JobObjectList, len(r.Items))
Expand Down
6 changes: 6 additions & 0 deletions api/v1/runnable_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ type RunnableSpec struct {
// PodSecurityContext describes the security context with which this action shall be executed.
PodSecurityContext *corev1.PodSecurityContext `json:"podSecurityContext,omitempty"`

// PodConfigRef describes the pod spec with wich this action shall be executed.
// It takes precedence over the Resources or PodSecurityContext field.
// It does not allow changing the image or the command of the resulting pod.
// This is for advanced use-cases only. Please only set this if you know what you're doing.
PodConfigRef *corev1.LocalObjectReference `json:"podConfigRef,omitempty"`

// Volumes List of volumes that can be mounted by containers belonging to the pod.
Volumes *[]RunnableVolumeSpec `json:"volumes,omitempty"`

Expand Down
13 changes: 13 additions & 0 deletions api/v1/schedule_types.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package v1

import (
"context"
"strings"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
)

// ScheduleSpec defines the schedules for the various job types.
Expand Down Expand Up @@ -36,6 +38,10 @@ type ScheduleSpec struct {

// PodSecurityContext describes the security context with which actions (such as backups) shall be executed.
PodSecurityContext *corev1.PodSecurityContext `json:"podSecurityContext,omitempty"`

// PodConfigRef will apply the given template to all job definitions in this Schedule.
// It can be overriden for specific jobs if necessary.
PodConfigRef *corev1.LocalObjectReference `json:"podConfigRef,omitempty"`
}

// ScheduleDefinition is the actual cron-type expression that defines the interval of the actions.
Expand Down Expand Up @@ -185,6 +191,13 @@ func (s *Schedule) GetSuccessfulJobsHistoryLimit() *int {
return s.Spec.KeepJobs
}

func (s *Schedule) GetPodConfig(ctx context.Context, c client.Client) (*PodConfig, error) {
if s.Spec.PodConfigRef == nil {
return nil, nil
}
return NewPodConfig(ctx, s.Spec.PodConfigRef.Name, s.GetNamespace(), c)
}

// String casts the value to string.
// "aScheduleDefinition.String()" and "string(aScheduleDefinition)" are equivalent.
func (s ScheduleDefinition) String() string {
Expand Down
100 changes: 100 additions & 0 deletions api/v1/zz_generated.deepcopy.go

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

0 comments on commit 212a033

Please sign in to comment.