@@ -83,27 +83,11 @@ func (r *EnvironmentReconciler) runTasksFromReferenceOnNode(ctx context.Context,
8383 task .Spec .Pod = * template .Spec .Pod .DeepCopy ()
8484 task .Spec .Control = * template .Spec .Control .DeepCopy ()
8585
86- // TODO: regarding error handling. Is it correct to stop while handling one task? this will fail the whole deployment,
87- // which might not be desirable outcome especially for non-critical tasks
88- if foundIdx := slices .IndexFunc (taskReference .Env , func (envVar v1.EnvVar ) bool { return envVar .Name == "OCC_CONTROL_PORT" }); foundIdx == - 1 {
89- log .Error (fmt .Errorf ("didn't find OCC_CONTROL_PORT in env" ), "failed to fill in env vars from template" )
90- return & reconcile.Result {}, nil
91- } else {
92- port , err := strconv .Atoi (taskReference .Env [foundIdx ].Value )
93- if err != nil {
94- log .Error (fmt .Errorf ("found OCC_CONTROL_PORT isn't convertible to number " ), "failed to fill in env vars from template" )
95- return & reconcile.Result {}, nil
96- }
97- task .Spec .Control .Port = port
98- }
99-
10086 if task .Spec .Arguments == nil {
10187 task .Spec .Arguments = make (map [string ]string )
10288 }
10389
104- // TODO: check for containers!
105- task .Spec .Pod .Containers [0 ].Env = append (task .Spec .Pod .Containers [0 ].Env , taskReference .Env ... )
106- task .Spec .Pod .Containers [0 ].Args = append (task .Spec .Pod .Containers [0 ].Args , taskReference .ArgsCLI ... )
90+ handleEnvVars (task , taskReference )
10791 maps .Copy (task .Spec .Arguments , taskReference .ArgsTransition )
10892
10993 task .Spec .Pod .NodeName = resolvedNodename
@@ -131,6 +115,34 @@ func (r *EnvironmentReconciler) runTasksFromReferenceOnNode(ctx context.Context,
131115 return nil , nil
132116}
133117
118+ func handleEnvVars (task * aliecsv1alpha1.Task , taskReference aliecsv1alpha1.TaskReference ) {
119+ // TODO: check for existence of containers
120+ // TODO: adapt for Pods with multiple containers
121+ // reference to existing Env Vars to overwrite them if found in taskReference
122+ existing := make (map [string ]int , len (task .Spec .Pod .Containers [0 ].Env ))
123+ for i , e := range task .Spec .Pod .Containers [0 ].Env {
124+ existing [e .Name ] = i
125+ }
126+ for _ , e := range taskReference .Env {
127+ if i , ok := existing [e .Name ]; ok {
128+ task .Spec .Pod .Containers [0 ].Env [i ] = e
129+ } else {
130+ task .Spec .Pod .Containers [0 ].Env = append (task .Spec .Pod .Containers [0 ].Env , e )
131+ }
132+ }
133+ task .Spec .Pod .Containers [0 ].Args = append (task .Spec .Pod .Containers [0 ].Args , taskReference .ArgsCLI ... )
134+
135+ // TODO: should the OCC_CONTROL_PORT handled in task_controller.go and set only Ports and types in this controller?
136+ for _ , envVar := range task .Spec .Pod .Containers [0 ].Env {
137+ if envVar .Name == "OCC_CONTROL_PORT" {
138+ if port , err := strconv .Atoi (envVar .Value ); err == nil {
139+ task .Spec .Control .Port = port
140+ }
141+ break
142+ }
143+ }
144+ }
145+
134146func (r * EnvironmentReconciler ) runTaskFromDefinitionOnNode (ctx context.Context , taskDefs []aliecsv1alpha1.TaskDefinition ,
135147 nodename string , resolvedNodename string , req ctrl.Request , environment * aliecsv1alpha1.Environment , log logr.Logger ,
136148) (* ctrl.Result , error ) {
0 commit comments