forked from tektoncd/pipeline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcondition.go
125 lines (108 loc) · 3.63 KB
/
condition.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
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 builder
import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
)
// ConditionOp is an operation which modifies a Condition struct.
type ConditionOp func(*v1alpha1.Condition)
// ConditionSpecOp is an operation which modifies a ConditionSpec struct.
type ConditionSpecOp func(spec *v1alpha1.ConditionSpec)
// Condition creates a Condition with default values.
// Any number of Condition modifiers can be passed to transform it.
func Condition(name string, ops ...ConditionOp) *v1alpha1.Condition {
condition := &v1alpha1.Condition{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
}
for _, op := range ops {
op(condition)
}
return condition
}
// Useful when tests need to specify the namespace
func ConditionNamespace(namespace string) ConditionOp {
return func(t *v1alpha1.Condition) {
t.ObjectMeta.Namespace = namespace
}
}
func ConditionLabels(labels map[string]string) ConditionOp {
return func(Condition *v1alpha1.Condition) {
if Condition.ObjectMeta.Labels == nil {
Condition.ObjectMeta.Labels = map[string]string{}
}
for key, value := range labels {
Condition.ObjectMeta.Labels[key] = value
}
}
}
// ConditionSpec creates a ConditionSpec with default values.
// Any number of ConditionSpec modifiers can be passed to transform it.
func ConditionSpec(ops ...ConditionSpecOp) ConditionOp {
return func(Condition *v1alpha1.Condition) {
ConditionSpec := &Condition.Spec
for _, op := range ops {
op(ConditionSpec)
}
Condition.Spec = *ConditionSpec
}
}
// ConditionSpecCheck adds a Container, with the specified name and image, to the Condition Spec Check.
// Any number of Container modifiers can be passed to transform it.
func ConditionSpecCheck(name, image string, ops ...ContainerOp) ConditionSpecOp {
return func(spec *v1alpha1.ConditionSpec) {
c := &corev1.Container{
Name: name,
Image: image,
}
for _, op := range ops {
op(c)
}
spec.Check.Container = *c
}
}
// ConditionDescription sets the description of the condition
func ConditionDescription(desc string) ConditionSpecOp {
return func(spec *v1alpha1.ConditionSpec) {
spec.Description = desc
}
}
func ConditionSpecCheckScript(script string) ConditionSpecOp {
return func(spec *v1alpha1.ConditionSpec) {
spec.Check.Script = script
}
}
// ConditionParamSpec adds a param, with specified name, to the Spec.
// Any number of ParamSpec modifiers can be passed to transform it.
func ConditionParamSpec(name string, pt v1alpha1.ParamType, ops ...ParamSpecOp) ConditionSpecOp {
return func(ps *v1alpha1.ConditionSpec) {
pp := &v1alpha1.ParamSpec{Name: name, Type: pt}
for _, op := range ops {
op(pp)
}
ps.Params = append(ps.Params, *pp)
}
}
// ConditionResource adds a resource with specified name, and type to the ConditionSpec.
func ConditionResource(name string, resourceType v1alpha1.PipelineResourceType) ConditionSpecOp {
return func(spec *v1alpha1.ConditionSpec) {
r := v1alpha1.ResourceDeclaration{
Name: name,
Type: resourceType,
}
spec.Resources = append(spec.Resources, r)
}
}