-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
git_resource.go
199 lines (174 loc) · 5.64 KB
/
git_resource.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
/*
Copyright 2019-2020 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 git
import (
"fmt"
"strconv"
"strings"
"github.com/tektoncd/pipeline/pkg/apis/pipeline"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
resource "github.com/tektoncd/pipeline/pkg/apis/resource/v1alpha1"
"github.com/tektoncd/pipeline/pkg/names"
corev1 "k8s.io/api/core/v1"
)
var (
gitSource = "git-source"
)
// Resource is an endpoint from which to get data which is required
// by a Build/Task for context (e.g. a repo from which to build an image).
type Resource struct {
Name string `json:"name"`
Type resource.PipelineResourceType `json:"type"`
URL string `json:"url"`
// Git revision (branch, tag, commit SHA or ref) to clone. See
// https://git-scm.com/docs/gitrevisions#_specifying_revisions for more
// information.
Revision string `json:"revision"`
Submodules bool `json:"submodules"`
Depth uint `json:"depth"`
SSLVerify bool `json:"sslVerify"`
HTTPProxy string `json:"httpProxy"`
HTTPSProxy string `json:"httpsProxy"`
NOProxy string `json:"noProxy"`
GitImage string `json:"-"`
}
// NewResource creates a new git resource to pass to a Task
func NewResource(gitImage string, r *resource.PipelineResource) (*Resource, error) {
if r.Spec.Type != resource.PipelineResourceTypeGit {
return nil, fmt.Errorf("git.Resource: Cannot create a Git resource from a %s Pipeline Resource", r.Spec.Type)
}
gitResource := Resource{
Name: r.Name,
Type: r.Spec.Type,
GitImage: gitImage,
Submodules: true,
Depth: 1,
SSLVerify: true,
}
for _, param := range r.Spec.Params {
switch {
case strings.EqualFold(param.Name, "URL"):
gitResource.URL = param.Value
case strings.EqualFold(param.Name, "Revision"):
gitResource.Revision = param.Value
case strings.EqualFold(param.Name, "Submodules"):
gitResource.Submodules = toBool(param.Value, true)
case strings.EqualFold(param.Name, "Depth"):
gitResource.Depth = toUint(param.Value, 1)
case strings.EqualFold(param.Name, "SSLVerify"):
gitResource.SSLVerify = toBool(param.Value, true)
case strings.EqualFold(param.Name, "HTTPProxy"):
gitResource.HTTPProxy = param.Value
case strings.EqualFold(param.Name, "HTTPSProxy"):
gitResource.HTTPSProxy = param.Value
case strings.EqualFold(param.Name, "NOProxy"):
gitResource.NOProxy = param.Value
}
}
// default revision to master if nothing is provided
if gitResource.Revision == "" {
gitResource.Revision = "master"
}
return &gitResource, nil
}
func toBool(s string, d bool) bool {
switch s {
case "true":
return true
case "false":
return false
default:
return d
}
}
func toUint(s string, d uint) uint {
v, err := strconv.ParseUint(s, 10, 32)
if err != nil {
return d
}
return uint(v)
}
// GetName returns the name of the resource
func (s Resource) GetName() string {
return s.Name
}
// GetType returns the type of the resource, in this case "Git"
func (s Resource) GetType() resource.PipelineResourceType {
return resource.PipelineResourceTypeGit
}
// GetURL returns the url to be used with this resource
func (s *Resource) GetURL() string {
return s.URL
}
// Replacements is used for template replacement on a GitResource inside of a Taskrun.
func (s *Resource) Replacements() map[string]string {
return map[string]string{
"name": s.Name,
"type": s.Type,
"url": s.URL,
"revision": s.Revision,
"depth": strconv.FormatUint(uint64(s.Depth), 10),
"sslVerify": strconv.FormatBool(s.SSLVerify),
"httpProxy": s.HTTPProxy,
"httpsProxy": s.HTTPSProxy,
"noProxy": s.NOProxy,
}
}
// GetInputTaskModifier returns the TaskModifier to be used when this resource is an input.
func (s *Resource) GetInputTaskModifier(_ *v1alpha1.TaskSpec, path string) (v1alpha1.TaskModifier, error) {
args := []string{
"-url", s.URL,
"-revision", s.Revision,
"-path", path,
}
if !s.Submodules {
args = append(args, "-submodules=false")
}
if s.Depth != 1 {
args = append(args, "-depth", strconv.FormatUint(uint64(s.Depth), 10))
}
if !s.SSLVerify {
args = append(args, "-sslVerify=false")
}
env := []corev1.EnvVar{{
Name: "TEKTON_RESOURCE_NAME",
Value: s.Name,
}}
if len(s.HTTPProxy) != 0 {
env = append(env, corev1.EnvVar{Name: "HTTP_PROXY", Value: s.HTTPProxy})
}
if len(s.HTTPSProxy) != 0 {
env = append(env, corev1.EnvVar{Name: "HTTPS_PROXY", Value: s.HTTPSProxy})
}
if len(s.NOProxy) != 0 {
env = append(env, corev1.EnvVar{Name: "NO_PROXY", Value: s.NOProxy})
}
step := v1alpha1.Step{
Container: corev1.Container{
Name: names.SimpleNameGenerator.RestrictLengthWithRandomSuffix(gitSource + "-" + s.Name),
Image: s.GitImage,
Command: []string{"/ko-app/git-init"},
Args: args,
WorkingDir: pipeline.WorkspaceDir,
// This is used to populate the ResourceResult status.
Env: env,
},
}
return &v1alpha1.InternalTaskModifier{
StepsToPrepend: []v1alpha1.Step{step},
}, nil
}
// GetOutputTaskModifier returns a No-op TaskModifier.
func (s *Resource) GetOutputTaskModifier(_ *v1alpha1.TaskSpec, _ string) (v1alpha1.TaskModifier, error) {
return &v1alpha1.InternalTaskModifier{}, nil
}