Skip to content

Commit e2bd7c1

Browse files
Dipta Dastamalsaha
authored andcommitted
Unify LocalSpec and RecoveredVolume (#259)
1 parent cde4f13 commit e2bd7c1

File tree

11 files changed

+68
-145
lines changed

11 files changed

+68
-145
lines changed

apis/stash/types.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,9 @@ type Backend struct {
7777
}
7878

7979
type LocalSpec struct {
80-
VolumeSource core.VolumeSource `json:"volumeSource,omitempty"`
81-
Path string `json:"path,omitempty"`
80+
VolumeSource core.VolumeSource `json:",inline"`
81+
MountPath string `json:"mountPath,omitempty"`
82+
SubPath string `json:"subPath,omitempty"`
8283
}
8384

8485
type S3Spec struct {
@@ -160,7 +161,7 @@ type RecoverySpec struct {
160161
Workload LocalTypedReference `json:"workload,omitempty"`
161162
PodOrdinal string `json:"podOrdinal,omitempty"`
162163
NodeName string `json:"nodeName,omitempty"`
163-
RecoveredVolumes []RecoveredVolume `json:"recoveredVolumes,omitempty"`
164+
RecoveredVolumes []LocalSpec `json:"recoveredVolumes,omitempty"`
164165
}
165166

166167
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
@@ -171,12 +172,6 @@ type RecoveryList struct {
171172
Items []Recovery `json:"items,omitempty"`
172173
}
173174

174-
type RecoveredVolume struct {
175-
VolumeSource core.VolumeSource `json:",inline"`
176-
MountPath string `json:"mountPath,omitempty"`
177-
SubPath string `json:"subPath,omitempty"`
178-
}
179-
180175
// LocalTypedReference contains enough information to let you inspect or modify the referred object.
181176
type LocalTypedReference struct {
182177
// Kind of the referent.

apis/stash/v1alpha1/helpers.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package v1alpha1
2+
3+
import (
4+
core "k8s.io/api/core/v1"
5+
)
6+
7+
func (l LocalSpec) ToVolumeAndMount(volName string) (core.Volume, core.VolumeMount) {
8+
vol := core.Volume{
9+
Name: volName,
10+
VolumeSource: l.VolumeSource,
11+
}
12+
mnt := core.VolumeMount{
13+
Name: volName,
14+
MountPath: l.MountPath,
15+
SubPath: l.SubPath,
16+
}
17+
return vol, mnt
18+
}

apis/stash/v1alpha1/types.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,9 @@ type Backend struct {
7777
}
7878

7979
type LocalSpec struct {
80-
VolumeSource core.VolumeSource `json:"volumeSource,omitempty"`
81-
Path string `json:"path,omitempty"`
80+
VolumeSource core.VolumeSource `json:",inline"`
81+
MountPath string `json:"mountPath,omitempty"`
82+
SubPath string `json:"subPath,omitempty"`
8283
}
8384

8485
type S3Spec struct {
@@ -160,7 +161,7 @@ type RecoverySpec struct {
160161
Workload LocalTypedReference `json:"workload,omitempty"`
161162
PodOrdinal string `json:"podOrdinal,omitempty"`
162163
NodeName string `json:"nodeName,omitempty"`
163-
RecoveredVolumes []RecoveredVolume `json:"recoveredVolumes,omitempty"`
164+
RecoveredVolumes []LocalSpec `json:"recoveredVolumes,omitempty"`
164165
}
165166

166167
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
@@ -171,12 +172,6 @@ type RecoveryList struct {
171172
Items []Recovery `json:"items,omitempty"`
172173
}
173174

174-
type RecoveredVolume struct {
175-
VolumeSource core.VolumeSource `json:",inline"`
176-
MountPath string `json:"mountPath,omitempty"`
177-
SubPath string `json:"subPath,omitempty"`
178-
}
179-
180175
// LocalTypedReference contains enough information to let you inspect or modify the referred object.
181176
type LocalTypedReference struct {
182177
// Kind of the referent.

apis/stash/v1alpha1/zz_generated.conversion.go

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@ func RegisterConversions(scheme *runtime.Scheme) error {
5252
Convert_stash_LocalSpec_To_v1alpha1_LocalSpec,
5353
Convert_v1alpha1_LocalTypedReference_To_stash_LocalTypedReference,
5454
Convert_stash_LocalTypedReference_To_v1alpha1_LocalTypedReference,
55-
Convert_v1alpha1_RecoveredVolume_To_stash_RecoveredVolume,
56-
Convert_stash_RecoveredVolume_To_v1alpha1_RecoveredVolume,
5755
Convert_v1alpha1_Recovery_To_stash_Recovery,
5856
Convert_stash_Recovery_To_v1alpha1_Recovery,
5957
Convert_v1alpha1_RecoveryList_To_stash_RecoveryList,
@@ -205,7 +203,8 @@ func Convert_stash_GCSSpec_To_v1alpha1_GCSSpec(in *stash.GCSSpec, out *GCSSpec,
205203

206204
func autoConvert_v1alpha1_LocalSpec_To_stash_LocalSpec(in *LocalSpec, out *stash.LocalSpec, s conversion.Scope) error {
207205
out.VolumeSource = in.VolumeSource
208-
out.Path = in.Path
206+
out.MountPath = in.MountPath
207+
out.SubPath = in.SubPath
209208
return nil
210209
}
211210

@@ -216,7 +215,8 @@ func Convert_v1alpha1_LocalSpec_To_stash_LocalSpec(in *LocalSpec, out *stash.Loc
216215

217216
func autoConvert_stash_LocalSpec_To_v1alpha1_LocalSpec(in *stash.LocalSpec, out *LocalSpec, s conversion.Scope) error {
218217
out.VolumeSource = in.VolumeSource
219-
out.Path = in.Path
218+
out.MountPath = in.MountPath
219+
out.SubPath = in.SubPath
220220
return nil
221221
}
222222

@@ -249,30 +249,6 @@ func Convert_stash_LocalTypedReference_To_v1alpha1_LocalTypedReference(in *stash
249249
return autoConvert_stash_LocalTypedReference_To_v1alpha1_LocalTypedReference(in, out, s)
250250
}
251251

252-
func autoConvert_v1alpha1_RecoveredVolume_To_stash_RecoveredVolume(in *RecoveredVolume, out *stash.RecoveredVolume, s conversion.Scope) error {
253-
out.VolumeSource = in.VolumeSource
254-
out.MountPath = in.MountPath
255-
out.SubPath = in.SubPath
256-
return nil
257-
}
258-
259-
// Convert_v1alpha1_RecoveredVolume_To_stash_RecoveredVolume is an autogenerated conversion function.
260-
func Convert_v1alpha1_RecoveredVolume_To_stash_RecoveredVolume(in *RecoveredVolume, out *stash.RecoveredVolume, s conversion.Scope) error {
261-
return autoConvert_v1alpha1_RecoveredVolume_To_stash_RecoveredVolume(in, out, s)
262-
}
263-
264-
func autoConvert_stash_RecoveredVolume_To_v1alpha1_RecoveredVolume(in *stash.RecoveredVolume, out *RecoveredVolume, s conversion.Scope) error {
265-
out.VolumeSource = in.VolumeSource
266-
out.MountPath = in.MountPath
267-
out.SubPath = in.SubPath
268-
return nil
269-
}
270-
271-
// Convert_stash_RecoveredVolume_To_v1alpha1_RecoveredVolume is an autogenerated conversion function.
272-
func Convert_stash_RecoveredVolume_To_v1alpha1_RecoveredVolume(in *stash.RecoveredVolume, out *RecoveredVolume, s conversion.Scope) error {
273-
return autoConvert_stash_RecoveredVolume_To_v1alpha1_RecoveredVolume(in, out, s)
274-
}
275-
276252
func autoConvert_v1alpha1_Recovery_To_stash_Recovery(in *Recovery, out *stash.Recovery, s conversion.Scope) error {
277253
out.ObjectMeta = in.ObjectMeta
278254
if err := Convert_v1alpha1_RecoverySpec_To_stash_RecoverySpec(&in.Spec, &out.Spec, s); err != nil {
@@ -337,7 +313,7 @@ func autoConvert_v1alpha1_RecoverySpec_To_stash_RecoverySpec(in *RecoverySpec, o
337313
}
338314
out.PodOrdinal = in.PodOrdinal
339315
out.NodeName = in.NodeName
340-
out.RecoveredVolumes = *(*[]stash.RecoveredVolume)(unsafe.Pointer(&in.RecoveredVolumes))
316+
out.RecoveredVolumes = *(*[]stash.LocalSpec)(unsafe.Pointer(&in.RecoveredVolumes))
341317
return nil
342318
}
343319

@@ -356,7 +332,7 @@ func autoConvert_stash_RecoverySpec_To_v1alpha1_RecoverySpec(in *stash.RecoveryS
356332
}
357333
out.PodOrdinal = in.PodOrdinal
358334
out.NodeName = in.NodeName
359-
out.RecoveredVolumes = *(*[]RecoveredVolume)(unsafe.Pointer(&in.RecoveredVolumes))
335+
out.RecoveredVolumes = *(*[]LocalSpec)(unsafe.Pointer(&in.RecoveredVolumes))
360336
return nil
361337
}
362338

apis/stash/v1alpha1/zz_generated.deepcopy.go

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,6 @@ func RegisterDeepCopies(scheme *runtime.Scheme) error {
6767
in.(*LocalTypedReference).DeepCopyInto(out.(*LocalTypedReference))
6868
return nil
6969
}, InType: reflect.TypeOf(&LocalTypedReference{})},
70-
conversion.GeneratedDeepCopyFunc{Fn: func(in interface{}, out interface{}, c *conversion.Cloner) error {
71-
in.(*RecoveredVolume).DeepCopyInto(out.(*RecoveredVolume))
72-
return nil
73-
}, InType: reflect.TypeOf(&RecoveredVolume{})},
7470
conversion.GeneratedDeepCopyFunc{Fn: func(in interface{}, out interface{}, c *conversion.Cloner) error {
7571
in.(*Recovery).DeepCopyInto(out.(*Recovery))
7672
return nil
@@ -289,23 +285,6 @@ func (in *LocalTypedReference) DeepCopy() *LocalTypedReference {
289285
return out
290286
}
291287

292-
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
293-
func (in *RecoveredVolume) DeepCopyInto(out *RecoveredVolume) {
294-
*out = *in
295-
in.VolumeSource.DeepCopyInto(&out.VolumeSource)
296-
return
297-
}
298-
299-
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new RecoveredVolume.
300-
func (in *RecoveredVolume) DeepCopy() *RecoveredVolume {
301-
if in == nil {
302-
return nil
303-
}
304-
out := new(RecoveredVolume)
305-
in.DeepCopyInto(out)
306-
return out
307-
}
308-
309288
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
310289
func (in *Recovery) DeepCopyInto(out *Recovery) {
311290
*out = *in
@@ -381,7 +360,7 @@ func (in *RecoverySpec) DeepCopyInto(out *RecoverySpec) {
381360
out.Workload = in.Workload
382361
if in.RecoveredVolumes != nil {
383362
in, out := &in.RecoveredVolumes, &out.RecoveredVolumes
384-
*out = make([]RecoveredVolume, len(*in))
363+
*out = make([]LocalSpec, len(*in))
385364
for i := range *in {
386365
(*in)[i].DeepCopyInto(&(*out)[i])
387366
}

apis/stash/zz_generated.deepcopy.go

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,6 @@ func RegisterDeepCopies(scheme *runtime.Scheme) error {
6767
in.(*LocalTypedReference).DeepCopyInto(out.(*LocalTypedReference))
6868
return nil
6969
}, InType: reflect.TypeOf(&LocalTypedReference{})},
70-
conversion.GeneratedDeepCopyFunc{Fn: func(in interface{}, out interface{}, c *conversion.Cloner) error {
71-
in.(*RecoveredVolume).DeepCopyInto(out.(*RecoveredVolume))
72-
return nil
73-
}, InType: reflect.TypeOf(&RecoveredVolume{})},
7470
conversion.GeneratedDeepCopyFunc{Fn: func(in interface{}, out interface{}, c *conversion.Cloner) error {
7571
in.(*Recovery).DeepCopyInto(out.(*Recovery))
7672
return nil
@@ -289,23 +285,6 @@ func (in *LocalTypedReference) DeepCopy() *LocalTypedReference {
289285
return out
290286
}
291287

292-
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
293-
func (in *RecoveredVolume) DeepCopyInto(out *RecoveredVolume) {
294-
*out = *in
295-
in.VolumeSource.DeepCopyInto(&out.VolumeSource)
296-
return
297-
}
298-
299-
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new RecoveredVolume.
300-
func (in *RecoveredVolume) DeepCopy() *RecoveredVolume {
301-
if in == nil {
302-
return nil
303-
}
304-
out := new(RecoveredVolume)
305-
in.DeepCopyInto(out)
306-
return out
307-
}
308-
309288
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
310289
func (in *Recovery) DeepCopyInto(out *Recovery) {
311290
*out = *in
@@ -381,7 +360,7 @@ func (in *RecoverySpec) DeepCopyInto(out *RecoverySpec) {
381360
out.Workload = in.Workload
382361
if in.RecoveredVolumes != nil {
383362
in, out := &in.RecoveredVolumes, &out.RecoveredVolumes
384-
*out = make([]RecoveredVolume, len(*in))
363+
*out = make([]LocalSpec, len(*in))
385364
for i := range *in {
386365
(*in)[i].DeepCopyInto(&(*out)[i])
387366
}

pkg/cli/env.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func (w *ResticWrapper) SetupEnv(backend api.Backend, secret *core.Secret, autoP
6868
w.sh.SetEnv(TMPDIR, tmpDir)
6969

7070
if backend.Local != nil {
71-
r := filepath.Join(backend.Local.Path, autoPrefix)
71+
r := filepath.Join(backend.Local.MountPath, autoPrefix)
7272
if err := os.MkdirAll(r, 0755); err != nil {
7373
return err
7474
}

pkg/controller/restics.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -210,16 +210,19 @@ func (c *StashController) runResticInjector(key string) error {
210210
in.Spec.JobTemplate.Labels[util.AnnotationRestic] = restic.Name
211211
in.Spec.JobTemplate.Labels[util.AnnotationOperation] = util.OperationDeletePods
212212

213-
core_util.UpsertContainer(in.Spec.JobTemplate.Spec.Template.Spec.Containers, core.Container{
214-
Name: util.KubectlContainer,
215-
Image: docker.ImageKubectl + ":" + c.options.KubectlImageTag,
216-
Args: []string{
217-
"kubectl",
218-
"delete",
219-
"pods",
220-
"-l " + selector.String(),
221-
},
222-
})
213+
in.Spec.JobTemplate.Spec.Template.Spec.Containers = core_util.UpsertContainer(
214+
in.Spec.JobTemplate.Spec.Template.Spec.Containers,
215+
core.Container{
216+
Name: util.KubectlContainer,
217+
Image: docker.ImageKubectl + ":" + c.options.KubectlImageTag,
218+
Args: []string{
219+
"kubectl",
220+
"delete",
221+
"pods",
222+
"-l " + selector.String(),
223+
},
224+
})
225+
223226
in.Spec.JobTemplate.Spec.Template.Spec.RestartPolicy = core.RestartPolicyNever
224227
if c.options.EnableRBAC {
225228
in.Spec.JobTemplate.Spec.Template.Spec.ServiceAccountName = in.Name

pkg/util/kubernetes.go

Lines changed: 17 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -264,10 +264,8 @@ func CreateSidecarContainer(r *api.Restic, tag string, workload api.LocalTypedRe
264264
})
265265
}
266266
if r.Spec.Backend.Local != nil {
267-
sidecar.VolumeMounts = append(sidecar.VolumeMounts, core.VolumeMount{
268-
Name: LocalVolumeName,
269-
MountPath: r.Spec.Backend.Local.Path,
270-
})
267+
_, mnt := r.Spec.Backend.Local.ToVolumeAndMount(LocalVolumeName)
268+
sidecar.VolumeMounts = append(sidecar.VolumeMounts, mnt)
271269
}
272270
return sidecar
273271
}
@@ -311,10 +309,11 @@ func MergeLocalVolume(volumes []core.Volume, old, new *api.Restic) []core.Volume
311309
}
312310
}
313311
if new.Spec.Backend.Local != nil {
312+
vol, _ := new.Spec.Backend.Local.ToVolumeAndMount(LocalVolumeName)
314313
if oldPos != -1 {
315-
volumes[oldPos] = core.Volume{Name: LocalVolumeName, VolumeSource: new.Spec.Backend.Local.VolumeSource}
314+
volumes[oldPos] = vol
316315
} else {
317-
volumes = core_util.UpsertVolume(volumes, core.Volume{Name: LocalVolumeName, VolumeSource: new.Spec.Backend.Local.VolumeSource})
316+
volumes = core_util.UpsertVolume(volumes, vol)
318317
}
319318
} else {
320319
if oldPos != -1 {
@@ -359,15 +358,9 @@ func CreateRecoveryJob(recovery *api.Recovery, tag string) *batch.Job {
359358
volumes := make([]core.Volume, 0)
360359
volumeMounts := make([]core.VolumeMount, 0)
361360
for i, recVol := range recovery.Spec.RecoveredVolumes {
362-
volumes = append(volumes, core.Volume{
363-
Name: fmt.Sprintf("vol-%d", i),
364-
VolumeSource: recVol.VolumeSource,
365-
})
366-
volumeMounts = append(volumeMounts, core.VolumeMount{
367-
Name: fmt.Sprintf("vol-%d", i),
368-
MountPath: recVol.MountPath,
369-
SubPath: recVol.SubPath,
370-
})
361+
vol, mnt := recVol.ToVolumeAndMount(fmt.Sprintf("vol-%d", i))
362+
volumes = append(volumes, vol)
363+
volumeMounts = append(volumeMounts, mnt)
371364
}
372365

373366
job := &batch.Job{
@@ -421,18 +414,10 @@ func CreateRecoveryJob(recovery *api.Recovery, tag string) *batch.Job {
421414

422415
// local backend
423416
if recovery.Spec.Backend.Local != nil {
424-
job.Spec.Template.Spec.Containers[0].VolumeMounts = append(job.Spec.Template.Spec.Containers[0].VolumeMounts,
425-
core.VolumeMount{
426-
Name: LocalVolumeName,
427-
MountPath: recovery.Spec.Backend.Local.Path,
428-
})
429-
430-
// user don't need to specify "stash-local" volume, we collect it from restic-spec
431-
job.Spec.Template.Spec.Volumes = append(job.Spec.Template.Spec.Volumes,
432-
core.Volume{
433-
Name: LocalVolumeName,
434-
VolumeSource: recovery.Spec.Backend.Local.VolumeSource,
435-
})
417+
vol, mnt := recovery.Spec.Backend.Local.ToVolumeAndMount(LocalVolumeName)
418+
job.Spec.Template.Spec.Containers[0].VolumeMounts = append(
419+
job.Spec.Template.Spec.Containers[0].VolumeMounts, mnt)
420+
job.Spec.Template.Spec.Volumes = append(job.Spec.Template.Spec.Volumes, vol)
436421
}
437422

438423
return job
@@ -557,19 +542,12 @@ func CreateCheckJob(restic *api.Restic, hostName string, smartPrefix string, tag
557542
}
558543

559544
// local backend
545+
// user don't need to specify "stash-local" volume, we collect it from restic-spec
560546
if restic.Spec.Backend.Local != nil {
561-
job.Spec.Template.Spec.Containers[0].VolumeMounts = append(job.Spec.Template.Spec.Containers[0].VolumeMounts,
562-
core.VolumeMount{
563-
Name: LocalVolumeName,
564-
MountPath: restic.Spec.Backend.Local.Path,
565-
})
566-
567-
// user don't need to specify "stash-local" volume, we collect it from restic-spec
568-
job.Spec.Template.Spec.Volumes = append(job.Spec.Template.Spec.Volumes,
569-
core.Volume{
570-
Name: LocalVolumeName,
571-
VolumeSource: restic.Spec.Backend.Local.VolumeSource,
572-
})
547+
vol, mnt := restic.Spec.Backend.Local.ToVolumeAndMount(LocalVolumeName)
548+
job.Spec.Template.Spec.Containers[0].VolumeMounts = append(
549+
job.Spec.Template.Spec.Containers[0].VolumeMounts, mnt)
550+
job.Spec.Template.Spec.Volumes = append(job.Spec.Template.Spec.Volumes, vol)
573551
}
574552

575553
return job

test/e2e/framework/recovery.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func (fi *Invocation) RecoveryForRestic(restic api.Restic) api.Recovery {
2727
Spec: api.RecoverySpec{
2828
Paths: paths,
2929
Backend: restic.Spec.Backend,
30-
RecoveredVolumes: []api.RecoveredVolume{
30+
RecoveredVolumes: []api.LocalSpec{
3131
{
3232
MountPath: restic.Spec.VolumeMounts[0].MountPath,
3333
VolumeSource: core.VolumeSource{

0 commit comments

Comments
 (0)