-
Notifications
You must be signed in to change notification settings - Fork 786
/
step_pre_extend.go
195 lines (176 loc) · 5.74 KB
/
step_pre_extend.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
package cmd
import (
"fmt"
"io"
"strings"
jenkinsv1client "github.com/jenkins-x/jx/pkg/client/clientset/versioned/typed/jenkins.io/v1"
"github.com/jenkins-x/jx/pkg/extensions"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
jenkinsv1 "github.com/jenkins-x/jx/pkg/apis/jenkins.io/v1"
"github.com/jenkins-x/jx/pkg/kube"
"github.com/pkg/errors"
"github.com/jenkins-x/jx/pkg/log"
"github.com/jenkins-x/jx/pkg/util"
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
"github.com/spf13/cobra"
"gopkg.in/AlecAivazis/survey.v1/terminal"
)
// StepPreBuildOptions contains the command line flags
type StepPreExtendOptions struct {
StepOptions
}
var (
StepPreExtendLong = templates.LongDesc(`
This pipeline step adds any extensions configured for this pipeline
`)
StepPreExtendExample = templates.Examples(`
jx step pre extend
`)
)
const extensionsConfigDefaultFile = "jenkins-x-extensions.yaml"
func NewCmdStepPreExtend(f Factory, in terminal.FileReader, out terminal.FileWriter, errOut io.Writer) *cobra.Command {
options := StepPreExtendOptions{
StepOptions: StepOptions{
CommonOptions: CommonOptions{
Factory: f,
In: in,
Out: out,
Err: errOut,
},
},
}
cmd := &cobra.Command{
Use: "extend",
Short: "Adds any extensions configured for this pipeline",
Long: StepPreExtendLong,
Example: StepPreExtendExample,
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
CheckErr(err)
},
}
return cmd
}
func (o *StepPreExtendOptions) Run() error {
// This will cause o.devNamespace to be populated
client, ns, err := o.JXClientAndDevNamespace()
if err != nil {
return err
}
apisClient, err := o.ApiExtensionsClient()
if err != nil {
return err
}
err = kube.RegisterExtensionCRD(apisClient)
if err != nil {
return err
}
extensionsClient := client.JenkinsV1().Extensions(ns)
repoExtensions, err := (&jenkinsv1.ExtensionConfigList{}).LoadFromFile(extensionsConfigDefaultFile)
if err != nil {
return err
}
availableExtensions, err := extensionsClient.List(metav1.ListOptions{})
if err != nil {
return err
}
availableExtensionsNames := []string{}
availableExtensionsUUIDLookup := make(map[string]jenkinsv1.ExtensionSpec, 0)
for _, ae := range availableExtensions.Items {
availableExtensionsNames = append(availableExtensionsNames, ae.Name)
availableExtensionsUUIDLookup[ae.Spec.UUID] = ae.Spec
}
if len(repoExtensions.Extensions) > 0 {
apisClient, err := o.ApiExtensionsClient()
if err != nil {
return err
}
err = kube.RegisterPipelineActivityCRD(apisClient)
if err != nil {
return err
}
gitInfo, err := o.FindGitInfo("")
appName := ""
if gitInfo != nil {
appName = gitInfo.Name
}
pipeline := ""
build := o.getBuildNumber()
pipeline, build = o.GetPipelineName(gitInfo, pipeline, build, appName)
if pipeline != "" && build != "" {
name := kube.ToValidName(pipeline + "-" + build)
key := &kube.PromoteStepActivityKey{
PipelineActivityKey: kube.PipelineActivityKey{
Name: name,
Pipeline: pipeline,
Build: build,
},
}
a, _, err := key.GetOrCreate(client, ns)
if err != nil {
return err
}
for _, v := range repoExtensions.Extensions {
e, err := extensionsClient.Get(v.FullyQualifiedKebabName(), metav1.GetOptions{})
if err != nil {
// Extension can't be found
log.Infof("Extension %s applied but cannot be found in this Jenkins X installation. Available extensions are %s\n", util.ColorInfo(fmt.Sprintf("%s", v.FullyQualifiedName())), util.ColorInfo(availableExtensionsNames))
} else {
result, err := o.walk(&e.Spec, availableExtensionsUUIDLookup, v.Parameters, 0, client.JenkinsV1().Extensions(ns))
if err != nil {
return err
}
a.Spec.PostExtensions = append(a.Spec.PostExtensions, result...)
}
}
a, err = client.JenkinsV1().PipelineActivities(ns).Update(a)
if err != nil {
return err
}
}
}
return nil
}
func (o *StepPreExtendOptions) walk(extension *jenkinsv1.ExtensionSpec, lookup map[string]jenkinsv1.ExtensionSpec, parameters []jenkinsv1.ExtensionParameterValue, depth int, exts jenkinsv1client.ExtensionInterface) (result []jenkinsv1.ExtensionExecution, err error) {
result = make([]jenkinsv1.ExtensionExecution, 0)
if len(extension.Children) > 0 {
if depth > 0 {
indent := ((depth - 1) * 2) + 7
log.Infof("%s└ %s version %s\n", strings.Repeat(" ", indent), util.ColorInfo(extension.FullyQualifiedName()), util.ColorInfo(extension.Version))
} else {
log.Infof("Adding %s version %s to pipeline\n", util.ColorInfo(extension.FullyQualifiedName()), util.ColorInfo(extension.Version))
}
for _, childRef := range extension.Children {
if child, ok := lookup[childRef]; ok {
children, err := o.walk(&child, lookup, parameters, depth+1, exts)
if err != nil {
return result, err
}
result = append(result, children...)
} else {
errors.New(fmt.Sprintf("Unable to locate extension %s", childRef))
}
}
} else {
if extension.IsPost() {
ext, envVarsFormatted, err := extensions.ToExecutable(extension, parameters, o.devNamespace, exts)
if err != nil {
return result, err
}
envVarsStr := ""
if len(envVarsFormatted) > 0 {
envVarsStr = fmt.Sprintf("with environment variables [ %s ]", util.ColorInfo(envVarsFormatted))
}
if depth > 0 {
indent := ((depth - 1) * 2) + 7
log.Infof("%s└ %s version %s %s\n", strings.Repeat(" ", indent), util.ColorInfo(extension.FullyQualifiedName()), util.ColorInfo(extension.Version), envVarsStr)
} else {
log.Infof("Adding %s version %s to pipeline %s\n", util.ColorInfo(extension.FullyQualifiedName()), util.ColorInfo(extension.Version), envVarsStr)
}
result = append(result, ext)
}
}
return result, nil
}