github.com/jenkins-x/jx/v2@v2.1.155/pkg/cmd/compliance/compliance_run.go (about)

     1  package compliance
     2  
     3  import (
     4  	"github.com/heptio/sonobuoy/pkg/client"
     5  	"github.com/heptio/sonobuoy/pkg/config"
     6  	"github.com/jenkins-x/jx-logging/pkg/log"
     7  	"github.com/jenkins-x/jx/v2/pkg/cmd/helper"
     8  	"github.com/jenkins-x/jx/v2/pkg/cmd/opts"
     9  	"github.com/jenkins-x/jx/v2/pkg/cmd/templates"
    10  	"github.com/pkg/errors"
    11  	"github.com/spf13/cobra"
    12  	v1 "k8s.io/api/core/v1"
    13  )
    14  
    15  var (
    16  	complianceRuntLong = templates.LongDesc(`
    17  		Runs the compliance tests
    18  	`)
    19  
    20  	complianceRunExample = templates.Examples(`
    21  		# Run the compliance tests
    22  		jx compliance run
    23  	`)
    24  )
    25  
    26  // ComplianceRunOptions options for "compliance run" command
    27  type ComplianceRunOptions struct {
    28  	*opts.CommonOptions
    29  }
    30  
    31  // NewCmdComplianceRun creates a command object for the "compliance run" action, which
    32  // starts the E2E compliance tests
    33  func NewCmdComplianceRun(commonOpts *opts.CommonOptions) *cobra.Command {
    34  	options := &ComplianceRunOptions{
    35  		CommonOptions: commonOpts,
    36  	}
    37  
    38  	cmd := &cobra.Command{
    39  		Use:     "run",
    40  		Short:   "Runs the compliance tests",
    41  		Long:    complianceRuntLong,
    42  		Example: complianceRunExample,
    43  		Run: func(cmd *cobra.Command, args []string) {
    44  			options.Cmd = cmd
    45  			options.Args = args
    46  			err := options.Run()
    47  			helper.CheckErr(err)
    48  		},
    49  	}
    50  
    51  	return cmd
    52  }
    53  
    54  // Run implements the "compliance run" command
    55  func (o *ComplianceRunOptions) Run() error {
    56  	cc, err := o.ComplianceClient()
    57  	if err != nil {
    58  		return errors.Wrap(err, "could not create the compliance client")
    59  	}
    60  	cfg := o.config()
    61  	if err := cc.Run(cfg); err != nil {
    62  		return errors.Wrap(err, "failed to start the compliance tests")
    63  	}
    64  
    65  	log.Logger().Infof("Compliance tests started in namespace %q", complianceNamespace)
    66  
    67  	return nil
    68  }
    69  
    70  func (o *ComplianceRunOptions) config() *client.RunConfig {
    71  	modeName := client.CertifiedConformance
    72  	mode := modeName.Get()
    73  	genCfg := &client.GenConfig{
    74  		E2EConfig:            &mode.E2EConfig,
    75  		Config:               o.getConfigWithMode(modeName),
    76  		EnableRBAC:           true,
    77  		ImagePullPolicy:      string(v1.PullAlways),
    78  		KubeConformanceImage: kubeConformanceImage,
    79  	}
    80  	return &client.RunConfig{
    81  		GenConfig: *genCfg,
    82  	}
    83  }
    84  
    85  func (o *ComplianceRunOptions) getConfigWithMode(mode client.Mode) *config.Config {
    86  	cfg := config.New()
    87  	modeConfig := mode.Get()
    88  	if modeConfig != nil {
    89  		cfg.PluginSelections = modeConfig.Selectors
    90  		cfg.Namespace = complianceNamespace
    91  		cfg.WorkerImage = complianceWorkerImage
    92  	}
    93  	return cfg
    94  }