99 "github.com/vshn/appcat/v4/pkg/comp-functions/functions/common"
1010 "github.com/vshn/appcat/v4/pkg/comp-functions/functions/common/maintenance"
1111 "github.com/vshn/appcat/v4/pkg/comp-functions/runtime"
12+ batchv1 "k8s.io/api/batch/v1"
13+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
14+ "k8s.io/utils/ptr"
1215)
1316
1417// AddMaintenanceJob will add a job to do the maintenance for the instance
@@ -27,7 +30,58 @@ func AddMaintenanceJob(ctx context.Context, comp *vshnv1.VSHNMariaDB, svc *runti
2730 instanceNamespace := comp .GetInstanceNamespace ()
2831 schedule := comp .GetFullMaintenanceSchedule ()
2932
30- return maintenance .New (comp , svc , schedule , instanceNamespace , comp .GetServiceName ()).
33+ result := maintenance .New (comp , svc , schedule , instanceNamespace , comp .GetServiceName ()).
3134 WithHelmBasedService ().
3235 Run (ctx )
36+
37+ if result != nil {
38+ return result
39+ }
40+
41+ return AddInitialMaintenanceJob (ctx , comp , svc )
42+ }
43+
44+ // AddInitialMaintenanceJob creates a one-time job from the CronJob template to run maintenance immediately after provisioning
45+ func AddInitialMaintenanceJob (ctx context.Context , comp * vshnv1.VSHNMariaDB , svc * runtime.ServiceRuntime ) * xfnproto.Result {
46+ // Check if initial maintenance has already been triggered
47+ if comp .Status .InitialMaintenanceRan {
48+ return nil
49+ }
50+
51+ cronJobName := comp .GetName () + "-maintenancejob"
52+ initialJobName := comp .GetName () + "-initial-maintenance"
53+
54+ observedCronJob := & batchv1.CronJob {}
55+ err := svc .GetObservedKubeObject (observedCronJob , cronJobName )
56+ if err != nil {
57+ return nil
58+ }
59+
60+ svc .Log .Info ("Creating initial maintenance job from CronJob template" )
61+
62+ job := & batchv1.Job {
63+ ObjectMeta : metav1.ObjectMeta {
64+ Name : initialJobName ,
65+ Namespace : observedCronJob .Namespace ,
66+ Labels : observedCronJob .Labels ,
67+ },
68+ Spec : batchv1.JobSpec {
69+ BackoffLimit : ptr .To (int32 (2 )),
70+ TTLSecondsAfterFinished : ptr .To (int32 (3600 )),
71+ Template : observedCronJob .Spec .JobTemplate .Spec .Template ,
72+ },
73+ }
74+
75+ errSet := svc .SetDesiredKubeObject (job , initialJobName , runtime .KubeOptionAllowDeletion )
76+ if errSet != nil {
77+ return runtime .NewFatalResult (fmt .Errorf ("failed to set desired kube object: %w" , errSet ))
78+ }
79+
80+ // Mark that initial maintenance has been triggered
81+ comp .Status .InitialMaintenanceRan = true
82+ if err := svc .SetDesiredCompositeStatus (comp ); err != nil {
83+ return runtime .NewWarningResult (fmt .Sprintf ("cannot update InitialMaintenanceRan status: %v" , err ))
84+ }
85+
86+ return nil
3387}
0 commit comments