-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit adds the basic APIs for conditionals in Tekton: 1. Condition CRD defines a condition how a condition is evaluated i.e. the container spec and any input parameters. 2. The `Conditions` field in `PipelineTask` references `Condition` resources that have to pass before the task is executed. 3. The `ConditionChecks` field in `PipelineRun.Status.TaskRuns` surfaces the status of conditions that were evaluated for that particular task.
- Loading branch information
Showing
20 changed files
with
1,077 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# Copyright 2018 The Knative Authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
apiVersion: apiextensions.k8s.io/v1beta1 | ||
kind: CustomResourceDefinition | ||
metadata: | ||
name: conditions.tekton.dev | ||
spec: | ||
group: tekton.dev | ||
names: | ||
kind: Condition | ||
plural: conditions | ||
categories: | ||
- all | ||
- tekton-pipelines | ||
scope: Namespaced | ||
# Opt into the status subresource so metadata.generation | ||
# starts to increment | ||
subresources: | ||
status: {} | ||
version: v1alpha1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* | ||
* Copyright 2019 The Tekton Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* / | ||
*/ | ||
|
||
package v1alpha1 | ||
|
||
import ( | ||
"github.com/knative/pkg/apis" | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
// Check that Task may be validated and defaulted. | ||
var _ apis.Validatable = (*Condition)(nil) | ||
|
||
// +genclient | ||
// +genclient:noStatus | ||
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object | ||
|
||
// Condition declares a step that is used to gate the execution of a Task in a Pipeline. | ||
// A condition execution (ConditionCheck) evaluates to either true or false | ||
// +k8s:openapi-gen=true | ||
type Condition struct { | ||
metav1.TypeMeta `json:",inline"` | ||
// +optional | ||
metav1.ObjectMeta `json:"metadata"` | ||
|
||
// Spec holds the desired state of the Condition from the client | ||
// +optional | ||
Spec ConditionSpec `json:"spec"` | ||
} | ||
|
||
// ConditionSpec defines the desired state of the Condition | ||
type ConditionSpec struct { | ||
// Check declares container whose exit code determines where a condition is true or false | ||
Check corev1.Container `json:"check,omitempty"` | ||
|
||
// Params is an optional set of parameters which must be supplied by the user when a Condition | ||
// is evaluated | ||
// +optional | ||
Params []ParamSpec `json:"params,omitempty"` | ||
} | ||
|
||
|
||
// ConditionCheck represents a single evaluation of a Condition step. | ||
type ConditionCheck TaskRun | ||
|
||
// ConditionCheckStatus is the observed state of a ConditionCheck | ||
type ConditionCheckStatus TaskRunStatus | ||
|
||
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object | ||
|
||
// ConditionList contains a list of Conditions | ||
type ConditionList struct { | ||
metav1.TypeMeta `json:",inline"` | ||
// +optional | ||
metav1.ListMeta `json:"metadata,omitempty"` | ||
Items []Condition `json:"items"` | ||
} | ||
|
||
func NewConditionCheck(tr *TaskRun) *ConditionCheck { | ||
if tr == nil { | ||
return nil | ||
} | ||
|
||
cc := ConditionCheck(*tr) | ||
return &cc | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
Copyright 2019 The Tekton Authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package v1alpha1 | ||
|
||
import ( | ||
"context" | ||
"github.com/knative/pkg/apis" | ||
"k8s.io/apimachinery/pkg/api/equality" | ||
) | ||
|
||
func (c Condition) Validate(ctx context.Context) *apis.FieldError { | ||
if err := validateObjectMetadata(c.GetObjectMeta()); err != nil { | ||
return err.ViaField("metadata") | ||
} | ||
return c.Spec.Validate(ctx).ViaField("Spec") | ||
} | ||
|
||
func (cs *ConditionSpec) Validate(ctx context.Context) *apis.FieldError { | ||
if equality.Semantic.DeepEqual(cs, ConditionSpec{}) { | ||
return apis.ErrMissingField(apis.CurrentField) | ||
} | ||
|
||
if cs.Check.Image == "" { | ||
return apis.ErrMissingField("Check.Image") | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* | ||
* | ||
* Copyright 2019 The Tekton Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* / | ||
*/ | ||
|
||
package v1alpha1 | ||
|
||
import ( | ||
"context" | ||
"github.com/google/go-cmp/cmp" | ||
"github.com/google/go-cmp/cmp/cmpopts" | ||
"github.com/knative/pkg/apis" | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"testing" | ||
) | ||
func TestCondition_Validate(t *testing.T) { | ||
c := Condition{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "taskname", | ||
}, | ||
Spec: ConditionSpec{ | ||
Check: corev1.Container{ | ||
Name: "foo", | ||
Image: "bar", | ||
}, | ||
Params: []ParamSpec{ | ||
{ | ||
Name: "expected", | ||
}, | ||
}, | ||
}, | ||
} | ||
if err := c.Validate(context.Background()); err != nil { | ||
t.Errorf("Condition.Validate() unexpected error = %v", err) | ||
} | ||
} | ||
|
||
func TestCondition_Invalidate(t *testing.T) { | ||
tcs := []struct{ | ||
name string | ||
cond Condition | ||
expectedError apis.FieldError | ||
}{{ | ||
name: "invalid meta", | ||
cond: Condition{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name : "invalid.,name", | ||
}, | ||
}, | ||
expectedError: apis.FieldError{ | ||
Message: "Invalid resource name: special character . must not be present", | ||
Paths: []string{"metadata.name"}, | ||
}, | ||
},{ | ||
name: "no image", | ||
cond:Condition{ | ||
ObjectMeta: metav1.ObjectMeta{Name: "cond"}, | ||
Spec: ConditionSpec{ | ||
Check: corev1.Container{}, | ||
}, | ||
}, | ||
expectedError: apis.FieldError{ | ||
Message: "missing field(s)", | ||
Paths: []string{"Spec.Check.Image"}, | ||
}, | ||
}} | ||
|
||
for _, tc := range tcs { | ||
t.Run(tc.name, func(t *testing.T) { | ||
err := tc.cond.Validate(context.Background()) | ||
if err == nil { | ||
t.Fatalf("Expected an Error, got nothing for %v", tc) | ||
} | ||
if d := cmp.Diff(tc.expectedError, *err, cmpopts.IgnoreUnexported(apis.FieldError{})); d != "" { | ||
t.Errorf("Condition.Validate() errors diff -want, +got: %v", d) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.