/
resource_types.go
239 lines (203 loc) · 9.6 KB
/
resource_types.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
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 v1beta1
import (
"fmt"
"github.com/google/go-cmp/cmp"
resource "github.com/tektoncd/pipeline/pkg/apis/resource/v1alpha1"
v1 "k8s.io/api/core/v1"
)
// PipelineResourceType represents the type of endpoint the pipelineResource is, so that the
// controller will know this pipelineResource should be fetched and optionally what
// additional metatdata should be provided for it.
type PipelineResourceType = resource.PipelineResourceType
var (
AllowedOutputResources = resource.AllowedOutputResources
)
const (
// PipelineResourceTypeGit indicates that this source is a GitHub repo.
PipelineResourceTypeGit PipelineResourceType = resource.PipelineResourceTypeGit
// PipelineResourceTypeStorage indicates that this source is a storage blob resource.
PipelineResourceTypeStorage PipelineResourceType = resource.PipelineResourceTypeStorage
// PipelineResourceTypeImage indicates that this source is a docker Image.
PipelineResourceTypeImage PipelineResourceType = resource.PipelineResourceTypeImage
// PipelineResourceTypeCluster indicates that this source is a k8s cluster Image.
PipelineResourceTypeCluster PipelineResourceType = resource.PipelineResourceTypeCluster
// PipelineResourceTypePullRequest indicates that this source is a SCM Pull Request.
PipelineResourceTypePullRequest PipelineResourceType = resource.PipelineResourceTypePullRequest
// PipelineResourceTypeCloudEvent indicates that this source is a cloud event URI
PipelineResourceTypeCloudEvent PipelineResourceType = resource.PipelineResourceTypeCloudEvent
)
// AllResourceTypes can be used for validation to check if a provided Resource type is one of the known types.
var AllResourceTypes = resource.AllResourceTypes
// TaskResources allows a Pipeline to declare how its DeclaredPipelineResources
// should be provided to a Task as its inputs and outputs.
type TaskResources struct {
// Inputs holds the mapping from the PipelineResources declared in
// DeclaredPipelineResources to the input PipelineResources required by the Task.
Inputs []TaskResource `json:"inputs,omitempty"`
// Outputs holds the mapping from the PipelineResources declared in
// DeclaredPipelineResources to the input PipelineResources required by the Task.
Outputs []TaskResource `json:"outputs,omitempty"`
}
// TaskResource defines an input or output Resource declared as a requirement
// by a Task. The Name field will be used to refer to these Resources within
// the Task definition, and when provided as an Input, the Name will be the
// path to the volume mounted containing this Resource as an input (e.g.
// an input Resource named `workspace` will be mounted at `/workspace`).
type TaskResource struct {
ResourceDeclaration `json:",inline"`
}
// TaskRunResources allows a TaskRun to declare inputs and outputs TaskResourceBinding
type TaskRunResources struct {
// Inputs holds the inputs resources this task was invoked with
Inputs []TaskResourceBinding `json:"inputs,omitempty"`
// Outputs holds the inputs resources this task was invoked with
Outputs []TaskResourceBinding `json:"outputs,omitempty"`
}
// TaskResourceBinding points to the PipelineResource that
// will be used for the Task input or output called Name.
type TaskResourceBinding struct {
PipelineResourceBinding `json:",inline"`
// Paths will probably be removed in #1284, and then PipelineResourceBinding can be used instead.
// The optional Path field corresponds to a path on disk at which the Resource can be found
// (used when providing the resource via mounted volume, overriding the default logic to fetch the Resource).
// +optional
Paths []string `json:"paths,omitempty"`
}
// ResourceDeclaration defines an input or output PipelineResource declared as a requirement
// by another type such as a Task or Condition. The Name field will be used to refer to these
// PipelineResources within the type's definition, and when provided as an Input, the Name will be the
// path to the volume mounted containing this PipelineResource as an input (e.g.
// an input Resource named `workspace` will be mounted at `/workspace`).
type ResourceDeclaration = resource.ResourceDeclaration
// PipelineResourceBinding connects a reference to an instance of a PipelineResource
// with a PipelineResource dependency that the Pipeline has declared
type PipelineResourceBinding struct {
// Name is the name of the PipelineResource in the Pipeline's declaration
Name string `json:"name,omitempty"`
// ResourceRef is a reference to the instance of the actual PipelineResource
// that should be used
// +optional
ResourceRef *PipelineResourceRef `json:"resourceRef,omitempty"`
// ResourceSpec is specification of a resource that should be created and
// consumed by the task
// +optional
ResourceSpec *resource.PipelineResourceSpec `json:"resourceSpec,omitempty"`
}
// PipelineResourceResult used to export the image name and digest as json
type PipelineResourceResult struct {
Key string `json:"key"`
Value string `json:"value"`
ResourceName string `json:"resourceName,omitempty"`
// The field ResourceRef should be deprecated and removed in the next API version.
// See https://github.com/tektoncd/pipeline/issues/2694 for more information.
ResourceRef *PipelineResourceRef `json:"resourceRef,omitempty"`
ResultType ResultType `json:"type,omitempty"`
}
// ResultType used to find out whether a PipelineResourceResult is from a task result or not
type ResultType string
// PipelineResourceRef can be used to refer to a specific instance of a Resource
type PipelineResourceRef struct {
// Name of the referent; More info: http://kubernetes.io/docs/user-guide/identifiers#names
Name string `json:"name,omitempty"`
// API version of the referent
// +optional
APIVersion string `json:"apiVersion,omitempty"`
}
// PipelineResourceInterface interface to be implemented by different PipelineResource types
type PipelineResourceInterface interface {
// GetName returns the name of this PipelineResource instance.
GetName() string
// GetType returns the type of this PipelineResource (often a super type, e.g. in the case of storage).
GetType() PipelineResourceType
// Replacements returns all the attributes that this PipelineResource has that
// can be used for variable replacement.
Replacements() map[string]string
// GetOutputTaskModifier returns the TaskModifier instance that should be used on a Task
// in order to add this kind of resource when it is being used as an output.
GetOutputTaskModifier(ts *TaskSpec, path string) (TaskModifier, error)
// GetInputTaskModifier returns the TaskModifier instance that should be used on a Task
// in order to add this kind of resource when it is being used as an input.
GetInputTaskModifier(ts *TaskSpec, path string) (TaskModifier, error)
}
// TaskModifier is an interface to be implemented by different PipelineResources
type TaskModifier interface {
GetStepsToPrepend() []Step
GetStepsToAppend() []Step
GetVolumes() []v1.Volume
}
// InternalTaskModifier implements TaskModifier for resources that are built-in to Tekton Pipelines.
type InternalTaskModifier struct {
StepsToPrepend []Step
StepsToAppend []Step
Volumes []v1.Volume
}
// GetStepsToPrepend returns a set of Steps to prepend to the Task.
func (tm *InternalTaskModifier) GetStepsToPrepend() []Step {
return tm.StepsToPrepend
}
// GetStepsToAppend returns a set of Steps to append to the Task.
func (tm *InternalTaskModifier) GetStepsToAppend() []Step {
return tm.StepsToAppend
}
// GetVolumes returns a set of Volumes to prepend to the Task pod.
func (tm *InternalTaskModifier) GetVolumes() []v1.Volume {
return tm.Volumes
}
// ApplyTaskModifier applies a modifier to the task by appending and prepending steps and volumes.
// If steps with the same name exist in ts an error will be returned. If identical Volumes have
// been added, they will not be added again. If Volumes with the same name but different contents
// have been added, an error will be returned.
func ApplyTaskModifier(ts *TaskSpec, tm TaskModifier) error {
steps := tm.GetStepsToPrepend()
for _, step := range steps {
if err := checkStepNotAlreadyAdded(step, ts.Steps); err != nil {
return err
}
}
ts.Steps = append(steps, ts.Steps...)
steps = tm.GetStepsToAppend()
for _, step := range steps {
if err := checkStepNotAlreadyAdded(step, ts.Steps); err != nil {
return err
}
}
ts.Steps = append(ts.Steps, steps...)
volumes := tm.GetVolumes()
for _, volume := range volumes {
var alreadyAdded bool
for _, v := range ts.Volumes {
if volume.Name == v.Name {
// If a Volume with the same name but different contents has already been added, we can't add both
if d := cmp.Diff(volume, v); d != "" {
return fmt.Errorf("tried to add volume %s already added but with different contents", volume.Name)
}
// If an identical Volume has already been added, don't add it again
alreadyAdded = true
}
}
if !alreadyAdded {
ts.Volumes = append(ts.Volumes, volume)
}
}
return nil
}
func checkStepNotAlreadyAdded(s Step, steps []Step) error {
for _, step := range steps {
if s.Name == step.Name {
return fmt.Errorf("Step %s cannot be added again", step.Name)
}
}
return nil
}