github.com/jenkins-x/jx/v2@v2.1.155/pkg/cmd/step/scheduler/step_scheduler_config_apply.go (about)

     1  package scheduler
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/jenkins-x/jx/v2/pkg/cmd/helper"
     7  	"github.com/jenkins-x/jx/v2/pkg/cmd/opts"
     8  	"github.com/jenkins-x/jx/v2/pkg/cmd/opts/step"
     9  	"github.com/jenkins-x/jx/v2/pkg/cmd/templates"
    10  	"github.com/jenkins-x/jx/v2/pkg/pipelinescheduler"
    11  	"github.com/pkg/errors"
    12  	"github.com/spf13/cobra"
    13  )
    14  
    15  // StepSchedulerConfigApplyOptions contains the command line flags
    16  type StepSchedulerConfigApplyOptions struct {
    17  	step.StepOptions
    18  	Agent         string
    19  	ApplyDirectly bool
    20  
    21  	// Used for testing
    22  	CloneDir string
    23  }
    24  
    25  var (
    26  	stepSchedulerConfigApplyLong = templates.LongDesc(`
    27          This command will transform your pipeline schedulers in to prow config. 
    28          If you are using gitops the prow config will be added to your environment repository. 
    29          For non-gitops environments the prow config maps will applied to your dev environment.
    30  `)
    31  	stepSchedulerConfigApplyExample = templates.Examples(`
    32  	
    33  	jx step scheduler config apply
    34  `)
    35  )
    36  
    37  // NewCmdStepSchedulerConfigApply Steps a command object for the "step" command
    38  func NewCmdStepSchedulerConfigApply(commonOpts *opts.CommonOptions) *cobra.Command {
    39  	options := &StepSchedulerConfigApplyOptions{
    40  		StepOptions: step.StepOptions{
    41  			CommonOptions: commonOpts,
    42  		},
    43  	}
    44  
    45  	cmd := &cobra.Command{
    46  		Use:     "apply",
    47  		Short:   "scheduler config apply",
    48  		Long:    stepSchedulerConfigApplyLong,
    49  		Example: stepSchedulerConfigApplyExample,
    50  		Run: func(cmd *cobra.Command, args []string) {
    51  			options.Cmd = cmd
    52  			options.Args = args
    53  			err := options.Run()
    54  			helper.CheckErr(err)
    55  		},
    56  	}
    57  	cmd.Flags().StringVarP(&options.Agent, "agent", "", "prow", "The scheduler agent to use e.g. Prow")
    58  	cmd.Flags().BoolVarP(&options.ApplyDirectly, "direct", "", false, "Skip generating a PR and apply the pipeline config directly to the cluster when using gitops mode.")
    59  	return cmd
    60  }
    61  
    62  // Run implements this command
    63  func (o *StepSchedulerConfigApplyOptions) Run() error {
    64  	gitOps, devEnv := o.GetDevEnv()
    65  	if devEnv == nil {
    66  		return helper.ErrDevEnvNotFound
    67  	}
    68  	switch o.Agent {
    69  	case "prow":
    70  		jxClient, ns, err := o.JXClientAndDevNamespace()
    71  		if err != nil {
    72  			return errors.WithStack(err)
    73  		}
    74  		teamSettings, err := o.TeamSettings()
    75  		if err != nil {
    76  			return err
    77  		}
    78  		if teamSettings == nil {
    79  			return fmt.Errorf("no TeamSettings for namespace %s", ns)
    80  		}
    81  		cfg, plugs, err := pipelinescheduler.GenerateProw(gitOps, true, jxClient, ns, teamSettings.DefaultScheduler.Name, devEnv, nil)
    82  		if err != nil {
    83  			return errors.Wrapf(err, "generating Prow config")
    84  		}
    85  		kubeClient, ns, err := o.KubeClientAndNamespace()
    86  		if err != nil {
    87  			return errors.WithStack(err)
    88  		}
    89  
    90  		if gitOps && !o.ApplyDirectly {
    91  			opts := pipelinescheduler.GitOpsOptions{
    92  				Verbose: o.Verbose,
    93  				DevEnv:  devEnv,
    94  			}
    95  			opts.PullRequestCloneDir = ""
    96  			if o.CloneDir != "" {
    97  				opts.PullRequestCloneDir = o.CloneDir
    98  			}
    99  
   100  			gitProvider, _, err := o.CreateGitProviderForURLWithoutKind(devEnv.Spec.Source.URL)
   101  			if err != nil {
   102  				return errors.Wrapf(err, "creating git provider for %s", devEnv.Spec.Source.URL)
   103  			}
   104  			opts.GitProvider = gitProvider
   105  			opts.Gitter = o.Git()
   106  			opts.Helmer = o.Helm()
   107  			err = opts.AddToEnvironmentRepo(cfg, plugs, kubeClient, ns)
   108  			if err != nil {
   109  				return errors.Wrapf(err, "adding Prow config to environment repo")
   110  			}
   111  		} else {
   112  			err = pipelinescheduler.ApplyDirectly(kubeClient, ns, cfg, plugs)
   113  			if err != nil {
   114  				return errors.Wrapf(err, "applying Prow config")
   115  			}
   116  		}
   117  	default:
   118  		return errors.Errorf("%s is an unsupported agent. Available agents are: prow", o.Agent)
   119  	}
   120  	return nil
   121  }