-
Notifications
You must be signed in to change notification settings - Fork 786
/
step_scheduler_config_migrate.go
137 lines (124 loc) · 4.78 KB
/
step_scheduler_config_migrate.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
package scheduler
import (
"github.com/jenkins-x/jx-logging/pkg/log"
"github.com/jenkins-x/jx/v2/pkg/cmd/helper"
"github.com/jenkins-x/jx/v2/pkg/cmd/opts"
"github.com/jenkins-x/jx/v2/pkg/cmd/opts/step"
"github.com/jenkins-x/jx/v2/pkg/cmd/templates"
"github.com/jenkins-x/jx/v2/pkg/pipelinescheduler"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
// StepSchedulerConfigMigrateOptions contains the command line flags
type StepSchedulerConfigMigrateOptions struct {
step.StepOptions
Agent string
ProwConfigFileLocation string
ProwPluginsFileLocation string
SkipVerification bool
DryRun bool
// Used for testing
CloneDir string
}
var (
stepSchedulerConfigMigrateLong = templates.LongDesc(`
This command will generate pipeline scheduler resources from either the prow config maps or prow config files.
For gitops users they will be added to the dev environment git repository.
For non gitops users they will be applied directly to the cluster if --dryRun=false.
`)
stepSchedulerConfigMigrateExample = templates.Examples(`
# Test the migration but do not apply
jx step scheduler config migrate
# Generate the pipeline schedulers and apply them either via gitops or directly to the cluster
jx step scheduler config migrate --dryRun=false
# Generate the pipeline schedulers from files instead of the existing configmaps in the cluster
jx step scheduler config migrate --prow-config-file=config.yaml --prow-plugins-file=plugins.yaml
# Disable validation checks when migrating to pipeline schedulers
jx step scheduler config migrate --skipVerification=true
`)
)
// NewCmdStepSchedulerConfigMigrate Steps a command object for the "step" command
func NewCmdStepSchedulerConfigMigrate(commonOpts *opts.CommonOptions) *cobra.Command {
options := &StepSchedulerConfigMigrateOptions{
StepOptions: step.StepOptions{
CommonOptions: commonOpts,
},
}
cmd := &cobra.Command{
Use: "migrate",
Short: "scheduler config migrate",
Long: stepSchedulerConfigMigrateLong,
Example: stepSchedulerConfigMigrateExample,
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
helper.CheckErr(err)
},
}
cmd.Flags().StringVarP(&options.Agent, "agent", "", "prow", "The scheduler agent to use e.g. Prow")
cmd.Flags().StringVarP(&options.ProwConfigFileLocation, "prow-config-file", "", "", "The location of the config file to use")
cmd.Flags().StringVarP(&options.ProwPluginsFileLocation, "prow-plugins-file", "", "", "The location of the plugins file to use")
cmd.Flags().BoolVarP(&options.SkipVerification, "skipVerification", "", false, "Skip verification of the new configuration")
cmd.Flags().BoolVarP(&options.DryRun, "dryRun", "", true, "Do not apply the generated configuration")
return cmd
}
// Run implements this command
func (o *StepSchedulerConfigMigrateOptions) Run() error {
gitOps, devEnv := o.GetDevEnv()
if devEnv == nil {
return helper.ErrDevEnvNotFound
}
jxClient, ns, err := o.JXClient()
if err != nil {
return errors.WithStack(err)
}
teamSettings, err := o.TeamSettings()
if err != nil {
return errors.WithStack(err)
}
switch o.Agent {
case "prow":
kubeClient, err := o.KubeClient()
if err != nil {
return errors.WithStack(err)
}
sourceRepoGroups, sourceRepos, schedulers, err := pipelinescheduler.CreateSchedulersFromProwConfig(o.ProwConfigFileLocation, o.ProwPluginsFileLocation, o.SkipVerification, o.DryRun, gitOps, jxClient, kubeClient, ns, teamSettings.DefaultScheduler.Name, devEnv)
if err != nil {
return errors.Wrapf(err, "generating Prow config")
}
if !o.DryRun {
if gitOps {
opts := pipelinescheduler.GitOpsOptions{
Verbose: o.Verbose,
DevEnv: devEnv,
}
opts.PullRequestCloneDir = ""
if o.CloneDir != "" {
opts.PullRequestCloneDir = o.CloneDir
}
gitProvider, _, err := o.CreateGitProviderForURLWithoutKind(devEnv.Spec.Source.URL)
if err != nil {
return errors.Wrapf(err, "creating git provider for %s", devEnv.Spec.Source.URL)
}
opts.GitProvider = gitProvider
opts.Gitter = o.Git()
opts.Helmer = o.Helm()
err = opts.AddSchedulersToEnvironmentRepo(sourceRepoGroups, sourceRepos, schedulers)
if err != nil {
return errors.Wrapf(err, "adding pipeline scheduler config to environment repo")
}
} else {
err = pipelinescheduler.ApplySchedulersDirectly(jxClient, ns, sourceRepoGroups, sourceRepos, schedulers, devEnv)
if err != nil {
return errors.Wrapf(err, "applying pipeline scheduler config")
}
}
} else {
log.Logger().Info("Running in dry run mode, pipeline scheduler config will be discarded")
}
default:
return errors.Errorf("%s is an unsupported agent. Available agents are: prow", o.Agent)
}
return nil
}