Skip to content

Commit acc6d8c

Browse files
Add Forgejo backup (#310)
* Add backup functionality
1 parent b470343 commit acc6d8c

File tree

7 files changed

+105
-6
lines changed

7 files changed

+105
-6
lines changed

apis/vshn/v1/dbaas_vshn_forgejo.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,11 +187,11 @@ func (v *VSHNForgejo) GetMaintenanceDayOfWeek() string {
187187
}
188188

189189
// GetMaintenanceTimeOfDay returns the currently set time of day
190-
func (v *VSHNForgejo) GetMaintenanceTimeOfDay() TimeOfDay {
190+
func (v *VSHNForgejo) GetMaintenanceTimeOfDay() *TimeOfDay {
191191
if v.Spec.Parameters.Maintenance.TimeOfDay != "" {
192-
return v.Spec.Parameters.Maintenance.TimeOfDay
192+
return &v.Spec.Parameters.Maintenance.TimeOfDay
193193
}
194-
return v.Status.Schedules.Maintenance.TimeOfDay
194+
return &v.Status.Schedules.Maintenance.TimeOfDay
195195
}
196196

197197
// SetMaintenanceDayOfWeek sets the day of week to the given value
@@ -208,7 +208,7 @@ func (v *VSHNForgejo) SetMaintenanceTimeOfDay(tod TimeOfDay) {
208208
func (v *VSHNForgejo) GetFullMaintenanceSchedule() VSHNDBaaSMaintenanceScheduleSpec {
209209
schedule := v.Spec.Parameters.Maintenance
210210
schedule.DayOfWeek = v.GetMaintenanceDayOfWeek()
211-
schedule.TimeOfDay = v.GetMaintenanceTimeOfDay()
211+
schedule.TimeOfDay = *v.GetMaintenanceTimeOfDay()
212212
return schedule
213213
}
214214

pkg/comp-functions/functions/common/backup/backup.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ func AddK8upBackup(ctx context.Context, svc *runtime.ServiceRuntime, comp common
5252
}
5353

5454
func createObjectBucket(ctx context.Context, comp common.InfoGetter, svc *runtime.ServiceRuntime) error {
55+
if comp.GetName() == "" {
56+
return fmt.Errorf("could not get composite name")
57+
}
5558

5659
ob := &appcatv1.XObjectBucket{
5760
ObjectMeta: metav1.ObjectMeta{

pkg/comp-functions/functions/common/schedule.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ package common
22

33
import (
44
"fmt"
5-
v1 "github.com/vshn/appcat/v4/apis/vshn/v1"
65
"math/rand"
76
"time"
7+
8+
v1 "github.com/vshn/appcat/v4/apis/vshn/v1"
89
)
910

1011
var (
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package vshnforgejo
2+
3+
import (
4+
"context"
5+
_ "embed"
6+
"encoding/json"
7+
"fmt"
8+
9+
xfnproto "github.com/crossplane/function-sdk-go/proto/v1beta1"
10+
xhelmv1 "github.com/vshn/appcat/v4/apis/helm/release/v1beta1"
11+
vshnv1 "github.com/vshn/appcat/v4/apis/vshn/v1"
12+
"github.com/vshn/appcat/v4/pkg/comp-functions/functions/common"
13+
"github.com/vshn/appcat/v4/pkg/comp-functions/functions/common/backup"
14+
"github.com/vshn/appcat/v4/pkg/comp-functions/runtime"
15+
)
16+
17+
//go:embed script/backup.sh
18+
var forgejoBackupScript string
19+
20+
func AddBackup(ctx context.Context, comp *vshnv1.VSHNForgejo, svc *runtime.ServiceRuntime) *xfnproto.Result {
21+
err := svc.GetObservedComposite(comp)
22+
if err != nil {
23+
return runtime.NewFatalResult(fmt.Errorf("can't get composite: %w", err))
24+
}
25+
26+
common.SetRandomSchedules(comp, comp)
27+
err = backup.AddK8upBackup(ctx, svc, comp)
28+
if err != nil {
29+
return runtime.NewWarningResult(fmt.Sprintf("cannot add k8s backup to the desired state: %v", err))
30+
}
31+
32+
err = backup.AddBackupScriptCM(svc, comp, forgejoBackupScript)
33+
if err != nil {
34+
return runtime.NewFatalResult(err)
35+
}
36+
37+
err = updateRelease(svc, comp)
38+
if err != nil {
39+
return runtime.NewWarningResult(fmt.Sprintf("cannot update release with backup configuration: %s", err))
40+
}
41+
42+
return nil
43+
}
44+
45+
func updateRelease(svc *runtime.ServiceRuntime, comp *vshnv1.VSHNForgejo) error {
46+
release := &xhelmv1.Release{}
47+
48+
err := svc.GetDesiredComposedResourceByName(release, comp.GetName())
49+
if err != nil {
50+
return err
51+
}
52+
53+
values, err := common.GetReleaseValues(release)
54+
if err != nil {
55+
return err
56+
}
57+
58+
err = backup.AddPVCAnnotationToValues(values, "persistence", "annotations")
59+
if err != nil {
60+
return fmt.Errorf("cannot add pvc annotations to values: %w", err)
61+
}
62+
63+
err = backup.AddPodAnnotationToValues(values, "/scripts/backup.sh", ".tar", "gitea", "podAnnotations")
64+
if err != nil {
65+
return fmt.Errorf("cannot add pod annotations to values: %w", err)
66+
}
67+
68+
err = backup.AddBackupCMToValues(values, []string{"extraVolumes"}, []string{"extraContainerVolumeMounts"})
69+
if err != nil {
70+
return fmt.Errorf("cannot add backup cm to values: %w", err)
71+
}
72+
73+
byteValues, err := json.Marshal(values)
74+
if err != nil {
75+
return err
76+
}
77+
release.Spec.ForProvider.Values.Raw = byteValues
78+
79+
return svc.SetDesiredComposedResourceWithName(release, comp.GetName())
80+
}

pkg/comp-functions/functions/vshnforgejo/deploy.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,17 @@ func addForgejo(ctx context.Context, svc *runtime.ServiceRuntime, comp *vshnv1.V
156156
},
157157
},
158158
},
159-
"persistance": map[string]any{
159+
"persistence": map[string]any{
160160
"enabled": true,
161161
},
162+
"extraVolumes": []map[string]any{{
163+
"name": "backup-scratch",
164+
"emptyDir": map[string]any{},
165+
}},
166+
"extraContainerVolumeMounts": []map[string]string{{
167+
"name": "backup-scratch",
168+
"mountPath": "/tmp/backup",
169+
}},
162170
"postgresql": map[string]any{
163171
"enabled": false,
164172
},

pkg/comp-functions/functions/vshnforgejo/register.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ func init() {
1313
Name: "deploy",
1414
Execute: DeployForgejo,
1515
},
16+
{
17+
Name: "backup",
18+
Execute: AddBackup,
19+
},
1620
},
1721
})
1822
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/bash -xef
2+
3+
/usr/local/bin/forgejo dump --type tar -t /tmp/backup -V -f -

0 commit comments

Comments
 (0)