-
Notifications
You must be signed in to change notification settings - Fork 787
/
pipelinerunner_cmd.go
160 lines (136 loc) · 5.04 KB
/
pipelinerunner_cmd.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
package pipeline
import (
"github.com/jenkins-x/jx/v2/pkg/cmd/helper"
"github.com/jenkins-x/jx/v2/pkg/cmd/opts/step"
"github.com/jenkins-x/jx/v2/pkg/cmd/step/git/credentials"
"github.com/jenkins-x/jx/v2/pkg/tekton"
"github.com/jenkins-x/jx/v2/pkg/tekton/metapipeline"
"github.com/pkg/errors"
"github.com/spf13/viper"
"github.com/jenkins-x/jx/v2/pkg/cmd/opts"
"github.com/jenkins-x/jx/v2/pkg/cmd/templates"
"github.com/spf13/cobra"
jxclient "github.com/jenkins-x/jx-api/pkg/client/clientset/versioned"
)
const (
useMetaPipelineOptionName = "use-meta-pipeline"
metaPipelineImageOptionName = "meta-pipeline-image"
portOptionName = "port"
bindOptionName = "bind"
)
// PipelineRunnerOptions holds the command line arguments
type PipelineRunnerOptions struct {
*opts.CommonOptions
BindAddress string
Path string
Port int
NoGitCredentialsInit bool
UseMetaPipeline bool
MetaPipelineImage string
SemanticRelease bool
}
var (
controllerPipelineRunnersLong = templates.LongDesc(`Runs the service to generate Tekton resources from source code webhooks such as from Prow`)
controllerPipelineRunnersExample = templates.Examples(`
# run the pipeline runner controller
jx controller pipelinerunner
`)
)
// NewCmdControllerPipelineRunner creates the command
func NewCmdControllerPipelineRunner(commonOpts *opts.CommonOptions) *cobra.Command {
options := PipelineRunnerOptions{
CommonOptions: commonOpts,
}
cmd := &cobra.Command{
Use: "pipelinerunner",
Short: "Runs the service to generate Tekton PipelineRun resources from source code webhooks such as from Prow",
Long: controllerPipelineRunnersLong,
Example: controllerPipelineRunnersExample,
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
helper.CheckErr(err)
},
}
cmd.Flags().IntVar(&options.Port, portOptionName, 8080, "The TCP port to listen on.")
cmd.Flags().StringVar(&options.BindAddress, bindOptionName, "0.0.0.0", "The interface address to bind to (by default, will listen on all interfaces/addresses).")
cmd.Flags().StringVar(&options.Path, "path", "/", "The path to listen on for requests to trigger a pipeline run.")
cmd.Flags().StringVar(&options.ServiceAccount, "service-account", tekton.DefaultPipelineSA, "The Kubernetes ServiceAccount to use to run the pipeline.")
cmd.Flags().BoolVar(&options.NoGitCredentialsInit, "no-git-init", false, "Disables checking we have setup git credentials on startup.")
cmd.Flags().BoolVar(&options.SemanticRelease, "semantic-release", false, "Enable semantic releases")
// TODO - temporary flags until meta pipeline is the default
cmd.Flags().BoolVar(&options.UseMetaPipeline, useMetaPipelineOptionName, true, "Uses the meta pipeline to create the pipeline.")
cmd.Flags().StringVar(&options.MetaPipelineImage, metaPipelineImageOptionName, "", "Specify the docker image to use if there is no image specified for a step.")
options.bindViper(cmd)
return cmd
}
func (o *PipelineRunnerOptions) bindViper(cmd *cobra.Command) {
_ = viper.BindEnv(useMetaPipelineOptionName)
_ = viper.BindPFlag(useMetaPipelineOptionName, cmd.Flags().Lookup(useMetaPipelineOptionName))
_ = viper.BindEnv(metaPipelineImageOptionName)
_ = viper.BindPFlag(metaPipelineImageOptionName, cmd.Flags().Lookup(metaPipelineImageOptionName))
}
// Run will implement this command
func (o *PipelineRunnerOptions) Run() error {
useMetaPipeline := viper.GetBool(useMetaPipelineOptionName)
if !o.NoGitCredentialsInit && !useMetaPipeline {
err := o.InitGitConfigAndUser()
if err != nil {
return err
}
err = o.stepGitCredentials()
if err != nil {
return err
}
}
jxClient, ns, err := o.getClientsAndNamespace()
if err != nil {
return err
}
metapipelineClient, err := metapipeline.NewMetaPipelineClient()
if err != nil {
return err
}
controller := controller{
bindAddress: o.BindAddress,
path: o.Path,
port: o.Port,
useMetaPipeline: useMetaPipeline,
metaPipelineImage: viper.GetString(metaPipelineImageOptionName),
semanticRelease: o.SemanticRelease,
serviceAccount: o.ServiceAccount,
jxClient: jxClient,
ns: ns,
metaPipelineClient: metapipelineClient,
}
controller.Start()
return nil
}
func (o *PipelineRunnerOptions) stepGitCredentials() error {
if !o.NoGitCredentialsInit {
copy := *o.CommonOptions
copy.BatchMode = true
gsc := &credentials.StepGitCredentialsOptions{
StepOptions: step.StepOptions{
CommonOptions: ©,
},
}
err := gsc.Run()
if err != nil {
return errors.Wrapf(err, "failed to run: jx step git credentials")
}
}
return nil
}
func (o *PipelineRunnerOptions) getClientsAndNamespace() (jxclient.Interface, string, error) {
jxClient, _, err := o.JXClient()
if err != nil {
return nil, "", errors.Wrap(err, "unable to create JX client")
}
_, ns, err := o.KubeClientAndDevNamespace()
if err != nil {
return nil, "", errors.Wrap(err, "unable to create Kube client")
}
return jxClient, ns, nil
}