Skip to content

Commit 550a827

Browse files
authored
feat(tekton-kfptask): Update kfptask to support pod metadata (#1449)
* update kfptask to support pod metadata * fix type
1 parent d5fc9fd commit 550a827

File tree

4 files changed

+58
-8
lines changed

4 files changed

+58
-8
lines changed

tekton-catalog/tekton-kfptask/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ require (
55
github.com/google/uuid v1.3.1
66
github.com/kubeflow/pipelines v0.0.0-20231027040853-58ce09e07d03
77
github.com/kubeflow/pipelines/api v0.0.0-20231027040853-58ce09e07d03
8-
github.com/kubeflow/pipelines/kubernetes_platform v0.0.0-20231027040853-58ce09e07d03
8+
github.com/kubeflow/pipelines/kubernetes_platform v0.0.0-20240111221413-aac4408237df
99
github.com/kubeflow/pipelines/third_party/ml-metadata v0.0.0-20231027040853-58ce09e07d03
1010
github.com/stretchr/testify v1.8.4
1111
github.com/tektoncd/pipeline v0.53.2

tekton-catalog/tekton-kfptask/go.sum

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tekton-catalog/tekton-kfptask/pkg/common/common.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,10 @@ func ParseParams(run *tektonv1beta1.CustomRun) (*driverOptions, *apis.FieldError
295295
return opts, nil
296296
}
297297

298+
func GetKubernetesExecutorConfig(options *driverOptions) *kubernetesplatform.KubernetesExecutorConfig {
299+
return options.options.KubernetesExecutorConfig
300+
}
301+
298302
func prettyPrint(jsonStr string) string {
299303
var prettyJSON bytes.Buffer
300304
err := json.Indent(&prettyJSON, []byte(jsonStr), "", " ")

tekton-catalog/tekton-kfptask/pkg/reconciler/kfptask/reconciler.go

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
kfptaskClient "github.com/kubeflow/kfp-tekton/tekton-catalog/tekton-kfptask/pkg/client/clientset/versioned"
2828
kfptaskListers "github.com/kubeflow/kfp-tekton/tekton-catalog/tekton-kfptask/pkg/client/listers/kfptask/v1alpha1"
2929
"github.com/kubeflow/kfp-tekton/tekton-catalog/tekton-kfptask/pkg/common"
30+
"github.com/kubeflow/pipelines/kubernetes_platform/go/kubernetesplatform"
3031
"github.com/tektoncd/pipeline/pkg/apis/pipeline"
3132
"github.com/tektoncd/pipeline/pkg/apis/pipeline/pod"
3233
tektonv1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
@@ -135,13 +136,13 @@ var annotationToDrop = map[string]string{
135136
}
136137

137138
// transite to next state based on current state
138-
func (kts *kfptaskFS) next(executionID string, executorInput string, podSpecPatch string) error {
139+
func (kts *kfptaskFS) next(executionID string, executorInput string, podSpecPatch string, executorConfig *kubernetesplatform.KubernetesExecutorConfig) error {
139140
kts.logger.Infof("kts state is %s", kts.state)
140141
switch kts.state {
141142
case StateInit:
142143
// create the corresponding TaskRun CRD and start the task
143144
// compose TaskRun
144-
tr, err := kts.constructTaskRun(executionID, executorInput, podSpecPatch)
145+
tr, err := kts.constructTaskRun(executionID, executorInput, podSpecPatch, executorConfig)
145146
if err != nil {
146147
kts.logger.Infof("Failed to construct a TaskRun:%v", err)
147148
kts.run.Status.MarkCustomRunFailed(kfptaskv1alpha1.KfpTaskRunReasonInternalError.String(), "Failed to construct a TaskRun: %v", err)
@@ -196,7 +197,47 @@ func (kts *kfptaskFS) next(executionID string, executorInput string, podSpecPatc
196197
return nil
197198
}
198199

199-
func (kts *kfptaskFS) constructTaskRun(executionID string, executorInput string, podSpecPatch string) (*tektonv1.TaskRun, error) {
200+
// Extends the PodMetadata to include Kubernetes-specific executor config.
201+
// Although the current podMetadata object is always empty, this function
202+
// doesn't overwrite the existing podMetadata because for security reasons
203+
// the existing podMetadata should have higher privilege than the user definition.
204+
func extendPodMetadata(
205+
podMetadata *metav1.ObjectMeta,
206+
kubernetesExecutorConfig *kubernetesplatform.KubernetesExecutorConfig,
207+
) {
208+
// Get pod metadata information
209+
if kubernetesExecutorConfig.GetPodMetadata() != nil {
210+
if kubernetesExecutorConfig.GetPodMetadata().GetLabels() != nil {
211+
if podMetadata.Labels == nil {
212+
podMetadata.Labels = kubernetesExecutorConfig.GetPodMetadata().GetLabels()
213+
} else {
214+
podMetadata.Labels = extendMetadataMap(podMetadata.Labels, kubernetesExecutorConfig.GetPodMetadata().GetLabels())
215+
}
216+
}
217+
if kubernetesExecutorConfig.GetPodMetadata().GetAnnotations() != nil {
218+
if podMetadata.Annotations == nil {
219+
podMetadata.Annotations = kubernetesExecutorConfig.GetPodMetadata().GetAnnotations()
220+
} else {
221+
podMetadata.Annotations = extendMetadataMap(podMetadata.Annotations, kubernetesExecutorConfig.GetPodMetadata().GetAnnotations())
222+
}
223+
}
224+
}
225+
}
226+
227+
// Extends metadata map values, highPriorityMap should overwrites lowPriorityMap values
228+
// The original Map inputs should have higher priority since its defined by admin
229+
// TODO: Use maps.Copy after moving to go 1.21+
230+
func extendMetadataMap(
231+
highPriorityMap map[string]string,
232+
lowPriorityMap map[string]string,
233+
) map[string]string {
234+
for k, v := range highPriorityMap {
235+
lowPriorityMap[k] = v
236+
}
237+
return lowPriorityMap
238+
}
239+
240+
func (kts *kfptaskFS) constructTaskRun(executionID string, executorInput string, podSpecPatch string, executorConfig *kubernetesplatform.KubernetesExecutorConfig) (*tektonv1.TaskRun, error) {
200241
ktSpec, err := kts.reconciler.getKfpTaskSpec(kts.ctx, kts.run)
201242
if err != nil {
202243
return nil, err
@@ -235,6 +276,10 @@ func (kts *kfptaskFS) constructTaskRun(executionID string, executorInput string,
235276
},
236277
}
237278

279+
if executorConfig != nil {
280+
extendPodMetadata(&tr.ObjectMeta, executorConfig)
281+
}
282+
238283
if podSpecPatch != "" {
239284
podSpec, err := parseTaskSpecPatch(podSpecPatch)
240285
if err != nil {
@@ -312,7 +357,7 @@ func (r *Reconciler) ReconcileKind(ctx context.Context, run *tektonv1beta1.Custo
312357
return nil
313358
}
314359
if ktstate.isRunning() {
315-
return ktstate.next("", "", "")
360+
return ktstate.next("", "", "", nil)
316361
}
317362
options, err := common.ParseParams(run)
318363
if err != nil {
@@ -321,6 +366,7 @@ func (r *Reconciler) ReconcileKind(ctx context.Context, run *tektonv1beta1.Custo
321366
"Run can't be run because it has an invalid param - %v", err)
322367
return nil
323368
}
369+
executorConfig := common.GetKubernetesExecutorConfig(options)
324370

325371
runResults, runTask, executionID, executorInput, podSpecPatch, driverErr := common.ExecDriver(ctx, options)
326372
if driverErr != nil {
@@ -341,7 +387,7 @@ func (r *Reconciler) ReconcileKind(ctx context.Context, run *tektonv1beta1.Custo
341387
return nil
342388
}
343389

344-
return ktstate.next(executionID, executorInput, podSpecPatch)
390+
return ktstate.next(executionID, executorInput, podSpecPatch, executorConfig)
345391
}
346392

347393
func (r *Reconciler) FinalizeKind(ctx context.Context, run *tektonv1beta1.CustomRun) reconciler.Event {

0 commit comments

Comments
 (0)