forked from jenkins-x-plugins/jx-gitops
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scheduler.go
335 lines (297 loc) · 10.7 KB
/
scheduler.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
package scheduler
import (
"fmt"
"os"
"path/filepath"
"strings"
jxcore "github.com/jenkins-x/jx-api/v4/pkg/apis/core/v4beta1"
"github.com/jenkins-x/go-scm/scm"
v1 "github.com/jenkins-x/jx-api/v4/pkg/apis/jenkins.io/v1"
"github.com/jenkins-x/jx-api/v4/pkg/client/clientset/versioned"
"github.com/jenkins-x/jx-api/v4/pkg/client/clientset/versioned/fake"
"github.com/jenkins-x/jx-gitops/pkg/schedulerapi"
"github.com/jenkins-x/jx-helpers/v3/pkg/files"
"github.com/jenkins-x/jx-helpers/v3/pkg/termcolor"
"github.com/jenkins-x/jx-helpers/v3/pkg/yamls"
"github.com/jenkins-x/jx-logging/v3/pkg/log"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"github.com/jenkins-x/jx-gitops/pkg/pipelinescheduler"
"github.com/jenkins-x/jx-gitops/pkg/rootcmd"
"github.com/jenkins-x/jx-helpers/v3/pkg/cobras/helper"
"github.com/jenkins-x/jx-helpers/v3/pkg/cobras/templates"
"github.com/jenkins-x/jx-helpers/v3/pkg/kyamls"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"sigs.k8s.io/kustomize/kyaml/yaml"
gyaml "github.com/ghodss/yaml"
)
const (
// ConfigMapConfigFileName file name of the ConfigMap for the 'config' configuration
ConfigMapConfigFileName = "config-cm.yaml"
// ConfigMapPluginsFileName file name of the ConfigMap for the 'plugins' configuration
ConfigMapPluginsFileName = "plugins-cm.yaml"
// ConfigKey the name of key in the ConfigMap for the configuration of the `config`
ConfigKey = "config.yaml"
// PluginsKey the name of key in the ConfigMap for the configuration of the `plugins`
PluginsKey = "plugins.yaml"
)
var (
cmdLong = templates.LongDesc(`
Generates the Lighthouse configuration from the SourceRepository and Scheduler resources
`)
cmdExample = templates.Examples(`
# regenerate the lighthouse configuration from the Environment, Scheduler, SourceRepository resources
%s scheduler --dir config-root/namespaces/jx -out src/base/namespaces/jx/lighthouse-config
`)
sourceResourceFilter = kyamls.Filter{
Kinds: []string{"jenkins.io/v1/Environment", "jenkins.io/v1/SourceRepository"},
}
schedulerResourceFilter = kyamls.Filter{
Kinds: []string{"Scheduler"},
}
)
// LabelOptions the options for the command
type Options struct {
Dir string
OutDir string
SourceRepoDir string
SchedulerDir []string
Namespace string
InRepoConfig bool
}
// NewCmdScheduler creates a command object for the command
func NewCmdScheduler() (*cobra.Command, *Options) {
o := &Options{}
cmd := &cobra.Command{
Use: "scheduler",
Aliases: []string{"schedulers", "lighthouse"},
Short: "Generates the Lighthouse configuration from the SourceRepository and Scheduler resources",
Long: cmdLong,
Example: fmt.Sprintf(cmdExample, rootcmd.BinaryName),
Run: func(cmd *cobra.Command, args []string) {
err := o.Run()
helper.CheckErr(err)
},
}
cmd.Flags().StringVarP(&o.Dir, "dir", "d", ".", "the current working directory")
cmd.Flags().StringVarP(&o.SourceRepoDir, "repo-dir", "", "", "the directory to look for SourceRepository resources. If not specified defaults config-root/namespaces/$ns")
cmd.Flags().StringArrayVarP(&o.SchedulerDir, "scheduler-dir", "", nil, "the directory to look for Scheduler resources. If not specified defaults 'schedulers' and 'versionStream/schedulers'")
cmd.Flags().StringVarP(&o.OutDir, "out", "o", "", "the output directory for the generated config files. If not specified defaults to config-root/namespaces/$ns/lighthouse-config")
cmd.Flags().StringVarP(&o.Namespace, "namespace", "n", "jx", "the namespace for the SourceRepository and Scheduler resources")
cmd.Flags().BoolVarP(&o.InRepoConfig, "in-repo-config", "", false, "enables in repo configuration in lighthouse")
return cmd, o
}
func (o *Options) Run() error {
ns := o.Namespace
if ns == "" {
ns = "jx"
}
if o.SourceRepoDir == "" {
o.SourceRepoDir = filepath.Join(o.Dir, "config-root", "namespaces", ns)
}
if len(o.SchedulerDir) == 0 {
paths := []string{
filepath.Join(o.Dir, "versionStream", "schedulers"),
"schedulers",
}
for _, path := range paths {
exists, err := files.DirExists(path)
if err != nil {
return errors.Wrapf(err, "failed to check if path exists %s", path)
}
if exists {
o.SchedulerDir = append(o.SchedulerDir, path)
}
}
}
if o.OutDir == "" {
o.OutDir = filepath.Join(o.Dir, "config-root", "namespaces", ns, "lighthouse-config")
}
err := os.MkdirAll(o.OutDir, files.DefaultDirWritePermissions)
if err != nil {
return errors.Wrapf(err, "failed to create the output directory %s", o.OutDir)
}
var devEnv *v1.Environment
var resources []runtime.Object
schedulerMap := map[string]*schedulerapi.Scheduler{}
repoList := &v1.SourceRepositoryList{}
sourceModifyFn := func(node *yaml.RNode, path string) (bool, error) {
namespace := kyamls.GetNamespace(node, path)
kind := kyamls.GetKind(node, path)
name := kyamls.GetName(node, path)
loaded := false
switch kind {
case "Environment":
if name == "dev" {
devEnv = &v1.Environment{}
err = yamls.LoadFile(path, devEnv)
if err != nil {
return false, errors.Wrapf(err, "failed to load file %s", path)
}
loaded = true
}
case "SourceRepository":
sr := &v1.SourceRepository{}
err = yamls.LoadFile(path, sr)
if err != nil {
return false, errors.Wrapf(err, "failed to load file %s", path)
}
repoList.Items = append(repoList.Items, *sr)
resources = append(resources, sr)
loaded = true
default:
log.Logger().Infof("ignored %s name %s in namespace %s", kind, name, namespace)
}
if loaded {
log.Logger().Infof("loaded %s name %s in namespace %s", kind, name, namespace)
}
return false, nil
}
err = kyamls.ModifyFiles(o.SourceRepoDir, sourceModifyFn, sourceResourceFilter)
if err != nil {
return errors.Wrapf(err, "failed to load resources from dir %s", o.SourceRepoDir)
}
log.Logger().Infof("loaded %d SourceRepository resources from %s", len(repoList.Items), o.SourceRepoDir)
schedulerModifyFn := func(node *yaml.RNode, path string) (bool, error) {
namespace := kyamls.GetNamespace(node, path)
kind := kyamls.GetKind(node, path)
name := kyamls.GetName(node, path)
loaded := false
scheduler := &schedulerapi.Scheduler{}
err = yamls.LoadFile(path, scheduler)
if err != nil {
return false, errors.Wrapf(err, "failed to load file %s", path)
}
schedulerMap[name] = scheduler
loaded = true
if loaded {
log.Logger().Infof("loaded %s name %s in namespace %s", kind, name, namespace)
}
return false, nil
}
for _, scheduleDir := range o.SchedulerDir {
err = kyamls.ModifyFiles(scheduleDir, schedulerModifyFn, schedulerResourceFilter)
if err != nil {
return errors.Wrapf(err, "failed to load resources from dir %s", scheduleDir)
}
}
log.Logger().Infof("loaded %d Scheduler resources from dirs %s", len(schedulerMap), strings.Join(o.SchedulerDir, ", "))
if devEnv == nil {
devEnv = &v1.Environment{
ObjectMeta: metav1.ObjectMeta{
Name: "dev",
Namespace: ns,
},
Spec: v1.EnvironmentSpec{
Label: "Dev",
Namespace: ns,
},
}
}
teamSettings := &devEnv.Spec.TeamSettings
// lets default the dev env scheduler if it doesn't have one:
for i := range repoList.Items {
sr := &repoList.Items[i]
if sr.Spec.URL == devEnv.Spec.Source.URL {
if sr.Spec.Scheduler.Name == "" {
sr.Spec.Scheduler.Name = "in-repo"
}
break
}
}
resources = append(resources, devEnv)
jxClient := fake.NewSimpleClientset(resources...)
loadSchedulers := func(jxClient versioned.Interface, ns string) (map[string]*schedulerapi.Scheduler, *v1.SourceRepositoryList, error) {
return schedulerMap, repoList, nil
}
config, plugins, err := pipelinescheduler.GenerateProw(true, true, jxClient, ns, teamSettings.DefaultScheduler.Name, devEnv, loadSchedulers)
if err != nil {
return errors.Wrapf(err, "failed to generate lighthouse configuration")
}
// lets check for in repo config
flag := true
for _, sr := range repoList.Items {
schedulerName := sr.Spec.Scheduler.Name
inRepo := schedulerName == "in-repo"
if schedulerName != "" {
scheduler := schedulerMap[schedulerName]
if scheduler == nil {
log.Logger().Warnf("no scheduler %s found for SourceRepository %s with URL %s", schedulerName, sr.Name, sr.Spec.URL)
} else if scheduler.Spec.InRepo {
inRepo = true
}
}
if inRepo {
if config.ProwConfig.InRepoConfig.Enabled == nil {
config.ProwConfig.InRepoConfig.Enabled = map[string]*bool{}
}
fullName := scm.Join(sr.Spec.Org, sr.Spec.Repo)
config.ProwConfig.InRepoConfig.Enabled[fullName] = &flag
}
}
// lets process any templated values
templater, err := o.createTemplater()
if err != nil {
return errors.Wrapf(err, "failed to create a templater")
}
config.Keeper.TargetURL, err = templater(config.Keeper.TargetURL)
if err != nil {
return errors.Wrapf(err, "failed to template the config.Keeper.TargetURL")
}
config.Keeper.PRStatusBaseURL, err = templater(config.Keeper.PRStatusBaseURL)
if err != nil {
return errors.Wrapf(err, "failed to template the config.Keeper.PRStatusBaseURL")
}
configConfigMap, err := createConfigMap(config, ns, "config", ConfigKey)
if err != nil {
return err
}
pluginsConfigMap, err := createConfigMap(plugins, ns, "plugins", PluginsKey)
if err != nil {
return err
}
// now lets save the files
configFileName := filepath.Join(o.OutDir, ConfigMapConfigFileName)
pluginsFileName := filepath.Join(o.OutDir, ConfigMapPluginsFileName)
err = yamls.SaveFile(configConfigMap, configFileName)
if err != nil {
return errors.Wrapf(err, "failed to save file %s", configFileName)
}
err = yamls.SaveFile(pluginsConfigMap, pluginsFileName)
if err != nil {
return errors.Wrapf(err, "failed to save file %s", pluginsFileName)
}
log.Logger().Infof("generated config ConfigMap %s and plugins ConfigMap %s", termcolor.ColorInfo(configFileName), termcolor.ColorInfo(pluginsFileName))
return nil
}
func (o *Options) createTemplater() (func(string) (string, error), error) {
requirementsResource, _, err := jxcore.LoadRequirementsConfig(o.Dir, false)
if err != nil {
return nil, errors.Wrapf(err, "failed to load requirements in dir %s", o.Dir)
}
requirements := &requirementsResource.Spec
return func(templateText string) (string, error) {
return EvaluateTemplate(templateText, requirements)
}, nil
}
func createConfigMap(resource interface{}, ns string, name string, key string) (*corev1.ConfigMap, error) {
data, err := gyaml.Marshal(resource)
if err != nil {
return nil, errors.Wrapf(err, "failed to marshal resource to YAML")
}
return &corev1.ConfigMap{
TypeMeta: metav1.TypeMeta{
APIVersion: "v1",
Kind: "ConfigMap",
},
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: ns,
},
Data: map[string]string{
key: string(data),
},
}, nil
}