@@ -77,19 +77,31 @@ func (sidecarPodBuilder) buildPodSpec(ctx context.Context, task *core.TaskTempla
7777 return & podSpec , nil
7878}
7979
80- func getPrimaryContainerNameFromConfig (task * core.TaskTemplate ) (string , error ) {
81- if len (task .GetConfig ()) == 0 {
82- return "" , errors .Errorf (errors .BadTaskSpecification ,
83- "invalid TaskSpecification, config needs to be non-empty and include missing [%s] key" , PrimaryContainerKey )
84- }
80+ func (sidecarPodBuilder ) getPrimaryContainerName (task * core.TaskTemplate , taskCtx pluginsCore.TaskExecutionContext ) (string , error ) {
81+ switch task .TaskTypeVersion {
82+ case 0 :
83+ // Handles pod tasks when they are defined as Sidecar tasks and marshal the podspec using k8s proto.
84+ sidecarJob := sidecarJob {}
85+ err := utils .UnmarshalStructToObj (task .GetCustom (), & sidecarJob )
86+ if err != nil {
87+ return "" , errors .Errorf (errors .BadTaskSpecification , "invalid TaskSpecification [%v], Err: [%v]" , task .GetCustom (), err .Error ())
88+ }
8589
86- primaryContainerName , ok := task .GetConfig ()[PrimaryContainerKey ]
87- if ! ok {
88- return "" , errors .Errorf (errors .BadTaskSpecification ,
89- "invalid TaskSpecification, config missing [%s] key in [%v]" , PrimaryContainerKey , task .GetConfig ())
90- }
90+ return sidecarJob .PrimaryContainerName , nil
91+ default :
92+ if len (task .GetConfig ()) == 0 {
93+ return "" , errors .Errorf (errors .BadTaskSpecification ,
94+ "invalid TaskSpecification, config needs to be non-empty and include missing [%s] key" , PrimaryContainerKey )
95+ }
9196
92- return primaryContainerName , nil
97+ primaryContainerName , ok := task .GetConfig ()[PrimaryContainerKey ]
98+ if ! ok {
99+ return "" , errors .Errorf (errors .BadTaskSpecification ,
100+ "invalid TaskSpecification, config missing [%s] key in [%v]" , PrimaryContainerKey , task .GetConfig ())
101+ }
102+
103+ return primaryContainerName , nil
104+ }
93105}
94106
95107func mergeMapInto (src map [string ]string , dst map [string ]string ) {
@@ -98,11 +110,10 @@ func mergeMapInto(src map[string]string, dst map[string]string) {
98110 }
99111}
100112
101- func (sidecarPodBuilder ) updatePodMetadata (ctx context.Context , pod * v1.Pod , task * core.TaskTemplate , taskCtx pluginsCore.TaskExecutionContext ) error {
113+ func (s sidecarPodBuilder ) updatePodMetadata (ctx context.Context , pod * v1.Pod , task * core.TaskTemplate , taskCtx pluginsCore.TaskExecutionContext ) error {
102114 pod .Annotations = make (map [string ]string )
103115 pod .Labels = make (map [string ]string )
104116
105- var primaryContainerName string
106117 switch task .TaskTypeVersion {
107118 case 0 :
108119 // Handles pod tasks when they are defined as Sidecar tasks and marshal the podspec using k8s proto.
@@ -114,32 +125,20 @@ func (sidecarPodBuilder) updatePodMetadata(ctx context.Context, pod *v1.Pod, tas
114125
115126 mergeMapInto (sidecarJob .Annotations , pod .Annotations )
116127 mergeMapInto (sidecarJob .Labels , pod .Labels )
117-
118- primaryContainerName = sidecarJob .PrimaryContainerName
119- case 1 :
120- // Handles pod tasks that marshal the pod spec to the task custom.
121- containerName , err := getPrimaryContainerNameFromConfig (task )
122- if err != nil {
123- return err
124- }
125-
126- primaryContainerName = containerName
127128 default :
128129 // Handles pod tasks that marshal the pod spec to the k8s_pod task target.
129- if task .GetK8SPod () == nil || task .GetK8SPod ().Metadata != nil {
130+ if task .GetK8SPod () != nil && task .GetK8SPod ().Metadata != nil {
130131 mergeMapInto (task .GetK8SPod ().Metadata .Annotations , pod .Annotations )
131132 mergeMapInto (task .GetK8SPod ().Metadata .Labels , pod .Labels )
132133 }
133-
134- containerName , err := getPrimaryContainerNameFromConfig (task )
135- if err != nil {
136- return err
137- }
138-
139- primaryContainerName = containerName
140134 }
141135
142136 // validate pod and update resource requirements
137+ primaryContainerName , err := s .getPrimaryContainerName (task , taskCtx )
138+ if err != nil {
139+ return err
140+ }
141+
143142 if err := validateAndFinalizePodSpec (ctx , taskCtx , primaryContainerName , & pod .Spec ); err != nil {
144143 return err
145144 }
0 commit comments