Skip to content

Commit

Permalink
[TEP-0076]Valide Taskrun Results in reconciler
Browse files Browse the repository at this point in the history
This is part of work in TEP-0076.
This commit provides the validation of results in reconciler after we
get the emitted results. It covers the validation of mismatched types
between the results emitted and specified, the results object
properties.
Previous this commit these validations are missing and errors are
ignored.
  • Loading branch information
Yongxuanzhang committed Jun 30, 2022
1 parent df074f2 commit 1e902b8
Show file tree
Hide file tree
Showing 7 changed files with 381 additions and 7 deletions.
17 changes: 17 additions & 0 deletions pkg/apis/pipeline/v1beta1/openapi_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions pkg/apis/pipeline/v1beta1/result_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ type TaskResult struct {
// +optional
Type ResultsType `json:"type,omitempty"`

// Properties is the JSON Schema properties to support key-value pairs results.
// +optional
Properties map[string]PropertySpec `json:"properties,omitempty"`

// Description is a human-readable description of the result
// +optional
Description string `json:"description"`
Expand Down
8 changes: 8 additions & 0 deletions pkg/apis/pipeline/v1beta1/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -2255,6 +2255,14 @@
"type": "string",
"default": ""
},
"properties": {
"description": "Properties is the JSON Schema properties to support key-value pairs results.",
"type": "object",
"additionalProperties": {
"default": {},
"$ref": "#/definitions/v1beta1.PropertySpec"
}
},
"type": {
"description": "Type is the user-specified type of the result. The possible type is currently \"string\" and will support \"array\" in following work.",
"type": "string"
Expand Down
11 changes: 10 additions & 1 deletion pkg/apis/pipeline/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions pkg/reconciler/taskrun/taskrun.go
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,10 @@ func (c *Reconciler) reconcile(ctx context.Context, tr *v1beta1.TaskRun, rtr *re
return err
}

if err := validateTaskRunResults(tr, rtr.TaskSpec); err != nil {
return err
}

logger.Infof("Successfully reconciled taskrun %s/%s with status: %#v", tr.Name, tr.Namespace, tr.Status.GetCondition(apis.ConditionSucceeded))
return nil
}
Expand Down
75 changes: 69 additions & 6 deletions pkg/reconciler/taskrun/validate_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,18 +180,18 @@ func MissingKeysObjectParamNames(paramSpecs []v1beta1.ParamSpec, params []v1beta
}
}

return validateObjectKeys(neededKeys, providedKeys)
return findMissingKeys(neededKeys, providedKeys)
}

// validateObjectKeys checks if objects have missing keys in its provider (either taskrun value or result value)
func validateObjectKeys(neededObjectKeys, providedObjectKeys map[string][]string) map[string][]string {
// findMissingKeys checks if objects have missing keys in its provider (either taskrun value or result value)
func findMissingKeys(neededKeys, providedKeys map[string][]string) map[string][]string {
missings := map[string][]string{}
for p, keys := range providedObjectKeys {
if _, ok := neededObjectKeys[p]; !ok {
for p, keys := range providedKeys {
if _, ok := neededKeys[p]; !ok {
// Ignore any missing objects - this happens when object param is provided with default
continue
}
missedKeys := list.DiffLeft(neededObjectKeys[p], keys)
missedKeys := list.DiffLeft(neededKeys[p], keys)
if len(missedKeys) != 0 {
missings[p] = missedKeys
}
Expand Down Expand Up @@ -281,3 +281,66 @@ func validateSidecarOverrides(ts *v1beta1.TaskSpec, trs *v1beta1.TaskRunSpec) er
}
return err
}

// validateResults checks the emitted results type and object properties against the ones defined in spec.
func validateTaskRunResults(tr *v1beta1.TaskRun, resolvedTaskSpec *v1beta1.TaskSpec) error {
specResults := []v1beta1.TaskResult{}
if tr.Spec.TaskSpec != nil {
specResults = append(specResults, tr.Spec.TaskSpec.Results...)
}

if resolvedTaskSpec != nil {
specResults = append(specResults, resolvedTaskSpec.Results...)
}

// When get the results, check if the type of result is the expected one
if missmatchedTypes := mismatchedTypesResults(tr, specResults); len(missmatchedTypes) != 0 {
return fmt.Errorf("missmatched Types for these results, %v", missmatchedTypes)
}

// When get the results, for object value need to check if they have missing keys.
if missingKeysObjectNames := missingKeysofObjectResults(tr, specResults); len(missingKeysObjectNames) != 0 {
return fmt.Errorf("missing keys for these results which are required in TaskResult's properties %v", missingKeysObjectNames)
}
return nil
}

// mismatchedTypesResults checks and returns all the mismatched types of emitted results against specified results.
func mismatchedTypesResults(tr *v1beta1.TaskRun, specResults []v1beta1.TaskResult) map[string][]string {
neededTypes := make(map[string][]string)
providedTypes := make(map[string][]string)
// collect needed keys for object results
for _, r := range specResults {
neededTypes[r.Name] = append(neededTypes[r.Name], string(r.Type))
}

// collect provided keys for object results
for _, trr := range tr.Status.TaskRunResults {
providedTypes[trr.Name] = append(providedTypes[trr.Name], string(trr.Type))
}
return findMissingKeys(neededTypes, providedTypes)
}

// missingKeysofObjectResults checks and returns the missing keys of object results.
func missingKeysofObjectResults(tr *v1beta1.TaskRun, specResults []v1beta1.TaskResult) map[string][]string {
neededKeys := make(map[string][]string)
providedKeys := make(map[string][]string)
// collect needed keys for object results
for _, r := range specResults {
if string(r.Type) == string(v1beta1.ParamTypeObject) {
for key := range r.Properties {
neededKeys[r.Name] = append(neededKeys[r.Name], key)
}
}
}

// collect provided keys for object results
for _, trr := range tr.Status.TaskRunResults {
if trr.Value.Type == v1beta1.ParamTypeObject {
for key := range trr.Value.ObjectVal {
providedKeys[trr.Name] = append(providedKeys[trr.Name], key)
}
}
}
return findMissingKeys(neededKeys, providedKeys)
}
Loading

0 comments on commit 1e902b8

Please sign in to comment.