github.com/olli-ai/jx/v2@v2.0.400-0.20210921045218-14731b4dd448/pkg/cmd/controller/pipeline/pipelinerunner_cmd.go (about) 1 package pipeline 2 3 import ( 4 "github.com/olli-ai/jx/v2/pkg/cmd/helper" 5 "github.com/olli-ai/jx/v2/pkg/cmd/opts/step" 6 "github.com/olli-ai/jx/v2/pkg/cmd/step/git/credentials" 7 "github.com/olli-ai/jx/v2/pkg/tekton" 8 "github.com/olli-ai/jx/v2/pkg/tekton/metapipeline" 9 "github.com/pkg/errors" 10 "github.com/spf13/viper" 11 12 "github.com/olli-ai/jx/v2/pkg/cmd/opts" 13 "github.com/olli-ai/jx/v2/pkg/cmd/templates" 14 "github.com/spf13/cobra" 15 16 jxclient "github.com/jenkins-x/jx-api/pkg/client/clientset/versioned" 17 ) 18 19 const ( 20 useMetaPipelineOptionName = "use-meta-pipeline" 21 metaPipelineImageOptionName = "meta-pipeline-image" 22 portOptionName = "port" 23 bindOptionName = "bind" 24 ) 25 26 // PipelineRunnerOptions holds the command line arguments 27 type PipelineRunnerOptions struct { 28 *opts.CommonOptions 29 BindAddress string 30 Path string 31 Port int 32 NoGitCredentialsInit bool 33 UseMetaPipeline bool 34 MetaPipelineImage string 35 SemanticRelease bool 36 } 37 38 var ( 39 controllerPipelineRunnersLong = templates.LongDesc(`Runs the service to generate Tekton resources from source code webhooks such as from Prow`) 40 41 controllerPipelineRunnersExample = templates.Examples(` 42 # run the pipeline runner controller 43 jx controller pipelinerunner 44 `) 45 ) 46 47 // NewCmdControllerPipelineRunner creates the command 48 func NewCmdControllerPipelineRunner(commonOpts *opts.CommonOptions) *cobra.Command { 49 options := PipelineRunnerOptions{ 50 CommonOptions: commonOpts, 51 } 52 cmd := &cobra.Command{ 53 Use: "pipelinerunner", 54 Short: "Runs the service to generate Tekton PipelineRun resources from source code webhooks such as from Prow", 55 Long: controllerPipelineRunnersLong, 56 Example: controllerPipelineRunnersExample, 57 Run: func(cmd *cobra.Command, args []string) { 58 options.Cmd = cmd 59 options.Args = args 60 err := options.Run() 61 helper.CheckErr(err) 62 }, 63 } 64 65 cmd.Flags().IntVar(&options.Port, portOptionName, 8080, "The TCP port to listen on.") 66 cmd.Flags().StringVar(&options.BindAddress, bindOptionName, "0.0.0.0", "The interface address to bind to (by default, will listen on all interfaces/addresses).") 67 cmd.Flags().StringVar(&options.Path, "path", "/", "The path to listen on for requests to trigger a pipeline run.") 68 cmd.Flags().StringVar(&options.ServiceAccount, "service-account", tekton.DefaultPipelineSA, "The Kubernetes ServiceAccount to use to run the pipeline.") 69 cmd.Flags().BoolVar(&options.NoGitCredentialsInit, "no-git-init", false, "Disables checking we have setup git credentials on startup.") 70 cmd.Flags().BoolVar(&options.SemanticRelease, "semantic-release", false, "Enable semantic releases") 71 72 // TODO - temporary flags until meta pipeline is the default 73 cmd.Flags().BoolVar(&options.UseMetaPipeline, useMetaPipelineOptionName, true, "Uses the meta pipeline to create the pipeline.") 74 cmd.Flags().StringVar(&options.MetaPipelineImage, metaPipelineImageOptionName, "", "Specify the docker image to use if there is no image specified for a step.") 75 76 options.bindViper(cmd) 77 return cmd 78 } 79 80 func (o *PipelineRunnerOptions) bindViper(cmd *cobra.Command) { 81 _ = viper.BindEnv(useMetaPipelineOptionName) 82 _ = viper.BindPFlag(useMetaPipelineOptionName, cmd.Flags().Lookup(useMetaPipelineOptionName)) 83 84 _ = viper.BindEnv(metaPipelineImageOptionName) 85 _ = viper.BindPFlag(metaPipelineImageOptionName, cmd.Flags().Lookup(metaPipelineImageOptionName)) 86 } 87 88 // Run will implement this command 89 func (o *PipelineRunnerOptions) Run() error { 90 useMetaPipeline := viper.GetBool(useMetaPipelineOptionName) 91 92 if !o.NoGitCredentialsInit && !useMetaPipeline { 93 err := o.InitGitConfigAndUser() 94 if err != nil { 95 return err 96 } 97 98 err = o.stepGitCredentials() 99 if err != nil { 100 return err 101 } 102 } 103 104 jxClient, ns, err := o.getClientsAndNamespace() 105 if err != nil { 106 return err 107 } 108 109 metapipelineClient, err := metapipeline.NewMetaPipelineClient() 110 if err != nil { 111 return err 112 } 113 114 controller := controller{ 115 bindAddress: o.BindAddress, 116 path: o.Path, 117 port: o.Port, 118 useMetaPipeline: useMetaPipeline, 119 metaPipelineImage: viper.GetString(metaPipelineImageOptionName), 120 semanticRelease: o.SemanticRelease, 121 serviceAccount: o.ServiceAccount, 122 jxClient: jxClient, 123 ns: ns, 124 metaPipelineClient: metapipelineClient, 125 } 126 127 controller.Start() 128 return nil 129 } 130 131 func (o *PipelineRunnerOptions) stepGitCredentials() error { 132 if !o.NoGitCredentialsInit { 133 copy := *o.CommonOptions 134 copy.BatchMode = true 135 gsc := &credentials.StepGitCredentialsOptions{ 136 StepOptions: step.StepOptions{ 137 CommonOptions: ©, 138 }, 139 } 140 err := gsc.Run() 141 if err != nil { 142 return errors.Wrapf(err, "failed to run: jx step git credentials") 143 } 144 } 145 return nil 146 } 147 148 func (o *PipelineRunnerOptions) getClientsAndNamespace() (jxclient.Interface, string, error) { 149 jxClient, _, err := o.JXClient() 150 if err != nil { 151 return nil, "", errors.Wrap(err, "unable to create JX client") 152 } 153 154 _, ns, err := o.KubeClientAndDevNamespace() 155 if err != nil { 156 return nil, "", errors.Wrap(err, "unable to create Kube client") 157 } 158 159 return jxClient, ns, nil 160 }