-
Notifications
You must be signed in to change notification settings - Fork 60
Add build-tool cli with a crd validation command #3
base: master
Are you sure you want to change the base?
Changes from 5 commits
583adbf
a5de0b8
e57a076
df76ee9
3528efe
45451f4
ff2659d
b727831
e033970
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,7 @@ required = [ | |
"k8s.io/code-generator/cmd/client-gen", | ||
"k8s.io/code-generator/cmd/lister-gen", | ||
"k8s.io/code-generator/cmd/informer-gen", | ||
"k8s.io/kube-openapi/cmd/openapi-gen", | ||
"k8s.io/gengo/args", | ||
] | ||
|
||
|
@@ -85,6 +86,10 @@ required = [ | |
# revision = "6702109cc68eb6fe6350b83e14407c8d7309fd1a" | ||
version = "kubernetes-1.13.5" | ||
|
||
[[override]] | ||
name = "k8s.io/kube-openapi" | ||
revision = "743ec37842bffe49dd4221d9026f30fb1d5adbc4" | ||
|
||
[[override]] | ||
name = "github.com/graymeta/stow" | ||
revision = "903027f87de7054953efcdb8ba70d5dc02df38c7" | ||
|
@@ -100,3 +105,12 @@ required = [ | |
[[override]] | ||
branch = "master" | ||
name = "golang.org/x/net" | ||
|
||
[[constraint]] | ||
name = "github.com/kubeflow/crd-validation" | ||
source = "github.com/bnsblue/crd-validation.git" | ||
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. Let's try to get this forked under lyft... if that proved to be too hard, then this is ok we can switch it later. 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. Sure. I am waiting on the fork request to be approved... |
||
branch = "master" | ||
|
||
[[override]] | ||
name = "github.com/spf13/viper" | ||
version = "1.4.0" |
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 | ||
} | ||
bnsblue marked this conversation as resolved.
Show resolved
Hide resolved
|
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 { | ||
|
||
bnsblue marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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() | ||
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") | ||
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 | ||
} |
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 | ||
} | ||
|
||
|
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 | ||
} |
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,17 @@ | ||||||||
package main | ||||||||
|
||||||||
import ( | ||||||||
"fmt" | ||||||||
"os" | ||||||||
|
||||||||
"github.com/lyft/flytepropeller/cmd/build-tool/cmd" | ||||||||
) | ||||||||
|
||||||||
func main() { | ||||||||
|
||||||||
bnsblue marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
rootCmd := cmd.NewBuildToolCommand() | ||||||||
if err := rootCmd.Execute(); err != nil { | ||||||||
fmt.Println(err) | ||||||||
os.Exit(1) | ||||||||
} | ||||||||
} | ||||||||
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. nit:
Suggested change
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do they not have versions?