mirror of
https://github.com/hanzo-fi/stack.git
synced 2026-10-30 20:34:43 +00:00
72 lines
2.1 KiB
Go
72 lines
2.1 KiB
Go
package databases
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
v1beta1 "github.com/formancehq/operator/api/formance.com/v1beta1"
|
|
"github.com/formancehq/operator/internal/core"
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
"k8s.io/apimachinery/pkg/types"
|
|
"sigs.k8s.io/controller-runtime/pkg/client"
|
|
)
|
|
|
|
func Create(ctx core.Context, stack *v1beta1.Stack, owner interface {
|
|
v1beta1.Object
|
|
IsDebug() bool
|
|
}) (*v1beta1.Database, error) {
|
|
condition := v1beta1.Condition{
|
|
Type: "DatabaseReady",
|
|
ObservedGeneration: owner.GetGeneration(),
|
|
LastTransitionTime: metav1.Now(),
|
|
}
|
|
defer func() {
|
|
owner.GetConditions().AppendOrReplace(condition, v1beta1.ConditionTypeMatch("DatabaseReady"))
|
|
}()
|
|
|
|
serviceName := strings.ToLower(owner.GetObjectKind().GroupVersionKind().Kind)
|
|
database, _, err := core.CreateOrUpdate[*v1beta1.Database](ctx, types.NamespacedName{
|
|
Name: core.GetObjectName(stack.Name, serviceName),
|
|
},
|
|
func(t *v1beta1.Database) error {
|
|
t.Spec.Stack = stack.Name
|
|
t.Spec.Service = serviceName
|
|
t.Spec.Debug = stack.Spec.Debug || owner.IsDebug()
|
|
|
|
return nil
|
|
},
|
|
core.WithController[*v1beta1.Database](ctx.GetScheme(), owner),
|
|
core.WithOwner[*v1beta1.Database](ctx.GetScheme(), stack),
|
|
)
|
|
if err != nil {
|
|
condition.Message = err.Error()
|
|
condition.Status = metav1.ConditionFalse
|
|
return nil, err
|
|
}
|
|
if !database.Status.Ready {
|
|
condition.Message = "database creation pending"
|
|
condition.Status = metav1.ConditionFalse
|
|
} else {
|
|
condition.Message = "database is ok"
|
|
condition.Status = metav1.ConditionTrue
|
|
}
|
|
|
|
return database, err
|
|
}
|
|
|
|
const ServiceVersion = "formance.com/module-version"
|
|
|
|
func SaveModuleVersion(ctx core.Context, database *v1beta1.Database, version string) error {
|
|
patch := client.MergeFrom(database.DeepCopy())
|
|
if database.Annotations == nil {
|
|
database.Annotations = make(map[string]string)
|
|
}
|
|
database.Annotations[ServiceVersion] = version
|
|
return errors.Wrap(ctx.GetClient().Patch(ctx, database, patch), "patching database")
|
|
}
|
|
|
|
func GetSavedModuleVersion(database *v1beta1.Database) string {
|
|
return database.Annotations[ServiceVersion]
|
|
}
|