-
Notifications
You must be signed in to change notification settings - Fork 786
/
step_create_build.go
260 lines (235 loc) · 6.92 KB
/
step_create_build.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package cmd
import (
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"github.com/ghodss/yaml"
"github.com/jenkins-x/jx/pkg/kube"
"github.com/pkg/errors"
"gopkg.in/AlecAivazis/survey.v1/terminal"
"github.com/jenkins-x/jx/pkg/config"
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
"github.com/jenkins-x/jx/pkg/log"
"github.com/spf13/cobra"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
buildapi "github.com/knative/build/pkg/apis/build/v1alpha1"
)
var (
createBuildLong = templates.LongDesc(`
Creates a Knative build resource for a project
`)
createBuildExample = templates.Examples(`
# create a Knative build and render to the console
jx step create build
# create a Knative build
jx step create build -o mybuild.yaml
`)
)
// StepCreateBuildOptions contains the command line flags
type StepCreateBuildOptions struct {
StepOptions
Dir string
OutputDir string
OutputFilePrefix string
BranchKind string
BuildNumber int
}
// NewCmdStepCreateBuild Creates a new Command object
func NewCmdStepCreateBuild(f Factory, in terminal.FileReader, out terminal.FileWriter, errOut io.Writer) *cobra.Command {
options := &StepCreateBuildOptions{
StepOptions: StepOptions{
CommonOptions: CommonOptions{
Factory: f,
In: in,
Out: out,
Err: errOut,
},
},
}
cmd := &cobra.Command{
Use: "build",
Short: "Creates a Knative build resource for a project",
Long: createBuildLong,
Example: createBuildExample,
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
CheckErr(err)
},
}
options.addCommonFlags(cmd)
cmd.Flags().StringVarP(&options.Dir, "dir", "d", "", "The directory to query to find the projects .git directory")
cmd.Flags().StringVarP(&options.BranchKind, "kind", "k", "", "The kind of build such as 'release' or 'pullRequest' otherwise all of the builds are created")
cmd.Flags().IntVarP(&options.BuildNumber, "build-number", "n", 1, "Which build number to use. <= 0 are ignored")
cmd.Flags().StringVarP(&options.OutputDir, "output-dir", "o", "", "The directory where the generated build yaml files will be output to")
cmd.Flags().StringVarP(&options.OutputFilePrefix, "output-prefix", "p", "build-", "The file name prefix used in the generated build files if output-dir is enabled")
return cmd
}
// Run implements this command
func (o *StepCreateBuildOptions) Run() error {
pc, _, err := config.LoadProjectConfig(o.Dir)
if err != nil {
return err
}
// TODO load the build pack jenkins-x to add any default build kinds?
for _, branchBuild := range pc.Builds {
if o.BranchKind != "" && branchBuild.Kind != o.BranchKind {
continue
}
build, err := o.generateBuild(pc, branchBuild)
if err != nil {
return err
}
data, err := yaml.Marshal(build)
if err != nil {
return err
}
if data == nil {
return fmt.Errorf("Could not marshal build to yaml")
}
outDir := o.OutputDir
if outDir != "" {
err = os.MkdirAll(outDir, DefaultWritePermissions)
if err != nil {
return err
}
output := filepath.Join(outDir, "build-"+branchBuild.Kind+".yml")
err = ioutil.WriteFile(output, data, DefaultWritePermissions)
if err != nil {
return err
}
} else {
log.Info(string(data))
}
}
return err
}
func (o *StepCreateBuildOptions) generateBuild(projectConfig *config.ProjectConfig, build *config.BranchBuild) (*buildapi.Build, error) {
dir := o.Dir
var err error
if dir == "" {
dir, err = os.Getwd()
if err != nil {
return nil, err
}
}
_, projectName := filepath.Split(dir)
buildName := projectName
buildNumber := o.BuildNumber
if buildNumber > 0 {
buildName = buildName + strconv.Itoa(buildNumber)
}
steps := []corev1.Container{}
answer := &buildapi.Build{
TypeMeta: metav1.TypeMeta{
APIVersion: "build.knative.dev/v1alpha1",
Kind: "Build",
},
ObjectMeta: metav1.ObjectMeta{
Name: kube.ToValidName(buildName),
},
Spec: buildapi.BuildSpec{
Steps: steps,
},
}
// TODO load default steps from build pack?
defaultImage := ""
podTemplate, err := o.loadPodTemplate(projectConfig.BuildPack)
if err != nil {
return answer, err
}
for _, step := range build.Build.Steps {
step2 := step
if step2.Image == "" {
step2.Image = defaultImage
}
if step2.Image == "" {
buildPack := projectConfig.BuildPack
if buildPack == "" {
return answer, fmt.Errorf("No build pack defined in the configuration file: %s", config.ProjectConfigFileName)
}
containers := podTemplate.Spec.Containers
if len(containers) > 0 {
step2.Image = containers[0].Image
}
if step2.Image == "" {
return answer, fmt.Errorf("No container image defined in the pod template for build pack %s", buildPack)
}
}
if step2.Image != "" {
defaultImage = step2.Image
}
err = o.addCommonSettings(&step2, projectConfig, build, podTemplate)
if err != nil {
return answer, err
}
steps = append(steps, step2)
}
answer.Spec.Steps = steps
return answer, nil
}
func (o *StepCreateBuildOptions) loadPodTemplate(buildPack string) (*corev1.Pod, error) {
if buildPack == "" {
return nil, nil
}
answer := &corev1.Pod{}
kubeClient, ns, err := o.KubeClientAndDevNamespace()
if err != nil {
return answer, err
}
configMapName := kube.ConfigMapJenkinsPodTemplates
cm, err := kubeClient.CoreV1().ConfigMaps(ns).Get(configMapName, metav1.GetOptions{})
if err != nil {
return answer, errors.Wrapf(err, "failed to find ConfigMap %s in namespace %s", configMapName, ns)
}
podTemplateYaml := ""
if cm.Data != nil {
podTemplateYaml = cm.Data[buildPack]
}
if podTemplateYaml == "" {
return answer, fmt.Errorf("No pod template is defiend in ConfigMap %s for build pack %s", configMapName, buildPack)
}
err = yaml.Unmarshal([]byte(podTemplateYaml), answer)
return answer, err
}
func (o *StepCreateBuildOptions) addCommonSettings(container *corev1.Container, projectConfig *config.ProjectConfig, branchBuild *config.BranchBuild, podTemplate *corev1.Pod) error {
build := &branchBuild.Build
for _, env := range branchBuild.Env {
if kube.GetEnvVar(container, env.Name) == nil {
container.Env = append(container.Env, env)
}
}
if podTemplate != nil {
containers := podTemplate.Spec.Containers
if len(containers) > 0 {
c := containers[0]
if !branchBuild.ExcludePodTemplateEnv {
for _, env := range c.Env {
if kube.GetEnvVar(container, env.Name) == nil {
container.Env = append(c.Env, env)
}
}
}
if !branchBuild.ExcludePodTemplateVolumes {
for _, v := range podTemplate.Spec.Volumes {
if kube.GetVolume(&build.Volumes, v.Name) == nil {
build.Volumes = append(build.Volumes, v)
}
for _, vm := range c.VolumeMounts {
if vm.Name == v.Name {
if kube.GetVolumeMount(&container.VolumeMounts, vm.Name) == nil {
container.VolumeMounts = append(container.VolumeMounts, vm)
}
}
}
}
}
}
}
return nil
}