Skip to content

Commit

Permalink
Resolve conditions in pipelinerun reconciler and add them to pr status
Browse files Browse the repository at this point in the history
  • Loading branch information
dibyom committed Jul 1, 2019
1 parent c7c5f5d commit 623eeda
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 1 deletion.
2 changes: 2 additions & 0 deletions cmd/controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ func main() {
taskRunInformer := pipelineInformerFactory.Tekton().V1alpha1().TaskRuns()
resourceInformer := pipelineInformerFactory.Tekton().V1alpha1().PipelineResources()
podInformer := kubeInformerFactory.Core().V1().Pods()
conditionInformer := pipelineInformerFactory.Tekton().V1alpha1().Conditions()

pipelineInformer := pipelineInformerFactory.Tekton().V1alpha1().Pipelines()
pipelineRunInformer := pipelineInformerFactory.Tekton().V1alpha1().PipelineRuns()
Expand All @@ -134,6 +135,7 @@ func main() {
clusterTaskInformer,
taskRunInformer,
resourceInformer,
conditionInformer,
timeoutHandler,
)
// Build all of our controllers, with the clients constructed above.
Expand Down
12 changes: 12 additions & 0 deletions pkg/apis/pipeline/v1alpha1/pipelinerun_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,18 @@ type PipelineRunTaskRunStatus struct {
// Status is the TaskRunStatus for the corresponding TaskRun
// +optional
Status *TaskRunStatus `json:"status,omitempty"`
// ConditionChecks is the Status for the corresponding ConditionCheck
// ConditionChecks are just task runs, so the status is a TaskRunStatus
// +optional
ConditionChecks map[string]*PipelineRunConditionCheckStatus `json:"conditionChecks,omitempty"`
}

type PipelineRunConditionCheckStatus struct{
// ConditionName is the name of the Condition
ConditionName string `json:"conditionName,omitempty"`
// Status is the ConditionCheckStatus fpr the corresponding ConditionCheck
// +optional
Status *TaskRunStatus `json:"status,omitempty"`
}

var pipelineRunCondSet = apis.NewBatchConditionSet()
Expand Down
19 changes: 19 additions & 0 deletions pkg/reconciler/v1alpha1/pipelinerun/pipelinerun.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ type Reconciler struct {
taskLister listers.TaskLister
clusterTaskLister listers.ClusterTaskLister
resourceLister listers.PipelineResourceLister
conditionLister listers.ConditionLister
tracker tracker.Interface
configStore configStore
timeoutHandler *reconciler.TimeoutSet
Expand All @@ -106,6 +107,7 @@ func NewController(
clusterTaskInformer informers.ClusterTaskInformer,
taskRunInformer informers.TaskRunInformer,
resourceInformer informers.PipelineResourceInformer,
conditionInformer informers.ConditionInformer,
timeoutHandler *reconciler.TimeoutSet,
) *controller.Impl {

Expand All @@ -117,6 +119,7 @@ func NewController(
clusterTaskLister: clusterTaskInformer.Lister(),
taskRunLister: taskRunInformer.Lister(),
resourceLister: resourceInformer.Lister(),
conditionLister: conditionInformer.Lister(),
timeoutHandler: timeoutHandler,
}

Expand Down Expand Up @@ -302,7 +305,11 @@ func (c *Reconciler) reconcile(ctx context.Context, pr *v1alpha1.PipelineRun) er
return c.clusterTaskLister.Get(name)
},
c.resourceLister.PipelineResources(pr.Namespace).Get,
func(name string) (*v1alpha1.Condition, error) {
return c.conditionLister.Conditions(pr.Namespace).Get(name)
},
p.Spec.Tasks, providedResources,
c.Logger,
)
if err != nil {
// This Run has failed, so we need to mark it as failed and stop reconciling it
Expand Down Expand Up @@ -414,6 +421,18 @@ func updateTaskRunsStatus(pr *v1alpha1.PipelineRun, pipelineState []*resources.R
prtrs = &v1alpha1.PipelineRunTaskRunStatus{
PipelineTaskName: rprt.PipelineTask.Name,
}
if rprt.ConditionChecks != nil {
cStatus := make(map[string]*v1alpha1.PipelineRunConditionCheckStatus)
for n, c := range rprt.ConditionChecks {
cStatus[n] = &v1alpha1.PipelineRunConditionCheckStatus{
ConditionName: "Coming Soon!",
}
if c != nil {
cStatus[n].Status = &c.Status
}
}
prtrs.ConditionChecks = cStatus
}
pr.Status.TaskRuns[rprt.TaskRun.Name] = prtrs
}
prtrs.Status = &rprt.TaskRun.Status
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package resources

import "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"

// GetCondition is a function used to retrieve PipelineConditions.
type GetCondition func(string) (*v1alpha1.Condition, error)

Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ type ResolvedPipelineRunTask struct {
TaskRun *v1alpha1.TaskRun
PipelineTask *v1alpha1.PipelineTask
ResolvedTaskResources *resources.ResolvedTaskResources
TaskConditions []*v1alpha1.Condition
// ConditionChecks ~~TaskRuns but for evaling conditions
// TODO: Move this onto a slice of resolvedConditionChecks struct
ConditionChecks map[string]*v1alpha1.TaskRun // Could also be a TaskRun or maybe just a Pod?
}

// PipelineRunState is a slice of ResolvedPipelineRunTasks the represents the current execution
Expand Down Expand Up @@ -211,8 +215,10 @@ func ResolvePipelineRun(
getTaskRun resources.GetTaskRun,
getClusterTask resources.GetClusterTask,
getResource resources.GetResource,
getCondition GetCondition,
tasks []v1alpha1.PipelineTask,
providedResources map[string]v1alpha1.PipelineResourceRef,
logger *zap.SugaredLogger,
) (PipelineRunState, error) {

state := []*ResolvedPipelineRunTask{}
Expand All @@ -224,7 +230,7 @@ func ResolvePipelineRun(
TaskRunName: getTaskRunName(pipelineRun.Status.TaskRuns, pt.Name, pipelineRun.Name),
}

// Find the Task that this task in the Pipeline this PipelineTask is using
// Find the Task that this PipelineTask is using
var t v1alpha1.TaskInterface
var err error
if pt.TaskRef.Kind == v1alpha1.ClusterTaskKind {
Expand Down Expand Up @@ -261,12 +267,50 @@ func ResolvePipelineRun(
if taskRun != nil {
rprt.TaskRun = taskRun
}

// Get all conditions that this pipelineTask will be using, if any
logger.Warnf("!!!!!!! %d", len(pt.Conditions))
if len(pt.Conditions) > 0 {
rprt.ConditionChecks = make(map[string]*v1alpha1.TaskRun)
logger.Warnf("LENGTH CONDITIONS >0")
for j := range pt.Conditions {
cName := pt.Conditions[j].ConditionRef
c, err := getCondition(cName)
if err != nil {
return nil, xerrors.Errorf("error retrieving Condition %s: %w", cName, err)
}
conditionCheckName := getConditionCheckName(pipelineRun.Status.TaskRuns, rprt.TaskRunName, cName)
conditionCheck, err := getTaskRun(conditionCheckName)
if err != nil {
if !errors.IsNotFound(err) {
return nil, xerrors.Errorf("error retrieving ConditionCheck %s: %w", conditionCheckName, err)
}
}
rprt.TaskConditions = append(rprt.TaskConditions, c)
rprt.ConditionChecks[conditionCheckName] = conditionCheck
}
}

// Add this task to the state of the PipelineRun
state = append(state, &rprt)
}
return state, nil
}

// getConditionCheckName should return a unique name for a `ConditionCheck` if one has not already been defined, and the existing one otherwise.
func getConditionCheckName(taskRunStatus map[string]*v1alpha1.PipelineRunTaskRunStatus, trName, conditionName string) string {
trStatus, ok := taskRunStatus[trName]
if ok && trStatus.ConditionChecks != nil {
for k,v := range trStatus.ConditionChecks {
// TODO: Support multiple conditions. We need a name parameter for conditions or use ordering to signify positions
if conditionName == v.ConditionName {
return k
}
}
}
return names.SimpleNameGenerator.RestrictLengthWithRandomSuffix(fmt.Sprintf("%s-%s", trName, conditionName))
}

// getTaskRunName should return a unique name for a `TaskRun` if one has not already been defined, and the existing one otherwise.
func getTaskRunName(taskRunsStatus map[string]*v1alpha1.PipelineRunTaskRunStatus, ptName, prName string) string {
for k, v := range taskRunsStatus {
Expand Down

0 comments on commit 623eeda

Please sign in to comment.