Skip to content

Commit 2320543

Browse files
committed
[backups] Implement a job strategy controller
## What this PR does A new `Job.strategy.backups.cozystack.io` resource is implemented to represent a backup strategy that uses a one-shot job to create and upload a backup. An accompanying controller is also created to respond to BackupJobs that are created with a reference to this strategy. This implementation can be used as a reference by third-party app developers to implement their own strategies and controllers. ### Release note ```release-note [backups] Implement a job backup strategy and controller ``` Signed-off-by: Timofei Larkin <[email protected]>
1 parent 4f5ae28 commit 2320543

12 files changed

Lines changed: 663 additions & 19 deletions

File tree

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
Copyright 2025.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
// Package v1alpha1 contains API Schema definitions for the v1alpha1 API group.
18+
// +kubebuilder:object:generate=true
19+
// +groupName=strategy.backups.cozystack.io
20+
package v1alpha1
21+
22+
import (
23+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
24+
"k8s.io/apimachinery/pkg/runtime"
25+
"k8s.io/apimachinery/pkg/runtime/schema"
26+
)
27+
28+
var (
29+
// GroupVersion is group version used to register these objects.
30+
GroupVersion = schema.GroupVersion{Group: "strategy.backups.cozystack.io", Version: "v1alpha1"}
31+
32+
// SchemeBuilder is used to add go types to the GroupVersionKind scheme.
33+
SchemeBuilder = runtime.NewSchemeBuilder(addKnownTypes)
34+
35+
// AddToScheme adds the types in this group-version to the given scheme.
36+
AddToScheme = SchemeBuilder.AddToScheme
37+
)
38+
39+
func addKnownTypes(scheme *runtime.Scheme) error {
40+
scheme.AddKnownTypes(
41+
GroupVersion,
42+
&Job{},
43+
&JobList{},
44+
)
45+
metav1.AddToGroupVersion(scheme, GroupVersion)
46+
return nil
47+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
// Package v1alpha1 defines strategy.backups.cozystack.io API types.
3+
//
4+
// Group: strategy.backups.cozystack.io
5+
// Version: v1alpha1
6+
package v1alpha1
7+
8+
import (
9+
corev1 "k8s.io/api/core/v1"
10+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
11+
)
12+
13+
// +kubebuilder:object:root=true
14+
// +kubebuilder:subresource:status
15+
// +kubebuilder:resource:scope=Cluster
16+
17+
const (
18+
JobStrategyKind = "Job"
19+
)
20+
21+
// Job defines a backup strategy using a one-shot Job
22+
type Job struct {
23+
metav1.TypeMeta `json:",inline"`
24+
metav1.ObjectMeta `json:"metadata,omitempty"`
25+
26+
Spec JobSpec `json:"spec,omitempty"`
27+
Status JobStatus `json:"status,omitempty"`
28+
}
29+
30+
// +kubebuilder:object:root=true
31+
32+
// JobList contains a list of backup Jobs.
33+
type JobList struct {
34+
metav1.TypeMeta `json:",inline"`
35+
metav1.ListMeta `json:"metadata,omitempty"`
36+
Items []Job `json:"items"`
37+
}
38+
39+
// JobSpec specifies the desired behavior of a backup job.
40+
type JobSpec struct {
41+
// Template holds a PodTemplateSpec with the right shape to
42+
// run a single pod to completion and create a tarball with
43+
// a given apps data. Helm-like Go templates are supported.
44+
// The values of the source application are available under
45+
// `.Values`. `.Release.Name` and `.Release.Namespace` are
46+
// also exported.
47+
Template corev1.PodTemplateSpec `json:"template"`
48+
}
49+
50+
type JobStatus struct {
51+
Conditions []metav1.Condition `json:"conditions,omitempty"`
52+
}

‎api/backups/strategy/v1alpha1/zz_generated.deepcopy.go‎

Lines changed: 123 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎api/backups/v1alpha1/backupjob_types.go‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ type BackupJobStatus struct {
7272
}
7373

7474
// +kubebuilder:object:root=true
75+
// +kubebuilder:selectablefield:JSONPath=`.spec.strategyRef.apiGroup`
76+
// +kubebuilder:selectablefield:JSONPath=`.spec.strategyRef.kind`
77+
// +kubebuilder:selectablefield:JSONPath=`.spec.strategyRef.name`
7578

7679
// BackupJob represents a single execution of a backup.
7780
// It is typically created by a Plan controller when a schedule fires.

‎api/backups/v1alpha1/zz_generated.deepcopy.go‎

Lines changed: 23 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)