|
| 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 | +} |
0 commit comments