This repository was archived by the owner on Oct 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 59
Add build-tool cli with a crd validation command #3
Open
bnsblue
wants to merge
9
commits into
master
Choose a base branch
from
wf-crd-validation
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
583adbf
rm fmt.println when crd-validation
bnsblue a5de0b8
dummy commit
bnsblue e57a076
create a separate cli for build tools, e.g., crd-validation
bnsblue df76ee9
remove a log
bnsblue 3528efe
dep ensure update
bnsblue 45451f4
Update cmd/build-tool/cmd/crd/flyteworkflow.go
bnsblue ff2659d
Remove an empty line at the beginning of a function
bnsblue b727831
Remove unnecessary comment in cmd/kubectl-flyte/cmd/root.go
bnsblue e033970
deleting gopkg files
bnsblue File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| package crd | ||
|
|
||
| import ( | ||
| "github.com/lyft/flytepropeller/pkg/apis/flyteworkflow/v1alpha1" | ||
| apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1" | ||
| "log" | ||
|
|
||
| "github.com/kubeflow/crd-validation/pkg/crd/exporter" | ||
| "github.com/kubeflow/crd-validation/pkg/utils" | ||
| ) | ||
|
|
||
| const ( | ||
| // CRDName is the name for FlyteWorkflow. | ||
| CRDNameFlyteWorkflow = "github.com/lyft/flytepropeller/pkg/apis/flyteworkflow/v1alpha1.FlyteWorkflow" | ||
|
|
||
| generatedFileFlyteWorkflow = "flyteworkflow-crd-v1alpha1.yaml" | ||
| ) | ||
|
|
||
| // FlyteWorkflowGenerator is the type for FlyteWorkflow CRD generator. | ||
| type FlyteWorkflowGenerator struct { | ||
| *exporter.Exporter | ||
| } | ||
|
|
||
| // Creates a new CRD generator which outputs to a file. | ||
| func NewFlyteWorkflowGenerator(outputDir string) *FlyteWorkflowGenerator { | ||
| return &FlyteWorkflowGenerator{ | ||
| Exporter: exporter.NewFileExporter(outputDir, generatedFileFlyteWorkflow), | ||
| } | ||
| } | ||
|
|
||
| // Creates a new CRD generator which outputs to stdout. | ||
| func NewFlyteWorkflowGeneratorStdout() *FlyteWorkflowGenerator { | ||
| return &FlyteWorkflowGenerator{ | ||
| Exporter: exporter.NewStdoutExporter(), | ||
| } | ||
| } | ||
|
|
||
| // Generate generates the crd. | ||
| func (t FlyteWorkflowGenerator) Generate(original *apiextensions.CustomResourceDefinition) *apiextensions.CustomResourceDefinition { | ||
| log.Println("Generating validation") | ||
| original.Spec.Validation = utils.GetCustomResourceValidation(CRDNameFlyteWorkflow, v1alpha1.GetOpenAPIDefinitions) | ||
| return original | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| package cmd | ||
|
|
||
| import ( | ||
| "github.com/spf13/viper" | ||
| "log" | ||
|
|
||
| compilerErrors "github.com/lyft/flytepropeller/pkg/compiler/errors" | ||
|
|
||
| "github.com/pkg/errors" | ||
| "github.com/spf13/cobra" | ||
|
|
||
| "github.com/kubeflow/crd-validation/pkg/config" | ||
| "github.com/lyft/flytepropeller/cmd/build-tool/cmd/crd" | ||
| ) | ||
|
|
||
| const ( | ||
| configKey = "config-file" | ||
| baseCrdKey = "base-crd" | ||
| ) | ||
|
|
||
|
|
||
| const crdValidationCmdName = "crd-validation" | ||
|
|
||
| type CrdValidationOpts struct { | ||
| *RootOptions | ||
| configFile string | ||
| baseCrdFile string | ||
| dryRun bool | ||
| } | ||
|
|
||
| func NewCrdValidationCommand(opts *RootOptions) *cobra.Command { | ||
|
|
||
| crdValidationOpts := &CrdValidationOpts{ | ||
| RootOptions: opts, | ||
| } | ||
|
|
||
| crdValidationCmd := &cobra.Command{ | ||
| Use: crdValidationCmdName, | ||
| Aliases: []string{"validate"}, | ||
| Short: "Augment a CRD YAML file with validation section based on a base CRD file", | ||
| Long: ``, | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| if err := requiredFlags(cmd, baseCrdKey); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| compilerErrors.SetIncludeSource() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is this for? |
||
|
|
||
| return crdValidationOpts.generateValidation() | ||
| }, | ||
| } | ||
|
|
||
| crdValidationCmd.Flags().StringVarP(&crdValidationOpts.configFile, configKey, "c", "", "Path of the config file for the execution of CRD validation") | ||
| crdValidationCmd.Flags().StringVarP(&crdValidationOpts.baseCrdFile, baseCrdKey, "b", "", "Path to base CRD file.") | ||
| crdValidationCmd.Flags().BoolVarP(&crdValidationOpts.dryRun, "dry-run", "d", false, "Compiles and transforms, but does not create a workflow. OutputsRef ts to STDOUT.") | ||
|
|
||
| return crdValidationCmd | ||
| } | ||
|
|
||
| func (c *CrdValidationOpts) initConfig() error { | ||
| if c.configFile != "" { // enable ability to specify config file via flag | ||
| viper.SetConfigFile(c.configFile) | ||
| log.Println("Using config file:", viper.ConfigFileUsed()) | ||
| } | ||
|
|
||
| viper.SetConfigType("yaml") // Set config type to yaml | ||
|
|
||
| // If a config file is found, read it in. | ||
| if err := viper.ReadInConfig(); err != nil { | ||
| return errors.Wrapf(err, "Failed to read config file.") | ||
| } else { | ||
| log.Println("Using config file:", viper.ConfigFileUsed()) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
|
|
||
| func (c *CrdValidationOpts) generateValidation() error { | ||
|
|
||
| err := c.initConfig() | ||
| var generator *crd.FlyteWorkflowGenerator | ||
| if err != nil { | ||
| log.Println("Output will be written to Stdout") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any reason you don't just fail here instead of trying to "recover" ? |
||
| generator = crd.NewFlyteWorkflowGeneratorStdout() | ||
| } else { | ||
| crdValidationConfig := config.GetCrdValidationConfig() | ||
| generator = crd.NewFlyteWorkflowGenerator(crdValidationConfig.OutputDir) | ||
| } | ||
|
|
||
| original := config.NewCustomResourceDefinition(c.baseCrdFile) | ||
| final := generator.Generate(original) | ||
| generator.Export(final) | ||
|
|
||
| return nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| package cmd | ||
|
|
||
| import ( | ||
| "context" | ||
| "flag" | ||
| "fmt" | ||
| "os" | ||
| "runtime" | ||
|
|
||
| "github.com/lyft/flytestdlib/logger" | ||
| "github.com/lyft/flytestdlib/version" | ||
| "github.com/spf13/pflag" | ||
|
|
||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| func init() { | ||
| pflag.CommandLine.AddGoFlagSet(flag.CommandLine) | ||
| err := flag.CommandLine.Parse([]string{}) | ||
| if err != nil { | ||
| logger.Error(context.TODO(), "Error in initializing: %v", err) | ||
| os.Exit(-1) | ||
| } | ||
| } | ||
|
|
||
| type RootOptions struct { | ||
| configFile string | ||
| } | ||
|
|
||
| func (r *RootOptions) executeRootCmd() error { | ||
| ctx := context.TODO() | ||
| logger.Infof(ctx, "Go Version: %s", runtime.Version()) | ||
| logger.Infof(ctx, "Go OS/Arch: %s/%s", runtime.GOOS, runtime.GOARCH) | ||
| version.LogBuildInformation("build-tool") | ||
| return fmt.Errorf("use one of the sub-commands") | ||
| } | ||
|
|
||
| // NewCommand returns a new instance of an argo command | ||
| func NewBuildToolCommand() *cobra.Command { | ||
| rootOpts := &RootOptions{} | ||
| command := &cobra.Command{ | ||
| Use: "build-tool", | ||
| Short: "build-tool are utility commands that help validating crds, etc.", | ||
| Long: `Flyte is a serverless workflow processing platform built for native execution on K8s. | ||
| It is extensible and flexible to allow adding new operators and comes with many operators built in`, | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| return rootOpts.executeRootCmd() | ||
| }, | ||
| } | ||
|
|
||
| command.AddCommand(NewCrdValidationCommand(rootOpts)) | ||
| return command | ||
| } | ||
|
|
||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| package cmd | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| func requiredFlags(cmd *cobra.Command, flags ...string) error { | ||
| for _, flag := range flags { | ||
| f := cmd.Flag(flag) | ||
| if f == nil { | ||
| return fmt.Errorf("unable to find Key [%v]", flag) | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
|
|
||
| "github.com/lyft/flytepropeller/cmd/build-tool/cmd" | ||
| ) | ||
|
|
||
| func main() { | ||
| rootCmd := cmd.NewBuildToolCommand() | ||
| if err := rootCmd.Execute(); err != nil { | ||
| fmt.Println(err) | ||
| os.Exit(1) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.