tekton——如何保证task中定义的step顺序执行 源码讲解

680 阅读1分钟

源码讲解基于tekton0.9.2

一个task对应一个pod, task中定义的每个step对应pod中每个container

tektoncd/pipeline/pkg/reconciler/taskrun/entrypoint/entrypoint.go # RedirectSteps 重新定义container步骤

tektoncd/pipeline/pkg/reconciler/taskrun/taskrun.go # reconcile -> createPod -> createRedirectedTaskSpec

tektoncd/pipeline/pkg/reconciler/taskrun/entrypoint/entrypoint.go # RedirectStep -> GetArgs

GetArgs代码片段讲解

func GetArgs(stepNum int, commands, args []string) []string {
	waitFile := getWaitFile(stepNum)
	// The binary we want to run must be separated from its arguments by --
	// so if commands has more than one value, we'll move the other values
	// into the arg list so we can separate them
	if len(commands) > 1 {
		args = append(commands[1:], args...)
		commands = commands[:1]
	}
	argsForEntrypoint := []string{
		"-wait_file", waitFile,
		"-post_file", getWaitFile(stepNum + 1),
	}
	if stepNum == 0 {
		argsForEntrypoint = append(argsForEntrypoint, "-wait_file_content")
	}
	argsForEntrypoint = append(argsForEntrypoint, "-entrypoint")
	argsForEntrypoint = append(argsForEntrypoint, commands...)
	// TODO: what if Command has multiple elements, do we need "--" between command and args?
	argsForEntrypoint = append(argsForEntrypoint, "--")
	return append(argsForEntrypoint, args...)
}

此代码执行完成后会给每一个step的步骤三个参数-wait_file, -post_file, -entrypoint;

第一个step添加-wait_file_content

以上四个讲解参见tektoncd/pipeline/cmd/entrypoint/README.md

如下:

spec:
  containers:
    - args:
        - '-wait_file'
        - /builder/downward/ready # 第一步骤不需要等待直接执行
        - '-post_file'
        - /builder/tools/0 # 执行的结果如果成功写入/builder/tools/0文件, 如果失败则写入/builder/tools/0.err
        - '-wait_file_content'
        - '-entrypoint'
        - /ko-app/bash
        - '--'
        - '-args'
        - mkdir -p /workspace
     ...
    - args:
        - '-wait_file'
        - /builder/tools/0 # 等待/builder/tools/0文件, 如果有变化说明第一个步骤执行成功, 开始执行步骤;
        - '-post_file'
        - /builder/tools/1 #将执行结果写入/builder/tools/1文件, 执行成功后将写入{{post_file}}文件,如果失败将写入{{post_file}}.err文件
        - '-entrypoint'
        - /ko-app/bash
        - '--'
        - '-args'
        - cp -r /pvc/dep-compile-78-task/tmp/. /workspace
     ...
    - args:
        - '-wait_file'
        - /builder/tools/1 #等待/builder/tools/1文件, 如果有变化说明第一个步骤执行成功, 开始执行步骤
        - '-post_file'
        - /builder/tools/2 #将执行结果写入/builder/tools/2文件
        - '-entrypoint'
        - /ko-app/git-init
        - '--'
        - '-url'
        - 'https://coding.jd.com/tengu/jbuild.git'
        - '-revision'
        - master
        - '-path'
        - /workspace/jbuild