@@ -43,9 +43,11 @@ import (
4343 "github.com/AliceO2Group/Control/common/utils/uid"
4444 "github.com/AliceO2Group/Control/control-operator/api/v1alpha1"
4545 k8sclient "github.com/AliceO2Group/Control/control-operator/pkg/client"
46+ "github.com/AliceO2Group/Control/core/controlcommands"
4647 "github.com/AliceO2Group/Control/core/task/channel"
4748 "github.com/AliceO2Group/Control/core/task/sm"
4849 "github.com/AliceO2Group/Control/core/task/taskclass"
50+ "github.com/k0kubun/pp"
4951 "github.com/spf13/viper"
5052)
5153
@@ -401,26 +403,60 @@ func (m *Manager) configureK8sTasks(ctx context.Context, envId uid.ID, tasks Tas
401403 return fmt .Errorf ("building property map for K8s task %s: %w" , t .GetClassName (), err )
402404 }
403405
404- k8sTaskList , err := m .k8sClient .ListTasksByLabel (ctx , map [string ]string {"taskID" : t .taskId })
405- if err != nil {
406- return fmt .Errorf ("error while listing K8s custom Task object for task %s: %w" , t .taskId , err )
407- }
408- if len (k8sTaskList ) == 0 {
409- return fmt .Errorf ("there were not tasks for taskId %s" , t .taskId )
410- } else if len (k8sTaskList ) > 1 {
411- return fmt .Errorf ("there was more than one task for taskId %s" , t .taskId )
406+ if err := m .setK8sTaskArguments (ctx , t .taskId , propMap ); err != nil {
407+ return err
412408 }
409+ }
410+ return m .transitionAndWaitK8sEnvState (ctx , envId , "configured" )
411+ }
413412
414- k8sTask := k8sTaskList [0 ]
415- if k8sTask .Spec .Arguments == nil {
416- k8sTask .Spec .Arguments = make (map [string ]string )
417- }
418- maps .Copy (k8sTask .Spec .Arguments , propMap )
419- if err := m .k8sClient .UpdateTask (ctx , & k8sTask ); err != nil {
420- return fmt .Errorf ("updating arguments for K8s custom Task object %s: %w" , k8sTask .Name , err )
413+ // pushArgsAndTransitionK8sEnvState pushes the transition arguments (eg. runNumber during START_ACTIVITY
414+ // or fmq channel settings in CONFIGURE) and does the transition.
415+ func (m * Manager ) pushArgsAndTransitionK8sEnvState (ctx context.Context , envId uid.ID , tasks Tasks , targetState string , commonArgs controlcommands.PropertyMap ) error {
416+ for _ , t := range tasks {
417+ if err := m .setK8sTaskArguments (ctx , t .taskId , commonArgs ); err != nil {
418+ return err
421419 }
422420 }
423- return m .transitionAndWaitK8sEnvState (ctx , envId , "configured" )
421+ return m .transitionAndWaitK8sEnvState (ctx , envId , targetState )
422+ }
423+
424+ // getK8sTaskForTaskId returns the custom Task object which corresponds to the given ECS task id.
425+ func (m * Manager ) getK8sTaskForTaskId (ctx context.Context , taskId string ) (* v1alpha1.Task , error ) {
426+ k8sTaskList , err := m .k8sClient .ListTasksByLabel (ctx , map [string ]string {"taskID" : taskId })
427+ if err != nil {
428+ return nil , fmt .Errorf ("error while listing K8s custom Task object for task %s: %w" , taskId , err )
429+ }
430+ if len (k8sTaskList ) == 0 {
431+ return nil , fmt .Errorf ("there were not tasks for taskId %s" , taskId )
432+ } else if len (k8sTaskList ) > 1 {
433+ return nil , fmt .Errorf ("there was more than one task for taskId %s" , taskId )
434+ }
435+ return & k8sTaskList [0 ], nil
436+ }
437+
438+ // setK8sTaskArguments replaces the arguments of the custom Task object for the given ECS task
439+ // id with args, which the operator then forwards to the task as OCC transition arguments.
440+ // Previous args in Task.Spec are rewritten with the new ones.
441+ func (m * Manager ) setK8sTaskArguments (ctx context.Context , taskId string , args controlcommands.PropertyMap ) error {
442+ k8sTask , err := m .getK8sTaskForTaskId (ctx , taskId )
443+ if err != nil {
444+ log .Errorf ("failed to get K8s task with id %s" , taskId )
445+ return err
446+ }
447+
448+ if maps .Equal (k8sTask .Spec .Arguments , args ) {
449+ return nil
450+ }
451+
452+ log .WithField ("map" , pp .Sprint (args )).
453+ WithField ("customTaskObj" , k8sTask .Name ).
454+ Debug ("pushing arguments to K8s task" )
455+
456+ if err := m .k8sClient .SetTaskArguments (ctx , k8sTask , args ); err != nil {
457+ return fmt .Errorf ("updating arguments for K8s custom Task object %s: %w" , k8sTask .Name , err )
458+ }
459+ return nil
424460}
425461
426462// transitionAndWaitK8sEnvState is the easy way to transition tasks. Posibility for future: make this async,
0 commit comments