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

Add Forgejo backup #310

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
8 changes: 4 additions & 4 deletions apis/vshn/v1/dbaas_vshn_forgejo.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,11 @@ func (v *VSHNForgejo) GetMaintenanceDayOfWeek() string {
}

// GetMaintenanceTimeOfDay returns the currently set time of day
func (v *VSHNForgejo) GetMaintenanceTimeOfDay() TimeOfDay {
func (v *VSHNForgejo) GetMaintenanceTimeOfDay() *TimeOfDay {
if v.Spec.Parameters.Maintenance.TimeOfDay != "" {
return v.Spec.Parameters.Maintenance.TimeOfDay
return &v.Spec.Parameters.Maintenance.TimeOfDay
}
return v.Status.Schedules.Maintenance.TimeOfDay
return &v.Status.Schedules.Maintenance.TimeOfDay
}

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

Expand Down
3 changes: 3 additions & 0 deletions pkg/comp-functions/functions/common/backup/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ func AddK8upBackup(ctx context.Context, svc *runtime.ServiceRuntime, comp common
}

func createObjectBucket(ctx context.Context, comp common.InfoGetter, svc *runtime.ServiceRuntime) error {
if comp.GetName() == "" {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should not be possible. The comp argument is the composite itself which cannot exist without a name.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This actually did return an empty string on a few occasions.
With an empty string, it creates a bucket with a bad policy -bucket which minio complains about and then the whole pipeline gets stuck until that bucket is deleted.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then we have a deeper problem somewhere in the code.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you debug this part? Try to reproduce and see why it's empty

return fmt.Errorf("could not get composition name")
}

ob := &appcatv1.XObjectBucket{
ObjectMeta: metav1.ObjectMeta{
Expand Down
2 changes: 1 addition & 1 deletion pkg/comp-functions/functions/common/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func NewRelease(ctx context.Context, svc *runtime.ServiceRuntime, comp InfoGette

release := &xhelmv1.Release{
ObjectMeta: metav1.ObjectMeta{
Name: comp.GetName(),
Name: resName,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why this has to change? This is in common package and will disrupt existing releases from other services, we have to be very carefully here.

},
Spec: xhelmv1.ReleaseSpec{
ForProvider: xhelmv1.ReleaseParameters{
Expand Down
3 changes: 2 additions & 1 deletion pkg/comp-functions/functions/common/schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package common

import (
"fmt"
v1 "github.com/vshn/appcat/v4/apis/vshn/v1"
"math/rand"
"time"

v1 "github.com/vshn/appcat/v4/apis/vshn/v1"
)

var (
Expand Down
80 changes: 80 additions & 0 deletions pkg/comp-functions/functions/vshnforgejo/backup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package vshnforgejo

import (
"context"
_ "embed"
"encoding/json"
"fmt"

xfnproto "github.com/crossplane/function-sdk-go/proto/v1beta1"
xhelmv1 "github.com/vshn/appcat/v4/apis/helm/release/v1beta1"
vshnv1 "github.com/vshn/appcat/v4/apis/vshn/v1"
"github.com/vshn/appcat/v4/pkg/comp-functions/functions/common"
"github.com/vshn/appcat/v4/pkg/comp-functions/functions/common/backup"
"github.com/vshn/appcat/v4/pkg/comp-functions/runtime"
)

//go:embed script/backup.sh
var forgejoBackupScript string

func AddBackup(ctx context.Context, comp *vshnv1.VSHNForgejo, svc *runtime.ServiceRuntime) *xfnproto.Result {
err := svc.GetDesiredComposite(comp)
if err != nil {
return runtime.NewFatalResult(fmt.Errorf("can't get composite: %w", err))
}

common.SetRandomSchedules(comp, comp)
err = backup.AddK8upBackup(ctx, svc, comp)
if err != nil {
return runtime.NewWarningResult(fmt.Sprintf("cannot add k8s backup to the desired state: %v", err))
}

err = backup.AddBackupScriptCM(svc, comp, forgejoBackupScript)
if err != nil {
return runtime.NewFatalResult(err)
}

err = updateRelease(svc, comp)
if err != nil {
return runtime.NewWarningResult(fmt.Sprintf("cannot update release with backup configuration: %s", err))
}

return nil
}

func updateRelease(svc *runtime.ServiceRuntime, comp *vshnv1.VSHNForgejo) error {
release := &xhelmv1.Release{}

err := svc.GetDesiredComposedResourceByName(release, comp.GetName()+"-release")
if err != nil {
return err
}

values, err := common.GetReleaseValues(release)
if err != nil {
return err
}

err = backup.AddPVCAnnotationToValues(values, "persistence", "annotations")
if err != nil {
return fmt.Errorf("cannot add pvc annotations to values: %w", err)
}

err = backup.AddPodAnnotationToValues(values, "/scripts/backup.sh", ".tar", "gitea", "podAnnotations")
if err != nil {
return fmt.Errorf("cannot add pod annotations to values: %w", err)
}

err = backup.AddBackupCMToValues(values, []string{"extraVolumes"}, []string{"extraContainerVolumeMounts"})
if err != nil {
return fmt.Errorf("cannot add backup cm to values: %w", err)
}

byteValues, err := json.Marshal(values)
if err != nil {
return err
}
release.Spec.ForProvider.Values.Raw = byteValues

return svc.SetDesiredComposedResourceWithName(release, comp.GetName()+"-release")
}
10 changes: 9 additions & 1 deletion pkg/comp-functions/functions/vshnforgejo/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,17 @@ func addForgejo(ctx context.Context, svc *runtime.ServiceRuntime, comp *vshnv1.V
},
},
},
"persistance": map[string]any{
"persistence": map[string]any{
"enabled": true,
},
"extraVolumes": []map[string]any{{
"name": "backup-scratch",
"emptyDir": map[string]any{},
}},
"extraContainerVolumeMounts": []map[string]string{{
"name": "backup-scratch",
"mountPath": "/tmp/backup",
}},
"postgresql": map[string]any{
"enabled": false,
},
Expand Down
4 changes: 4 additions & 0 deletions pkg/comp-functions/functions/vshnforgejo/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ func init() {
Name: "deploy",
Execute: DeployForgejo,
},
{
Name: "backup",
Execute: AddBackup,
},
},
})
}
3 changes: 3 additions & 0 deletions pkg/comp-functions/functions/vshnforgejo/script/backup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

forgejo dump --type tar -t /tmp/backup -V -f -
Loading