Skip to content

Commit

Permalink
Continue addressing review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
rhkp committed Oct 8, 2024
1 parent 062ac5d commit 2ecd72b
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 144 deletions.
6 changes: 2 additions & 4 deletions api/v1alpha08/sonataflowplatform_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ const (
ReasonDBMigrationStatusFailed string = "The database may be unreachable, invalid credentials supplied or flyway migration failed. Please check logs for further details."
)

type SonataFlowPlatformDBMigrationStatus struct {
type SonataFlowPlatformDBMigrationPhase struct {
Status DBMigrationStatus `json:"dbMigrationStatus,omitempty"`
Message string `json:"message,omitempty"`
Reason string `json:"reason,omitempty"`
Expand All @@ -137,7 +137,7 @@ type SonataFlowPlatformStatus struct {
//+operator-sdk:csv:customresourcedefinitions:type=status,displayName="triggers"
Triggers []SonataFlowPlatformTriggerRef `json:"triggers,omitempty"`
//+operator-sdk:csv:customresourcedefinitions:type=status,displayName="dbMigrationStatus"
SonataFlowPlatformDBMigrationStatus *SonataFlowPlatformDBMigrationStatus `json:"sonataFlowPlatformDBMigrationStatus,omitempty"`
SonataFlowPlatformDBMigrationPhase *SonataFlowPlatformDBMigrationPhase `json:"sonataFlowPlatformDBMigrationPhase,omitempty"`
}

// SonataFlowPlatformTriggerRef defines a trigger created for the SonataFlowPlatform.
Expand All @@ -148,8 +148,6 @@ type SonataFlowPlatformTriggerRef struct {
// Namespace of the Trigger
//+operator-sdk:csv:customresourcedefinitions:type=spec,displayName="Trigger_NS"
Namespace string `json:"namespace"`
//+operator-sdk:csv:customresourcedefinitions:type=status,displayName="dbMigrationStatus"
SonataFlowPlatformDBMigrationStatus *SonataFlowPlatformDBMigrationStatus `json:"sonataFlowPlatformDBMigrationStatus,omitempty"`
}

// SonataFlowClusterPlatformRefStatus information related to the (optional) active SonataFlowClusterPlatform
Expand Down
23 changes: 8 additions & 15 deletions api/v1alpha08/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 1 addition & 10 deletions bundle/manifests/sonataflow.org_sonataflowplatforms.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16896,7 +16896,7 @@ spec:
description: The generation observed by the deployment controller.
format: int64
type: integer
sonataFlowPlatformDBMigrationStatus:
sonataFlowPlatformDBMigrationPhase:
properties:
dbMigrationStatus:
type: string
Expand All @@ -16917,15 +16917,6 @@ spec:
namespace:
description: Namespace of the Trigger
type: string
sonataFlowPlatformDBMigrationStatus:
properties:
dbMigrationStatus:
type: string
message:
type: string
reason:
type: string
type: object
required:
- name
- namespace
Expand Down
11 changes: 1 addition & 10 deletions config/crd/bases/sonataflow.org_sonataflowplatforms.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16896,7 +16896,7 @@ spec:
description: The generation observed by the deployment controller.
format: int64
type: integer
sonataFlowPlatformDBMigrationStatus:
sonataFlowPlatformDBMigrationPhase:
properties:
dbMigrationStatus:
type: string
Expand All @@ -16917,15 +16917,6 @@ spec:
namespace:
description: Namespace of the Trigger
type: string
sonataFlowPlatformDBMigrationStatus:
properties:
dbMigrationStatus:
type: string
message:
type: string
reason:
type: string
type: object
required:
- name
- namespace
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,10 @@ spec:
displayName: info
path: info
- displayName: dbMigrationStatus
path: sonataFlowPlatformDBMigrationStatus
path: sonataFlowPlatformDBMigrationPhase
- description: Triggers list of triggers created for the SonataFlowPlatform
displayName: triggers
path: triggers
- displayName: dbMigrationStatus
path: triggers[0].sonataFlowPlatformDBMigrationStatus
- description: Version the operator version controlling this Platform
displayName: version
path: version
Expand Down
23 changes: 0 additions & 23 deletions internal/controller/cfg/controllers_cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,6 @@ const (
defaultConfigMountPath = "/config/controllers_cfg.yaml"
)

const (
dbMigrationJobName = "sonataflow-db-migrator-job"
dbMigrationContainerName = "db-migration-container"
dbMigratorToolImage = "quay.io/rhkp/incubator-kie-kogito-service-db-migration-postgresql:latest"
dbMigrationCmd = "./migration.sh"
)

type DBMigrationJobCfg struct {
JobName string `yaml:"jobName,omitempty"`
ContainerName string `yaml:"containerName,omitempty"`
ToolImageName string `yaml:"toolImageName,omitempty"`
MigrationCmd string `yaml:"migrationCmd,omitempty"`
}

var controllersCfg *ControllersCfg

var defaultControllersCfg = &ControllersCfg{
Expand Down Expand Up @@ -123,12 +109,3 @@ func GetCfg() *ControllersCfg {
}
return controllersCfg
}

func GetDBMigrationJobCfg() *DBMigrationJobCfg {
return &DBMigrationJobCfg{
JobName: dbMigrationJobName,
ContainerName: dbMigrationContainerName,
ToolImageName: dbMigratorToolImage,
MigrationCmd: dbMigrationCmd,
}
}
65 changes: 48 additions & 17 deletions internal/controller/platform/dbMigratorJob.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
"github.com/apache/incubator-kie-kogito-serverless-operator/api/v1alpha08"
operatorapi "github.com/apache/incubator-kie-kogito-serverless-operator/api/v1alpha08"
"github.com/apache/incubator-kie-kogito-serverless-operator/container-builder/client"
"github.com/apache/incubator-kie-kogito-serverless-operator/internal/controller/cfg"
"github.com/apache/incubator-kie-kogito-serverless-operator/internal/controller/platform/services"

"github.com/apache/incubator-kie-kogito-serverless-operator/log"
Expand All @@ -54,6 +53,20 @@ type DBMigratorJob struct {
JobsServiceDataSource *QuarkusDataSource
}

const (
dbMigrationJobName = "sonataflow-db-migrator-job"
dbMigrationContainerName = "db-migration-container"
dbMigratorToolImage = "quay.io/rhkp/incubator-kie-kogito-service-db-migration-postgresql:latest"
dbMigrationCmd = "./migration.sh"
)

type DBMigrationJobCfg struct {
JobName string
ContainerName string
ToolImageName string
MigrationCmd string
}

func getDBSchemaName(persistencePostgreSQL *operatorapi.PersistencePostgreSQL, defaultSchemaName string) string {
jdbcURL := persistencePostgreSQL.JdbcUrl

Expand Down Expand Up @@ -99,32 +112,41 @@ func getQuarkusDataSourceFromPersistence(ctx context.Context, platform *operator
}

func NewDBMigratorJobData(ctx context.Context, client client.Client, platform *operatorapi.SonataFlowPlatform, pshDI services.PlatformServiceHandler, pshJS services.PlatformServiceHandler) (*DBMigratorJob, error) {
if (pshDI.IsServiceSetInSpec() && pshDI.IsJobsBasedDBMigration()) || (pshJS.IsServiceSetInSpec() && pshJS.IsJobsBasedDBMigration()) {

diJobsBasedDBMigration := false
jsJobsBasedDBMigration := false

if pshDI.IsPersistenceEnabledtInSpec() {
diJobsBasedDBMigration = services.IsJobsBasedDBMigration(platform.Spec.Services.DataIndex.Persistence)
}
if pshJS.IsPersistenceEnabledtInSpec() {
jsJobsBasedDBMigration = services.IsJobsBasedDBMigration(platform.Spec.Services.JobService.Persistence)
}

if (pshDI.IsServiceSetInSpec() && diJobsBasedDBMigration) || (pshJS.IsServiceSetInSpec() && jsJobsBasedDBMigration) {
quarkusDataSourceDataIndex := getNewQuarkusDataSource("", "", "", "")
quarkusDataSourceJobService := getNewQuarkusDataSource("", "", "", "")

migrateDbDataindex := pshDI.IsJobsBasedDBMigration()
if migrateDbDataindex {
if diJobsBasedDBMigration {
quarkusDataSourceDataIndex = getQuarkusDataSourceFromPersistence(ctx, platform, platform.Spec.Services.DataIndex.Persistence, "defaultDi")
}

migrateDbJobsservice := pshJS.IsJobsBasedDBMigration()
if migrateDbJobsservice {
if jsJobsBasedDBMigration {
quarkusDataSourceJobService = getQuarkusDataSourceFromPersistence(ctx, platform, platform.Spec.Services.JobService.Persistence, "defaultJs")
}

return &DBMigratorJob{
MigrateDBDataIndex: migrateDbDataindex,
MigrateDBDataIndex: diJobsBasedDBMigration,
DataIndexDataSource: quarkusDataSourceDataIndex,
MigrateDBJobsService: migrateDbJobsservice,
MigrateDBJobsService: jsJobsBasedDBMigration,
JobsServiceDataSource: quarkusDataSourceJobService,
}, nil
}
return nil, nil
}

func (dbmj DBMigratorJob) CreateJobDBMigration(platform *operatorapi.SonataFlowPlatform) *batchv1.Job {
dbMigrationJobCfg := cfg.GetDBMigrationJobCfg()
dbMigrationJobCfg := getDBMigrationJobCfg()
job := &batchv1.Job{
ObjectMeta: metav1.ObjectMeta{
Name: dbMigrationJobCfg.JobName,
Expand Down Expand Up @@ -193,15 +215,15 @@ func (dbmj DBMigratorJob) CreateJobDBMigration(platform *operatorapi.SonataFlowP
return job
}

func NewSonataFlowPlatformDBMigrationStatus(status v1alpha08.DBMigrationStatus, message string, reason string) *operatorapi.SonataFlowPlatformDBMigrationStatus {
return &operatorapi.SonataFlowPlatformDBMigrationStatus{
func NewSonataFlowPlatformDBMigrationPhase(status v1alpha08.DBMigrationStatus, message string, reason string) *operatorapi.SonataFlowPlatformDBMigrationPhase {
return &operatorapi.SonataFlowPlatformDBMigrationPhase{
Status: status,
Message: message,
Reason: reason,
}
}

func UpdateSonataFlowPlatformDBMigrationStatus(dbMigrationStatus *operatorapi.SonataFlowPlatformDBMigrationStatus, status v1alpha08.DBMigrationStatus, message string, reason string) *operatorapi.SonataFlowPlatformDBMigrationStatus {
func UpdateSonataFlowPlatformDBMigrationPhase(dbMigrationStatus *operatorapi.SonataFlowPlatformDBMigrationPhase, status v1alpha08.DBMigrationStatus, message string, reason string) *operatorapi.SonataFlowPlatformDBMigrationPhase {
if dbMigrationStatus != nil {
dbMigrationStatus.Status = status
dbMigrationStatus.Message = message
Expand All @@ -211,26 +233,35 @@ func UpdateSonataFlowPlatformDBMigrationStatus(dbMigrationStatus *operatorapi.So
return nil
}

func getDBMigrationJobCfg() *DBMigrationJobCfg {
return &DBMigrationJobCfg{
JobName: dbMigrationJobName,
ContainerName: dbMigrationContainerName,
ToolImageName: dbMigratorToolImage,
MigrationCmd: dbMigrationCmd,
}
}

func (dbmj DBMigratorJob) ReconcileMigratorJob(ctx context.Context, client client.Client, platform *operatorapi.SonataFlowPlatform) error {
platform.Status.SonataFlowPlatformDBMigrationStatus = NewSonataFlowPlatformDBMigrationStatus(operatorapi.DBMigrationStatusStarted, operatorapi.MessageDBMigrationStatusStarted, operatorapi.ReasonDBMigrationStatusStarted)
platform.Status.SonataFlowPlatformDBMigrationPhase = NewSonataFlowPlatformDBMigrationPhase(operatorapi.DBMigrationStatusStarted, operatorapi.MessageDBMigrationStatusStarted, operatorapi.ReasonDBMigrationStatusStarted)

for {
job, err := client.BatchV1().Jobs(platform.Namespace).Get(ctx, cfg.GetDBMigrationJobCfg().JobName, metav1.GetOptions{})
job, err := client.BatchV1().Jobs(platform.Namespace).Get(ctx, dbMigrationJobName, metav1.GetOptions{})
if err != nil {
klog.V(log.E).InfoS("Error getting DB migrator job while monitoring completion: ", "error", err)
return err
}

klog.V(log.I).InfoS("Started to monitor the db migration job: ", "error", err)
platform.Status.SonataFlowPlatformDBMigrationStatus = UpdateSonataFlowPlatformDBMigrationStatus(platform.Status.SonataFlowPlatformDBMigrationStatus, operatorapi.DBMigrationStatusInProgress, operatorapi.MessageDBMigrationStatusInProgress, operatorapi.ReasonDBMigrationStatusInProgress)
platform.Status.SonataFlowPlatformDBMigrationPhase = UpdateSonataFlowPlatformDBMigrationPhase(platform.Status.SonataFlowPlatformDBMigrationPhase, operatorapi.DBMigrationStatusInProgress, operatorapi.MessageDBMigrationStatusInProgress, operatorapi.ReasonDBMigrationStatusInProgress)

klog.V(log.I).InfoS("Db migration job status: ", "active", job.Status.Active, "ready", job.Status.Ready, "failed", job.Status.Failed, "success", job.Status.Succeeded, "CompletedIndexes", job.Status.CompletedIndexes, "terminatedPods", job.Status.UncountedTerminatedPods)
if job.Status.Failed > 0 {
platform.Status.SonataFlowPlatformDBMigrationStatus = UpdateSonataFlowPlatformDBMigrationStatus(platform.Status.SonataFlowPlatformDBMigrationStatus, operatorapi.DBMigrationStatusFailed, operatorapi.MessageDBMigrationStatusFailed, operatorapi.ReasonDBMigrationStatusFailed)
platform.Status.SonataFlowPlatformDBMigrationPhase = UpdateSonataFlowPlatformDBMigrationPhase(platform.Status.SonataFlowPlatformDBMigrationPhase, operatorapi.DBMigrationStatusFailed, operatorapi.MessageDBMigrationStatusFailed, operatorapi.ReasonDBMigrationStatusFailed)
klog.V(log.E).InfoS("DB migrator job failed")
return errors.New("DB migrator job failed and could not complete")
} else if job.Status.Succeeded > 0 {
platform.Status.SonataFlowPlatformDBMigrationStatus = UpdateSonataFlowPlatformDBMigrationStatus(platform.Status.SonataFlowPlatformDBMigrationStatus, operatorapi.DBMigrationStatusSucceeded, operatorapi.MessageDBMigrationStatusSucceeded, operatorapi.ReasonDBMigrationStatusSucceeded)
platform.Status.SonataFlowPlatformDBMigrationPhase = UpdateSonataFlowPlatformDBMigrationPhase(platform.Status.SonataFlowPlatformDBMigrationPhase, operatorapi.DBMigrationStatusSucceeded, operatorapi.MessageDBMigrationStatusSucceeded, operatorapi.ReasonDBMigrationStatusSucceeded)
klog.V(log.E).InfoS("DB migrator job completed successful")
return nil
} else {
Expand Down
Loading

0 comments on commit 2ecd72b

Please sign in to comment.